testValidatePasswordShouldFailWithABadPassword()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 19
rs 9.7666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
7
class ChangePasswordFormTest extends \Codeception\Test\Unit
8
{
9
  private $user;
10
11
  public function setUp() {
12
    $this->container = new \yii\di\Container;
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
13
    $this->user = $this->getMockBuilder('\site\tests\_support\MockUser')
14
      ->setMethods(['validatePassword', 'setPassword', 'save'])
15
      ->getMock();
16
    parent::setUp();
17
  }
18
19
  public function tearDown() {
20
    $this->user = null;
21
    parent::tearDown();
22
  }
23
24
  public function testSimpleRulesShouldValidate() {
25
    $user = $this->user;
26
    $this->container = new \yii\di\Container;
0 ignored issues
show
Bug Best Practice introduced by
The property container does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
    $form = $this->container->get('\site\models\ChangePasswordForm', [$user]);
28
29
    $form->attributes = [];
30
    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...
31
    $form->attributes = ['old_password' => '12345678'];
32
    expect('with one value, 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 = ['new_password' => '12345678'];
34
    expect('with one value, 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...
35
36
    $user
37
      ->expects($this->never())
38
      ->method('validatePassword');
39
    $form->attributes = [
40
      'old_password' => '123456',
41
      'new_password' => '789012',
42
    ];
43
    expect('with a new password of < 8 chars, 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...
44
  }
45
46
  public function testValidatePasswordShouldFailWithABadPassword() {
47
    $user = $this->user;
48
49
    $old = 'not_my_current_pass';
50
    $user
51
      ->expects($this->once())
52
      ->method('validatePassword')
53
      ->with($this->equalTo($old));
54
    $user
55
      ->method('validatePassword')
56
      ->willReturn(false);
57
58
    $form = $this->container->get('\site\models\ChangePasswordForm', [$user]);
59
    $form->attributes = [
60
      'old_password' => $old,
61
      'new_password' => 'my_desired_Pass',
62
    ];
63
    expect('a failed password 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...
64
    expect('a failed password should add an error fail validation', $this->assertGreaterThan(0, count($form->errors)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertGreaterThan(0, count($form->errors)) targeting PHPUnit\Framework\Assert::assertGreaterThan() 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...
65
  }
66
67
  public function testValidatePasswordShouldPassWithAGoodPassword() {
68
    $user = $this->user;
69
    $old = 'my_current_pass';
70
71
    $user
72
      ->method('validatePassword')
73
      ->willReturn(true);
74
    $user
75
      ->expects($this->once())
76
      ->method('validatePassword')
77
      ->with($this->equalTo($old));
78
79
    $form = $this->container->get('\site\models\ChangePasswordForm', [$user]);
80
    $form->attributes = [
81
      'old_password' => $old,
82
      'new_password' => 'my_desired_Pass',
83
    ];
84
    expect('a good password should pass validation', $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...
85
    expect('a good password should not add any errors', $this->assertEquals(0, count($form->errors)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(0, count($form->errors)) 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...
86
  }
87
88
  public function testChangePassword() {
89
    $user = $this->user;
90
    $old = 'my_current_pass';
91
    $new = 'my_desired_Pass';
92
93
    $user
94
      ->method('save')
95
      ->willReturn(true);
96
    $user
97
      ->expects($this->once())
98
      ->method('setPassword')
99
      ->with($this->equalTo($new));
100
101
    $form = $this->container->get('\site\models\ChangePasswordForm', [$user]);
102
    $form->attributes = [
103
      'old_password' => $old,
104
      'new_password' => $new,
105
    ];
106
    expect('changePassword should return true if save returns true', $this->assertTrue($form->changePassword()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form->changePassword()) 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...
107
  }
108
}
109