|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Keboola\NotificationClient\Tests; |
| 6 | + |
| 7 | +use GuzzleHttp\Handler\MockHandler; |
| 8 | +use GuzzleHttp\HandlerStack; |
| 9 | +use GuzzleHttp\Middleware; |
| 10 | +use GuzzleHttp\Psr7\Request; |
| 11 | +use GuzzleHttp\Psr7\Response; |
| 12 | +use Keboola\NotificationClient\Exception\ClientException; |
| 13 | +use Keboola\NotificationClient\NotificationsClient; |
| 14 | +use Keboola\NotificationClient\Requests\PostNotification\ProjectEmail; |
| 15 | +use Keboola\NotificationClient\Requests\PostSubscription\EmailRecipient; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class NotificationsFunctionalClientTest extends TestCase |
| 19 | +{ |
| 20 | + private function getClient(): NotificationsClient |
| 21 | + { |
| 22 | + return new NotificationsClient( |
| 23 | + (string) getenv('TEST_NOTIFICATION_API_URL'), |
| 24 | + (string) getenv('TEST_MANAGE_API_APPLICATION_TOKEN'), |
| 25 | + [ |
| 26 | + 'backoffMaxTries' => 3, |
| 27 | + 'userAgent' => 'Test', |
| 28 | + ], |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + public function testCreateClientInvalidToken(): void |
| 34 | + { |
| 35 | + $this->expectException(ClientException::class); |
| 36 | + $this->expectExceptionMessage('Application token must be non-empty, "" provided.'); |
| 37 | + new NotificationsClient( |
| 38 | + 'https://example.com', |
| 39 | + '', |
| 40 | + [ |
| 41 | + 'backoffMaxTries' => 3, |
| 42 | + 'userAgent' => 'Test', |
| 43 | + ], |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function testPostNotification(): void |
| 48 | + { |
| 49 | + $client = $this->getClient(); |
| 50 | + $response = $client->postNotification(new ProjectEmail( |
| 51 | + new EmailRecipient('devel+test-notification-api-client@keboola.com'), |
| 52 | + (string) getenv('TEST_STORAGE_API_PROJECT_ID'), |
| 53 | + 'Test Project', |
| 54 | + 'Notification API client test', |
| 55 | + 'Test Notification Body', |
| 56 | + )); |
| 57 | + |
| 58 | + self::assertIsNumeric($response->getId()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testPostNotificationHeaders(): void |
| 62 | + { |
| 63 | + $mock = new MockHandler([ |
| 64 | + new Response( |
| 65 | + 200, |
| 66 | + ['Content-Type' => 'application/json'], |
| 67 | + '{"id": "1", "event": "2", "filters": [], "recipient": {"channel": "foo", "address": "bar"}}', |
| 68 | + ), |
| 69 | + ]); |
| 70 | + // Add the history middleware to the handler stack. |
| 71 | + $requestHistory = []; |
| 72 | + $history = Middleware::history($requestHistory); |
| 73 | + $stack = HandlerStack::create($mock); |
| 74 | + $stack->push($history); |
| 75 | + $client = new NotificationsClient( |
| 76 | + 'https://example.com/', |
| 77 | + 'testToken', |
| 78 | + ['handler' => $stack, 'backoffMaxTries' => 3, 'userAgent' => 'Test'], |
| 79 | + ); |
| 80 | + $client->postNotification(new ProjectEmail( |
| 81 | + new EmailRecipient('devel+test-notification-api-client@keboola.com'), |
| 82 | + (string) getenv('TEST_STORAGE_API_PROJECT_ID'), |
| 83 | + 'Test Project', |
| 84 | + 'Notification API client test', |
| 85 | + 'Test Notification Body', |
| 86 | + )); |
| 87 | + |
| 88 | + /** @var Request $request */ |
| 89 | + $request = $requestHistory[0]['request']; |
| 90 | + self::assertSame('POST', $request->getMethod()); |
| 91 | + self::assertSame( |
| 92 | + ['Content-Length', 'User-Agent', 'X-Kbc-ManageApiToken', 'Host', 'Content-type'], |
| 93 | + array_keys($request->getHeaders()), |
| 94 | + ); |
| 95 | + self::assertSame(['application/json'], $request->getHeaders()['Content-type']); |
| 96 | + } |
| 97 | +} |
0 commit comments