Completed
Push — master ( 9721ca...863deb )
by Corey
03:08
created

ResetPasswordFormTest::testResetPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
use \site\models\ResetPasswordForm;
7
use \yii\base\InvalidArgumentException;
8
9
class ResetPasswordFormTest extends \Codeception\Test\Unit {
10
  use \Codeception\Specify;
11
12
  public function testShouldThrowWhenNotPassedAWellFormattedToken() {
13
      $this->expectException(InvalidArgumentException::class);
14
      new ResetPasswordForm('', $this->getUser());
15
  }
16
17
  public function testShouldThrowWhenTokenIsNotAssociatedWithAUser() {
18
      $this->expectException(InvalidArgumentException::class);
19
      $token = 'this_is_fine';
20
      $user = $this->getUser();
21
      $user->method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn(null);
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

21
      $user->/** @scrutinizer ignore-call */ 
22
             method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn(null);

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...
22
      new ResetPasswordForm($token, $user);
23
  }
24
25
  public function testRulesShouldValidate() {
26
    $token = 'doesnt_matter';
27
    $user = $this->getUser();
28
    $user->method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn($user);
29
    $form = new ResetPasswordForm($token, $user);
30
31
    $form->attributes = [];
32
    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...
33
    $form->attributes = ['password' => null];
34
    expect('the form should not validate if the password 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...
35
    $form->attributes = ['password' => '1'];
36
    expect('the form should not validate if the password is too short', $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...
37
    $form->attributes = ['password' => 'super-secret-password-here'];
38
    expect('the form should validate if the provided password is long enough', $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...
39
  }
40
41
  public function testResetPassword() {
42
    $user = $this->getUser();
43
    $token = 'doesnt_matter';
44
    $user->method('findbypasswordresettoken')->with($this->equalTo($token))->willReturn($user);
45
    $user->password = 'old';
0 ignored issues
show
Bug introduced by
Accessing password on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
47
    $form = new ResetPasswordForm($token, $user);
48
49
    $user
50
      ->expects($this->once())
51
      ->method('removepasswordresettoken');
52
    $user
53
      ->expects($this->once())
54
      ->method('save');
55
56
    $form->password = 'new';
57
    $form->resetPassword();
58
    $this->assertNotEquals($user->password, 'old');
59
    $this->assertEquals($user->password, 'new');
60
  }
61
62
  private function getUser() {
63
    $user = $this->getmockbuilder('\common\models\user')
64
      ->disableoriginalconstructor()
65
      ->setmethods(['getisnewrecord', 'attributes', 'save', 'findbypasswordresettoken', 'removepasswordresettoken'])
66
      ->getmock();
67
    $user->method('attributes')->willReturn([
68
      'email',
69
      'password',
70
      'password_hash',
71
      'partner_email1',
72
      'partner_email2',
73
      'partner_email3',
74
    ]);
75
    return $user;
76
  }
77
}
78