domain tracking implementation fix active param, fix tests

This commit is contained in:
Artem Bondarenko 2020-10-12 20:31:10 +03:00 committed by David Garcia
parent 4f8888daed
commit ab8324a178
9 changed files with 142 additions and 44 deletions

View File

@ -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,
];

View File

@ -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';
}
}

View File

@ -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';
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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, '<b>Test</b>', 'Test1'],
[false, '<s>Test</s>', 'Test2'],
['true', '<b>Test</b>', 'Test1'],
['false', '<s>Test</s>', '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');
}
}

View File

@ -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": "<s>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('<s>Test</s>', $model->getUnsubscribe()->getHtmlFooter());
$this->assertEquals('Test', $model->getUnsubscribe()->getTextFooter());
}

View File

@ -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());
}
}

View File

@ -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());
}
}