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

Add email assertions trait #9322

Merged
merged 9 commits into from
Aug 30, 2016
Merged
Changes from 2 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
153 changes: 153 additions & 0 deletions src/TestSuite/EmailAssertTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.3.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.3.3 :)

* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\TestSuite\Assertion;

use Cake\Mailer\Email;

/**
* Email and mailer assertions.
*
* @method \PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount any()
* @method void assertSame($expected, $result, $message)
* @method void assertTextContains($needle, $haystack, $message)
* @method \PHPUnit_Framework_MockObject_MockBuilder getMockBuilder($className)
*/
trait EmailAssertTrait
{

/**
* @var \Cake\Mailer\Email
*/
protected $_email;

/**
* @param array|string|null $content The email's content to send.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function send($content = null)
{
$this->email(true)->send($content);
}

/**
* @param bool $new Tells if new instance should forcebly be created.
* @return \Cake\Mailer\Email
*/
public function email($new = false)
{
if ($new || !$this->_email) {
$this->_email = new Email();
$this->_email->profile(['transport' => 'debug'] + $this->_email->profile());
}

return $this->_email;
}

/**
* @param string $className The mailer's FQCN.
* @param array $methods The methods to mock on the mailer.
* @return \Cake\Mailer\Mailer|\PHPUnit_Framework_MockObject_MockObject
*/
public function getMockForMailer($className, array $methods = [])
{
$name = current(array_slice(explode('\\', $className), -1));

if (!in_array('profile', $methods)) {
$methods[] = 'profile';
}

$mailer = $this->getMockBuilder($className)
->setMockClassName($name)
->setMethods($methods)
->setConstructorArgs([$this->email()])
->getMock();

$mailer->expects($this->any())
->method('profile')
->willReturn($mailer);

return $mailer;
}

/**
* @param string $needle Text to look for.
* @param string $message The failure message to define.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function assertEmailMessageContains($needle, $message = null)
{
$this->assertEmailHtmlMessageContains($needle, $message);
$this->assertEmailTextMessageContains($needle, $message);
}

/**
* @param string $needle Text to look for.
* @param string $message The failure message to define.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function assertEmailHtmlMessageContains($needle, $message = null)
{
$haystack = $this->email()->message('html');
$this->assertTextContains($needle, $haystack, $message);
}

/**
* @param string $needle Text to look for.
* @param string $message The failure message to define.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function assertEmailTextMessageContains($needle, $message = null)
{
$haystack = $this->email()->message('text');
$this->assertTextContains($needle, $haystack, $message);
}

/**
* @param string $expected Email's subject.
* @param string $message The failure message to define.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function assertEmailSubject($expected, $message = null)
{
$result = $this->email()->subject();
$this->assertSame($expected, $result, $message);
}

/**
* @param string $email Sender's email address.
* @param string $name Sender's name.
* @param string $message The failure message to define.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function assertEmailFrom($email, $name, $message = null)
{
$expected = [$email => $name];
$result = $this->email()->from();
$this->assertSame($expected, $result, $message);
}

/**
* @param string $email Sender's email address.
* @param string $name Sender's name.
* @param string $message The failure message to define.
* @return void
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return tag in function comment

public function assertEmailTo($email, $name, $message = null)
{
$expected = [$email => $name];
$result = $this->email()->to();
$this->assertSame($expected, $result, $message);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there also be assertions for cc, bcc and attachments?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also maybe a assertEmailToContains() to look for an email address in a list

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally, was just a PoC to start with.

}