Completed
Pull Request — master (#164)
by Corey
03:18
created

ChangeEmailFormTest::testChangeEmail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 32
nc 1
nop 0
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
use \site\models\ChangeEmailForm;
7
8
class ChangeEmailFormTest extends \Codeception\Test\Unit {
9
  use \Codeception\Specify;
10
11
  public function testSimpleRulesShouldValidate() {
12
    $user = $this->getUser();
13
    $form = new ChangeEmailForm($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 = ['desired_email' => 'not_an_email'];
18
    expect('with a value that is not an email, 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...
19
    $form->attributes = ['desired_email' => '[email protected]'];
20
    expect('with a valid email, the form 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...
21
  }
22
23
  public function testChangeEmail() {
24
25
    $this->specify('changeEmail() should return true and do nothing when the email is already taken', function() {
26
      $user = $this->getUser();
27
      $user
28
        ->expects($this->never())
29
        ->method('save');
30
      $desired_email = '[email protected]';
31
      $user
32
        ->method('findByEmail')
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

32
        ->/** @scrutinizer ignore-call */ 
33
          method('findByEmail')

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...
33
        ->willReturn(true); // true, as a stand-in for some valid user object
34
35
      $form = new ChangeEmailForm($user);
36
      $form->desired_email = $desired_email;
37
      $this->assertTrue($form->changeEmail(), 'should always return true');
38
      $this->assertNull($user->desired_email, 'should be null because we didn\'t set the new email');
0 ignored issues
show
Bug introduced by
Accessing desired_email on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
39
      $this->assertNotEquals($user->desired_email, $desired_email);
40
    });
41
42
    $this->specify('changeEmail() should return true, set values, and send an email when the email is available', function() {
43
      $user = $this->getUser();
44
      $desired_email = '[email protected]';
45
      $user
46
        ->method('findByEmail')
47
        ->willReturn(null);
48
      $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...
49
50
      $form = new ChangeEmailForm($user);
51
      $form->desired_email = $desired_email;
52
53
      expect('changeEmail should return true', $this->assertTrue($form->changeEmail()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form->changeEmail()) 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...
54
55
      expect('the user\'s desired email should now match what they entered in the form', $this->assertEquals($user->desired_email, $desired_email));
0 ignored issues
show
Bug introduced by
Accessing desired_email on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Are you sure the usage of $this->assertEquals($use..._email, $desired_email) 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...
56
57
      $this->tester->seeEmailIsSent();
0 ignored issues
show
Bug Best Practice introduced by
The property tester does not exist on site\tests\unit\models\ChangeEmailFormTest. Did you maybe forget to declare it?
Loading history...
58
      $emailMessage = $this->tester->grabLastSentEmail();
59
      expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
60
      expect($emailMessage->getTo())->hasKey($desired_email);
61
      expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']);
62
      expect($emailMessage->getSubject())->equals(Yii::$app->name . ' -- your requested email change');
63
      expect($emailMessage->toString())->contains($user->email);
64
    });
65
  }
66
67
  private function getUser() {
68
    $user = $this->getmockbuilder('\common\models\user')
69
      ->disableoriginalconstructor()
70
      ->setmethods(['getisnewrecord', 'attributes', 'generatechangeemailtoken', 'findbyemail', 'save'])
71
      ->getmock();
72
    $user->method('attributes')->willreturn([
73
      'email',
74
      'desired_email',
75
      'change_email_token',
76
    ]);
77
    return $user;
78
  }
79
}
80