|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -namespace Spatie\WebhookServer\Tests; |
4 |
| - |
5 | 3 | use Illuminate\Support\Facades\Queue;
|
6 | 4 | use Spatie\WebhookServer\CallWebhookJob;
|
7 | 5 | use Spatie\WebhookServer\Exceptions\CouldNotCallWebhook;
|
|
10 | 8 | use Spatie\WebhookServer\Exceptions\InvalidWebhookJob;
|
11 | 9 | use Spatie\WebhookServer\WebhookCall;
|
12 | 10 |
|
13 |
| -class WebhookTest extends TestCase |
14 |
| -{ |
15 |
| - public function setUp(): void |
16 |
| - { |
17 |
| - parent::setUp(); |
| 11 | +use function PHPUnit\Framework\assertTrue; |
18 | 12 |
|
19 |
| - Queue::fake(); |
20 |
| - } |
| 13 | +beforeEach(function () { |
| 14 | + Queue::fake(); |
| 15 | +}); |
21 | 16 |
|
22 |
| - /** @test */ |
23 |
| - public function it_can_dispatch_a_job_that_calls_a_webhook() |
24 |
| - { |
25 |
| - $url = 'https://localhost'; |
26 |
| - |
27 |
| - WebhookCall::create()->url($url)->useSecret('123')->dispatch(); |
28 |
| - |
29 |
| - Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
30 |
| - $config = config('webhook-server'); |
31 |
| - |
32 |
| - $this->assertEquals($config['queue'], $job->queue); |
33 |
| - $this->assertEquals($url, $job->webhookUrl); |
34 |
| - $this->assertEquals($config['http_verb'], $job->httpVerb); |
35 |
| - $this->assertEquals($config['tries'], $job->tries); |
36 |
| - $this->assertEquals($config['timeout_in_seconds'], $job->requestTimeout); |
37 |
| - $this->assertEquals($config['backoff_strategy'], $job->backoffStrategyClass); |
38 |
| - $this->assertContains($config['signature_header_name'], array_keys($job->headers)); |
39 |
| - $this->assertEquals($config['verify_ssl'], $job->verifySsl); |
40 |
| - $this->assertEquals($config['throw_exception_on_failure'], $job->throwExceptionOnFailure); |
41 |
| - $this->assertEquals($config['tags'], $job->tags); |
42 |
| - |
43 |
| - return true; |
44 |
| - }); |
45 |
| - } |
46 |
| - |
47 |
| - /** @test */ |
48 |
| - public function it_can_keep_default_config_headers_and_set_new_ones() |
49 |
| - { |
50 |
| - $url = 'https://localhost'; |
| 17 | +it('can dispatch a job that calls a webhook', function () { |
| 18 | + $url = 'https://localhost'; |
51 | 19 |
|
52 |
| - WebhookCall::create()->url($url) |
53 |
| - ->withHeaders(['User-Agent' => 'Spatie/Laravel-Webhook-Server']) |
54 |
| - ->useSecret('123') |
55 |
| - ->dispatch() |
56 |
| - ; |
| 20 | + WebhookCall::create()->url($url)->useSecret('123')->dispatch(); |
57 | 21 |
|
58 |
| - Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
59 |
| - $config = config('webhook-server'); |
| 22 | + Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
| 23 | + $config = config('webhook-server'); |
60 | 24 |
|
61 |
| - $this->assertArrayHasKey('Content-Type', $job->headers); |
62 |
| - $this->assertArrayHasKey('User-Agent', $job->headers); |
| 25 | + expect($job->queue)->toEqual($config['queue']) |
| 26 | + ->and($job->webhookUrl)->toEqual($url) |
| 27 | + ->and($job->httpVerb)->toEqual($config['http_verb']) |
| 28 | + ->and($job->tries)->toEqual($config['tries']) |
| 29 | + ->and($job->requestTimeout)->toEqual($config['timeout_in_seconds']) |
| 30 | + ->and($job->backoffStrategyClass)->toEqual($config['backoff_strategy']) |
| 31 | + ->and(array_keys($job->headers))->toContain($config['signature_header_name']) |
| 32 | + ->and($job->verifySsl)->toEqual($config['verify_ssl']) |
| 33 | + ->and($job->throwExceptionOnFailure)->toEqual($config['throw_exception_on_failure']) |
| 34 | + ->and($job->tags)->toEqual($config['tags']); |
63 | 35 |
|
64 |
| - return true; |
65 |
| - }); |
66 |
| - } |
| 36 | + return true; |
| 37 | + }); |
| 38 | +}); |
67 | 39 |
|
68 |
| - /** @test */ |
69 |
| - public function it_can_override_default_config_headers() |
70 |
| - { |
71 |
| - $url = 'https://localhost'; |
| 40 | +it('can keep default config headers and set new ones', function () { |
| 41 | + $url = 'https://localhost'; |
72 | 42 |
|
73 |
| - WebhookCall::create()->url($url) |
74 |
| - ->withHeaders(['Content-Type' => 'text/plain']) |
75 |
| - ->useSecret('123') |
76 |
| - ->dispatch() |
77 |
| - ; |
| 43 | + WebhookCall::create()->url($url) |
| 44 | + ->withHeaders(['User-Agent' => 'Spatie/Laravel-Webhook-Server']) |
| 45 | + ->useSecret('123') |
| 46 | + ->dispatch(); |
78 | 47 |
|
79 |
| - Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
80 |
| - $config = config('webhook-server'); |
| 48 | + Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
| 49 | + $config = config('webhook-server'); |
81 | 50 |
|
82 |
| - $this->assertArrayHasKey('Content-Type', $job->headers); |
83 |
| - $this->assertEquals('text/plain', $job->headers['Content-Type']); |
| 51 | + expect($job->headers)->toHaveKeys(['Content-Type', 'User-Agent']); |
84 | 52 |
|
85 |
| - return true; |
86 |
| - }); |
87 |
| - } |
| 53 | + return true; |
| 54 | + }); |
| 55 | +}); |
88 | 56 |
|
89 |
| - /** @test */ |
90 |
| - public function it_can_override_default_queue_connection() |
91 |
| - { |
92 |
| - $url = 'https://localhost'; |
| 57 | +it('can override default config headers', function () { |
| 58 | + $url = 'https://localhost'; |
93 | 59 |
|
94 |
| - WebhookCall::create()->url($url) |
95 |
| - ->onConnection('foo') |
96 |
| - ->useSecret('123') |
97 |
| - ->dispatch() |
98 |
| - ; |
| 60 | + WebhookCall::create()->url($url) |
| 61 | + ->withHeaders(['Content-Type' => 'text/plain']) |
| 62 | + ->useSecret('123') |
| 63 | + ->dispatch(); |
99 | 64 |
|
100 |
| - Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
101 |
| - $this->assertEquals('foo', $job->connection); |
| 65 | + Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
| 66 | + $config = config('webhook-server'); |
102 | 67 |
|
103 |
| - return true; |
104 |
| - }); |
105 |
| - } |
| 68 | + expect($job->headers)->toHaveKey('Content-Type'); |
| 69 | + expect($job->headers['Content-Type'])->toEqual('text/plain'); |
106 | 70 |
|
107 |
| - /** @test */ |
108 |
| - public function it_will_throw_an_exception_when_calling_a_webhook_without_proving_an_url() |
109 |
| - { |
110 |
| - $this->expectException(CouldNotCallWebhook::class); |
| 71 | + return true; |
| 72 | + }); |
| 73 | +}); |
111 | 74 |
|
112 |
| - WebhookCall::create()->dispatch(); |
113 |
| - } |
| 75 | +it('can override default queue connection', function () { |
| 76 | + $url = 'https://localhost'; |
114 | 77 |
|
115 |
| - /** @test */ |
116 |
| - public function it_will_throw_an_exception_when_no_secret_has_been_set() |
117 |
| - { |
118 |
| - $this->expectException(CouldNotCallWebhook::class); |
| 78 | + WebhookCall::create()->url($url) |
| 79 | + ->onConnection('foo') |
| 80 | + ->useSecret('123') |
| 81 | + ->dispatch(); |
119 | 82 |
|
120 |
| - WebhookCall::create()->url('https://localhost')->dispatch(); |
121 |
| - } |
| 83 | + Queue::assertPushed(CallWebhookJob::class, function (CallWebhookJob $job) use ($url) { |
| 84 | + $this->assertEquals('foo', $job->connection); |
122 | 85 |
|
123 |
| - /** @test */ |
124 |
| - public function it_will_not_throw_an_exception_if_there_is_not_secret_and_the_request_should_not_be_signed() |
125 |
| - { |
126 |
| - WebhookCall::create()->doNotSign()->url('https://localhost')->dispatch(); |
| 86 | + return true; |
| 87 | + }); |
| 88 | +}); |
127 | 89 |
|
128 |
| - $this->assertTrue(true); |
129 |
| - } |
| 90 | +it('will throw an exception when calling a webhook without proving an url', function () { |
| 91 | + WebhookCall::create()->dispatch(); |
| 92 | +})->throws(CouldNotCallWebhook::class); |
130 | 93 |
|
131 |
| - /** @test */ |
132 |
| - public function it_will_throw_an_exception_when_using_an_invalid_backoff_strategy() |
133 |
| - { |
134 |
| - $this->expectException(InvalidBackoffStrategy::class); |
| 94 | +it('will throw an exception when no secret has been set', function () { |
| 95 | + WebhookCall::create()->url('https://localhost')->dispatch(); |
| 96 | +})->throws(CouldNotCallWebhook::class); |
135 | 97 |
|
136 |
| - WebhookCall::create()->useBackoffStrategy(static::class); |
137 |
| - } |
| 98 | +it('will not throw an exception if there is not secret and the request should not be signed', function () { |
| 99 | + WebhookCall::create()->doNotSign()->url('https://localhost')->dispatch(); |
138 | 100 |
|
139 |
| - /** @test */ |
140 |
| - public function it_will_throw_and_exception_when_using_an_invalid_signer() |
141 |
| - { |
142 |
| - $this->expectException(InvalidSigner::class); |
| 101 | + assertTrue(true); |
| 102 | +}); |
143 | 103 |
|
144 |
| - WebhookCall::create()->signUsing(static::class); |
145 |
| - } |
| 104 | +it('will throw an exception when using an invalid backoff strategy', function () { |
| 105 | + WebhookCall::create()->useBackoffStrategy(static::class); |
| 106 | +})->throws(InvalidBackoffStrategy::class); |
146 | 107 |
|
147 |
| - /** @test */ |
148 |
| - public function it_will_throw_an_exception_when_using_an_invalid_webhook_job() |
| 108 | +it('will throw and exception when using an invalid signer', function () { |
| 109 | + WebhookCall::create()->signUsing(static::class); |
| 110 | +})->throws(InvalidSigner::class); |
| 111 | + |
| 112 | +it('will throw an exception when using an invalid webhook job', function () { |
| 113 | + $invalidJob = new class |
149 | 114 | {
|
150 |
| - $this->expectException(InvalidWebhookJob::class); |
| 115 | + }; |
151 | 116 |
|
152 |
| - WebhookCall::create()->useJob(static::class); |
153 |
| - } |
| 117 | + WebhookCall::create()->useJob($invalidJob::class); |
| 118 | +})->throws(InvalidWebhookJob::class); |
154 | 119 |
|
155 |
| - /** @test */ |
156 |
| - public function it_can_get_the_uuid_property() |
157 |
| - { |
158 |
| - $webhookCall = WebhookCall::create()->uuid('my-unique-identifier'); |
| 120 | +it('can get the UUID property', function () { |
| 121 | + $webhookCall = WebhookCall::create()->uuid('my-unique-identifier'); |
159 | 122 |
|
160 |
| - $this->assertIsString($webhookCall->getUuid()); |
161 |
| - $this->assertSame('my-unique-identifier', $webhookCall->getUuid()); |
162 |
| - } |
| 123 | + expect($webhookCall->getUuid()) |
| 124 | + ->toBeString() |
| 125 | + ->toEqual('my-unique-identifier'); |
| 126 | +}); |
163 | 127 |
|
164 |
| - /** @test */ |
165 |
| - public function it_can_dispatch_a_job_that_calls_a_webhook_if_condition_true() |
166 |
| - { |
167 |
| - $url = 'https://localhost'; |
| 128 | +it('can dispatch a job that calls a webhook if condition true', function () { |
| 129 | + $url = 'https://localhost'; |
168 | 130 |
|
169 |
| - WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true); |
| 131 | + WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true); |
170 | 132 |
|
171 |
| - Queue::assertPushed(CallWebhookJob::class); |
172 |
| - } |
| 133 | + Queue::assertPushed(CallWebhookJob::class); |
| 134 | +}); |
173 | 135 |
|
174 |
| - /** @test */ |
175 |
| - public function it_can_not_dispatch_a_job_that_calls_a_webhook_if_condition_false() |
176 |
| - { |
177 |
| - $url = 'https://localhost'; |
| 136 | +it('can not dispatch a job that calls a webhook if condition false', function () { |
| 137 | + $url = 'https://localhost'; |
178 | 138 |
|
179 |
| - WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(false); |
| 139 | + WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(false); |
180 | 140 |
|
181 |
| - Queue::assertNotPushed(CallWebhookJob::class); |
182 |
| - } |
| 141 | + Queue::assertNotPushed(CallWebhookJob::class); |
| 142 | +}); |
183 | 143 |
|
184 |
| - /** @test */ |
185 |
| - public function it_can_not_dispatch_a_job_that_calls_a_webhook_unless_condition_true() |
186 |
| - { |
187 |
| - $url = 'https://localhost'; |
| 144 | +it('cannot dispatch a job that calls a webhook unless condition true', function () { |
| 145 | + $url = 'https://localhost'; |
188 | 146 |
|
189 |
| - WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(true); |
| 147 | + WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(true); |
190 | 148 |
|
191 |
| - Queue::assertNotPushed(CallWebhookJob::class); |
192 |
| - } |
| 149 | + Queue::assertNotPushed(CallWebhookJob::class); |
| 150 | +}); |
193 | 151 |
|
194 |
| - /** @test */ |
195 |
| - public function it_can_dispatch_a_job_that_calls_a_webhook_unless_condition_false() |
196 |
| - { |
197 |
| - $url = 'https://localhost'; |
| 152 | +it('can dispatch a job that calls a webhook unless condition false', function () { |
| 153 | + $url = 'https://localhost'; |
198 | 154 |
|
199 |
| - WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(false); |
| 155 | + WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(false); |
200 | 156 |
|
201 |
| - Queue::assertPushed(CallWebhookJob::class); |
202 |
| - } |
203 |
| -} |
| 157 | + Queue::assertPushed(CallWebhookJob::class); |
| 158 | +}); |
0 commit comments