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

ChangeEmailFormTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 11 1
B testChangeEmail() 0 41 1
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 testChangeEmail() {
12
13
    $this->specify('changeEmail() should return true and do nothing when the email is already taken', function() {
14
      $user = $this->getUser();
15
      $user
16
        ->expects($this->never())
17
        ->method('save');
18
      $desired_email = '[email protected]';
19
      $user
20
        ->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

20
        ->/** @scrutinizer ignore-call */ 
21
          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...
21
        ->willReturn(true); // true, as a stand-in for some valid user object
22
23
      $form = new ChangeEmailForm($user);
24
      $form->desired_email = $desired_email;
25
      $this->assertTrue($form->changeEmail(), 'should always return true');
26
      $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...
27
      $this->assertNotEquals($user->desired_email, $desired_email);
28
    });
29
30
    $this->specify('changeEmail() should return true, set values, and send an email when the email is available', function() {
31
      $user = $this->getUser();
32
      $desired_email = '[email protected]';
33
      $user
34
        ->method('findByEmail')
35
        ->willReturn(null);
36
      $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...
37
38
      $form = new ChangeEmailForm($user);
39
      $form->desired_email = $desired_email;
40
41
      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...
42
43
      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
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...
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...
44
45
      $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...
46
      $emailMessage = $this->tester->grabLastSentEmail();
47
      expect('valid email is sent', $emailMessage)->isInstanceOf('yii\mail\MessageInterface');
48
      expect($emailMessage->getTo())->hasKey($desired_email);
49
      expect($emailMessage->getFrom())->hasKey(Yii::$app->params['supportEmail']);
50
      expect($emailMessage->getSubject())->equals(Yii::$app->name . ' -- your requested email change');
51
      expect($emailMessage->toString())->contains($user->email);
52
    });
53
  }
54
55
  private function getUser() {
56
    $user = $this->getmockbuilder('\common\models\user')
57
      ->disableoriginalconstructor()
58
      ->setmethods(['getisnewrecord', 'attributes', 'generatechangeemailtoken', 'findbyemail', 'save'])
59
      ->getmock();
60
    $user->method('attributes')->willreturn([
61
      'email',
62
      'desired_email',
63
      'change_email_token',
64
    ]);
65
    return $user;
66
  }
67
}
68