email validation v4, bulk validation, bulk preview

This commit is contained in:
Artem Bondarenko 2020-11-25 23:26:36 +02:00 committed by David Garcia
parent 43a76d046c
commit c24a92822c
24 changed files with 2043 additions and 0 deletions

View File

@ -0,0 +1,257 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Api;
use Exception;
use Mailgun\Assert;
use Mailgun\Exception\InvalidArgumentException;
use Mailgun\Model\EmailValidationV4\CreateBulkJobResponse;
use Mailgun\Model\EmailValidationV4\CreateBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\DeleteBulkJobResponse;
use Mailgun\Model\EmailValidationV4\GetBulkJobResponse;
use Mailgun\Model\EmailValidationV4\GetBulkJobsResponse;
use Mailgun\Model\EmailValidationV4\GetBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\GetBulkPreviewsResponse;
use Mailgun\Model\EmailValidationV4\PromoteBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\ValidateResponse;
use Psr\Http\Message\ResponseInterface;
/**
* @see https://documentation.mailgun.com/en/latest/api-email-validation.html
*/
class EmailValidationV4 extends HttpApi
{
/**
* Addresses are validated based off defined checks.
*
* @param string $address An email address to validate. Maximum: 512 characters.
* @param bool $providerLookup
* @return ValidateResponse|ResponseInterface
* @throws Exception Thrown when we don't catch a Client or Server side Exception
*/
public function validate(string $address, bool $providerLookup = true)
{
Assert::stringNotEmpty($address);
$params = [
'address' => $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);
}
}

View File

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

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class CreateBulkJobResponse implements ApiResponse
{
/**
* @var string|null
*/
private $id;
/**
* @var string|null
*/
private $message;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class CreateBulkPreviewResponse implements ApiResponse
{
/**
* @var string|null
*/
private $id;
/**
* @var string|null
*/
private $message;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class DeleteBulkJobResponse implements ApiResponse
{
/**
* @var string|null
*/
private $message;
private function __construct()
{
}
public static function create(?array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
return $model;
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
final class GetBulkJobResponse extends Job
{
}

View File

@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
use Mailgun\Model\PaginationResponse;
final class GetBulkJobsResponse implements ApiResponse
{
use PaginationResponse;
/**
* @var int
*/
private $total = 0;
/**
* @var array
*/
private $jobs = [];
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$jobs = [];
if (isset($data['jobs'])) {
foreach ($data['jobs'] as $job) {
$jobs[] = Job::create($job);
}
}
$model->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;
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class GetBulkPreviewResponse implements ApiResponse
{
/**
* @var Preview
*/
private $preview = [];
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->preview = Preview::create($data['preview']);
return $model;
}
/**
* @return Preview
*/
public function getPreview(): Preview
{
return $this->preview;
}
}

View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class GetBulkPreviewsResponse implements ApiResponse
{
/**
* @var array
*/
private $previews = [];
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$previews = [];
if (isset($data['previews'])) {
foreach ($data['previews'] as $job) {
$previews[] = Preview::create($job);
}
}
$model->previews = $previews;
return $model;
}
/**
* @return array
*/
public function getPreviews(): array
{
return $this->previews;
}
}

View File

@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use DateTimeImmutable;
use Mailgun\Model\ApiResponse;
class Job implements ApiResponse
{
/**
* @var DateTimeImmutable|null
*/
private $createdAt;
/**
* @var JobDownloadUrl
*/
private $downloadUrl;
/**
* @var string|null
*/
private $id;
/**
* @var int
*/
private $quantity = 0;
/**
* @var int
*/
private $recordsProcessed = 0;
/**
* @var string|null
*/
private $status;
/**
* @var Summary
*/
private $summary;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new static();
$model->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;
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class JobDownloadUrl implements ApiResponse
{
private $csv;
private $json;
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use DateTimeImmutable;
use Mailgun\Model\ApiResponse;
class Preview implements ApiResponse
{
/**
* @var string|null
*/
private $id;
/**
* @var bool
*/
private $valid;
/**
* @var string|null
*/
private $status;
/**
* @var int
*/
private $quantity = 0;
/**
* @var DateTimeImmutable|null
*/
private $createdAt;
/**
* @var Summary
*/
private $summary;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new static();
$model->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;
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class PromoteBulkPreviewResponse implements ApiResponse
{
/**
* @var string|null
*/
private $message;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
return $model;
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class Summary implements ApiResponse
{
/**
* @var SummaryResult
*/
private $result;
/**
* @var SummaryRisk
*/
private $risk;
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class SummaryResult implements ApiResponse
{
/**
* @var int
*/
private $deliverable = 0;
/**
* @var int
*/
private $doNotSend = 0;
/**
* @var int
*/
private $undeliverable = 0;
/**
* @var int
*/
private $catchAll = 0;
/**
* @var int
*/
private $unknown = 0;
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class SummaryRisk implements ApiResponse
{
/**
* @var int
*/
private $high = 0;
/**
* @var int
*/
private $low = 0;
/**
* @var int
*/
private $medium = 0;
/**
* @var int
*/
private $unknown = 0;
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,140 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\EmailValidationV4;
use Mailgun\Model\ApiResponse;
final class ValidateResponse implements ApiResponse
{
/**
* @var string|null
*/
private $address;
/**
* @var string|null
*/
private $didYouMean;
/**
* @var bool
*/
private $isDisposableAddress = false;
/**
* @var bool
*/
private $isRoleAddress = false;
/**
* @var array
*/
private $reason = [];
/**
* @var string|null
*/
private $result;
/**
* @var string|null
*/
private $risk;
/**
* @var string|null
*/
private $rootAddress;
private function __construct()
{
}
public static function create(array $data): self
{
$model = new self();
$model->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;
}
}

View File

@ -0,0 +1,457 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Api;
use GuzzleHttp\Psr7\Response;
use Mailgun\Api\EmailValidationV4;
use Mailgun\Model\EmailValidationV4\CreateBulkJobResponse;
use Mailgun\Model\EmailValidationV4\CreateBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\DeleteBulkJobResponse;
use Mailgun\Model\EmailValidationV4\GetBulkJobResponse;
use Mailgun\Model\EmailValidationV4\GetBulkJobsResponse;
use Mailgun\Model\EmailValidationV4\GetBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\GetBulkPreviewsResponse;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Preview;
use Mailgun\Model\EmailValidationV4\PromoteBulkPreviewResponse;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Model\EmailValidationV4\ValidateResponse;
class EmailValidationV4Test extends TestCase
{
protected function getApiClass()
{
return EmailValidationV4::class;
}
protected function getApiInstance($apiKey = null): EmailValidationV4
{
return parent::getApiInstance($apiKey);
}
public function testInvalidEmail()
{
$this->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": "<download_link>",
"json": "<download_link>"
},
"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": "<download_link>",
"json": "<download_link>"
},
"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": "<download_link>",
"json": "<download_link>"
},
"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());
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Event;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Tests\Model\BaseModelTest;
class JobDownloadUrlTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"csv": "https://example.com/file.csv",
"json": "https://example.com/file.json"
}
JSON;
$model = JobDownloadUrl::create(json_decode($json, true));
$this->assertEquals('https://example.com/file.csv', $model->getCsv());
$this->assertEquals('https://example.com/file.json', $model->getJson());
}
}

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Event;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Tests\Model\BaseModelTest;
class JobTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"created_at": "Sat, 23 Feb 2019 21:30:03 GMT",
"download_url": {
"csv": "<download_link>",
"json": "<download_link>"
},
"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());
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Event;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Preview;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Tests\Model\BaseModelTest;
class PreviewTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"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
}
}
}
JSON;
$model = Preview::create(json_decode($json, true));
$this->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());
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Event;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Model\EmailValidationV4\SummaryResult;
use Mailgun\Model\EmailValidationV4\SummaryRisk;
use Mailgun\Tests\Model\BaseModelTest;
class SummaryResultTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"deliverable": 181854,
"do_not_send": 5647,
"undeliverable": 12116,
"catch_all": 2345,
"unknown": 5613
}
JSON;
$model = SummaryResult::create(json_decode($json, true));
$this->assertEquals(181854, $model->getDeliverable());
$this->assertEquals(5647, $model->getDoNotSend());
$this->assertEquals(12116, $model->getUndeliverable());
$this->assertEquals(2345, $model->getCatchAll());
$this->assertEquals(5613, $model->getUnknown());
}
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Event;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Model\EmailValidationV4\SummaryResult;
use Mailgun\Model\EmailValidationV4\SummaryRisk;
use Mailgun\Tests\Model\BaseModelTest;
class SummaryRiskTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"high": 17763,
"low": 142547,
"medium": 41652,
"unknown": 5613
}
JSON;
$model = SummaryRisk::create(json_decode($json, true));
$this->assertEquals(17763, $model->getHigh());
$this->assertEquals(142547, $model->getLow());
$this->assertEquals(41652, $model->getMedium());
$this->assertEquals(5613, $model->getUnknown());
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Event;
use Mailgun\Model\EmailValidationV4\Job;
use Mailgun\Model\EmailValidationV4\JobDownloadUrl;
use Mailgun\Model\EmailValidationV4\Summary;
use Mailgun\Model\EmailValidationV4\SummaryResult;
use Mailgun\Model\EmailValidationV4\SummaryRisk;
use Mailgun\Tests\Model\BaseModelTest;
class SummaryTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"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 = Summary::create(json_decode($json, true));
$this->assertInstanceOf(SummaryResult::class, $model->getResult());
$this->assertInstanceOf(SummaryRisk::class, $model->getRisk());
}
}