Browse Source

Add functional tests for `EmailAssertTrait`

Jad Bitar 9 years ago
parent
commit
96effcc278

+ 49 - 0
tests/TestCase/TestSuite/EmailAssertTraitTest.php

@@ -0,0 +1,49 @@
+<?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\Test\TestCase\TestSuite;
+
+use Cake\TestSuite\EmailAssertTrait;
+use Cake\TestSuite\TestCase;
+use TestApp\Mailer\TestUserMailer;
+
+class EmailAssertTraitTest extends TestCase
+{
+
+    use EmailAssertTrait;
+
+    public function testFunctional()
+    {
+        $mailer = $this->getMockForMailer(TestUserMailer::class);
+        $email = $mailer->getEmailForAssertion();
+        $this->assertSame($this->_email, $email);
+
+        $mailer->invite('lorenzo@cakephp.org');
+        $this->assertEmailSubject('CakePHP');
+        $this->assertEmailFrom('jadb@cakephp.org');
+        $this->assertEmailTo('lorenzo@cakephp.org');
+        $this->assertEmailToContains('lorenzo@cakephp.org');
+        $this->assertEmailToContains('lorenzo@cakephp.org', 'lorenzo@cakephp.org');
+        $this->assertEmailCcContains('markstory@cakephp.org');
+        $this->assertEmailCcContains('admad@cakephp.org', 'Adnan');
+        $this->assertEmailBccContains('dereuromark@cakephp.org');
+        $this->assertEmailBccContains('antograssiot@cakephp.org');
+        $this->assertEmailTextMessageContains('Hello lorenzo@cakephp.org');
+        $this->assertEmailAttachmentsContains('TestUserMailer.php');
+        $this->assertEmailAttachmentsContains('TestMailer.php', [
+            'file' => dirname(dirname(__DIR__)) . DS . 'test_app' . DS . 'TestApp' . DS . 'Mailer' . DS . 'TestMailer.php',
+            'mimetype' => 'application/octet-stream',
+        ]);
+    }
+}

+ 4 - 0
tests/bootstrap.php

@@ -19,6 +19,8 @@ use Cake\Chronos\MutableDateTime;
 use Cake\Core\Configure;
 use Cake\Datasource\ConnectionManager;
 use Cake\Log\Log;
+use Cake\Mailer\Email;
+use Cake\Mailer\Transport\DebugTransport;
 
 require_once 'vendor/autoload.php';
 
@@ -103,6 +105,8 @@ Configure::write('Session', [
     'defaults' => 'php'
 ]);
 
+Email::configTransport('debug', ['className' => DebugTransport::class]);
+
 Log::config([
     // 'queries' => [
     //     'className' => 'Console',

+ 39 - 0
tests/test_app/TestApp/Mailer/TestUserMailer.php

@@ -0,0 +1,39 @@
+<?php
+/**
+ * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
+ * 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
+ * @since         3.4.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace TestApp\Mailer;
+
+/**
+ * Test Suite Test App Mailer class.
+ */
+class TestUserMailer extends TestMailer
+{
+
+    public function invite($email)
+    {
+        $this->_email
+            ->subject('CakePHP')
+            ->from('jadb@cakephp.org')
+            ->to($email)
+            ->cc('markstory@cakephp.org')
+            ->addCc('admad@cakephp.org', 'Adnan')
+            ->bcc('dereuromark@cakephp.org', 'Mark')
+            ->addBcc('antograssiot@cakephp.org')
+            ->attachments([
+                dirname(__FILE__) . DS . 'TestMailer.php',
+                dirname(__FILE__) . DS . 'TestUserMailer.php'
+            ])
+            ->send('Hello ' . $email);
+    }
+}