Completed
Push — master ( a6b9e0...462ccf )
by Corey
03:34
created

PasswordResetRequestFormTest::testSendEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 31
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
use \site\models\PasswordResetRequestForm;
7
use \yii\base\InvalidArgumentException;
8
9
class PasswordResetRequestFormTest extends \Codeception\Test\Unit {
10
  use \Codeception\Specify;
11
12
  public function testRulesShouldValidate() {
13
    $user = $this->getUser();
14
    $form = new PasswordResetRequestForm($user);
15
16
    $form->attributes = [];
17
    expect('with no values, the form should not pass validation', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
18
    $form->attributes = ['email' => null];
19
    expect('the form should not validate if the email is not provided', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
20
    $form->attributes = ['email' => '1'];
21
    expect('the form should not validate if the email is not a valid format', $this->assertFalse($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($form->validate()) targeting PHPUnit\Framework\Assert::assertFalse() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
22
    $form->attributes = ['email' => '   [email protected]  '];
23
    expect('the form should validate', $this->assertTrue($form->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form->validate()) targeting PHPUnit\Framework\Assert::assertTrue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
24
    expect('the form should lowercase and trim the provided email', $this->assertEquals($form->email, '[email protected]'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($for...ail, '[email protected]') targeting PHPUnit\Framework\Assert::assertEquals() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
25
  }
26
27
  /*
28
   * temporarily commenting this out while I figure out how to fix this test
29
   * with the User::findOne() call in sendEmail()
30
   *
31
  public function testSendEmail() {
32
    $user = $this->getUser();
33
    $user->generatePasswordResetToken();
34
35
    $user->isGuest = false;
36
    $user
37
      ->expects($this->once())
38
      ->method('isTokenCurrent')
39
      ->willReturn(true);
40
41
    $user
42
      ->expects($this->once())
43
      ->method('findOne')
44
      ->willReturn($user);
45
46
    $user
47
      ->expects($this->once())
48
      ->method('save')
49
      ->willReturn(true);
50
51
    $form = new PasswordResetRequestForm($user);
52
53
    $form->attributes = [
54
      'email' => '  [email protected]  ',
55
    ];
56
57
    $form->validate();
58
    $form->sendEmail();
59
60
    $this->tester->seeEmailIsSent();
61
    $emailMessage = $this->tester->grabLastSentEmail();
62
    expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
63
    expect($emailMessage->getTo())->hasKey('[email protected]');
64
    expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']);
65
    expect($emailMessage->getSubject())->equals('Password reset for ' . \Yii::$app->name);
66
    expect($emailMessage->toString())->contains('Follow the link below to reset your password');
67
  }
68
   */
69
70
  private function getUser() {
71
    $user = $this->getmockbuilder('\common\models\user')
72
      ->disableoriginalconstructor()
73
      ->setmethods(['getisnewrecord', 'attributes', 'save', 'generatepasswordresettoken', 'istokencurrent', 'findOne'])
74
      ->getmock();
75
    $user->method('attributes')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
    $user->/** @scrutinizer ignore-call */ 
76
           method('attributes')->willReturn([

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.

Loading history...
76
      'isGuest',
77
      'email',
78
      'password',
79
      'password_reset_token',
80
      'password_hash',
81
    ]);
82
    return $user;
83
  }
84
}
85