Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add html assertions #25

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ class MyTest extends KernelTestCase // or WebTestCase
->assertTextContains('some text')
->assertHtmlContains('some text')
->assertContains('some text') // asserts text and html both contain a value
->assertHasFile('file.txt', 'text/plain', 'Hello there!')

// html assertions (requires zenstruck/assert-html)
->assertHtml()
->contains('some text')
->containsIn('h1', 'some title')
->back() // go back to the TestEmail

// attachment assertions
->assertHasFile('file.txt') // that file.txt is attached
->assertHasFile('file.txt', 'text/plain') // that file.txt is attached with content type "text/plain"
->assertHasFile('file.txt', 'text/plain', 'Hello there!') // that file.txt is attached with content type "text/plain" and content is "Hello there!"

// tag/meta data assertions (https://symfony.com/doc/current/mailer.html#adding-tags-and-metadata-to-emails)
->assertHasTag('password-reset')
Expand All @@ -70,8 +80,12 @@ class MyTest extends KernelTestCase // or WebTestCase
}
```

**NOTE**: Emails are persisted between kernel reboots within each test. You can reset the
collected emails with `$this->mailer()->reset()`.
> **Note**: Emails are persisted between kernel reboots within each test. You can reset the
> collected emails with `$this->mailer()->reset()`.

> **Note**: `assertHtml()` requires [`zenstruck/assert-html`](https://github.com/zenstruck/assert-html).
> Install with `composer require --dev zenstruck/assert-html`. [View documentation for all
> available assertions](https://github.com/zenstruck/assert#html-expectations).

### SentEmails Collection

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"symfony/phpunit-bridge": "^6.0",
"symfony/var-dumper": "^5.4|^6.0",
"symfony/yaml": "^5.4|^6.0",
"zenstruck/assert-html": "^0.2.1",
"zenstruck/browser": "^1.0"
},
"config": {
Expand Down
31 changes: 31 additions & 0 deletions src/Expectation/HtmlExpectation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the zenstruck/mailer-test package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Mailer\Test\Expectation;

use Zenstruck\Assert\HtmlExpectation as BaseHtmlExpectation;
use Zenstruck\Mailer\Test\TestEmail;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class HtmlExpectation extends BaseHtmlExpectation
{
public function __construct(string $html, private TestEmail $email)
{
parent::__construct($html);
}

public function back(): TestEmail
{
return $this->email;
}
}
13 changes: 13 additions & 0 deletions src/TestEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\ParameterizedHeader;
use Zenstruck\Assert;
use Zenstruck\Assert\HtmlExpectation as BaseHtmlException;
use Zenstruck\Callback;
use Zenstruck\Callback\Parameter;
use Zenstruck\Mailer\Test\Expectation\HtmlExpectation;

/**
* @mixin Email
Expand Down Expand Up @@ -300,6 +302,17 @@ final public function assertHasMetadata(string $expectedKey, ?string $expectedVa
return $this;
}

public function assertHtml(): HtmlExpectation
{
if (!\class_exists(BaseHtmlException::class)) {
throw new \LogicException('zenstruck/assert-html is required to use the HTML assertions (composer require --dev zenstruck/assert-html).');
}

Assert::that($this->email->getHtmlBody())->isNotEmpty('No [text/html] part found.');

return new HtmlExpectation((string) $this->email->getHtmlBody(), $this);
}

/**
* @return static
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/Email1.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct()
->attachFromPath(__DIR__.'/files/attachment.txt')
->attachFromPath(__DIR__.'/files/attachment.txt', 'name with space.txt')
->subject('email subject')
->html('html body')
->html('<h1>title</h1><div>html body</div>')
->text('text body')
;

Expand Down
4 changes: 4 additions & 0 deletions tests/InteractsWithMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function can_assert_email_sent(string $environment): void
->assertSubjectContains('sub')
->assertHtmlContains('html body')
->assertTextContains('text body')
->assertHtml()
->contains('html body')
->containsIn('h1', 'title')
->back()
->assertContains('body')
->assertHasFile('attachment.txt')
->assertHasFile('attachment.txt', 'text/plain')
Expand Down