From 0a9562e73c990c2e33c6a60faf468ff1dbb48718 Mon Sep 17 00:00:00 2001 From: Artem Bondarenko Date: Mon, 12 Oct 2020 23:24:36 +0300 Subject: [PATCH] mailing list validation --- src/Api/MailingList.php | 57 +++++++++++++ src/Model/MailingList/ValidateResponse.php | 43 ++++++++++ .../MailingList/ValidationCancelResponse.php | 36 +++++++++ .../ValidationStatusDownloadUrl.php | 43 ++++++++++ .../MailingList/ValidationStatusResponse.php | 79 +++++++++++++++++++ .../MailingList/ValidationStatusSummary.php | 43 ++++++++++ .../ValidationStatusSummaryResult.php | 57 +++++++++++++ .../ValidationStatusSummaryRisk.php | 57 +++++++++++++ tests/Api/MailingListTest.php | 33 ++++++++ .../MailingList/ValidateResponseTest.php | 32 ++++++++ .../ValidationCancelResponseTest.php | 30 +++++++ .../ValidationStatusDownloadUrlTest.php | 32 ++++++++ .../ValidationStatusResponseTest.php | 74 +++++++++++++++++ .../ValidationStatusSummaryResultTest.php | 36 +++++++++ .../ValidationStatusSummaryRiskTest.php | 36 +++++++++ .../ValidationStatusSummaryTest.php | 53 +++++++++++++ 16 files changed, 741 insertions(+) create mode 100644 src/Model/MailingList/ValidateResponse.php create mode 100644 src/Model/MailingList/ValidationCancelResponse.php create mode 100644 src/Model/MailingList/ValidationStatusDownloadUrl.php create mode 100644 src/Model/MailingList/ValidationStatusResponse.php create mode 100644 src/Model/MailingList/ValidationStatusSummary.php create mode 100644 src/Model/MailingList/ValidationStatusSummaryResult.php create mode 100644 src/Model/MailingList/ValidationStatusSummaryRisk.php create mode 100644 tests/Model/MailingList/ValidateResponseTest.php create mode 100644 tests/Model/MailingList/ValidationCancelResponseTest.php create mode 100644 tests/Model/MailingList/ValidationStatusDownloadUrlTest.php create mode 100644 tests/Model/MailingList/ValidationStatusResponseTest.php create mode 100644 tests/Model/MailingList/ValidationStatusSummaryResultTest.php create mode 100644 tests/Model/MailingList/ValidationStatusSummaryRiskTest.php create mode 100644 tests/Model/MailingList/ValidationStatusSummaryTest.php diff --git a/src/Api/MailingList.php b/src/Api/MailingList.php index 2f48883..e87f607 100644 --- a/src/Api/MailingList.php +++ b/src/Api/MailingList.php @@ -13,11 +13,14 @@ namespace Mailgun\Api; use Mailgun\Api\MailingList\Member; use Mailgun\Assert; +use Mailgun\Model\EmailValidation\ValidateResponse; use Mailgun\Model\MailingList\CreateResponse; use Mailgun\Model\MailingList\DeleteResponse; use Mailgun\Model\MailingList\PagesResponse; use Mailgun\Model\MailingList\ShowResponse; use Mailgun\Model\MailingList\UpdateResponse; +use Mailgun\Model\MailingList\ValidationCancelResponse; +use Mailgun\Model\MailingList\ValidationStatusResponse; /** * @see https://documentation.mailgun.com/en/latest/api-mailinglists.html @@ -154,4 +157,58 @@ class MailingList extends HttpApi return $this->hydrateResponse($response, DeleteResponse::class); } + + /** + * Validates mailing list. + * + * @param string $address Address of the mailing list + * + * @return ValidateResponse + * + * @throws \Exception + */ + public function validate(string $address) + { + Assert::stringNotEmpty($address); + + $response = $this->httpPost(sprintf('/v3/lists/%s/validate', $address)); + + return $this->hydrateResponse($response, ValidateResponse::class); + } + + /** + * Get mailing list validation status. + * + * @param string $address Address of the mailing list + * + * @return ValidationStatusResponse + * + * @throws \Exception + */ + public function getValidationStatus(string $address) + { + Assert::stringNotEmpty($address); + + $response = $this->httpGet(sprintf('/v3/lists/%s/validate', $address)); + + return $this->hydrateResponse($response, ValidationStatusResponse::class); + } + + /** + * Cancel mailing list validation. + * + * @param string $address Address of the mailing list + * + * @return ValidationCancelResponse + * + * @throws \Exception + */ + public function cancelValidation(string $address) + { + Assert::stringNotEmpty($address); + + $response = $this->httpDelete(sprintf('/v3/lists/%s/validate', $address)); + + return $this->hydrateResponse($response, ValidationCancelResponse::class); + } } diff --git a/src/Model/MailingList/ValidateResponse.php b/src/Model/MailingList/ValidateResponse.php new file mode 100644 index 0000000..6dc8a48 --- /dev/null +++ b/src/Model/MailingList/ValidateResponse.php @@ -0,0 +1,43 @@ +id = $data['id'] ?? ''; + $model->message = $data['message'] ?? ''; + + return $model; + } + + private function __construct() + { + } + + public function getMessage(): string + { + return $this->message; + } + + public function getId(): string + { + return $this->id; + } +} diff --git a/src/Model/MailingList/ValidationCancelResponse.php b/src/Model/MailingList/ValidationCancelResponse.php new file mode 100644 index 0000000..ec7b971 --- /dev/null +++ b/src/Model/MailingList/ValidationCancelResponse.php @@ -0,0 +1,36 @@ +message = $data['message'] ?? null; + + return $model; + } + + private function __construct() + { + } + + public function getMessage(): ?string + { + return $this->message; + } +} diff --git a/src/Model/MailingList/ValidationStatusDownloadUrl.php b/src/Model/MailingList/ValidationStatusDownloadUrl.php new file mode 100644 index 0000000..60f9958 --- /dev/null +++ b/src/Model/MailingList/ValidationStatusDownloadUrl.php @@ -0,0 +1,43 @@ +csv = $data['csv'] ?? ''; + $model->json = $data['json'] ?? ''; + + return $model; + } + + private function __construct() + { + } + + public function getCsv(): string + { + return $this->csv; + } + + public function getJson(): string + { + return $this->json; + } +} diff --git a/src/Model/MailingList/ValidationStatusResponse.php b/src/Model/MailingList/ValidationStatusResponse.php new file mode 100644 index 0000000..5f3dc4a --- /dev/null +++ b/src/Model/MailingList/ValidationStatusResponse.php @@ -0,0 +1,79 @@ +id = $data['id'] ?? ''; + $model->createdAt = $data['created_at'] ?? null; + $model->downloadUrl = ValidationStatusDownloadUrl::create($data['download_url']); + $model->id = $data['id'] ?? ''; + $model->quantity = $data['quantity'] ?? 0; + $model->recordsProcessed = $data['records_processed'] ?? 0; + $model->status = $data['status'] ?? ''; + $model->summary = ValidationStatusSummary::create($data['summary'] ?? []); + + return $model; + } + + private function __construct() + { + } + + public function getCreatedAt(): string + { + return $this->createdAt; + } + + public function getDownloadUrl(): ValidationStatusDownloadUrl + { + return $this->downloadUrl; + } + + public function getId(): string + { + return $this->id; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function getRecordsProcessed(): int + { + return $this->recordsProcessed; + } + + public function getStatus(): string + { + return $this->status; + } + + public function getSummary(): ValidationStatusSummary + { + return $this->summary; + } +} diff --git a/src/Model/MailingList/ValidationStatusSummary.php b/src/Model/MailingList/ValidationStatusSummary.php new file mode 100644 index 0000000..8cb494d --- /dev/null +++ b/src/Model/MailingList/ValidationStatusSummary.php @@ -0,0 +1,43 @@ +result = ValidationStatusSummaryResult::create($data['result'] ?? []); + $model->risk = ValidationStatusSummaryRisk::create($data['risk'] ?? []); + + return $model; + } + + private function __construct() + { + } + + public function getResult(): ValidationStatusSummaryResult + { + return $this->result; + } + + public function getRisk(): ValidationStatusSummaryRisk + { + return $this->risk; + } +} diff --git a/src/Model/MailingList/ValidationStatusSummaryResult.php b/src/Model/MailingList/ValidationStatusSummaryResult.php new file mode 100644 index 0000000..d14418f --- /dev/null +++ b/src/Model/MailingList/ValidationStatusSummaryResult.php @@ -0,0 +1,57 @@ +deliverable = $data['deliverable'] ?? null; + $model->doNotSend = $data['do_not_send'] ?? null; + $model->undeliverable = $data['undeliverable'] ?? null; + $model->unknown = $data['unknown'] ?? null; + + return $model; + } + + private function __construct() + { + } + + public function getDeliverable(): ?int + { + return $this->deliverable; + } + + public function getDoNotSend(): ?int + { + return $this->doNotSend; + } + + public function getUndeliverable(): ?int + { + return $this->undeliverable; + } + + public function getUnknown(): ?int + { + return $this->unknown; + } +} diff --git a/src/Model/MailingList/ValidationStatusSummaryRisk.php b/src/Model/MailingList/ValidationStatusSummaryRisk.php new file mode 100644 index 0000000..b8fce41 --- /dev/null +++ b/src/Model/MailingList/ValidationStatusSummaryRisk.php @@ -0,0 +1,57 @@ +high = $data['high'] ?? null; + $model->low = $data['low'] ?? null; + $model->medium = $data['medium'] ?? null; + $model->unknown = $data['unknown'] ?? null; + + return $model; + } + + private function __construct() + { + } + + public function getHigh(): ?int + { + return $this->high; + } + + public function getLow(): ?int + { + return $this->low; + } + + public function getMedium(): ?int + { + return $this->medium; + } + + public function getUnknown(): ?int + { + return $this->unknown; + } +} diff --git a/tests/Api/MailingListTest.php b/tests/Api/MailingListTest.php index ae1f38c..fec1aed 100644 --- a/tests/Api/MailingListTest.php +++ b/tests/Api/MailingListTest.php @@ -14,6 +14,9 @@ namespace Mailgun\Tests\Api; use GuzzleHttp\Psr7\Response; use Mailgun\Api\MailingList; use Mailgun\Exception\InvalidArgumentException; +use Mailgun\Model\EmailValidation\ValidateResponse; +use Mailgun\Model\MailingList\ValidationCancelResponse; +use Mailgun\Model\MailingList\ValidationStatusResponse; class MailingListTest extends TestCase { @@ -133,6 +136,36 @@ class MailingListTest extends TestCase $api->delete('address'); } + public function testValidate() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v3/lists/address@domain/validate'); + $this->setHydrateClass(ValidateResponse::class); + + $api = $this->getApiInstance(); + $api->validate('address@domain'); + } + + public function testValidationStatus() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v3/lists/address@domain/validate'); + $this->setHydrateClass(ValidationStatusResponse::class); + + $api = $this->getApiInstance(); + $api->getValidationStatus('address@domain'); + } + + public function testCancelValidate() + { + $this->setRequestMethod('DELETE'); + $this->setRequestUri('/v3/lists/address@domain/validate'); + $this->setHydrateClass(ValidationCancelResponse::class); + + $api = $this->getApiInstance(); + $api->cancelValidation('address@domain'); + } + /** * {@inheritdoc} */ diff --git a/tests/Model/MailingList/ValidateResponseTest.php b/tests/Model/MailingList/ValidateResponseTest.php new file mode 100644 index 0000000..949758a --- /dev/null +++ b/tests/Model/MailingList/ValidateResponseTest.php @@ -0,0 +1,32 @@ +assertEquals('The validation job was submitted.', $model->getMessage()); + $this->assertEquals('listname@yourdomain.com', $model->getId()); + } +} diff --git a/tests/Model/MailingList/ValidationCancelResponseTest.php b/tests/Model/MailingList/ValidationCancelResponseTest.php new file mode 100644 index 0000000..9f7c10a --- /dev/null +++ b/tests/Model/MailingList/ValidationCancelResponseTest.php @@ -0,0 +1,30 @@ +assertEquals('Validation job canceled.', $model->getMessage()); + } +} diff --git a/tests/Model/MailingList/ValidationStatusDownloadUrlTest.php b/tests/Model/MailingList/ValidationStatusDownloadUrlTest.php new file mode 100644 index 0000000..dc563a6 --- /dev/null +++ b/tests/Model/MailingList/ValidationStatusDownloadUrlTest.php @@ -0,0 +1,32 @@ +assertEquals('http://example.com/filname.csv', $model->getCsv()); + $this->assertEquals('http://example.com/filname.json', $model->getJson()); + } +} diff --git a/tests/Model/MailingList/ValidationStatusResponseTest.php b/tests/Model/MailingList/ValidationStatusResponseTest.php new file mode 100644 index 0000000..7d70712 --- /dev/null +++ b/tests/Model/MailingList/ValidationStatusResponseTest.php @@ -0,0 +1,74 @@ +assertEquals('Tue, 26 Feb 2019 21:30:03 GMT', $model->getCreatedAt()); + $this->assertInstanceOf(ValidationStatusDownloadUrl::class, $model->getDownloadUrl()); + $this->assertEquals('http://example.com/filname.csv', $model->getDownloadUrl()->getCsv()); + $this->assertEquals('http://example.com/filname.json', $model->getDownloadUrl()->getJson()); + $this->assertEquals('listname@mydomain.sandbox.mailgun.org', $model->getId()); + $this->assertEquals(207665, $model->getQuantity()); + $this->assertEquals(207665, $model->getRecordsProcessed()); + $this->assertEquals('uploaded', $model->getStatus()); + $this->assertInstanceOf(ValidationStatusSummary::class, $model->getSummary()); + $this->assertInstanceOf(ValidationStatusSummaryResult::class, $model->getSummary()->getResult()); + $this->assertEquals(184199, $model->getSummary()->getResult()->getDeliverable()); + $this->assertEquals(5647, $model->getSummary()->getResult()->getDoNotSend()); + $this->assertEquals(12116, $model->getSummary()->getResult()->getUndeliverable()); + $this->assertEquals(5613, $model->getSummary()->getResult()->getUnknown()); + $this->assertInstanceOf(ValidationStatusSummaryRisk::class, $model->getSummary()->getRisk()); + $this->assertEquals(17763, $model->getSummary()->getRisk()->getHigh()); + $this->assertEquals(142547, $model->getSummary()->getRisk()->getLow()); + $this->assertEquals(41652, $model->getSummary()->getRisk()->getMedium()); + $this->assertEquals(5614, $model->getSummary()->getRisk()->getUnknown()); + } +} diff --git a/tests/Model/MailingList/ValidationStatusSummaryResultTest.php b/tests/Model/MailingList/ValidationStatusSummaryResultTest.php new file mode 100644 index 0000000..b80acd2 --- /dev/null +++ b/tests/Model/MailingList/ValidationStatusSummaryResultTest.php @@ -0,0 +1,36 @@ +assertEquals(184199, $model->getDeliverable()); + $this->assertEquals(5647, $model->getDoNotSend()); + $this->assertEquals(12116, $model->getUndeliverable()); + $this->assertEquals(5613, $model->getUnknown()); + } +} diff --git a/tests/Model/MailingList/ValidationStatusSummaryRiskTest.php b/tests/Model/MailingList/ValidationStatusSummaryRiskTest.php new file mode 100644 index 0000000..18b3858 --- /dev/null +++ b/tests/Model/MailingList/ValidationStatusSummaryRiskTest.php @@ -0,0 +1,36 @@ +assertEquals(17763, $model->getHigh()); + $this->assertEquals(142547, $model->getLow()); + $this->assertEquals(41652, $model->getMedium()); + $this->assertEquals(5614, $model->getUnknown()); + } +} diff --git a/tests/Model/MailingList/ValidationStatusSummaryTest.php b/tests/Model/MailingList/ValidationStatusSummaryTest.php new file mode 100644 index 0000000..7c325aa --- /dev/null +++ b/tests/Model/MailingList/ValidationStatusSummaryTest.php @@ -0,0 +1,53 @@ +assertInstanceOf(ValidationStatusSummaryResult::class, $model->getResult()); + $this->assertEquals(184199, $model->getResult()->getDeliverable()); + $this->assertEquals(5647, $model->getResult()->getDoNotSend()); + $this->assertEquals(12116, $model->getResult()->getUndeliverable()); + $this->assertEquals(5613, $model->getResult()->getUnknown()); + + $this->assertInstanceOf(ValidationStatusSummaryRisk::class, $model->getRisk()); + $this->assertEquals(17763, $model->getRisk()->getHigh()); + $this->assertEquals(142547, $model->getRisk()->getLow()); + $this->assertEquals(41652, $model->getRisk()->getMedium()); + $this->assertEquals(5614, $model->getRisk()->getUnknown()); + } +}