-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f508833
commit 2b73a54
Showing
8 changed files
with
230 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,40 +2,161 @@ | |
|
||
namespace Kirby\Email; | ||
|
||
use Kirby\Cms\App; | ||
use Kirby\Cms\User; | ||
use Kirby\Exception\InvalidArgumentException; | ||
use Kirby\Exception\NotFoundException; | ||
use Kirby\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Email\Body | ||
* @covers ::__construct | ||
*/ | ||
class BodyTest extends TestCase | ||
{ | ||
public function testConstruct() | ||
{ | ||
$body = new Body(); | ||
public const FIXTURES = __DIR__ . '/fixtures'; | ||
|
||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactory(): void | ||
{ | ||
$body = Body::factory($text = 'test'); | ||
$this->assertSame($text, $body->text()); | ||
$this->assertSame('', $body->html()); | ||
$this->assertSame('', $body->text()); | ||
|
||
$body = Body::factory([ | ||
'text' => $text, | ||
'html' => $html = '<b>test</b>' | ||
]); | ||
$this->assertSame($text, $body->text()); | ||
$this->assertSame($html, $body->html()); | ||
} | ||
|
||
public function testConstructParams() | ||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryTemplate(): void | ||
{ | ||
$data = [ | ||
'html' => '<strong>We will never reply</strong>', | ||
'text' => 'We will never reply' | ||
]; | ||
new App([ | ||
'templates' => [ | ||
'emails/contact' => static::FIXTURES . '/contact.php' | ||
] | ||
]); | ||
|
||
$body = new Body($data); | ||
$body = Body::factory(null, 'contact', ['name' => 'Alex']); | ||
$this->assertSame('Cheers, Alex!', $body->text()); | ||
} | ||
|
||
$this->assertSame($data['html'], $body->html()); | ||
$this->assertSame($data['text'], $body->text()); | ||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryTemplateHtml(): void | ||
{ | ||
new App([ | ||
'templates' => [ | ||
'emails/media.html' => static::FIXTURES . '/media.html.php' | ||
] | ||
]); | ||
|
||
$body = Body::factory(null, 'media'); | ||
$this->assertSame('<b>Image:</b> <img src=""/>', $body->html()); | ||
$this->assertSame('', $body->text()); | ||
} | ||
|
||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryTemplateHtmlText(): void | ||
{ | ||
new App([ | ||
'templates' => [ | ||
'emails/media.html' => static::FIXTURES . '/media.html.php', | ||
'emails/media.text' => static::FIXTURES . '/media.text.php', | ||
] | ||
]); | ||
|
||
$body = Body::factory(null, 'media'); | ||
$this->assertSame('<b>Image:</b> <img src=""/>', $body->html()); | ||
$this->assertSame('Image: Description', $body->text()); | ||
} | ||
|
||
public function testConstructNullParams() | ||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryTemplateData(): void | ||
{ | ||
$body = new Body([ | ||
'html' => null, | ||
'text' => null | ||
new App([ | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'templates' => [ | ||
'emails/user-info' => static::FIXTURES . '/user-info.php' | ||
] | ||
]); | ||
|
||
$user = new User([ | ||
'email' => '[email protected]', | ||
'name' => 'Mario' | ||
]); | ||
|
||
$body = Body::factory(null, 'user-info', ['user' => $user]); | ||
$this->assertSame('Welcome, Mario!', trim($body->text())); | ||
} | ||
|
||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryTemplateInvalid(): void | ||
{ | ||
$this->expectException(NotFoundException::class); | ||
$this->expectExceptionMessage('The email template "subscription" cannot be found'); | ||
Body::factory(null, 'subscription'); | ||
} | ||
|
||
/** | ||
* @covers ::factory | ||
*/ | ||
public function testFactoryInvalid(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Email requires either body or template'); | ||
Body::factory(); | ||
} | ||
|
||
/** | ||
* @covers ::html | ||
*/ | ||
public function testHtml(): void | ||
{ | ||
$body = new Body(); | ||
$this->assertSame('', $body->html()); | ||
|
||
$body = new Body(html: $html = '<strong>Foo</strong>'); | ||
$this->assertSame($html, $body->html()); | ||
} | ||
|
||
/** | ||
* @covers ::isHtml | ||
*/ | ||
public function testIsHtml(): void | ||
{ | ||
$body = new Body(); | ||
$this->assertFalse($body->isHtml()); | ||
|
||
$body = new Body(html: '<strong>Foo</strong>'); | ||
$this->assertTrue($body->isHtml()); | ||
} | ||
|
||
/** | ||
* @covers ::text | ||
*/ | ||
public function testText(): void | ||
{ | ||
$body = new Body(); | ||
$this->assertSame('', $body->text()); | ||
|
||
$body = new Body(text: $text = 'Foo'); | ||
$this->assertSame($text, $body->text()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,37 @@ public function testProperties() | |
$this->assertSame(['type' => 'mail'], $email->transport()); | ||
} | ||
|
||
public function testToArray() | ||
{ | ||
$email = $this->_email([ | ||
'from' => $from = '[email protected]', | ||
'to' => $to = '[email protected]', | ||
'subject' => $subject = 'Thank you for your contact request', | ||
'body' => $body = 'We will never reply', | ||
]); | ||
|
||
$expected = [ | ||
'attachments' => [], | ||
'bcc' => [], | ||
'body' => [ | ||
'html' => '', | ||
'text' => $body | ||
], | ||
'cc' => [], | ||
'from' => $from, | ||
'fromName' => null, | ||
'replyTo' => '', | ||
'replyToName' => null, | ||
'subject' => $subject, | ||
'to' => [$to => null], | ||
'transport' => [ | ||
'type' => 'mail' | ||
] | ||
]; | ||
|
||
$this->assertSame($expected, $email->toArray()); | ||
} | ||
|
||
public function testRequiredProperty() | ||
{ | ||
$this->expectException(Exception::class); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Cheers, <?= $name ?>! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<b>Image:</b> <img src=""/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Image: Description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Welcome, <?= $user->name() ?>! |