Skip to content

Commit

Permalink
[minor] use zenstruck/assert for assertions instead of phpunit (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Sep 30, 2021
1 parent 23c7876 commit 14a87b8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"php": ">=7.4",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/mailer": "^4.4|^5.0",
"zenstruck/assert": "^1.0",
"zenstruck/callback": "^1.1"
},
"require-dev": {
Expand Down
50 changes: 28 additions & 22 deletions src/TestEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Zenstruck\Mailer\Test;

use PHPUnit\Framework\Assert as PHPUnit;
use Symfony\Component\Mime\Email;
use Zenstruck\Assert;

/**
* @author Kevin Bond <kevinbond@gmail.com>
Expand All @@ -26,7 +26,7 @@ final public function __call($name, $arguments)

final public function assertSubject(string $expected): self
{
PHPUnit::assertSame($expected, $this->email->getSubject());
Assert::that($this->email->getSubject())->is($expected);

return $this;
}
Expand All @@ -38,13 +38,13 @@ final public function assertFrom(string $expectedEmail, string $expectedName = '
continue;
}

PHPUnit::assertSame($expectedEmail, $address->getAddress());
PHPUnit::assertSame($expectedName, $address->getName());
Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

PHPUnit::fail("Message does not have from [{$expectedEmail}]");
Assert::fail("Message does not have from [{$expectedEmail}]");
}

final public function assertTo(string $expectedEmail, string $expectedName = ''): self
Expand All @@ -54,13 +54,13 @@ final public function assertTo(string $expectedEmail, string $expectedName = '')
continue;
}

PHPUnit::assertSame($expectedEmail, $address->getAddress());
PHPUnit::assertSame($expectedName, $address->getName());
Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

PHPUnit::fail("Message does not have to [{$expectedEmail}]");
Assert::fail("Message does not have to [{$expectedEmail}]");
}

final public function assertCc(string $expectedEmail, string $expectedName = ''): self
Expand All @@ -70,13 +70,13 @@ final public function assertCc(string $expectedEmail, string $expectedName = '')
continue;
}

PHPUnit::assertSame($expectedEmail, $address->getAddress());
PHPUnit::assertSame($expectedName, $address->getName());
Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

PHPUnit::fail("Message does not have cc [{$expectedEmail}]");
Assert::fail("Message does not have cc [{$expectedEmail}]");
}

final public function assertBcc(string $expectedEmail, string $expectedName = ''): self
Expand All @@ -86,13 +86,13 @@ final public function assertBcc(string $expectedEmail, string $expectedName = ''
continue;
}

PHPUnit::assertSame($expectedEmail, $address->getAddress());
PHPUnit::assertSame($expectedName, $address->getName());
Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

PHPUnit::fail("Message does not have bcc [{$expectedEmail}]");
Assert::fail("Message does not have bcc [{$expectedEmail}]");
}

final public function assertReplyTo(string $expectedEmail, string $expectedName = ''): self
Expand All @@ -102,13 +102,13 @@ final public function assertReplyTo(string $expectedEmail, string $expectedName
continue;
}

PHPUnit::assertSame($expectedEmail, $address->getAddress());
PHPUnit::assertSame($expectedName, $address->getName());
Assert::that($address->getAddress())->is($expectedEmail);
Assert::that($address->getName())->is($expectedName);

return $this;
}

PHPUnit::fail("Message does not have reply-to [{$expectedEmail}]");
Assert::fail("Message does not have reply-to [{$expectedEmail}]");
}

/**
Expand All @@ -124,14 +124,18 @@ final public function assertContains(string $expected): self

final public function assertHtmlContains(string $expected): self
{
PHPUnit::assertStringContainsString($expected, $this->email->getHtmlBody(), "The [text/html] part does not contain [{$expected}]");
Assert::that($this->email->getHtmlBody())
->contains($expected, 'The [text/html] part does not contain "{expected}".')
;

return $this;
}

final public function assertTextContains(string $expected): self
{
PHPUnit::assertStringContainsString($expected, $this->email->getTextBody(), "The [text/plain] part does not contain [{$expected}]");
Assert::that($this->email->getTextBody())
->contains($expected, 'The [text/plain] part does not contain "{expected}".')
;

return $this;
}
Expand All @@ -143,12 +147,14 @@ final public function assertHasFile(string $expectedFilename, string $expectedCo
continue;
}

PHPUnit::assertSame($expectedContents, $attachment->getBody());
PHPUnit::assertSame($expectedContentType.'; name='.$expectedFilename, $attachment->getPreparedHeaders()->get('content-type')->getBodyAsString());
Assert::that($attachment->getBody())->is($expectedContents);
Assert::that($attachment->getPreparedHeaders()->get('content-type')->getBodyAsString())
->is($expectedContentType.'; name='.$expectedFilename)
;

return $this;
}

PHPUnit::fail("Message does not include file with filename [{$expectedFilename}]");
Assert::fail("Message does not include file with filename [{$expectedFilename}]");
}
}
6 changes: 4 additions & 2 deletions src/TestMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Zenstruck\Mailer\Test;

use PHPUnit\Framework\Assert;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Event\MessageEvents;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\RawMessage;
use Zenstruck\Assert;
use Zenstruck\Callback;
use Zenstruck\Callback\Parameter;

Expand Down Expand Up @@ -69,7 +69,9 @@ public function assertNoEmailSent(): self

public function assertSentEmailCount(int $count): self
{
Assert::assertCount($count, $this->sentEmails(), \sprintf('Expected %d emails to be sent, but %d emails were sent.', $count, \count($this->sentEmails())));
Assert::that($this->sentEmails())
->hasCount($count, 'Expected {expected} emails to be sent, but {actual} emails were sent.')
;

return $this;
}
Expand Down

0 comments on commit 14a87b8

Please sign in to comment.