Skip to content

Commit

Permalink
Add tests for the admin client
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteel committed Feb 18, 2022
1 parent 32a567a commit 1c65501
Show file tree
Hide file tree
Showing 2 changed files with 311 additions and 36 deletions.
40 changes: 4 additions & 36 deletions src/Postmark/PostmarkAdminClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function deleteServer(int $id): DynamicResponseModel
* link tracking enabled.
* @param string|null $clickHookUrl URL to POST to everytime a click event occurs.
* @param string|null $deliveryHookUrl URL to POST to everytime a click event occurs.
* @param string|null $enableSmtpApiErrorHooks Specifies whether SMTP API Errors will be included
* @param bool|null $enableSmtpApiErrorHooks Specifies whether SMTP API Errors will be included
* with bounce webhooks.
*/
public function editServer(
Expand All @@ -115,7 +115,7 @@ public function editServer(
?string $trackLinks = null,
?string $clickHookUrl = null,
?string $deliveryHookUrl = null,
?string $enableSmtpApiErrorHooks = null
?bool $enableSmtpApiErrorHooks = null
): DynamicResponseModel {
$body = [];
$body['name'] = $name;
Expand Down Expand Up @@ -165,7 +165,7 @@ public function editServer(
* have link tracking enabled.
* @param string|null $clickHookUrl URL to POST to everytime an click event occurs.
* @param string|null $deliveryHookUrl URL to POST to everytime an click event occurs.
* @param string|null $enableSmtpApiErrorHooks Specifies whether or not SMTP API Errors will be included
* @param bool|null $enableSmtpApiErrorHooks Specifies whether or not SMTP API Errors will be included
* with bounce webhooks.
*/
public function createServer(
Expand All @@ -183,7 +183,7 @@ public function createServer(
?string $trackLinks = null,
?string $clickHookUrl = null,
?string $deliveryHookUrl = null,
?string $enableSmtpApiErrorHooks = null
?bool $enableSmtpApiErrorHooks = null
): DynamicResponseModel {
$body = [];
$body['name'] = $name;
Expand Down Expand Up @@ -307,38 +307,6 @@ public function resendSenderSignatureConfirmation(int $id): DynamicResponseModel
);
}

/**
* Request that the Postmark API updates verify the SPF records associated
* with the Sender Signature's email address's domain. Configuring SPF is not required to use
* Postmark, but it is highly recommended, and can improve delivery rates.
*
* @deprecated verifyDomainSPF replaces this method
*
* @param int $id The ID for the Sender Signature for which we wish to verify the SPF records.
*/
public function verifySenderSignatureSPF(int $id): DynamicResponseModel
{
return new DynamicResponseModel(
$this->processRestRequest('POST', sprintf('/senders/%s/verifyspf', $id))
);
}

/**
* Cause a new DKIM key to be generated and associated with the Sender Signature. This key must be added
* to your email domain's DNS records. Including DKIM is not required, but is recommended. For more information
* on DKIM and its purpose, see http://www.dkim.org/
*
* @deprecated rotateDKIMForDomain replaces this method.
*
* @param int $id The ID for the Sender Signature for which we wish to get an updated DKIM configuration.
*/
public function requestNewSenderSignatureDKIM(int $id): DynamicResponseModel
{
return new DynamicResponseModel(
$this->processRestRequest('POST', sprintf('/senders/%s/requestnewdkim', $id))
);
}

/**
* Get a "page" of Domains.
*
Expand Down
307 changes: 307 additions & 0 deletions tests/Unit/PostmarkAdminClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
<?php

declare(strict_types=1);

namespace Postmark\Tests\Unit;

use Postmark\PostmarkAdminClient;

class PostmarkAdminClientTest extends MockClientTestCase
{
private PostmarkAdminClient $client;

protected function setUp(): void
{
parent::setUp();
$client = new PostmarkAdminClient('token', $this->mockClient);
$this->client = $client->withBaseUri('https://example.com');
$response = ResponseFixture::fromFileName('EmptyStubResponse.json', 200)->toResponse();
$this->mockClient->setDefaultResponse($response);
}

/** @return array<string, array{0: string, 1: int, 2: string, 3: string}> */
public function singleIntegerIdArgumentMethodProvider(): array
{
return [
'getServer' => ['getServer', 41, '/servers/41', 'GET'],
'deleteServer' => ['deleteServer', 42, '/servers/42', 'DELETE'],
'getSenderSignature' => ['getSenderSignature', 22, '/senders/22', 'GET'],
'deleteSenderSignature' => ['deleteSenderSignature', 32, '/senders/32', 'DELETE'],
'resendSenderSignatureConfirmation' => ['resendSenderSignatureConfirmation', 19, '/senders/19/resend', 'POST'],
'getDomain' => ['getDomain', 4, '/domains/4', 'GET'],
'deleteDomain' => ['deleteDomain', 7, '/domains/7', 'DELETE'],
'verifyDomainSPF' => ['verifyDomainSPF', 7, '/domains/7/verifyspf', 'POST'],
'rotateDKIMForDomain' => ['rotateDKIMForDomain', 7, '/domains/7/rotatedkim', 'POST'],
];
}

/**
* @covers \Postmark\PostmarkAdminClient::getServer
* @covers \Postmark\PostmarkAdminClient::deleteServer
* @covers \Postmark\PostmarkAdminClient::getSenderSignature
* @covers \Postmark\PostmarkAdminClient::deleteSenderSignature
* @covers \Postmark\PostmarkAdminClient::resendSenderSignatureConfirmation
* @covers \Postmark\PostmarkAdminClient::getDomain
* @covers \Postmark\PostmarkAdminClient::deleteDomain
* @covers \Postmark\PostmarkAdminClient::verifyDomainSPF
* @covers \Postmark\PostmarkAdminClient::rotateDKIMForDomain
* @dataProvider singleIntegerIdArgumentMethodProvider
*/
public function testSingleIntegerIdMethods(string $method, int $id, string $expectPath, string $httpMethod): void
{
$this->client->$method($id);
$this->assertLastRequestMethodWas($httpMethod);
$this->assertLastRequestPathEquals($expectPath);
}

public function testListServersWithoutArgument(): void
{
$this->client->listServers();
$this->assertLastRequestMethodWas('GET');
$this->assertLastRequestPathEquals('/servers/');
$this->assertQueryParameterValueEquals('count', '100');
$this->assertQueryParameterValueEquals('offset', '0');
$this->assertQueryParameterIsAbsent('name');
}

public function testListServersWithArguments(): void
{
$this->client->listServers(1, 2, 'foo');
$this->assertLastRequestMethodWas('GET');
$this->assertLastRequestPathEquals('/servers/');
$this->assertQueryParameterValueEquals('count', '1');
$this->assertQueryParameterValueEquals('offset', '2');
$this->assertQueryParameterValueEquals('name', 'foo');
}

public function testEditServerWithMinimalParameters(): void
{
$this->client->editServer(1);
$this->assertLastRequestMethodWas('PUT');
$this->assertLastRequestPathEquals('/servers/1');

$absent = [
'name',
'color',
'rawEmailEnabled',
'smtpApiActivated',
'inboundHookUrl',
'bounceHookUrl',
'openHookUrl',
'postFirstOpenOnly',
'trackOpens',
'inboundDomain',
'inboundSpamThreshold',
'trackLinks',
'ClickHookUrl',
'DeliveryHookUrl',
'EnableSmtpApiErrorHooks',
];

foreach ($absent as $name) {
$this->assertBodyParameterIsAbsent($name);
}
}

public function testEditServerWithArguments(): void
{
$this->client->editServer(
1,
'Name',
'Red',
true,
false,
'Inbound',
'Bounce',
'Open',
true,
false,
'Domain-In',
23,
'Track',
'Click',
'Deliver',
true
);

$expected = [
'name' => 'Name',
'color' => 'Red',
'rawEmailEnabled' => true,
'smtpApiActivated' => false,
'inboundHookUrl' => 'Inbound',
'bounceHookUrl' => 'Bounce',
'openHookUrl' => 'Open',
'postFirstOpenOnly' => true,
'trackOpens' => false,
'inboundDomain' => 'Domain-In',
'inboundSpamThreshold' => 23,
'trackLinks' => 'Track',
'ClickHookUrl' => 'Click',
'DeliveryHookUrl' => 'Deliver',
'EnableSmtpApiErrorHooks' => true,
];

foreach ($expected as $name => $value) {
$this->assertBodyParameterValueEquals($name, $value);
}
}

public function testCreateServerWithMinimalParameters(): void
{
$this->client->createServer('Name');
$this->assertLastRequestMethodWas('POST');
$this->assertLastRequestPathEquals('/servers/');

$absent = [
'color',
'rawEmailEnabled',
'smtpApiActivated',
'inboundHookUrl',
'bounceHookUrl',
'openHookUrl',
'postFirstOpenOnly',
'trackOpens',
'inboundDomain',
'inboundSpamThreshold',
'trackLinks',
'ClickHookUrl',
'DeliveryHookUrl',
'EnableSmtpApiErrorHooks',
];

$this->assertBodyParameterValueEquals('name', 'Name');

foreach ($absent as $name) {
$this->assertBodyParameterIsAbsent($name);
}
}

public function testCreateServerWithArguments(): void
{
$this->client->createServer(
'Name',
'Red',
true,
false,
'Inbound',
'Bounce',
'Open',
true,
false,
'Domain-In',
23,
'Track',
'Click',
'Deliver',
true
);

$expected = [
'name' => 'Name',
'color' => 'Red',
'rawEmailEnabled' => true,
'smtpApiActivated' => false,
'inboundHookUrl' => 'Inbound',
'bounceHookUrl' => 'Bounce',
'openHookUrl' => 'Open',
'postFirstOpenOnly' => true,
'trackOpens' => false,
'inboundDomain' => 'Domain-In',
'inboundSpamThreshold' => 23,
'trackLinks' => 'Track',
'ClickHookUrl' => 'Click',
'DeliveryHookUrl' => 'Deliver',
'EnableSmtpApiErrorHooks' => true,
];

foreach ($expected as $name => $value) {
$this->assertBodyParameterValueEquals($name, $value);
}
}

public function testListSenders(): void
{
$this->client->listSenderSignatures();
$this->assertLastRequestMethodWas('GET');
$this->assertLastRequestPathEquals('/senders/');
$this->assertQueryParameterValueEquals('count', '100');
$this->assertQueryParameterValueEquals('offset', '0');

$this->client->listSenderSignatures(1, 2);
$this->assertQueryParameterValueEquals('count', '1');
$this->assertQueryParameterValueEquals('offset', '2');
}

public function testListDomains(): void
{
$this->client->listDomains();
$this->assertLastRequestMethodWas('GET');
$this->assertLastRequestPathEquals('/domains/');
$this->assertQueryParameterValueEquals('count', '100');
$this->assertQueryParameterValueEquals('offset', '0');

$this->client->listDomains(1, 2);
$this->assertQueryParameterValueEquals('count', '1');
$this->assertQueryParameterValueEquals('offset', '2');
}

public function testCreateSenderSignature(): void
{
$this->client->createSenderSignature('FROM', 'NAME', 'REPLY', 'RETURN');
$this->assertLastRequestMethodWas('POST');
$this->assertLastRequestPathEquals('/senders/');

$expect = [
'fromEmail' => 'FROM',
'name' => 'NAME',
'replyToEmail' => 'REPLY',
'returnPathDomain' => 'RETURN',
];

foreach ($expect as $name => $value) {
$this->assertBodyParameterValueEquals($name, $value);
}
}

public function testEditSenderSignature(): void
{
$this->client->editSenderSignature(2, 'NAME', 'REPLY', 'RETURN');
$this->assertLastRequestMethodWas('PUT');
$this->assertLastRequestPathEquals('/senders/2');

$expect = [
'name' => 'NAME',
'replyToEmail' => 'REPLY',
'returnPathDomain' => 'RETURN',
];

foreach ($expect as $name => $value) {
$this->assertBodyParameterValueEquals($name, $value);
}
}

public function testCreateDomain(): void
{
$this->client->createDomain('DOMAIN', 'RETURN');
$this->assertLastRequestMethodWas('POST');
$this->assertLastRequestPathEquals('/domains/');

$expect = [
'returnPathDomain' => 'RETURN',
'name' => 'DOMAIN',
];

foreach ($expect as $name => $value) {
$this->assertBodyParameterValueEquals($name, $value);
}
}

public function testEditDomain(): void
{
$this->client->editDomain(2, 'RETURN');
$this->assertLastRequestMethodWas('PUT');
$this->assertLastRequestPathEquals('/domains/2');
$this->assertBodyParameterValueEquals('returnPathDomain', 'RETURN');
}
}

0 comments on commit 1c65501

Please sign in to comment.