Skip to content

Commit

Permalink
Issue 129 (#130)
Browse files Browse the repository at this point in the history
This addresses issue #129.

---------

Co-authored-by: Ash <[email protected]>
Co-authored-by: Eli Wood <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2024
1 parent 610d78a commit 3c2c848
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Postmark/Models/PostmarkBounce.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class PostmarkBounce
{
public string $RecordType;
public int $ID;
public string $Type;
public int $TypeCode;
Expand All @@ -25,6 +26,7 @@ class PostmarkBounce

public function __construct(array $values)
{
$this->RecordType = !empty($values['RecordType']) ? $values['RecordType'] : '';
$this->ID = !empty($values['ID']) ? $values['ID'] : 0;
$this->Type = !empty($values['Type']) ? $values['Type'] : 0;
$this->TypeCode = !empty($values['TypeCode']) ? $values['TypeCode'] : '';
Expand All @@ -45,6 +47,17 @@ public function __construct(array $values)
$this->Content = !empty($values['Content']) ? $values['Content'] : '';
}

public function getRecordType(): mixed
{
return $this->RecordType;
}

public function setRecordType(mixed $RecordType): PostmarkBounce
{
$this->RecordType = $RecordType;
return $this;
}

public function getID(): int
{
return $this->ID;
Expand Down
9 changes: 5 additions & 4 deletions src/Postmark/Models/PostmarkBounceActivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PostmarkBounceActivation
public function __construct(array $values)
{
$this->Message = !empty($values['Message']) ? $values['Message'] : '';
$this->Bounce = !empty($values['Bounce']) ? $values['Bounce'] : new PostmarkBounce([]);
$this->setBounce(!empty($values['Bounce']) ? $values['Bounce'] : array());
}

/**
Expand Down Expand Up @@ -40,11 +40,12 @@ public function getBounce(): mixed
}

/**
* @param mixed|PostmarkBounce $Bounce
* @param array $Bounce
* @return PostmarkBounceActivation
*/
public function setBounce(mixed $Bounce): PostmarkBounceActivation
public function setBounce(array $Bounce): PostmarkBounceActivation
{
$this->Bounce = $Bounce;
$this->Bounce = new PostmarkBounce($Bounce);

return $this;
}
Expand Down
74 changes: 74 additions & 0 deletions tests/PostmarkClientBounceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

require_once __DIR__ . '/PostmarkClientBaseTest.php';

use Postmark\Models;
use Postmark\PostmarkClient;
use Postmark\tests;

/**
* @internal
Expand All @@ -13,6 +15,11 @@
*/
class PostmarkClientBounceTest extends PostmarkClientBaseTest
{
public static function setUpBeforeClass(): void
{
PostmarkClientSuppressionsTest::tearDownAfterClass();
}

public function testClientCanGetDeliveryStatistics()
{
$tk = parent::$testKeys;
Expand Down Expand Up @@ -41,6 +48,7 @@ public function testClientCanGetBounce()
$id = $bounces->getBounces()[0]->getID();
$bounce = $client->getBounce($id);
$this->assertNotEmpty($bounce);
$this->assertEquals($id, $bounce->getID());
}

public function testClientCanGetBounceDump()
Expand All @@ -51,5 +59,71 @@ public function testClientCanGetBounceDump()
$id = $bounces->Bounces[0]->getID();
$dump = $client->getBounceDump($id);
$this->assertNotEmpty($dump);
$this->assertNotEmpty($dump->getBody());
}

public function testClientCanActivateBounce()
{
$tk = parent::$testKeys;
$client = new PostmarkClient($tk->WRITE_TEST_SERVER_TOKEN, $tk->TEST_TIMEOUT);

// make sure that this email is not suppressed
// generate a bounces
$fromEmail = $tk->WRITE_TEST_SENDER_EMAIL_ADDRESS;
$toEmail = "[email protected]"; // special email to generate bounce
$subject = "Hello from Postmark!";
$htmlBody = "<strong>Hello</strong> dear Postmark user.";
$textBody = "Hello dear Postmark user.";
$tag = "example-email-tag";
$trackOpens = true;
$trackLinks = "None";

$sendResult = $client->sendEmail(
$fromEmail,
$toEmail,
$subject,
$htmlBody,
$textBody,
$tag,
$trackOpens,
NULL, // Reply To
NULL, // CC
NULL, // BCC
NULL, // Header array
NULL, // Attachment array
$trackLinks,
NULL // Metadata array
);

// make sure there is enough time for the bounce to take place.
sleep(180);

$bounceList = $client->getBounces(20, 0);
$id = 0;
$sentId = $sendResult->getMessageID();
$bounces = $bounceList->getBounces();

$this->assertNotEmpty($bounces);
$this->assertNotEmpty($sentId);

foreach ($bounces as $bounce)
{
$bmid = $bounce->getMessageID();
echo "\n Bounce ID: $bmid Sent id: $sentId";
if ($sentId === $bmid)
{
$id = $bounce->getID();
echo "Made it!! $id";
break;
}
}

$this->assertGreaterThan(0, $id);

$bounceActivation = $client->activateBounce($id);
$actBounce = $bounceActivation->getBounce();

$this->assertNotEmpty($actBounce);
$this->assertEquals($id, $actBounce->getID());
}
}
9 changes: 9 additions & 0 deletions tests/PostmarkClientSuppressionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public static function tearDownAfterClass(): void
{
$tk = parent::$testKeys;
$client = new PostmarkClient($tk->WRITE_TEST_SERVER_TOKEN, $tk->TEST_TIMEOUT);

// remove all suppressions on the default stream
$sups = $client->getSuppressions();
foreach ($sups->getSuppressions() as $sup)
{
$suppressionChanges = [new SuppressionChangeRequest($sup->getEmailAddress())];
$messageStream = 'outbound';
$client->deleteSuppressions($suppressionChanges, $messageStream);
}
}

// create suppression
Expand Down
32 changes: 31 additions & 1 deletion tests/PostmarkClientTemplatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_once __DIR__ . '/PostmarkClientBaseTest.php';

use Postmark\Models\MessageStream\PostmarkMessageStream;
use Postmark\Models\PostmarkAttachment;
use Postmark\Models\TemplatedPostmarkMessage;
use Postmark\PostmarkClient;
Expand Down Expand Up @@ -156,12 +157,41 @@ public function testClientCanSendMailWithTemplate()
{
$tk = parent::$testKeys;
$client = new PostmarkClient($tk->WRITE_TEST_SERVER_TOKEN, $tk->TEST_TIMEOUT);

// make sure the message stream exists
$id = 'php-test';
$messageStreamType = 'Transactional';
$name = 'PHP Test';
$description = 'Test Stream Description';
$createdStream = new PostmarkMessageStream(array());
try
{
$createdStream = $client->getMessageStream($id);
}
catch (\Exception $ex)
{
$createdStream = $client->createMessageStream($id, $messageStreamType, $name, $description);
}

$this->assertEquals($id, $createdStream->getID());

$result = $client->createTemplate('test-php-template-' . date('c'), '{{subject}}', 'Hello <b>{{name}}</b>!', 'Hello {{name}}!');
$emailResult = $client->sendEmailWithTemplate(
$tk->WRITE_TEST_SENDER_EMAIL_ADDRESS,
$tk->WRITE_TEST_EMAIL_RECIPIENT_ADDRESS,
$result->getTemplateId(),
['subjectValue' => 'Hello!']
['subjectValue' => 'Hello!'],
false,
"TestTag",
true,
$tk->WRITE_TEST_SENDER_EMAIL_ADDRESS,
null, //cc
null, //bcc
null, // headers
null, // attachments
null, // tracklinks
null, // metadata
"php-test" // stream name
);

$this->assertEquals(0, $emailResult->getErrorCode());
Expand Down

0 comments on commit 3c2c848

Please sign in to comment.