|
2 | 2 |
|
3 | 3 | namespace Tests\Issues;
|
4 | 4 |
|
| 5 | +use Illuminate\Http\Client\Request; |
| 6 | +use Illuminate\Http\Client\RequestException; |
5 | 7 | use Illuminate\Support\Facades\Http;
|
6 | 8 | use Monolog\Level;
|
7 | 9 | use Monolog\LogRecord;
|
@@ -35,91 +37,118 @@ function createRecord(): LogRecord
|
35 | 37 | );
|
36 | 38 | }
|
37 | 39 |
|
38 |
| -test('it creates new github issue when no duplicate exists', function () { |
39 |
| - $handler = createHandler(); |
40 |
| - $record = createRecord(); |
| 40 | +beforeEach(function () { |
| 41 | + Http::preventStrayRequests(); |
| 42 | +}); |
41 | 43 |
|
| 44 | +test('it creates new github issue when no duplicate exists', function () { |
42 | 45 | Http::fake([
|
43 | 46 | 'github.com/search/issues*' => Http::response(['items' => []]),
|
44 | 47 | 'github.com/repos/test/repo/issues' => Http::response(['number' => 1]),
|
45 | 48 | ]);
|
46 | 49 |
|
| 50 | + $handler = createHandler(); |
| 51 | + $record = createRecord(); |
| 52 | + |
47 | 53 | $handler->handle($record);
|
48 | 54 |
|
49 |
| - Http::assertSent(function ($request) { |
50 |
| - return str_contains($request->url(), '/repos/test/repo/issues') |
51 |
| - && $request->method() === 'POST'; |
| 55 | + Http::assertSent(function (Request $request) { |
| 56 | + return str($request->url())->endsWith('/repos/test/repo/issues'); |
52 | 57 | });
|
53 | 58 | });
|
54 | 59 |
|
55 | 60 | test('it comments on existing github issue', function () {
|
56 |
| - $handler = createHandler(); |
57 |
| - $record = createRecord(); |
58 |
| - |
59 | 61 | Http::fake([
|
60 | 62 | 'github.com/search/issues*' => Http::response(['items' => [['number' => 1]]]),
|
61 | 63 | 'github.com/repos/test/repo/issues/1/comments' => Http::response(['id' => 1]),
|
62 | 64 | ]);
|
63 | 65 |
|
| 66 | + $handler = createHandler(); |
| 67 | + $record = createRecord(); |
| 68 | + |
64 | 69 | $handler->handle($record);
|
65 | 70 |
|
66 | 71 | Http::assertSent(function ($request) {
|
67 |
| - return str_contains($request->url(), '/issues/1/comments') |
68 |
| - && $request->method() === 'POST'; |
| 72 | + return str($request->url())->endsWith('/issues/1/comments'); |
69 | 73 | });
|
70 | 74 | });
|
71 | 75 |
|
72 | 76 | test('it includes signature in issue search', function () {
|
73 |
| - $handler = createHandler(); |
74 |
| - $record = createRecord(); |
75 |
| - |
76 | 77 | Http::fake([
|
77 | 78 | 'github.com/search/issues*' => Http::response(['items' => []]),
|
78 | 79 | 'github.com/repos/test/repo/issues' => Http::response(['number' => 1]),
|
79 | 80 | ]);
|
80 | 81 |
|
| 82 | + $handler = createHandler(); |
| 83 | + $record = createRecord(); |
| 84 | + |
81 | 85 | $handler->handle($record);
|
82 | 86 |
|
83 | 87 | Http::assertSent(function ($request) {
|
84 |
| - return str_contains($request->url(), '/search/issues') |
85 |
| - && str_contains($request['q'], 'test-signature'); |
| 88 | + return str($request->url())->contains('/search/issues') |
| 89 | + && str_contains($request->data()['q'], 'test-signature'); |
86 | 90 | });
|
87 | 91 | });
|
88 | 92 |
|
89 | 93 | test('it throws exception when issue search fails', function () {
|
90 |
| - $handler = createHandler(); |
91 |
| - $record = createRecord(); |
92 |
| - |
93 | 94 | Http::fake([
|
94 | 95 | 'github.com/search/issues*' => Http::response(['error' => 'Failed'], 500),
|
95 | 96 | ]);
|
96 | 97 |
|
97 |
| - expect(fn () => $handler->handle($record)) |
98 |
| - ->toThrow('Failed to search GitHub issues'); |
99 |
| -}); |
100 |
| - |
101 |
| -test('it throws exception when issue creation fails', function () { |
102 | 98 | $handler = createHandler();
|
103 | 99 | $record = createRecord();
|
104 | 100 |
|
| 101 | + $handler->handle($record); |
| 102 | +})->throws(RequestException::class, exceptionCode: 500); |
| 103 | + |
| 104 | +test('it throws exception when issue creation fails', function () { |
105 | 105 | Http::fake([
|
106 | 106 | 'github.com/search/issues*' => Http::response(['items' => []]),
|
107 | 107 | 'github.com/repos/test/repo/issues' => Http::response(['error' => 'Failed'], 500),
|
108 | 108 | ]);
|
109 | 109 |
|
110 |
| - expect(fn () => $handler->handle($record)) |
111 |
| - ->toThrow('Failed to create GitHub issue'); |
112 |
| -}); |
113 |
| - |
114 |
| -test('it throws exception when comment creation fails', function () { |
115 | 110 | $handler = createHandler();
|
116 | 111 | $record = createRecord();
|
117 | 112 |
|
| 113 | + $handler->handle($record); |
| 114 | +})->throws(RequestException::class, exceptionCode: 500); |
| 115 | + |
| 116 | +test('it throws exception when comment creation fails', function () { |
118 | 117 | Http::fake([
|
119 | 118 | 'github.com/search/issues*' => Http::response(['items' => [['number' => 1]]]),
|
120 | 119 | 'github.com/repos/test/repo/issues/1/comments' => Http::response(['error' => 'Failed'], 500),
|
121 | 120 | ]);
|
122 | 121 |
|
123 |
| - expect(fn () => $handler->handle($record)) |
124 |
| - ->toThrow('Failed to comment on GitHub issue'); |
| 122 | + $handler = createHandler(); |
| 123 | + $record = createRecord(); |
| 124 | + |
| 125 | + $handler->handle($record); |
| 126 | +})->throws(RequestException::class, exceptionCode: 500); |
| 127 | + |
| 128 | +test('it creates fallback issue when 4xx error occurs', function () { |
| 129 | + $errorMessage = 'Validation failed for the issue'; |
| 130 | + |
| 131 | + Http::fake([ |
| 132 | + 'github.com/search/issues*' => Http::response(['items' => []]), |
| 133 | + 'github.com/repos/test/repo/issues' => Http::sequence() |
| 134 | + ->push(['error' => $errorMessage], 422) |
| 135 | + ->push(['number' => 1]), |
| 136 | + ]); |
| 137 | + |
| 138 | + $handler = createHandler(); |
| 139 | + $record = createRecord(); |
| 140 | + |
| 141 | + $handler->handle($record); |
| 142 | + |
| 143 | + Http::assertSent(function ($request) { |
| 144 | + return str($request->url())->endsWith('/repos/test/repo/issues') |
| 145 | + && !str_contains($request->data()['title'], '[GitHub Monolog Error]'); |
| 146 | + }); |
| 147 | + |
| 148 | + Http::assertSent(function ($request) use ($errorMessage) { |
| 149 | + return str($request->url())->endsWith('/repos/test/repo/issues') |
| 150 | + && str_contains($request->data()['title'], '[GitHub Monolog Error]') |
| 151 | + && str_contains($request->data()['body'], $errorMessage) |
| 152 | + && in_array('monolog-integration-error', $request->data()['labels']); |
| 153 | + }); |
125 | 154 | });
|
0 commit comments