forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersMailerTest.php
More file actions
172 lines (152 loc) · 5 KB
/
Copy pathUsersMailerTest.php
File metadata and controls
172 lines (152 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Test\TestCase\Email;
use Cake\Mailer\Email;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
/**
* Test Case
*/
class UsersMailerTest extends TestCase
{
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'plugin.CakeDC/Users.social_accounts',
'plugin.CakeDC/Users.users',
];
/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->Email = $this->getMockBuilder('Cake\Mailer\Email')
->setMethods(['setTo', 'setSubject', 'setViewVars', 'setTemplate'])
->getMock();
$this->UsersMailer = $this->getMockBuilder('CakeDC\Users\Mailer\UsersMailer')
->setConstructorArgs([$this->Email])
->setMethods(['setTo', 'setSubject', 'setViewVars', 'setTemplate'])
->getMock();
}
/**
* tearDown
*
* @return void
*/
public function tearDown()
{
unset($this->UsersMailer);
unset($this->Email);
parent::tearDown();
}
/**
* test sendValidationEmail
*
* @return void
*/
public function testValidation()
{
$table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users');
$data = [
'first_name' => 'FirstName',
'email' => 'test@example.com',
'token' => '12345'
];
$user = $table->newEntity($data);
$this->UsersMailer->expects($this->once())
->method('setTo')
->with($user['email'])
->will($this->returnValue($this->Email));
$this->Email->expects($this->once())
->method('setSubject')
->with('FirstName, Your account validation link')
->will($this->returnValue($this->Email));
$this->Email->expects($this->once())
->method('setViewVars')
->with($data)
->will($this->returnValue($this->Email));
$this->invokeMethod($this->UsersMailer, 'validation', [$user]);
}
/**
* test SocialAccountValidation
*
* @return void
*/
public function testSocialAccountValidation()
{
$social = TableRegistry::getTableLocator()->get('CakeDC/Users.SocialAccounts')
->get('00000000-0000-0000-0000-000000000001', ['contain' => 'Users']);
$this->UsersMailer->expects($this->once())
->method('setTo')
->with('user-1@test.com')
->will($this->returnValue($this->Email));
$this->Email->expects($this->once())
->method('setSubject')
->with('first1, Your social account validation link')
->will($this->returnValue($this->Email));
$this->Email->expects($this->once())
->method('setViewVars')
->with(['user' => $social->user, 'socialAccount' => $social])
->will($this->returnValue($this->Email));
$this->invokeMethod($this->UsersMailer, 'socialAccountValidation', [$social->user, $social]);
}
/**
* test sendValidationEmail including 'template'
*
* @return void
*/
public function testResetPassword()
{
$table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users');
$data = [
'first_name' => 'FirstName',
'email' => 'test@example.com',
'token' => '12345'
];
$user = $table->newEntity($data);
$this->UsersMailer->expects($this->once())
->method('setTo')
->with($user['email'])
->will($this->returnValue($this->Email));
$this->Email->expects($this->once())
->method('setSubject')
->with('FirstName, Your reset password link')
->will($this->returnValue($this->Email));
$this->Email->expects($this->once())
->method('setViewVars')
->with($data)
->will($this->returnValue($this->Email));
$this->invokeMethod($this->UsersMailer, 'resetPassword', [$user]);
}
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
}