-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Changes from 1 commit
2468c25
f813a52
b491934
2373385
96effcc
57e715d
22e150b
7f7399f
43178fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?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 | ||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @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 $className | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter name |
||
* @param array $methods | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
* @param string $message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing parameter comment |
||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing @return tag in function comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there also be assertions for cc, bcc and attachments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and also maybe a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally, was just a PoC to start with. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.3.3 :)