From 37b87deba5ea493fa5d9dc888bcf481ae1f5fe21 Mon Sep 17 00:00:00 2001 From: eliot lauger Date: Thu, 26 Sep 2024 15:07:58 +0200 Subject: [PATCH] feat: factory static to controller + change version of phpunit + add new test --- .phpunit.result.cache | 1 + Makefile | 1 + composer.json | 7 +- phpunit.xml.dist | 37 ++++++ src/SubscribeMe/Factory.php | 28 ++--- .../Subscriber/SendInBlueSubscriber.php | 16 ++- src/SubscribeMe/Subscriber/YmlpSubscriber.php | 2 - .../SubscriberFactoryInterface.php | 16 +++ ...MailerTestCase.php => BrevoMailerTest.php} | 106 +++++++++++++++++- ...erTestCase.php => MailchimpMailerTest.php} | 88 ++++++++++++++- ...ilerTestCase.php => MailjetMailerTest.php} | 50 ++++++++- tests/Unit/YmlpMailerTest.php | 104 +++++++++++++++++ tests/Unit/YmlpMailerTestCase.php | 56 --------- 13 files changed, 424 insertions(+), 88 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 phpunit.xml.dist create mode 100644 src/SubscribeMe/SubscriberFactoryInterface.php rename tests/Unit/{BrevoMailerTestCase.php => BrevoMailerTest.php} (50%) rename tests/Unit/{MailchimpMailerTestCase.php => MailchimpMailerTest.php} (51%) rename tests/Unit/{MailjetMailerTestCase.php => MailjetMailerTest.php} (68%) create mode 100644 tests/Unit/YmlpMailerTest.php delete mode 100644 tests/Unit/YmlpMailerTestCase.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..a461101 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"Unit\\MailchimpMailerTest::testSubscribe":3,"Unit\\MailjetMailerTest::testSubscribe":4,"Unit\\MailchimpMailerTest::testSubscribeWithMemberExist":3,"Unit\\MailchimpMailerTest::testSubscribeWithoutId":3,"Unit\\BrevoMailerTest::testSubscribeWithoutContactExist":3,"Unit\\BrevoMailerTest::testSubscribeWithContactExist":3,"Unit\\YmlpMailerTest::testSubscribe":3,"Unit\\YmlpMailerTest::testSubscribeWithoutCode":4},"times":{"Unit\\BrevoMailerTestCase::testSubscribe":0.003,"Unit\\BrevoMailerTestCase::testSendTransactionalEmail":0,"Unit\\BrevoMailerTestCase::testExceptionApiKey":0,"Unit\\BrevoMailerTest::testSubscribe":0.003,"Unit\\BrevoMailerTest::testSendTransactionalEmail":0,"Unit\\BrevoMailerTest::testExceptionApiKey":0,"Unit\\MailchimpMailerTest::testSubscribe":0,"Unit\\MailchimpMailerTest::testSendTransactionalEmail":0,"Unit\\MailchimpMailerTest::testExceptionApiKey":0,"Unit\\MailjetMailerTest::testSubscribe":0,"Unit\\MailjetMailerTest::testSendTransactionalEmail":0,"Unit\\MailjetMailerTest::testExceptionApiKey":0,"Unit\\YmlpMailerTest::testSubscribe":0,"Unit\\YmlpMailerTest::testSendTransactionalEmail":0,"Unit\\MailchimpMailerTest::testSubscribeWithMemberExist":0,"Unit\\MailchimpMailerTest::testSubscribeWithoutId":0,"Unit\\MailjetMailerTest::testSubscribeWithCodeError":0,"Unit\\BrevoMailerTest::testSubscribeWithoutContactExist":0,"Unit\\BrevoMailerTest::testSubscribeWithoutId":0,"Unit\\BrevoMailerTest::testSubscribeWithContactExist":0,"Unit\\YmlpMailerTest::testSubscribeWithoutCode":0}} \ No newline at end of file diff --git a/Makefile b/Makefile index 698a2cd..6113801 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ test: vendor/bin/phpcbf -p vendor/bin/phpstan analyse -c phpstan.neon + vendor/bin/phpunit changelog: git-cliff -o CHANGELOG.md diff --git a/composer.json b/composer.json index 23d7727..c617f3e 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require-dev": { "phpstan/phpstan": "^1.9.2", "squizlabs/php_codesniffer": "^3.5.4", - "phpunit/phpunit": "^11.3", + "phpunit/phpunit": "^9.6", "php-http/mock-client": "^1.6", "nyholm/psr7": "^1.8" }, @@ -31,5 +31,10 @@ "allow-plugins": { "php-http/discovery": true } + }, + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..37f322c --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,37 @@ + + + + + + + + + + + + + tests/Unit + + + + + + src + + + + + + + + \ No newline at end of file diff --git a/src/SubscribeMe/Factory.php b/src/SubscribeMe/Factory.php index a628b62..b60cfd2 100644 --- a/src/SubscribeMe/Factory.php +++ b/src/SubscribeMe/Factory.php @@ -14,30 +14,30 @@ use SubscribeMe\Subscriber\SubscriberInterface; use SubscribeMe\Subscriber\YmlpSubscriber; -class Factory +final class Factory implements SubscriberFactoryInterface { - /** - * @param string $platform - * @param ClientInterface $client - * @param RequestFactoryInterface $requestFactory - * @param StreamFactoryInterface $streamFactory - * @return SubscriberInterface - */ - public static function createFor(string $platform, ClientInterface $client, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory): SubscriberInterface + public function __construct( + private ClientInterface $client, + private RequestFactoryInterface $requestFactory, + private StreamFactoryInterface $streamFactory + ) { + } + + public function createFor(string $platform): SubscriberInterface { switch (strtolower($platform)) { case 'mailjet': - return new MailjetSubscriber($client, $requestFactory, $streamFactory); + return new MailjetSubscriber($this->client, $this->requestFactory, $this->streamFactory); case 'mailchimp': - return new MailchimpSubscriber($client, $requestFactory, $streamFactory); + return new MailchimpSubscriber($this->client, $this->requestFactory, $this->streamFactory); case 'sendinblue': case 'brevo': - return new BrevoSubscriber($client, $requestFactory, $streamFactory); + return new BrevoSubscriber($this->client, $this->requestFactory, $this->streamFactory); case 'sendinblue-doi': case 'brevo-doi': - return new BrevoDoubleOptInSubscriber($client, $requestFactory, $streamFactory); + return new BrevoDoubleOptInSubscriber($this->client, $this->requestFactory, $this->streamFactory); case 'ymlp': - return new YmlpSubscriber($client, $requestFactory, $streamFactory); + return new YmlpSubscriber($this->client, $this->requestFactory, $this->streamFactory); } throw new \InvalidArgumentException('No subscriber class found for ' . $platform); } diff --git a/src/SubscribeMe/Subscriber/SendInBlueSubscriber.php b/src/SubscribeMe/Subscriber/SendInBlueSubscriber.php index 2ec1931..4fcf2b3 100644 --- a/src/SubscribeMe/Subscriber/SendInBlueSubscriber.php +++ b/src/SubscribeMe/Subscriber/SendInBlueSubscriber.php @@ -108,22 +108,20 @@ protected function doSubscribe(string $uri, array $body): bool|int // https://developers.sendinblue.com/reference/createcontact if ($res->getStatusCode() === 200 || $res->getStatusCode() === 201 || - $res->getStatusCode() === 204 + $res->getStatusCode() === 204 || + $res->getStatusCode() === 400 ) { /** @var array $body */ $body = json_decode($res->getBody()->getContents(), true); if (isset($body['id'])) { return (int) $body['id']; } - } - if ($res->getStatusCode() === 400 && - isset($body['message']) && - $body['message'] == 'Contact already exist') { - /* - * Do not throw exception if subscriber already exists - */ - return true; + if ($res->getStatusCode() === 400 && + isset($body['message']) && + $body['message'] == 'Contact already exist') { + return true; + } } } catch (ClientExceptionInterface $exception) { throw new CannotSubscribeException($exception->getMessage(), $exception); diff --git a/src/SubscribeMe/Subscriber/YmlpSubscriber.php b/src/SubscribeMe/Subscriber/YmlpSubscriber.php index abb60a2..aa5951c 100644 --- a/src/SubscribeMe/Subscriber/YmlpSubscriber.php +++ b/src/SubscribeMe/Subscriber/YmlpSubscriber.php @@ -115,8 +115,6 @@ public function subscribe(string $email, array $options, array $userConsents = [ * Do not throw exception if subscriber already exists */ return true; - } elseif (isset($body['Output']) && is_string($body['Output'])) { - throw new CannotSubscribeException($body['Output']); } elseif (isset($body['Output']) && $body['Output'] == 'Email address already in selected groups') { /* diff --git a/src/SubscribeMe/SubscriberFactoryInterface.php b/src/SubscribeMe/SubscriberFactoryInterface.php new file mode 100644 index 0000000..bf4c3f5 --- /dev/null +++ b/src/SubscribeMe/SubscriberFactoryInterface.php @@ -0,0 +1,16 @@ + 1], JSON_THROW_ON_ERROR)); + $client->setDefaultResponse($response); + + $brevoSubscriber = new BrevoSubscriber($client, $factory, $factory); + + $email = 'elly@example.com'; + $options = [ + 'FNAME' => 'Elly', + 'LNAME' => 'Roger', + 'COUNTRY' => [ + 'India', + 'China' + ] + ]; + + $brevoSubscriber->setContactListId('3,5'); + $brevoSubscriber->setApiKey('928f601b-5476-4480-8eb0-c8d979f3b68f'); + $returnCode = $brevoSubscriber->subscribe($email, $options); + + $requests = $client->getRequests(); + + $body = [ + 'updateEnabled' => true, + 'email' => $email, + 'listIds' => [3,5], + 'attributes' => $options + ]; + $body = json_encode($body); + + $this->assertEquals(1, $returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('928f601b-5476-4480-8eb0-c8d979f3b68f', $requests[0]->getHeaders()['api-key'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertJsonStringEqualsJsonString($body ?: '{}', $content); + $this->assertEquals('api.brevo.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSubscribeWithContactExist(): void + { + $client = new Client(); + $factory = new Psr17Factory(); + + $response = new Response(400, [], json_encode(['message' => 'Contact already exist'], JSON_THROW_ON_ERROR)); + $client->setDefaultResponse($response); + + $brevoSubscriber = new BrevoSubscriber($client, $factory, $factory); + + $email = 'elly@example.com'; + $options = [ + 'FNAME' => 'Elly', + 'LNAME' => 'Roger', + 'COUNTRY' => [ + 'India', + 'China' + ] + ]; + + $brevoSubscriber->setContactListId('3,5'); + $brevoSubscriber->setApiKey('928f601b-5476-4480-8eb0-c8d979f3b68f'); + $returnCode = $brevoSubscriber->subscribe($email, $options); + + $requests = $client->getRequests(); + + $body = [ + 'updateEnabled' => true, + 'email' => $email, + 'listIds' => [3,5], + 'attributes' => $options + ]; + $body = json_encode($body); + + $this->assertTrue($returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('928f601b-5476-4480-8eb0-c8d979f3b68f', $requests[0]->getHeaders()['api-key'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertJsonStringEqualsJsonString($body ?: '{}', $content); + $this->assertEquals('api.brevo.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSubscribeWithoutId(): void + { + $client = new Client(); + $factory = new Psr17Factory(); + + $response = new Response(400); + $client->setDefaultResponse($response); + $brevoSubscriber = new BrevoSubscriber($client, $factory, $factory); $email = 'elly@example.com'; @@ -35,7 +136,7 @@ public function testSubscribe(): void $brevoSubscriber->setContactListId('3,5'); $brevoSubscriber->setApiKey('928f601b-5476-4480-8eb0-c8d979f3b68f'); - $brevoSubscriber->subscribe($email, $options); + $returnCode = $brevoSubscriber->subscribe($email, $options); $requests = $client->getRequests(); @@ -47,6 +148,7 @@ public function testSubscribe(): void ]; $body = json_encode($body); + $this->assertFalse($returnCode); $this->assertCount(1, $requests); $content = $requests[0]->getBody()->getContents(); $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); diff --git a/tests/Unit/MailchimpMailerTestCase.php b/tests/Unit/MailchimpMailerTest.php similarity index 51% rename from tests/Unit/MailchimpMailerTestCase.php rename to tests/Unit/MailchimpMailerTest.php index 342c785..7201549 100644 --- a/tests/Unit/MailchimpMailerTestCase.php +++ b/tests/Unit/MailchimpMailerTest.php @@ -4,15 +4,18 @@ namespace Unit; +use Http\Client\Exception; use Http\Discovery\Psr17Factory; use Http\Mock\Client; use JsonException; +use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\ResponseInterface; use SubscribeMe\Exception\MissingApiCredentialsException; use SubscribeMe\Subscriber\MailchimpSubscriber; use SubscribeMe\ValueObject\EmailAddress; -class MailchimpMailerTestCase extends TestCase +class MailchimpMailerTest extends TestCase { /** * @throws JsonException @@ -21,12 +24,92 @@ public function testSubscribe(): void { $client = new Client(); $factory = new Psr17Factory(); + + $response = new Response(200, [], json_encode(['id' => 1 ,'title' => 'test'], JSON_THROW_ON_ERROR)); + $client->setDefaultResponse($response); + + $mailchimpSubscriber = new MailchimpSubscriber($client, $factory, $factory); + + $mailchimpSubscriber->setContactListId('1'); + $mailchimpSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); + $mailchimpSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); + $returnCode = $mailchimpSubscriber->subscribe("jdoe@example.com", []); + + $requests = $client->getRequests(); + + $body = [ + 'status' => 'subscribed', + 'email_address' => 'jdoe@example.com', + ]; + $body = json_encode($body); + + + $this->assertEquals(1, $returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('Basic ' . base64_encode(sprintf('%s:%s', '3f62c1f4-efb7-4bc7-b76d-0c2217d307b0', 'df30148e-6cda-43ae-8665-9904f5f4f12a')), $requests[0]->getHeaders()['Authorization'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertJsonStringEqualsJsonString($body ?: '{}', $content); + $this->assertEquals('us16.api.mailchimp.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSubscribeWithMemberExist(): void + { + $client = new Client(); + $factory = new Psr17Factory(); + + $response = new Response(200, [], json_encode(['title' => 'Member Exists'], JSON_THROW_ON_ERROR)); + $client->setDefaultResponse($response); + + $mailchimpSubscriber = new MailchimpSubscriber($client, $factory, $factory); + + $mailchimpSubscriber->setContactListId('1'); + $mailchimpSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); + $mailchimpSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); + $returnCode = $mailchimpSubscriber->subscribe("jdoe@example.com", []); + + $requests = $client->getRequests(); + + $body = [ + 'status' => 'subscribed', + 'email_address' => 'jdoe@example.com', + ]; + $body = json_encode($body); + + + $this->assertTrue($returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('Basic ' . base64_encode(sprintf('%s:%s', '3f62c1f4-efb7-4bc7-b76d-0c2217d307b0', 'df30148e-6cda-43ae-8665-9904f5f4f12a')), $requests[0]->getHeaders()['Authorization'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertJsonStringEqualsJsonString($body ?: '{}', $content); + $this->assertEquals('us16.api.mailchimp.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSubscribeWithoutId(): void + { + $client = new Client(); + $factory = new Psr17Factory(); + + $response = new Response(200, [], json_encode(['id' => null ,'title' => ''], JSON_THROW_ON_ERROR)); + $client->setDefaultResponse($response); + $mailchimpSubscriber = new MailchimpSubscriber($client, $factory, $factory); $mailchimpSubscriber->setContactListId('1'); $mailchimpSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); $mailchimpSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); - $mailchimpSubscriber->subscribe("jdoe@example.com", []); + $returnCode = $mailchimpSubscriber->subscribe("jdoe@example.com", []); $requests = $client->getRequests(); @@ -36,6 +119,7 @@ public function testSubscribe(): void ]; $body = json_encode($body); + $this->assertFalse($returnCode); $this->assertCount(1, $requests); $content = $requests[0]->getBody()->getContents(); $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); diff --git a/tests/Unit/MailjetMailerTestCase.php b/tests/Unit/MailjetMailerTest.php similarity index 68% rename from tests/Unit/MailjetMailerTestCase.php rename to tests/Unit/MailjetMailerTest.php index 2fb9957..fde8f7b 100644 --- a/tests/Unit/MailjetMailerTestCase.php +++ b/tests/Unit/MailjetMailerTest.php @@ -7,12 +7,13 @@ use Http\Discovery\Psr17Factory; use Http\Mock\Client; use JsonException; +use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use SubscribeMe\Exception\MissingApiCredentialsException; use SubscribeMe\Subscriber\MailjetSubscriber; use SubscribeMe\ValueObject\EmailAddress; -class MailjetMailerTestCase extends TestCase +class MailjetMailerTest extends TestCase { /** * @throws JsonException @@ -21,6 +22,50 @@ public function testSubscribe(): void { $client = new Client(); $factory = new Psr17Factory(); + + $client->setDefaultResponse(new Response(200, [], json_encode(['Total' => 1, 'Data' => [['ContactID' => 1]] ], JSON_THROW_ON_ERROR))); + + $mailjetSubscriber = new MailjetSubscriber($client, $factory, $factory); + + $options = [ + 'Name' => 'Passenger 1', + ]; + + $mailjetSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); + $mailjetSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); + $returnCode = $mailjetSubscriber->subscribe("passenger@mailjet.com", $options); + + $requests = $client->getRequests(); + + $body = [ + 'Action' => 'addnoforce', + 'Email' => 'passenger@mailjet.com', + 'Name' => 'Passenger 1', + 'Properties' => [] + ]; + $body = json_encode($body); + + $this->assertEquals(1, $returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('Basic ' . base64_encode(sprintf('%s:%s', '3f62c1f4-efb7-4bc7-b76d-0c2217d307b0', 'df30148e-6cda-43ae-8665-9904f5f4f12a')), $requests[0]->getHeaders()['Authorization'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertJsonStringEqualsJsonString($body ?: '{}', $content); + $this->assertEquals('api.mailjet.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSubscribeWithCodeError(): void + { + $client = new Client(); + $factory = new Psr17Factory(); + + $client->setDefaultResponse(new Response(400)); + $mailjetSubscriber = new MailjetSubscriber($client, $factory, $factory); $options = [ @@ -29,7 +74,7 @@ public function testSubscribe(): void $mailjetSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); $mailjetSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); - $mailjetSubscriber->subscribe("passenger@mailjet.com", $options); + $returnCode = $mailjetSubscriber->subscribe("passenger@mailjet.com", $options); $requests = $client->getRequests(); @@ -41,6 +86,7 @@ public function testSubscribe(): void ]; $body = json_encode($body); + $this->assertFalse($returnCode); $this->assertCount(1, $requests); $content = $requests[0]->getBody()->getContents(); $this->assertEquals('application/json', $requests[0]->getHeaders()['Content-Type'][0]); diff --git a/tests/Unit/YmlpMailerTest.php b/tests/Unit/YmlpMailerTest.php new file mode 100644 index 0000000..d620cc1 --- /dev/null +++ b/tests/Unit/YmlpMailerTest.php @@ -0,0 +1,104 @@ + '3'], JSON_THROW_ON_ERROR)); + $client->setDefaultResponse($response); + + $ymlpSubscriber = new YmlpSubscriber($client, $factory, $factory); + + $ymlpSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); + $ymlpSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); + $returnCode = $ymlpSubscriber->subscribe("jdoe@example.com", []); + + $requests = $client->getRequests(); + + $body = [ + 'Key' => $ymlpSubscriber->getApiSecret(), + 'Username' => $ymlpSubscriber->getApiKey(), + 'OverruleUnsubscribedBounced' => 0, + 'Email' => 'jdoe@example.com', + 'GroupID' => 0, + 'Output' => 'JSON' + ]; + $body = http_build_query($body, '', '&'); + + $this->assertTrue($returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('x-www-form-urlencoded', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertEquals($body, $content); + $this->assertEquals('www.ymlp.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSubscribeWithoutCode(): void + { + $client = new Client(); + $factory = new Psr17Factory(); + + $response = new Response( + 200, + [], + json_encode(['Output' => 'Email address already in selected groups'], JSON_THROW_ON_ERROR) + ); + $client->setDefaultResponse($response); + + $ymlpSubscriber = new YmlpSubscriber($client, $factory, $factory); + + $ymlpSubscriber->setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); + $ymlpSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); + $returnCode = $ymlpSubscriber->subscribe("jdoe@example.com", []); + + $requests = $client->getRequests(); + + $body = "Key=df30148e-6cda-43ae-8665-9904f5f4f12a&Username=3f62c1f4-efb7-4bc7-b76d-0c2217d307b0&OverruleUnsubscribedBounced=0&Email=jdoe%40example.com&GroupID=0&Output=JSON"; + + $this->assertTrue($returnCode); + $this->assertCount(1, $requests); + $content = $requests[0]->getBody()->getContents(); + $this->assertEquals('x-www-form-urlencoded', $requests[0]->getHeaders()['Content-Type'][0]); + $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); + $this->assertEquals('POST', $requests[0]->getMethod()); + $this->assertEquals($body, $content); + $this->assertEquals('www.ymlp.com', $requests[0]->getUri()->getHost()); + } + + /** + * @throws JsonException + */ + public function testSendTransactionalEmail(): void + { + $this->expectException(UnsupportedTransactionalEmailPlatformException::class); + $client = new Client(); + $factory = new Psr17Factory(); + $ymlpSubscriber = new YmlpSubscriber($client, $factory, $factory); + $emails[0] = new EmailAddress('passenger1@mailjet.com', 'passenger 1'); + $ymlpSubscriber->sendTransactionalEmail($emails, '1', []); + } +} diff --git a/tests/Unit/YmlpMailerTestCase.php b/tests/Unit/YmlpMailerTestCase.php deleted file mode 100644 index d9d97fa..0000000 --- a/tests/Unit/YmlpMailerTestCase.php +++ /dev/null @@ -1,56 +0,0 @@ -setApiKey('3f62c1f4-efb7-4bc7-b76d-0c2217d307b0'); - $ymlpSubscriber->setApiSecret('df30148e-6cda-43ae-8665-9904f5f4f12a'); - $ymlpSubscriber->subscribe("jdoe@example.com", []); - - $requests = $client->getRequests(); - - $body = "Key=df30148e-6cda-43ae-8665-9904f5f4f12a&Username=3f62c1f4-efb7-4bc7-b76d-0c2217d307b0&OverruleUnsubscribedBounced=0&Email=jdoe%40example.com&GroupID=0&Output=JSON"; - - $this->assertCount(1, $requests); - $content = $requests[0]->getBody()->getContents(); - $this->assertEquals('x-www-form-urlencoded', $requests[0]->getHeaders()['Content-Type'][0]); - $this->assertEquals('rezozero/subscribeme', $requests[0]->getHeaders()['User-Agent'][0]); - $this->assertEquals('POST', $requests[0]->getMethod()); - $this->assertEquals($body, $content); - $this->assertEquals('www.ymlp.com', $requests[0]->getUri()->getHost()); - } - - /** - * @throws JsonException - */ - public function testSendTransactionalEmail(): void - { - $this->expectException(UnsupportedTransactionalEmailPlatformException::class); - $client = new Client(); - $factory = new Psr17Factory(); - $ymlpSubscriber = new YmlpSubscriber($client, $factory, $factory); - $emails[0] = new EmailAddress('passenger1@mailjet.com', 'passenger 1'); - $ymlpSubscriber->sendTransactionalEmail($emails, '1', []); - } -}