282 lines
11 KiB
PHP
282 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Mailjet;
|
|
|
|
use App\Mailjet\ApiClient;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\NullLogger;
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
|
class ApiClientTest extends TestCase
|
|
{
|
|
public function testChecksSubscriptionAgainstSuppliedListId(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
if (true === str_contains($url, '/Contact?')) {
|
|
return new MockResponse(json_encode([
|
|
'Data' => [
|
|
['ID' => 99],
|
|
],
|
|
], JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
return new MockResponse(json_encode([
|
|
'Data' => [
|
|
[
|
|
'ContactID' => 99,
|
|
'IsActive' => true,
|
|
'IsUnsubscribed' => false,
|
|
],
|
|
],
|
|
], JSON_THROW_ON_ERROR));
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test', '111');
|
|
|
|
$subscribed = $mailjet->isSubscribed(' [email protected] ', 123);
|
|
|
|
self::assertTrue($subscribed);
|
|
self::assertSame('123', (string) $requests[1][2]['query']['ContactsList']);
|
|
}
|
|
|
|
public function testUpsertContactCreatesContactAndUpdatesProperties(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
if (true === str_contains($url, '/Contact?')) {
|
|
return new MockResponse(json_encode([
|
|
'Data' => [],
|
|
], JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
if (true === str_contains($url, '/Contact')) {
|
|
return new MockResponse(json_encode([
|
|
'Data' => [
|
|
['ID' => 99],
|
|
],
|
|
], JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
return new MockResponse(json_encode([
|
|
'Data' => [],
|
|
], JSON_THROW_ON_ERROR));
|
|
});
|
|
|
|
$mailjet = new ApiClient(
|
|
$client,
|
|
new NullLogger(),
|
|
'key',
|
|
'secret',
|
|
'https://mailjet.test',
|
|
'111',
|
|
[
|
|
'firstName' => 'vorname',
|
|
'lastName' => 'nachname',
|
|
],
|
|
);
|
|
|
|
$mailjet->upsertContact(' [email protected] ', ' Mia ', ' Muster ');
|
|
|
|
self::assertCount(3, $requests);
|
|
self::assertSame('GET', $requests[0][0]);
|
|
self::assertSame('https://mailjet.test/[email protected]&Limit=1', $requests[0][1]);
|
|
self::assertSame('[email protected]', (string) $requests[0][2]['query']['Email']);
|
|
self::assertSame('POST', $requests[1][0]);
|
|
self::assertSame('https://mailjet.test/Contact', $requests[1][1]);
|
|
self::assertSame([
|
|
'Email' => '[email protected]',
|
|
], json_decode((string) $requests[1][2]['body'], true, 512, JSON_THROW_ON_ERROR));
|
|
self::assertSame('POST', $requests[2][0]);
|
|
self::assertSame('https://mailjet.test/contactdata', $requests[2][1]);
|
|
self::assertSame([
|
|
'ContactID' => 99,
|
|
'Data' => [
|
|
[
|
|
'Name' => 'vorname',
|
|
'Value' => 'Mia',
|
|
],
|
|
[
|
|
'Name' => 'nachname',
|
|
'Value' => 'Muster',
|
|
],
|
|
],
|
|
], json_decode((string) $requests[2][2]['body'], true, 512, JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
public function testUpsertContactTruncatesLongNamesBeforeSending(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
if (true === str_contains($url, '/Contact?')) {
|
|
return new MockResponse(json_encode([
|
|
'Data' => [
|
|
['ID' => 99],
|
|
],
|
|
], JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
if (true === str_contains($url, '/Contact')) {
|
|
return new MockResponse(json_encode([
|
|
'Data' => [],
|
|
], JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
return new MockResponse(json_encode(['Data' => []], JSON_THROW_ON_ERROR));
|
|
});
|
|
|
|
$mailjet = new ApiClient(
|
|
$client,
|
|
new NullLogger(),
|
|
'key',
|
|
'secret',
|
|
'https://mailjet.test',
|
|
'111',
|
|
[
|
|
'firstName' => 'vorname',
|
|
'lastName' => 'nachname',
|
|
],
|
|
);
|
|
|
|
$mailjet->upsertContact('[email protected]', str_repeat('A', 300), str_repeat('B', 300));
|
|
|
|
$payload = json_decode((string) $requests[1][2]['body'], true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
self::assertSame(255, strlen($payload['Data'][0]['Value']));
|
|
self::assertSame(255, strlen($payload['Data'][1]['Value']));
|
|
self::assertSame(str_repeat('A', 255), $payload['Data'][0]['Value']);
|
|
self::assertSame(str_repeat('B', 255), $payload['Data'][1]['Value']);
|
|
}
|
|
|
|
public function testUpsertContactDoesNothingWithoutNames(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
return new MockResponse(json_encode(['Data' => []], JSON_THROW_ON_ERROR));
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test', '111');
|
|
|
|
$mailjet->upsertContact(' [email protected] ');
|
|
|
|
self::assertSame([], $requests);
|
|
}
|
|
|
|
public function testEnsureSubscribedUsesSuppliedListId(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
return new MockResponse(json_encode(['Data' => []], JSON_THROW_ON_ERROR));
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test', '111');
|
|
|
|
$mailjet->ensureSubscribed(' [email protected] ', 456);
|
|
|
|
self::assertCount(1, $requests);
|
|
self::assertSame('POST', $requests[0][0]);
|
|
self::assertSame('https://mailjet.test/Contactslist/456/managecontact', $requests[0][1]);
|
|
self::assertSame([
|
|
'Email' => '[email protected]',
|
|
'Action' => 'addforce',
|
|
], json_decode((string) $requests[0][2]['body'], true, 512, JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
public function testAddContactsToListForcesSubscriptionAndSendsOnlyTheFirstName(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
return new MockResponse(json_encode(['Data' => [['JobID' => 555]]], JSON_THROW_ON_ERROR), ['http_code' => 201]);
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test', null, [
|
|
'firstName' => 'vorname',
|
|
'lastName' => 'nachname',
|
|
]);
|
|
|
|
$jobId = $mailjet->addContactsToList(42, [
|
|
['email' => ' [email protected] ', 'firstName' => ' Mia '],
|
|
['email' => '[email protected]', 'firstName' => null],
|
|
]);
|
|
|
|
self::assertSame(555, $jobId);
|
|
self::assertSame('POST', $requests[0][0]);
|
|
self::assertSame('https://mailjet.test/contactslist/42/managemanycontacts', $requests[0][1]);
|
|
self::assertSame([
|
|
'Action' => 'addforce',
|
|
'Contacts' => [
|
|
['Email' => '[email protected]', 'Properties' => ['vorname' => 'Mia']],
|
|
['Email' => '[email protected]'],
|
|
],
|
|
], json_decode((string) $requests[0][2]['body'], true, 512, JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
public function testCreateContactsListReturnsTheNewListId(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url, $options];
|
|
|
|
return new MockResponse(json_encode(['Data' => [['ID' => 42, 'Name' => 'LPJMP281126']]], JSON_THROW_ON_ERROR), ['http_code' => 201]);
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test');
|
|
|
|
self::assertSame(42, $mailjet->createContactsList('LPJMP281126'));
|
|
self::assertSame('https://mailjet.test/contactslist', $requests[0][1]);
|
|
self::assertSame(['Name' => 'LPJMP281126'], json_decode((string) $requests[0][2]['body'], true, 512, JSON_THROW_ON_ERROR));
|
|
}
|
|
|
|
public function testGetListContactIdsPagesThroughAllRecipients(): void
|
|
{
|
|
$offsets = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$offsets): MockResponse {
|
|
$offset = (int) $options['query']['Offset'];
|
|
$offsets[] = $offset;
|
|
$data = 0 === $offset
|
|
? array_map(static fn (int $id): array => ['ContactID' => $id], range(1, 1000))
|
|
: [['ContactID' => 1001]];
|
|
|
|
return new MockResponse(json_encode(['Data' => $data], JSON_THROW_ON_ERROR));
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://mailjet.test');
|
|
|
|
$contactIds = $mailjet->getListContactIds(42);
|
|
|
|
self::assertSame([0, 1000], $offsets);
|
|
self::assertCount(1001, $contactIds);
|
|
}
|
|
|
|
public function testDeleteContactUsesTheV4Api(): void
|
|
{
|
|
$requests = [];
|
|
$client = new MockHttpClient(function (string $method, string $url, array $options) use (&$requests): MockResponse {
|
|
$requests[] = [$method, $url];
|
|
|
|
return new MockResponse('', ['http_code' => 200]);
|
|
});
|
|
|
|
$mailjet = new ApiClient($client, new NullLogger(), 'key', 'secret', 'https://api.mailjet.com/v3/REST');
|
|
|
|
$mailjet->deleteContact(99);
|
|
|
|
self::assertSame([['DELETE', 'https://api.mailjet.com/v4/contacts/99']], $requests);
|
|
}
|
|
}
|