1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \site\models\ChangeEmailForm; |
7
|
|
|
|
8
|
|
|
class ChangeEmailFormTest extends \Codeception\Test\Unit { |
9
|
|
|
use \Codeception\Specify; |
10
|
|
|
|
11
|
|
|
public function testChangeEmail() { |
12
|
|
|
|
13
|
|
|
$this->specify('changeEmail() should return true and do nothing when the email is already taken', function() { |
14
|
|
|
$user = $this->getUser(); |
15
|
|
|
$user |
16
|
|
|
->expects($this->never()) |
17
|
|
|
->method('save'); |
18
|
|
|
$desired_email = '[email protected]'; |
19
|
|
|
$user |
20
|
|
|
->method('findByEmail') |
|
|
|
|
21
|
|
|
->willReturn(true); // true, as a stand-in for some valid user object |
22
|
|
|
|
23
|
|
|
$form = new ChangeEmailForm($user); |
24
|
|
|
$form->desired_email = $desired_email; |
25
|
|
|
$this->assertTrue($form->changeEmail(), 'should always return true'); |
26
|
|
|
$this->assertNull($user->desired_email, 'should be null because we didn\'t set the new email'); |
|
|
|
|
27
|
|
|
$this->assertNotEquals($user->desired_email, $desired_email); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
$this->specify('changeEmail() should return true, set values, and send an email when the email is available', function() { |
31
|
|
|
$user = $this->getUser(); |
32
|
|
|
$desired_email = '[email protected]'; |
33
|
|
|
$user |
34
|
|
|
->method('findByEmail') |
35
|
|
|
->willReturn(null); |
36
|
|
|
$user->email = '[email protected]'; |
|
|
|
|
37
|
|
|
|
38
|
|
|
$form = new ChangeEmailForm($user); |
39
|
|
|
$form->desired_email = $desired_email; |
40
|
|
|
|
41
|
|
|
expect('changeEmail should return true', $this->assertTrue($form->changeEmail())); |
|
|
|
|
42
|
|
|
|
43
|
|
|
expect('the user\'s desired email should now match what they entered in the form', $this->assertEquals($user->desired_email, $desired_email)); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->tester->seeEmailIsSent(); |
|
|
|
|
46
|
|
|
$emailMessage = $this->tester->grabLastSentEmail(); |
47
|
|
|
expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface'); |
48
|
|
|
expect($emailMessage->getTo())->hasKey($desired_email); |
49
|
|
|
expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']); |
50
|
|
|
expect($emailMessage->getSubject())->equals(Yii::$app->name . ' -- your requested email change'); |
51
|
|
|
expect($emailMessage->toString())->contains($user->email); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getUser() { |
56
|
|
|
$user = $this->getmockbuilder('\common\models\user') |
57
|
|
|
->disableoriginalconstructor() |
58
|
|
|
->setmethods(['getisnewrecord', 'attributes', 'generatechangeemailtoken', 'findbyemail', 'save']) |
59
|
|
|
->getmock(); |
60
|
|
|
$user->method('attributes')->willreturn([ |
61
|
|
|
'email', |
62
|
|
|
'desired_email', |
63
|
|
|
'change_email_token', |
64
|
|
|
]); |
65
|
|
|
return $user; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.