Completed
Push — master ( f84a67...9721ca )
by Corey
08:20
created

DeleteAccountFormTest::getUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
use \site\models\DeleteAccountForm;
7
8
class DeleteAccountFormTest extends \Codeception\Test\Unit {
9
  use \Codeception\Specify;
10
11
  public function testRulesShouldValidate() {
12
    $user = $this->getUser();
13
    $form = new DeleteAccountForm($user);
14
15
    $form->attributes = [];
16
    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...
17
    $form->attributes = ['password' => null];
18
    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...
19
    $form->attributes = ['password' => '1'];
20
    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...
21
    $form->attributes = ['password' => 'super-secret-password-here'];
22
    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...
23
  }
24
25
  public function testDeleteAccount() {
26
    $this->specify('deleteAccount() should return false if the form does not validate', function() {
27
      $user = $this->getUser();
28
      $user
29
        ->expects($this->never())
30
        ->method('delete');
31
32
      $form = new DeleteAccountForm($user);
33
      $form->password = '1';
34
      $this->assertFalse($form->deleteAccount(), 'deleteAccount should not be successful');
35
    });
36
37
    $this->specify('deleteAccount() should return false if the user\'s password is incorrect', function() {
38
      $user = $this->getUser();
39
      $user
40
        ->expects($this->never())
41
        ->method('delete');
42
43
      $form = new DeleteAccountForm($user);
44
      $form->password = '1';
45
      $this->assertFalse($form->deleteAccount(), 'deleteAccount should not be successful');
46
    });
47
48
    $this->specify('deleteAccount() should return true, send a notification email, and delete the user when the form validates and the user\'s password is correct', function() {
49
      $password = 'password';
50
      $user = $this->getUser();
51
      $user->email = '[email protected]';
0 ignored issues
show
Bug introduced by
Accessing email on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
52
      $user->partner_email1 = '[email protected]';
0 ignored issues
show
Bug introduced by
Accessing partner_email1 on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
      $user->setPassword($password);
0 ignored issues
show
Bug introduced by
The method setPassword() 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

53
      $user->/** @scrutinizer ignore-call */ 
54
             setPassword($password);

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...
54
      $user
55
        ->method('isPartnerEnabled')
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

55
        ->/** @scrutinizer ignore-call */ 
56
          method('isPartnerEnabled')

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...
56
        ->willReturn(true);
57
58
      $user
59
        ->expects($this->once())
60
        ->method('delete');
61
62
      $form = new DeleteAccountForm($user);
63
      $form->password = $password;
64
65
      $this->assertTrue($form->deleteAccount(), 'should be successful');
66
67
      $this->tester->seeEmailIsSent();
0 ignored issues
show
Bug Best Practice introduced by
The property tester does not exist on site\tests\unit\models\DeleteAccountFormTest. Did you maybe forget to declare it?
Loading history...
68
      $emailMessage = $this->tester->grabLastSentEmail();
69
      expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
70
      // an email was sent to both the user
71
      // and to partner1. We grab the most recently sent one to examine though, thus partner1
72
      expect($emailMessage->getTo())->hasKey('[email protected]');
73
      expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']);
74
      expect($emailMessage->getSubject())->equals('[email protected] has deleted their The Faster Scale App account');
75
      expect($emailMessage->toString())->contains($user->email);
76
    });
77
  }
78
79
  private function getUser() {
80
    $user = $this->getmockbuilder('\common\models\user')
81
      ->disableoriginalconstructor()
82
      ->setmethods(['getisnewrecord', 'attributes', 'generatechangeemailtoken', 'findbyemail', 'save', 'delete', 'ispartnerenabled'])
83
      ->getmock();
84
    $user->method('attributes')->willreturn([
85
      'email',
86
      'password',
87
      'password_hash',
88
      'partner_email1',
89
      'partner_email2',
90
      'partner_email3',
91
    ]);
92
    return $user;
93
  }
94
}
95