Completed
Pull Request — master (#170)
by Corey
03:00
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
  public function testSendEmail() {
28
    $user = $this->getUser();
29
    $user->generatePasswordResetToken();
0 ignored issues
show
Bug introduced by
The method generatePasswordResetToken() 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

29
    $user->/** @scrutinizer ignore-call */ 
30
           generatePasswordResetToken();

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...
30
31
    $user->isGuest = false;
0 ignored issues
show
Bug introduced by
Accessing isGuest on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
32
    $user
33
      ->expects($this->once())
34
      ->method('isTokenCurrent')
35
      ->willReturn(true);
36
37
    $user
38
      ->expects($this->once())
39
      ->method('save')
40
      ->willReturn(true);
41
42
    $form = new PasswordResetRequestForm($user);
43
44
    $form->attributes = [
45
      'email' => '  [email protected]  ',
46
    ];
47
48
    $form->validate();
49
    $form->sendEmail();
50
51
    $this->tester->seeEmailIsSent();
0 ignored issues
show
Bug Best Practice introduced by
The property tester does not exist on site\tests\unit\models\P...ordResetRequestFormTest. Did you maybe forget to declare it?
Loading history...
52
    $emailMessage = $this->tester->grabLastSentEmail();
53
    expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
54
    expect($emailMessage->getTo())->hasKey('[email protected]');
55
    expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']);
56
    expect($emailMessage->getSubject())->equals('Password reset for ' . \Yii::$app->name);
57
    expect($emailMessage->toString())->contains('Follow the link below to reset your password');
58
  }
59
60
  private function getUser() {
61
    $user = $this->getmockbuilder('\common\models\user')
62
      ->disableoriginalconstructor()
63
      ->setmethods(['getisnewrecord', 'attributes', 'save', 'generatepasswordresettoken', 'istokencurrent'])
64
      ->getmock();
65
    $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

65
    $user->/** @scrutinizer ignore-call */ 
66
           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...
66
      'isGuest',
67
      'email',
68
      'password',
69
      'password_reset_token',
70
      'password_hash',
71
    ]);
72
    return $user;
73
  }
74
}
75