Completed
Push — master ( da4528...803e91 )
by Corey
03:17 queued 31s
created

SignupFormTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 54
rs 9.232
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
7
class SignupFormTest extends \Codeception\Test\Unit
8
{
9
  use \Codeception\Specify;
10
11
  private $vals = [
12
    'password'        => 'password',
13
    'email'           => '[email protected]',
14
    'timezone'        => "America/Los_Angeles",
15
    'send_email'      => true,
16
    'partner_email1'  => '[email protected]',
17
    'partner_email2'  => '[email protected]',
18
    'partner_email3'  => '[email protected]',
19
  ];
20
21
  private $whitelist = [
22
    'email',
23
    'timezone',
24
    'send_email',
25
    'partner_email1',
26
    'partner_email2',
27
    'partner_email3'
28
  ];
29
30
  public function setUp() {
31
    // define interfaces for injection
32
    $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...
33
    $this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser');
34
    $this->container->set('common\interfaces\UserBehaviorInterface', '\site\tests\_support\MockUserBehavior');
35
    $this->container->set('common\interfaces\TimeInterface', function () {
36
      return new \common\components\Time('America/Los_Angeles');
37
    });
38
39
    // instantiate mock objects
40
    $user_behavior = $this->container->get('common\interfaces\UserBehaviorInterface');
41
    $time        = $this->container->get('common\interfaces\TimeInterface');
42
43
    $question = $this->getMockBuilder('\common\models\Question')
44
      ->setMethods(['save', 'attributes'])
45
      ->getMock();
46
47
    $user_methods = ['save', 'attributes', 'findByEmail', 'sendSignupNotificationEmail', 'sendVerifyEmail'];
48
    $this->user = $this->getMockBuilder('\common\models\User')
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
49
      ->setConstructorArgs([$user_behavior, $question, $time])
50
      ->setMethods($user_methods)
51
      ->getMock();
52
    $user_attrs = [
53
      'id',
54
      'password_hash',
55
      'password_reset_token',
56
      'verify_email_token',
57
      'email',
58
      'auth_key',
59
      'role',
60
      'status',
61
      'created_at',
62
      'updated_at',
63
      'password',
64
      'timezone',
65
      'send_email',
66
      'email_category',
67
      'partner_email1',
68
      'partner_email2',
69
      'partner_email3',
70
    ];
71
    $this->user->method('attributes')->willReturn($user_attrs);
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

71
    $this->user->/** @scrutinizer ignore-call */ 
72
                 method('attributes')->willReturn($user_attrs);

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...
72
73
    $this->existing_user = $this->getMockBuilder('\common\models\User')
0 ignored issues
show
Bug Best Practice introduced by
The property existing_user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
      ->setConstructorArgs([$user_behavior, $question, $time])
75
      ->setMethods($user_methods)
76
      ->getMock();
77
    $this->existing_user->method('attributes')->willReturn($user_attrs);
78
79
    $this->existing_user->id          = 1;
0 ignored issues
show
Bug introduced by
Accessing id on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
80
    $this->existing_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...
81
    $this->existing_user->setIsNewRecord(false);
0 ignored issues
show
Bug introduced by
The method setIsNewRecord() 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

81
    $this->existing_user->/** @scrutinizer ignore-call */ 
82
                          setIsNewRecord(false);

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...
82
83
    parent::setUp();
84
  }
85
86
  protected function tearDown() {
87
    $this->user = null;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
88
    parent::tearDown();
89
  }
90
91
  public function testRules() {
92
    $form = $this->container->get('\site\models\SignupForm', [$this->user]);
93
94
    $form->attributes = $this->vals; // massive assignment
95
    expect('the form should pass validation with good values', $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...
96
97
    $form->attributes     = $this->vals; // massive assignment
98
    $form->partner_email1 = null;
99
    expect('the form should fail validation when send_email is true but no partner_emails are 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...
100
101
    $form->attributes = $this->vals; // massive assignment
102
    $form->timezone = 'FakePlace/FakeCity';
103
    expect('the form should fail validation when a bad timezone is 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...
104
  }
105
106
  public function testSignupNewUser() {
107
108
    // set up a side effect to occur when the user's save() function is called
109
    $saved = false;
110
    $this->user->method('save')->will($this->returnCallback(function($runValidation=true, $attributeNames=null) use(&$saved) {
0 ignored issues
show
Unused Code introduced by
The parameter $attributeNames is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    $this->user->method('save')->will($this->returnCallback(function($runValidation=true, /** @scrutinizer ignore-unused */ $attributeNames=null) use(&$saved) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $runValidation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    $this->user->method('save')->will($this->returnCallback(function(/** @scrutinizer ignore-unused */ $runValidation=true, $attributeNames=null) use(&$saved) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
      $saved = true;
112
      return $this->user;
113
    }));
114
115
    $form = $this->getForm();
116
117
    /////////////////////////////////////////////////////////////////////
118
119
    $this->specify('a brand-new user should be able to sign up', function() use ($form, &$saved) {
120
      expect('the form should validate correctly', $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...
121
      $user = $form->signup();
122
      expect('signup() should return a newly-saved user when the user does not already exist with the submitted values',
123
             $this->assertEquals($user->getAttributes($this->whitelist), array_splice($this->vals, 1)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($use...splice($this->vals, 1)) 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...
124
      expect('the "Remember Me" auth_key value should be set to a random string (defaults to a 32 char length)',
125
             $this->assertEquals(32, strlen($user->auth_key)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(32, strlen($user->auth_key)) 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...
126
      expect('password_hash to be set to something like a hash',
127
             $this->assertStringStartsWith('$2y$13$', $user->password_hash));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertStringStart..., $user->password_hash) targeting PHPUnit\Framework\Assert::assertStringStartsWith() 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...
128
      expect('when the user is created, signup() should call save()',
129
             $this->assertTrue($saved));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($saved) 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...
130
      expect('This user should be an isNewRecord -- they should NOT already exist', $this->assertTrue($user->getIsNewRecord()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($user->getIsNewRecord()) 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...
131
    });
132
  }
133
 
134
  public function testSignupExistingConfirmedUser() {
135
    $saved = false;
136
    $this->existing_user->method('save')->will($this->returnCallback(function($runValidation=true, $attributeNames=null) use(&$saved) {
0 ignored issues
show
Unused Code introduced by
The parameter $attributeNames is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

136
    $this->existing_user->method('save')->will($this->returnCallback(function($runValidation=true, /** @scrutinizer ignore-unused */ $attributeNames=null) use(&$saved) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $runValidation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

136
    $this->existing_user->method('save')->will($this->returnCallback(function(/** @scrutinizer ignore-unused */ $runValidation=true, $attributeNames=null) use(&$saved) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
      $saved = true;
138
      return $this->existing_user;
139
    }));
140
141
    $this->user->method('findByEmail')->willReturn($this->existing_user);
142
    $this->existing_user->verify_email_token = $this->getValidToken() . "_confirmed";
143
    $form2 = $this->getForm();
144
145
    $this->specify('an existing and confirmed user should not be signed up', function() use ($form2, &$saved) {
146
      expect('the form should validate correctly', $this->assertTrue($form2->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form2->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...
147
      $user = $form2->signup();
148
      expect('when the user exists and is confirmed, signup() should return null', $this->assertNull($user));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertNull($user) targeting PHPUnit\Framework\Assert::assertNull() 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...
149
      expect('when the user exists and is confirmed, signup() should NOT call save()', $this->assertFalse($saved));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($saved) 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...
150
    });
151
  }
152
153
  public function testSignupExistingUnconfirmedUser() {
154
    $saved = false;
155
    $this->existing_user->method('save')->will($this->returnCallback(function($runValidation=true, $attributeNames=null) use(&$saved) {
0 ignored issues
show
Unused Code introduced by
The parameter $runValidation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
    $this->existing_user->method('save')->will($this->returnCallback(function(/** @scrutinizer ignore-unused */ $runValidation=true, $attributeNames=null) use(&$saved) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attributeNames is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
    $this->existing_user->method('save')->will($this->returnCallback(function($runValidation=true, /** @scrutinizer ignore-unused */ $attributeNames=null) use(&$saved) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
      $saved = true;
157
      return $this->existing_user;
158
    }));
159
 
160
    $this->existing_user->verify_email_token = $this->getExpiredToken();
161
    // make sure our fake token actually _is_ expired
162
    expect('token is not confirmed', $this->assertFalse($this->existing_user->isTokenConfirmed()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($this...er->isTokenConfirmed()) 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...
163
    expect('token is expired', $this->assertFalse($this->existing_user->isTokenCurrent($this->existing_user->verify_email_token, 'user.verifyAccountTokenExpire')));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($this...fyAccountTokenExpire')) 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...
164
165
    $this->user->method('findByEmail')->willReturn($this->existing_user);
166
    $form3 = $this->getForm();
167
168
    $this->specify('an existing and unconfirmed user should be able to sign up again, but their information will be UPDATED', function() use ($form3, &$saved) {
169
      expect('the form should validate correctly', $this->assertTrue($form3->validate()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form3->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...
170
      $user = $form3->signup();
171
      expect('This user should not be an isNewRecord -- they should already exist', $this->assertFalse($user->getIsNewRecord()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertFalse($user->getIsNewRecord()) 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...
172
      expect('signup() should return a newly-saved user when the user does not already exist with the submitted values',
173
             $this->assertEquals($user->getAttributes($this->whitelist), array_splice($this->vals, 1)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($use...splice($this->vals, 1)) 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...
174
      expect('when the user exists and is NOT confirmed, signup() should call save() to UPDATE their info', $this->assertTrue($saved));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($saved) 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...
175
    });
176
  }
177
178
  public function testSetFields() {
179
    $container = new yii\di\Container;
180
    $form = $container->get('\site\models\SignupForm', [$this->user]);
181
182
    $form = $this->setAttrs($form, $this->vals);
183
184
    $result = $form->setFields($this->user);
185
    $this->specify('populate should set the values of the user model from the form', function() use ($result) {
186
      foreach($this->whitelist as $attr) {
187
        $this->assertEquals($this->vals[$attr], $result->$attr);
188
      }
189
    });
190
  }
191
192
  private function setAttrs($form, Array $vals) {
193
    foreach($vals as $key => $val) {
194
      $form->$key = $val;
195
    }
196
    return $form;
197
  }
198
199
  private function getExpiredToken() {
200
    // subtract the expiration time and a little more from the current time
201
    $expire = \Yii::$app->params['user.verifyAccountTokenExpire'];
202
    return \Yii::$app
203
      ->getSecurity()
204
      ->generateRandomString() . '_' . (time() - $expire - 1);
205
  }
206
207
  private function getValidToken() {
208
    return \Yii::$app
209
      ->getSecurity()
210
      ->generateRandomString() . '_' . time();
211
  }
212
213
  private function getForm($user = null) {
214
    if(is_null($user)) $user = $this->user;
215
    $form = $this->container->get('\site\models\SignupForm', [$user]);
216
    $form->attributes = $this->vals; // massive assignment
217
    return $form;
218
  }
219
}
220