From ab8324a1786117fa567ebbb7b39723399d1c5011 Mon Sep 17 00:00:00 2001 From: Artem Bondarenko Date: Mon, 12 Oct 2020 20:31:10 +0300 Subject: [PATCH] domain tracking implementation fix active param, fix tests --- src/Api/Domain.php | 33 +++++--- src/Model/Domain/ClickTracking.php | 22 +++++- src/Model/Domain/OpenTracking.php | 20 ++++- src/Model/Domain/TrackingResponse.php | 2 +- src/Model/Domain/UnsubscribeTracking.php | 7 +- tests/Api/DomainTest.php | 78 ++++++++++++++----- tests/Model/Domain/TrackingResponseTest.php | 15 ++-- .../UpdateClickTrackingResponseTest.php | 5 +- .../Domain/UpdateOpenTrackingResponseTest.php | 4 +- 9 files changed, 142 insertions(+), 44 deletions(-) diff --git a/src/Api/Domain.php b/src/Api/Domain.php index 68d3585..2847113 100644 --- a/src/Api/Domain.php +++ b/src/Api/Domain.php @@ -325,13 +325,18 @@ class Domain extends HttpApi * * @param string $domain name of the domain * - * @param bool $active + * @param string $active * @return UpdateClickTrackingResponse|array|ResponseInterface + * @throws \Exception */ - public function updateClickTracking(string $domain, bool $active) + public function updateClickTracking(string $domain, string $active) { + Assert::stringNotEmpty($domain); + Assert::stringNotEmpty($active); + Assert::oneOf($active, ['yes', 'no', 'htmlonly']); + $params = [ - 'active' => $active ? 'true' : 'false', + 'active' => $active, ]; $response = $this->httpPut(sprintf('/v3/domains/%s/tracking/click', $domain), $params); @@ -347,10 +352,14 @@ class Domain extends HttpApi * * @return UpdateOpenTrackingResponse|array|ResponseInterface */ - public function updateOpenTracking(string $domain, bool $active) + public function updateOpenTracking(string $domain, string $active) { + Assert::stringNotEmpty($domain); + Assert::stringNotEmpty($active); + Assert::oneOf($active, ['yes', 'no']); + $params = [ - 'active' => $active ? 'true' : 'false', + 'active' => $active, ]; $response = $this->httpPut(sprintf('/v3/domains/%s/tracking/open', $domain), $params); @@ -362,15 +371,21 @@ class Domain extends HttpApi * Updates a domain unsubscribe tracking settings. * * @param string $domain name of the domain - * @param bool $active + * @param string $active * @param string $htmlFooter - * @param string $textFooter + * @param string|null $textFooter * @return UpdateUnsubscribeTrackingResponse|array|ResponseInterface */ - public function updateUnsubscribeTracking(string $domain, bool $active, string $htmlFooter, string $textFooter) + public function updateUnsubscribeTracking(string $domain, string $active, string $htmlFooter, string $textFooter) { + Assert::stringNotEmpty($domain); + Assert::stringNotEmpty($active); + Assert::oneOf($active, ['yes', 'no', 'true', 'false']); + Assert::stringNotEmpty($htmlFooter); + Assert::nullOrString($textFooter); + $params = [ - 'active' => $active ? 'true' : 'false', + 'active' => (in_array($active, ['yes', 'true'])) ? 'true' : 'false', 'html_footer' => $htmlFooter, 'text_footer' => $textFooter, ]; diff --git a/src/Model/Domain/ClickTracking.php b/src/Model/Domain/ClickTracking.php index 6101604..97825ad 100644 --- a/src/Model/Domain/ClickTracking.php +++ b/src/Model/Domain/ClickTracking.php @@ -23,7 +23,15 @@ final class ClickTracking public static function create(array $data): self { $model = new self(); - $model->active = (bool) ($data['active'] ?? null); + $active = $data['active'] ?? null; + + if (true === $active) { + $model->active = 'yes'; + } elseif (false === $active) { + $model->active = 'no'; + } else { + $model->active = $active; + } return $model; } @@ -32,8 +40,18 @@ final class ClickTracking { } - public function isActive(): bool + public function getActive(): ?string { return $this->active; } + + public function isActive(): bool + { + return $this->getActive() === 'yes'; + } + + public function isHtmlOnly(): bool + { + return $this->getActive() === 'htmlonly'; + } } diff --git a/src/Model/Domain/OpenTracking.php b/src/Model/Domain/OpenTracking.php index a99febd..768863c 100644 --- a/src/Model/Domain/OpenTracking.php +++ b/src/Model/Domain/OpenTracking.php @@ -23,7 +23,15 @@ final class OpenTracking public static function create(array $data): self { $model = new self(); - $model->active = (bool) ($data['active'] ?? null); + $active = $data['active'] ?? null; + + if (true === $active) { + $model->active = 'yes'; + } elseif (false === $active) { + $model->active = 'no'; + } else { + $model->active = $active; + } return $model; } @@ -32,8 +40,16 @@ final class OpenTracking { } - public function isActive(): bool + /** + * @return string + */ + public function getActive(): ?string { return $this->active; } + + public function isActive(): bool + { + return $this->getActive() === 'yes'; + } } diff --git a/src/Model/Domain/TrackingResponse.php b/src/Model/Domain/TrackingResponse.php index 0f68805..9a3d204 100644 --- a/src/Model/Domain/TrackingResponse.php +++ b/src/Model/Domain/TrackingResponse.php @@ -32,7 +32,7 @@ final class TrackingResponse implements ApiResponse $model = new self(); $model->click = ClickTracking::create($trackingSettings['click'] ?? []); - $model->open = OpenTracking::create($trackingSettings['click'] ?? []); + $model->open = OpenTracking::create($trackingSettings['open'] ?? []); $model->unsubscribe = UnsubscribeTracking::create($trackingSettings['unsubscribe'] ?? []); return $model; diff --git a/src/Model/Domain/UnsubscribeTracking.php b/src/Model/Domain/UnsubscribeTracking.php index e6c5d63..e2d510a 100644 --- a/src/Model/Domain/UnsubscribeTracking.php +++ b/src/Model/Domain/UnsubscribeTracking.php @@ -25,7 +25,7 @@ final class UnsubscribeTracking public static function create(array $data): self { $model = new self(); - $model->active = (bool) ($data['active'] ?? null); + $model->active = ($data['active'] ?? null) ? 'true' : 'false'; $model->htmlFooter = $data['html_footer'] ?? ''; $model->textFooter = $data['text_footer'] ?? ''; @@ -37,6 +37,11 @@ final class UnsubscribeTracking } public function isActive(): bool + { + return $this->active === 'true'; + } + + public function getActive(): string { return $this->active; } diff --git a/tests/Api/DomainTest.php b/tests/Api/DomainTest.php index 42bd103..3ad4023 100644 --- a/tests/Api/DomainTest.php +++ b/tests/Api/DomainTest.php @@ -13,6 +13,7 @@ namespace Mailgun\Tests\Api; use GuzzleHttp\Psr7\Response; use Mailgun\Api\Domain; +use Mailgun\Exception\InvalidArgumentException; use Mailgun\Model\Domain\ConnectionResponse; use Mailgun\Model\Domain\CreateCredentialResponse; use Mailgun\Model\Domain\CreateResponse; @@ -274,23 +275,24 @@ JSON $api->tracking('example.com'); } - public function activeInactiveDataProvider(): array + public function updateClickTrackingDataProvider(): array { return [ - [true], - [false], + ['yes'], + ['no'], + ['htmlonly'], ]; } /** - * @dataProvider activeInactiveDataProvider + * @dataProvider updateClickTrackingDataProvider */ - public function testUpdateClickTracking(bool $isActive) + public function testUpdateClickTracking(string $active) { $this->setRequestMethod('PUT'); $this->setRequestUri('/v3/domains/example.com/tracking/click'); $this->setRequestBody([ - 'active' => $isActive ? 'true' : 'false', + 'active' => $active, ]); $this->setHydrateClass(UpdateClickTrackingResponse::class); @@ -298,18 +300,37 @@ JSON * @var $api Domain */ $api = $this->getApiInstance(); - $api->updateClickTracking('example.com', $isActive); + $api->updateClickTracking('example.com', $active); + } + + public function testUpdateClickTrackingException() + { + $this->expectException(InvalidArgumentException::class); + + /** + * @var $api Domain + */ + $api = $this->getApiInstance(); + $api->updateClickTracking('example.com', 'non-valid-active-param'); + } + + public function updateOpenTrackingDataProvider(): array + { + return [ + ['yes'], + ['no'], + ]; } /** - * @dataProvider activeInactiveDataProvider + * @dataProvider updateOpenTrackingDataProvider */ - public function testUpdateOpenTracking(bool $isActive) + public function testUpdateOpenTracking(string $active) { $this->setRequestMethod('PUT'); $this->setRequestUri('/v3/domains/example.com/tracking/open'); $this->setRequestBody([ - 'active' => $isActive ? 'true' : 'false', + 'active' => $active, ]); $this->setHydrateClass(UpdateOpenTrackingResponse::class); @@ -317,29 +338,37 @@ JSON * @var $api Domain */ $api = $this->getApiInstance(); - $api->updateOpenTracking('example.com', $isActive); + $api->updateOpenTracking('example.com', $active); + } + + public function testUpdateOpenTrackingException() + { + $this->expectException(InvalidArgumentException::class); + + /** + * @var $api Domain + */ + $api = $this->getApiInstance(); + $api->updateOpenTracking('example.com', 'non-valid-active-param'); } public function unsubscribeDataProvider(): array { return [ - [true, 'Test', 'Test1'], - [false, 'Test', 'Test2'], + ['true', 'Test', 'Test1'], + ['false', 'Test', 'Test2'], ]; } /** * @dataProvider unsubscribeDataProvider - * @param bool $isActive - * @param string $htmlFooter - * @param string $textFooter */ - public function testUpdateUnsubscribeTracking(bool $isActive, string $htmlFooter, string $textFooter) + public function testUpdateUnsubscribeTracking(string $active, string $htmlFooter, string $textFooter) { $this->setRequestMethod('PUT'); $this->setRequestUri('/v3/domains/example.com/tracking/unsubscribe'); $this->setRequestBody([ - 'active' => $isActive ? 'true' : 'false', + 'active' => $active, 'html_footer' => $htmlFooter, 'text_footer' => $textFooter, ]); @@ -349,6 +378,17 @@ JSON * @var $api Domain */ $api = $this->getApiInstance(); - $api->updateUnsubscribeTracking('example.com', $isActive, $htmlFooter, $textFooter); + $api->updateUnsubscribeTracking('example.com', $active, $htmlFooter, $textFooter); + } + + public function testUpdateUnsubscribeTrackingException() + { + $this->expectException(InvalidArgumentException::class); + + /** + * @var $api Domain + */ + $api = $this->getApiInstance(); + $api->updateUnsubscribeTracking('example.com', 'non-valid-active-param', 'html-footer', 'text-footer'); } } diff --git a/tests/Model/Domain/TrackingResponseTest.php b/tests/Model/Domain/TrackingResponseTest.php index 371c984..29a01bb 100644 --- a/tests/Model/Domain/TrackingResponseTest.php +++ b/tests/Model/Domain/TrackingResponseTest.php @@ -26,13 +26,13 @@ class TrackingResponseTest extends BaseModelTest { "tracking": { "click": { - "active": true + "active": "htmlonly" }, "open": { - "active": true + "active": "no" }, "unsubscribe": { - "active": true, + "active": false, "html_footer": "Test<\/s>", "text_footer": "Test" } @@ -42,15 +42,18 @@ JSON; $model = TrackingResponse::create(json_decode($json, true)); $this->assertNotEmpty($model->getClick()); $this->assertInstanceOf(ClickTracking::class, $model->getClick()); - $this->assertTrue($model->getClick()->isActive()); + $this->assertEquals('htmlonly', $model->getClick()->getActive()); + $this->assertFalse($model->getClick()->isActive()); $this->assertNotEmpty($model->getOpen()); $this->assertInstanceOf(OpenTracking::class, $model->getOpen()); - $this->assertTrue($model->getOpen()->isActive()); + $this->assertEquals('no', $model->getOpen()->getActive()); + $this->assertFalse($model->getOpen()->isActive()); $this->assertNotEmpty($model->getUnsubscribe()); $this->assertInstanceOf(UnsubscribeTracking::class, $model->getUnsubscribe()); - $this->assertTrue($model->getUnsubscribe()->isActive()); + $this->assertEquals('false', $model->getUnsubscribe()->getActive()); + $this->assertFalse($model->getUnsubscribe()->isActive()); $this->assertEquals('Test', $model->getUnsubscribe()->getHtmlFooter()); $this->assertEquals('Test', $model->getUnsubscribe()->getTextFooter()); } diff --git a/tests/Model/Domain/UpdateClickTrackingResponseTest.php b/tests/Model/Domain/UpdateClickTrackingResponseTest.php index 6aa27e0..335b6cc 100644 --- a/tests/Model/Domain/UpdateClickTrackingResponseTest.php +++ b/tests/Model/Domain/UpdateClickTrackingResponseTest.php @@ -23,7 +23,7 @@ class UpdateClickTrackingResponseTest extends BaseModelTest <<<'JSON' { "click": { - "active": true + "active": "htmlonly" }, "message": "Domain tracking settings have been updated" } @@ -33,6 +33,7 @@ JSON; $this->assertEquals('Domain tracking settings have been updated', $model->getMessage()); $this->assertNotEmpty($model->getClick()); $this->assertInstanceOf(ClickTracking::class, $model->getClick()); - $this->assertTrue($model->getClick()->isActive()); + $this->assertEquals('htmlonly', $model->getClick()->getActive()); + $this->assertFalse($model->getClick()->isActive()); } } diff --git a/tests/Model/Domain/UpdateOpenTrackingResponseTest.php b/tests/Model/Domain/UpdateOpenTrackingResponseTest.php index 6afd7c2..9fbdd6a 100644 --- a/tests/Model/Domain/UpdateOpenTrackingResponseTest.php +++ b/tests/Model/Domain/UpdateOpenTrackingResponseTest.php @@ -23,7 +23,7 @@ class UpdateOpenTrackingResponseTest extends BaseModelTest <<<'JSON' { "open": { - "active": true + "active": "no" }, "message": "Domain tracking settings have been updated" } @@ -33,6 +33,6 @@ JSON; $this->assertEquals('Domain tracking settings have been updated', $model->getMessage()); $this->assertNotEmpty($model->getOpen()); $this->assertInstanceOf(OpenTracking::class, $model->getOpen()); - $this->assertTrue($model->getOpen()->isActive()); + $this->assertFalse($model->getOpen()->isActive()); } }