diff --git a/src/Api/EmailValidationV4.php b/src/Api/EmailValidationV4.php new file mode 100644 index 0000000..64fba92 --- /dev/null +++ b/src/Api/EmailValidationV4.php @@ -0,0 +1,257 @@ + $address, + 'provider_lookup' => $providerLookup, + ]; + + $response = $this->httpGet('/v4/address/validate', $params); + + return $this->hydrateResponse($response, ValidateResponse::class); + } + + /** + * @param string $listId + * @param $filePath - file path or file content + * @return mixed|ResponseInterface + * @throws Exception + */ + public function createBulkJob(string $listId, $filePath) + { + Assert::stringNotEmpty($listId); + + if (strlen($filePath) < PHP_MAXPATHLEN && is_file($filePath)) { + $fileData = ['filePath' => $filePath]; + } else { + $fileData = [ + 'fileContent' => $filePath, + 'filename' => 'file', + ]; + } + + $postDataMultipart = []; + $postDataMultipart[] = $this->prepareFile('file', $fileData); + + $response = $this->httpPostRaw(sprintf('/v4/address/validate/bulk/%s', $listId), $postDataMultipart); + $this->closeResources($postDataMultipart); + + return $this->hydrateResponse($response, CreateBulkJobResponse::class); + } + + /** + * @param string $fieldName + * @param array $filePath ['fileContent' => 'content'] or ['filePath' => '/foo/bar'] + * + * @return array + */ + private function prepareFile(string $fieldName, array $filePath): array + { + $filename = isset($filePath['filename']) ? $filePath['filename'] : null; + + if (isset($filePath['fileContent'])) { + // File from memory + $resource = fopen('php://temp', 'r+'); + fwrite($resource, $filePath['fileContent']); + rewind($resource); + } elseif (isset($filePath['filePath'])) { + // File form path + $path = $filePath['filePath']; + + // Remove leading @ symbol + if (0 === strpos($path, '@')) { + $path = substr($path, 1); + } + + $resource = fopen($path, 'r'); + } else { + throw new InvalidArgumentException('When using a file you need to specify parameter "fileContent" or "filePath"'); + } + + return [ + 'name' => $fieldName, + 'content' => $resource, + 'filename' => $filename, + ]; + } + + /** + * Close open resources. + * @param array $params + */ + private function closeResources(array $params): void + { + foreach ($params as $param) { + if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) { + fclose($param['content']); + } + } + } + + /** + * @param string $listId + * @return DeleteBulkJobResponse|ResponseInterface + * @throws Exception + */ + public function deleteBulkJob(string $listId) + { + Assert::stringNotEmpty($listId); + + $response = $this->httpDelete(sprintf('/v4/address/validate/bulk/%s', $listId)); + + return $this->hydrateResponse($response, DeleteBulkJobResponse::class); + } + + /** + * @param string $listId + * @return GetBulkJobResponse|ResponseInterface + * @throws Exception + */ + public function getBulkJob(string $listId) + { + Assert::stringNotEmpty($listId); + + $response = $this->httpGet(sprintf('/v4/address/validate/bulk/%s', $listId)); + + return $this->hydrateResponse($response, GetBulkJobResponse::class); + } + + /** + * @param int $limit + * @return GetBulkJobsResponse|ResponseInterface + * @throws Exception + */ + public function getBulkJobs(int $limit = 500) + { + Assert::integer($limit); + Assert::greaterThan($limit, 0); + + $response = $this->httpGet('/v4/address/validate/bulk', [ + 'limit' => $limit, + ]); + + return $this->hydrateResponse($response, GetBulkJobsResponse::class); + } + + public function getBulkPreviews(int $limit = 500) + { + Assert::integer($limit); + Assert::greaterThan($limit, 0); + + $response = $this->httpGet('/v4/address/validate/preview', [ + 'limit' => $limit, + ]); + + return $this->hydrateResponse($response, GetBulkPreviewsResponse::class); + } + + /** + * @param string $previewId + * @param $filePath + * @return mixed|ResponseInterface + * @throws Exception + */ + public function createBulkPreview(string $previewId, $filePath) + { + Assert::stringNotEmpty($previewId); + + if (strlen($filePath) < PHP_MAXPATHLEN && is_file($filePath)) { + $fileData = ['filePath' => $filePath]; + } else { + $fileData = [ + 'fileContent' => $filePath, + 'filename' => 'file', + ]; + } + + $postDataMultipart = []; + $postDataMultipart[] = $this->prepareFile('file', $fileData); + + $response = $this->httpPostRaw(sprintf('/v4/address/validate/preview/%s', $previewId), $postDataMultipart); + $this->closeResources($postDataMultipart); + + return $this->hydrateResponse($response, CreateBulkPreviewResponse::class); + } + + /** + * @param string $previewId + * @return mixed|ResponseInterface + * @throws Exception + */ + public function getBulkPreview(string $previewId) + { + Assert::stringNotEmpty($previewId); + + $response = $this->httpGet(sprintf('/v4/address/validate/preview/%s', $previewId)); + + return $this->hydrateResponse($response, GetBulkPreviewResponse::class); + } + + /** + * @param string $previewId + * @return bool + */ + public function deleteBulkPreview(string $previewId) + { + Assert::stringNotEmpty($previewId); + + $response = $this->httpDelete(sprintf('/v4/address/validate/preview/%s', $previewId)); + + return $response->getStatusCode() === 204; + } + + /** + * @param string $previewId + * @return mixed|ResponseInterface + * @throws Exception + */ + public function promoteBulkPreview(string $previewId) + { + Assert::stringNotEmpty($previewId); + + $response = $this->httpPut(sprintf('/v4/address/validate/preview/%s', $previewId)); + + return $this->hydrateResponse($response, PromoteBulkPreviewResponse::class); + } +} diff --git a/src/Mailgun.php b/src/Mailgun.php index 0ce768f..de4c58b 100644 --- a/src/Mailgun.php +++ b/src/Mailgun.php @@ -94,6 +94,11 @@ class Mailgun return new Api\EmailValidation($this->httpClient, $this->requestBuilder, $this->hydrator); } + public function emailValidationV4(): Api\EmailValidationV4 + { + return new Api\EmailValidationV4($this->httpClient, $this->requestBuilder, $this->hydrator); + } + public function events(): Api\Event { return new Api\Event($this->httpClient, $this->requestBuilder, $this->hydrator); diff --git a/src/Model/EmailValidationV4/CreateBulkJobResponse.php b/src/Model/EmailValidationV4/CreateBulkJobResponse.php new file mode 100644 index 0000000..75bf7d6 --- /dev/null +++ b/src/Model/EmailValidationV4/CreateBulkJobResponse.php @@ -0,0 +1,57 @@ +id = $data['id'] ?? null; + $model->message = $data['message'] ?? null; + + return $model; + } + + /** + * @return string|null + */ + public function getId(): ?string + { + return $this->id; + } + + /** + * @return string|null + */ + public function getMessage(): ?string + { + return $this->message; + } +} diff --git a/src/Model/EmailValidationV4/CreateBulkPreviewResponse.php b/src/Model/EmailValidationV4/CreateBulkPreviewResponse.php new file mode 100644 index 0000000..264f3a4 --- /dev/null +++ b/src/Model/EmailValidationV4/CreateBulkPreviewResponse.php @@ -0,0 +1,57 @@ +id = $data['id'] ?? null; + $model->message = $data['message'] ?? null; + + return $model; + } + + /** + * @return string|null + */ + public function getId(): ?string + { + return $this->id; + } + + /** + * @return string|null + */ + public function getMessage(): ?string + { + return $this->message; + } +} diff --git a/src/Model/EmailValidationV4/DeleteBulkJobResponse.php b/src/Model/EmailValidationV4/DeleteBulkJobResponse.php new file mode 100644 index 0000000..1e12c6e --- /dev/null +++ b/src/Model/EmailValidationV4/DeleteBulkJobResponse.php @@ -0,0 +1,43 @@ +message = $data['message'] ?? null; + + return $model; + } + + /** + * @return string|null + */ + public function getMessage(): ?string + { + return $this->message; + } +} diff --git a/src/Model/EmailValidationV4/GetBulkJobResponse.php b/src/Model/EmailValidationV4/GetBulkJobResponse.php new file mode 100644 index 0000000..2d1b848 --- /dev/null +++ b/src/Model/EmailValidationV4/GetBulkJobResponse.php @@ -0,0 +1,16 @@ +jobs = $jobs; + $model->total = $data['total'] ?? 0; + $model->paging = $data['paging']; + + return $model; + } + + /** + * @return int + */ + public function getTotal(): int + { + return $this->total; + } + + /** + * @return array + */ + public function getJobs(): array + { + return $this->jobs; + } +} diff --git a/src/Model/EmailValidationV4/GetBulkPreviewResponse.php b/src/Model/EmailValidationV4/GetBulkPreviewResponse.php new file mode 100644 index 0000000..1e3a14f --- /dev/null +++ b/src/Model/EmailValidationV4/GetBulkPreviewResponse.php @@ -0,0 +1,42 @@ +preview = Preview::create($data['preview']); + + return $model; + } + + /** + * @return Preview + */ + public function getPreview(): Preview + { + return $this->preview; + } +} diff --git a/src/Model/EmailValidationV4/GetBulkPreviewsResponse.php b/src/Model/EmailValidationV4/GetBulkPreviewsResponse.php new file mode 100644 index 0000000..94ed581 --- /dev/null +++ b/src/Model/EmailValidationV4/GetBulkPreviewsResponse.php @@ -0,0 +1,51 @@ +previews = $previews; + + return $model; + } + + /** + * @return array + */ + public function getPreviews(): array + { + return $this->previews; + } +} diff --git a/src/Model/EmailValidationV4/Job.php b/src/Model/EmailValidationV4/Job.php new file mode 100644 index 0000000..42d54e2 --- /dev/null +++ b/src/Model/EmailValidationV4/Job.php @@ -0,0 +1,128 @@ +createdAt = isset($data['created_at']) ? new DateTimeImmutable($data['created_at']) : null; + $model->downloadUrl = JobDownloadUrl::create($data['download_url']); + $model->id = $data['id'] ?? null; + $model->quantity = $data['quantity'] ?? null; + $model->recordsProcessed = $data['records_processed'] ?? null; + $model->status = $data['status'] ?? null; + $model->summary = Summary::create($data['summary']); + + return $model; + } + + /** + * @return DateTimeImmutable|null + */ + public function getCreatedAt(): ?DateTimeImmutable + { + return $this->createdAt; + } + + /** + * @return JobDownloadUrl + */ + public function getDownloadUrl(): JobDownloadUrl + { + return $this->downloadUrl; + } + + /** + * @return string|null + */ + public function getId(): ?string + { + return $this->id; + } + + /** + * @return int + */ + public function getQuantity(): int + { + return $this->quantity; + } + + /** + * @return int + */ + public function getRecordsProcessed(): int + { + return $this->recordsProcessed; + } + + /** + * @return string|null + */ + public function getStatus(): ?string + { + return $this->status; + } + + /** + * @return mixed + */ + public function getSummary(): Summary + { + return $this->summary; + } +} diff --git a/src/Model/EmailValidationV4/JobDownloadUrl.php b/src/Model/EmailValidationV4/JobDownloadUrl.php new file mode 100644 index 0000000..b2e7095 --- /dev/null +++ b/src/Model/EmailValidationV4/JobDownloadUrl.php @@ -0,0 +1,43 @@ +csv = $data['csv'] ?? null; + $model->json = $data['json'] ?? null; + + return $model; + } + + private function __construct() + { + } + + public function getCsv(): ?string + { + return $this->csv; + } + + public function getJson(): ?string + { + return $this->json; + } +} diff --git a/src/Model/EmailValidationV4/Preview.php b/src/Model/EmailValidationV4/Preview.php new file mode 100644 index 0000000..0f5469b --- /dev/null +++ b/src/Model/EmailValidationV4/Preview.php @@ -0,0 +1,114 @@ +id = $data['id'] ?? null; + $model->valid = $data['valid'] ?? null; + $model->status = $data['status'] ?? null; + $model->quantity = $data['quantity'] ?? null; + $model->createdAt = isset($data['created_at']) ? DateTimeImmutable::createFromFormat('U', (string)($data['created_at'])) : null; + $model->summary = Summary::create($data['summary']); + + return $model; + } + + /** + * @return string|null + */ + public function getId(): ?string + { + return $this->id; + } + + /** + * @return bool|null + */ + public function isValid(): ?bool + { + return $this->valid; + } + + /** + * @return int + */ + public function getQuantity(): int + { + return $this->quantity; + } + + /** + * @return string|null + */ + public function getStatus(): ?string + { + return $this->status; + } + + /** + * @return DateTimeImmutable|null + */ + public function getCreatedAt(): ?DateTimeImmutable + { + return $this->createdAt; + } + + /** + * @return mixed + */ + public function getSummary(): Summary + { + return $this->summary; + } +} diff --git a/src/Model/EmailValidationV4/PromoteBulkPreviewResponse.php b/src/Model/EmailValidationV4/PromoteBulkPreviewResponse.php new file mode 100644 index 0000000..2933d99 --- /dev/null +++ b/src/Model/EmailValidationV4/PromoteBulkPreviewResponse.php @@ -0,0 +1,43 @@ +message = $data['message'] ?? null; + + return $model; + } + + /** + * @return string|null + */ + public function getMessage(): ?string + { + return $this->message; + } +} diff --git a/src/Model/EmailValidationV4/Summary.php b/src/Model/EmailValidationV4/Summary.php new file mode 100644 index 0000000..3a58700 --- /dev/null +++ b/src/Model/EmailValidationV4/Summary.php @@ -0,0 +1,57 @@ +result = SummaryResult::create($data['result']); + $model->risk = SummaryRisk::create($data['risk']); + + return $model; + } + + private function __construct() + { + } + + /** + * @return SummaryResult + */ + public function getResult(): SummaryResult + { + return $this->result; + } + + /** + * @return SummaryRisk + */ + public function getRisk(): SummaryRisk + { + return $this->risk; + } +} diff --git a/src/Model/EmailValidationV4/SummaryResult.php b/src/Model/EmailValidationV4/SummaryResult.php new file mode 100644 index 0000000..528855f --- /dev/null +++ b/src/Model/EmailValidationV4/SummaryResult.php @@ -0,0 +1,98 @@ +deliverable = $data['deliverable'] ?? 0; + $model->doNotSend = $data['do_not_send'] ?? 0; + $model->undeliverable = $data['undeliverable'] ?? 0; + $model->catchAll = $data['catch_all'] ?? 0; + $model->unknown = $data['unknown'] ?? 0; + + return $model; + } + + private function __construct() + { + } + + /** + * @return int + */ + public function getDeliverable(): int + { + return $this->deliverable; + } + + /** + * @return int + */ + public function getDoNotSend(): int + { + return $this->doNotSend; + } + + /** + * @return int + */ + public function getUndeliverable(): int + { + return $this->undeliverable; + } + + /** + * @return int + */ + public function getCatchAll(): int + { + return $this->catchAll; + } + + /** + * @return int + */ + public function getUnknown(): int + { + return $this->unknown; + } +} diff --git a/src/Model/EmailValidationV4/SummaryRisk.php b/src/Model/EmailValidationV4/SummaryRisk.php new file mode 100644 index 0000000..c373e7b --- /dev/null +++ b/src/Model/EmailValidationV4/SummaryRisk.php @@ -0,0 +1,85 @@ +high = $data['high'] ?? 0; + $model->low = $data['low'] ?? 0; + $model->medium = $data['medium'] ?? 0; + $model->unknown = $data['unknown'] ?? 0; + + return $model; + } + + private function __construct() + { + } + + /** + * @return int + */ + public function getHigh(): int + { + return $this->high; + } + + /** + * @return int + */ + public function getLow(): int + { + return $this->low; + } + + /** + * @return int + */ + public function getMedium(): int + { + return $this->medium; + } + + /** + * @return int + */ + public function getUnknown(): int + { + return $this->unknown; + } +} diff --git a/src/Model/EmailValidationV4/ValidateResponse.php b/src/Model/EmailValidationV4/ValidateResponse.php new file mode 100644 index 0000000..70023d4 --- /dev/null +++ b/src/Model/EmailValidationV4/ValidateResponse.php @@ -0,0 +1,140 @@ +address = $data['address'] ?? null; + $model->didYouMean = $data['did_you_mean'] ?? null; + $model->isDisposableAddress = $data['is_disposable_address'] ?? false; + $model->isRoleAddress = $data['is_role_address'] ?? false; + $model->reason = $data['reason'] ?? []; + $model->result = $data['result'] ?? null; + $model->risk = $data['risk'] ?? null; + $model->rootAddress = $data['root_address'] ?? null; + + return $model; + } + + /** + * @return string|null + */ + public function getAddress(): ?string + { + return $this->address; + } + + /** + * @return string|null + */ + public function getDidYouMean(): ?string + { + return $this->didYouMean; + } + + /** + * @return bool + */ + public function isDisposableAddress(): bool + { + return $this->isDisposableAddress; + } + + /** + * @return bool + */ + public function isRoleAddress(): bool + { + return $this->isRoleAddress; + } + + /** + * @return array + */ + public function getReason(): array + { + return $this->reason; + } + + /** + * @return string|null + */ + public function getResult(): ?string + { + return $this->result; + } + + /** + * @return string|null + */ + public function getRisk(): ?string + { + return $this->risk; + } + + /** + * @return string|null + */ + public function getRootAddress(): ?string + { + return $this->rootAddress; + } +} diff --git a/tests/Api/EmailValidationV4Test.php b/tests/Api/EmailValidationV4Test.php new file mode 100644 index 0000000..147c020 --- /dev/null +++ b/tests/Api/EmailValidationV4Test.php @@ -0,0 +1,457 @@ +setRequestMethod('GET'); + $this->setRequestUri('/v4/address/validate?address=email%40example.com&provider_lookup=1'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "address": "email@example.com", + "did_you_mean": "email@domain.com", + "is_disposable_address": true, + "is_role_address": false, + "reason": ["no_data"], + "result": "undeliverable", + "risk": "high" +} +JSON + )); + + $api = $this->getApiInstance(); + + /** + * @var $response ValidateResponse + */ + $response = $api->validate('email@example.com', true); + + $this->assertInstanceOf(ValidateResponse::class, $response); + $this->assertEquals('email@example.com', $response->getAddress()); + $this->assertEquals('email@domain.com', $response->getDidYouMean()); + $this->assertTrue($response->isDisposableAddress()); + $this->assertFalse($response->isRoleAddress()); + $this->assertEquals(['no_data'], $response->getReason()); + $this->assertEquals('undeliverable', $response->getResult()); + $this->assertEquals('high', $response->getRisk()); + $this->assertNull($response->getRootAddress()); + } + + public function testValidEmail() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v4/address/validate?address=email3%40example.com&provider_lookup=0'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "address": "email3@example.com", + "is_disposable_address": false, + "is_role_address": true, + "reason": [], + "result": "deliverable", + "risk": "low", + "root_address": "email2@example.com" +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response ValidateResponse + */ + $response = $api->validate('email3@example.com', false); + + $this->assertInstanceOf(ValidateResponse::class, $response); + $this->assertEquals('email3@example.com', $response->getAddress()); + $this->assertNull($response->getDidYouMean()); + $this->assertFalse($response->isDisposableAddress()); + $this->assertTrue($response->isRoleAddress()); + $this->assertEmpty($response->getReason()); + $this->assertEquals('deliverable', $response->getResult()); + $this->assertEquals('low', $response->getRisk()); + $this->assertEquals('email2@example.com', $response->getRootAddress()); + } + + public function testGetBulkJobs() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v4/address/validate/bulk?limit=50'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "jobs": [ + { + "created_at": "Tue, 26 Feb 2019 21:30:03 GMT", + "download_url": { + "csv": "", + "json": "" + }, + "id": "bulk_validations_sandbox2_mailgun_org", + "quantity": 207665, + "records_processed": 207665, + "status": "uploaded", + "summary": { + "result": { + "deliverable": 181854, + "do_not_send": 5647, + "undeliverable": 12116, + "catch_all": 2345, + "unknown": 5613 + }, + "risk": { + "high": 17763, + "low": 142547, + "medium": 41652, + "unknown": 5613 + } + } + }, + { + "created_at": "Tue, 23 Feb 2019 21:30:03 GMT", + "download_url": { + "csv": "", + "json": "" + }, + "id": "bulk_validations_sandbox_mailgun_org", + "quantity": 207, + "records_processed": 207, + "status": "uploaded", + "summary": { + "result": { + "deliverable": 181854, + "do_not_send": 5647, + "undeliverable": 12116, + "catch_all": 2345, + "unknown": 5613 + }, + "risk": { + "high": 17763, + "low": 142547, + "medium": 41652, + "unknown": 5613 + } + } + } + ], + "total": 2, + "paging": { + "next": "https://url_to_next_page", + "previous": "https://url_to_previous_page", + "first": "https://url_to_first_page", + "last": "https://url_to_last_page" + } +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response GetBulkJobsResponse + */ + $response = $api->getBulkJobs(50); + + $this->assertInstanceOf(GetBulkJobsResponse::class, $response); + $this->assertCount(2, $response->getJobs()); + $this->assertContainsOnlyInstancesOf(Job::class, $response->getJobs()); + $this->assertTrue(method_exists($response, 'getNextUrl')); + $this->assertTrue(method_exists($response, 'getPreviousUrl')); + $this->assertTrue(method_exists($response, 'getFirstUrl')); + $this->assertTrue(method_exists($response, 'getLastUrl')); + } + + public function testGetBulkJob() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v4/address/validate/bulk/listId123'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "created_at": "Sat, 23 Feb 2019 21:30:03 GMT", + "download_url": { + "csv": "", + "json": "" + }, + "id": "bulk_validations_sandbox_mailgun_org", + "quantity": 207, + "records_processed": 208, + "status": "uploaded", + "summary": { + "result": { + "deliverable": 181854, + "do_not_send": 5647, + "undeliverable": 12116, + "catch_all": 2345, + "unknown": 5613 + }, + "risk": { + "high": 17763, + "low": 142547, + "medium": 41652, + "unknown": 5613 + } + } +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response GetBulkJobResponse + */ + $response = $api->getBulkJob('listId123'); + + $this->assertInstanceOf(GetBulkJobResponse::class, $response); + $this->assertInstanceOf(Job::class, $response); + $this->assertEquals('2019-02-23 21:30:03', $response->getCreatedAt()->format('Y-m-d H:i:s')); + $this->assertInstanceOf(JobDownloadUrl::class, $response->getDownloadUrl()); + $this->assertEquals('bulk_validations_sandbox_mailgun_org', $response->getId()); + $this->assertEquals(207, $response->getQuantity()); + $this->assertEquals(208, $response->getRecordsProcessed()); + $this->assertEquals('uploaded', $response->getStatus()); + $this->assertInstanceOf(Summary::class, $response->getSummary()); + } + + public function testDeleteBulkJob() + { + $this->setRequestMethod('DELETE'); + $this->setRequestUri('/v4/address/validate/bulk/listId321'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "message": "Validation job canceled." +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response DeleteBulkJobResponse + */ + $response = $api->deleteBulkJob('listId321'); + + $this->assertInstanceOf(DeleteBulkJobResponse::class, $response); + $this->assertEquals('Validation job canceled.', $response->getMessage()); + } + + public function testCreateBulkJob() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v4/address/validate/bulk/listId1'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "id":"listId1", + "message": "The validation job was submitted." +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response CreateBulkJobResponse + */ + $response = $api->createBulkJob('listId1', __FILE__); + + $this->assertInstanceOf(CreateBulkJobResponse::class, $response); + $this->assertEquals('listId1', $response->getId()); + $this->assertEquals('The validation job was submitted.', $response->getMessage()); + } + + public function testGetBulkPreviews() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v4/address/validate/preview?limit=50'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "previews": [ + { + "id": "test_500", + "valid": true, + "status": "preview_complete", + "quantity": 8, + "created_at": 1590080191, + "summary": { + "result": { + "deliverable": 37.5, + "do_not_send": 0, + "undeliverable": 23, + "catch_all": 2, + "unknown": 37.5 + }, + "risk": { + "high": 25, + "low": 25, + "medium": 12.5, + "unknown": 37.5 + } + } + }, + { + "id": "test_501", + "valid": true, + "status": "preview_complete", + "quantity": 8, + "created_at": 1590155015, + "summary": { + "result": { + "deliverable": 37.5, + "do_not_send": 0, + "undeliverable": 23, + "catch_all": 2, + "unknown": 37.5 + }, + "risk": { + "high": 25, + "low": 25, + "medium": 12.5, + "unknown": 37.5 + } + } + } + ] +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response GetBulkPreviewsResponse + */ + $response = $api->getBulkPreviews(50); + + $this->assertInstanceOf(GetBulkPreviewsResponse::class, $response); + $this->assertCount(2, $response->getPreviews()); + $this->assertContainsOnlyInstancesOf(Preview::class, $response->getPreviews()); + } + + public function testGetBulkPreview() + { + $this->setRequestMethod('GET'); + $this->setRequestUri('/v4/address/validate/preview/test_500'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "preview": { + "id": "test_500", + "valid": true, + "status": "preview_complete", + "quantity": 8, + "created_at": 1590080191, + "summary": { + "result": { + "deliverable": 37.5, + "undeliverable": 23, + "catch_all": 2, + "unknown": 37.5 + }, + "risk": { + "high": 25, + "low": 25, + "medium": 12.5, + "unknown": 37.5 + } + } + } +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response GetBulkPreviewResponse + */ + $response = $api->getBulkPreview('test_500'); + + $this->assertInstanceOf(GetBulkPreviewResponse::class, $response); + $this->assertInstanceOf(Preview::class, $response->getPreview()); + } + + public function testDeleteBulkPreview() + { + $this->setRequestMethod('DELETE'); + $this->setRequestUri('/v4/address/validate/preview/previewId1'); + $this->setHttpResponse(new Response(204, ['Content-Type' => 'application/json'])); + $api = $this->getApiInstance(); + + /** + * @var $response bool + */ + $status = $api->deleteBulkPreview('previewId1'); + + $this->assertTrue($status); + } + + public function testPromoteBulkPreview() + { + $this->setRequestMethod('PUT'); + $this->setRequestUri('/v4/address/validate/preview/previewId2'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "message": "Validation preview promoted." +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response PromoteBulkPreviewResponse + */ + $response = $api->promoteBulkPreview('previewId2'); + + $this->assertInstanceOf(PromoteBulkPreviewResponse::class, $response); + } + + public function testCreateBulkPreview() + { + $this->setRequestMethod('POST'); + $this->setRequestUri('/v4/address/validate/preview/preview3'); + $this->setHttpResponse(new Response(200, ['Content-Type' => 'application/json'], <<<'JSON' +{ + "id":"preview3", + "message": "The bulk preview was submitted." +} +JSON + )); + $api = $this->getApiInstance(); + + /** + * @var $response CreateBulkPreviewResponse + */ + $response = $api->createBulkPreview('preview3', __FILE__); + + $this->assertInstanceOf(CreateBulkPreviewResponse::class, $response); + $this->assertEquals('preview3', $response->getId()); + $this->assertEquals('The bulk preview was submitted.', $response->getMessage()); + } +} diff --git a/tests/Model/EmailValidationV4/JobDownloadUrlTest.php b/tests/Model/EmailValidationV4/JobDownloadUrlTest.php new file mode 100644 index 0000000..e436c58 --- /dev/null +++ b/tests/Model/EmailValidationV4/JobDownloadUrlTest.php @@ -0,0 +1,34 @@ +assertEquals('https://example.com/file.csv', $model->getCsv()); + $this->assertEquals('https://example.com/file.json', $model->getJson()); + } +} diff --git a/tests/Model/EmailValidationV4/JobTest.php b/tests/Model/EmailValidationV4/JobTest.php new file mode 100644 index 0000000..2d65dbc --- /dev/null +++ b/tests/Model/EmailValidationV4/JobTest.php @@ -0,0 +1,61 @@ +", + "json": "" + }, + "id": "bulk_validations_sandbox_mailgun_org", + "quantity": 207, + "records_processed": 208, + "status": "uploaded", + "summary": { + "result": { + "deliverable": 181854, + "do_not_send": 5647, + "undeliverable": 12116, + "catch_all" : 2345, + "unknown": 5613 + }, + "risk": { + "high": 17763, + "low": 142547, + "medium": 41652, + "unknown": 5613 + } + } +} +JSON; + $model = Job::create(json_decode($json, true)); + $this->assertEquals('2019-02-23 21:30:03', $model->getCreatedAt()->format('Y-m-d H:i:s')); + $this->assertInstanceOf(JobDownloadUrl::class, $model->getDownloadUrl()); + $this->assertEquals('bulk_validations_sandbox_mailgun_org', $model->getId()); + $this->assertEquals(207, $model->getQuantity()); + $this->assertEquals(208, $model->getRecordsProcessed()); + $this->assertEquals('uploaded', $model->getStatus()); + $this->assertInstanceOf(Summary::class, $model->getSummary()); + } +} diff --git a/tests/Model/EmailValidationV4/PreviewTest.php b/tests/Model/EmailValidationV4/PreviewTest.php new file mode 100644 index 0000000..2adf2ff --- /dev/null +++ b/tests/Model/EmailValidationV4/PreviewTest.php @@ -0,0 +1,57 @@ +assertEquals('test_500', $model->getId()); + $this->assertEquals(true, $model->isValid()); + $this->assertEquals('preview_complete', $model->getStatus()); + $this->assertEquals(8, $model->getQuantity()); + $this->assertEquals('1590080191', $model->getCreatedAt()->format('U')); + $this->assertInstanceOf(Summary::class, $model->getSummary()); + } +} diff --git a/tests/Model/EmailValidationV4/SummaryResultTest.php b/tests/Model/EmailValidationV4/SummaryResultTest.php new file mode 100644 index 0000000..18d2008 --- /dev/null +++ b/tests/Model/EmailValidationV4/SummaryResultTest.php @@ -0,0 +1,42 @@ +assertEquals(181854, $model->getDeliverable()); + $this->assertEquals(5647, $model->getDoNotSend()); + $this->assertEquals(12116, $model->getUndeliverable()); + $this->assertEquals(2345, $model->getCatchAll()); + $this->assertEquals(5613, $model->getUnknown()); + } +} diff --git a/tests/Model/EmailValidationV4/SummaryRiskTest.php b/tests/Model/EmailValidationV4/SummaryRiskTest.php new file mode 100644 index 0000000..ae7bf40 --- /dev/null +++ b/tests/Model/EmailValidationV4/SummaryRiskTest.php @@ -0,0 +1,40 @@ +assertEquals(17763, $model->getHigh()); + $this->assertEquals(142547, $model->getLow()); + $this->assertEquals(41652, $model->getMedium()); + $this->assertEquals(5613, $model->getUnknown()); + } +} diff --git a/tests/Model/EmailValidationV4/SummaryTest.php b/tests/Model/EmailValidationV4/SummaryTest.php new file mode 100644 index 0000000..18d48ee --- /dev/null +++ b/tests/Model/EmailValidationV4/SummaryTest.php @@ -0,0 +1,47 @@ +assertInstanceOf(SummaryResult::class, $model->getResult()); + $this->assertInstanceOf(SummaryRisk::class, $model->getRisk()); + } +}