Completed
Pull Request — master (#163)
by Corey
05:18
created

FakeUserBehavior::getCheckInBreakdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace site\tests\unit\models;
4
5
use Yii;
6
use \site\models\EditProfileForm;
7
8
class EditProfileFormTest extends \Codeception\Test\Unit {
9
	public $graph;
10
	public $values = [
11
			'timezone' => 'America/Los_Angeles',
12
      'send_email' => true,
13
			'partner_email1' => '[email protected]',
14
			'partner_email2' => '[email protected]',
15
			'partner_email3' => '[email protected]',
16
			'expose_graph' => true,
17
		];
18
19
	public function setUp() {
20
		$this->graph = $this->getMockBuilder(common\components\Graph::class)
0 ignored issues
show
Bug introduced by
The type site\tests\unit\models\common\components\Graph was not found. Did you mean common\components\Graph? If so, make sure to prefix the type with \.
Loading history...
21
			->setMethods(['create', 'destroy'])
22
			->getMock();
23
		parent::setUp();
24
	}
25
26
  public function testLoadUser() {
27
    $user = $this->getUser();
28
    $form = new \site\models\EditProfileForm($user);
29
    $form->loadUser();
30
    expect('timezone should be equal', $this->assertEquals($form->timezone, $user->timezone));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($for...ezone, $user->timezone) 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 timezone on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
31
    expect('partner_email1 should be equal', $this->assertEquals($form->partner_email1, $user->partner_email1));
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...
Bug introduced by
Are you sure the usage of $this->assertEquals($for... $user->partner_email1) 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...
32
    expect('partner_email2 should be equal', $this->assertEquals($form->partner_email2, $user->partner_email2));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($for... $user->partner_email2) 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 partner_email2 on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
33
    expect('partner_email3 should be equal', $this->assertEquals($form->partner_email3, $user->partner_email3));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($for... $user->partner_email3) 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 partner_email3 on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
34
    expect('send_email should be true', $this->assertTrue($form->send_email));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertTrue($form->send_email) 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...
35
  }
36
37
  public function testSaveProfile() {
38
    $user = $this->getUser();
39
    $form = new \site\models\EditProfileForm($user);
40
    $form->loadUser();
41
42
    // Set up dependency injections
43
    Yii::$container
44
      ->set(\common\interfaces\TimeInterface::class, function () { return new \common\components\Time('UTC'); });
45
    Yii::$container
46
      ->set(\common\components\Graph::class, function () { return $this->graph; });
47
    Yii::$container
48
      ->set(\common\interfaces\UserBehaviorInterface::class, function () { return new FakeUserBehavior(); });
49
50
    // Actually begin testing
51
    $form->attributes = $this->values;
52
    expect('saveProfile should return the user', $this->assertEquals($user, $form->saveProfile()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($user, $form->saveProfile()) 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...
53
    return true;
54
		expect('saveProfile should set the user\'s attributes to be the form values', $this->assertEquals($this->values, $user->attributes));
0 ignored issues
show
Unused Code introduced by
expect('saveProfile shou...es, $user->attributes)) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
55
56
    $form->send_email = 'not_a_boolean';
57
    expect('saveProfile should return the user with the partner-related settings equal to null', $this->assertNull($form->saveProfile()));
58
59
		$null_vals = [
60
			'partner_email1'  => null,
61
			'partner_email2'  => null,
62
			'partner_email3'  => null,
63
      'send_email'      => null,
64
			'timezone'        => 'America/Los_Angeles',
65
		];
66
    $form->send_email = false;
67
		$ret = $form->saveProfile();
68
    expect('saveProfile should return the user with the partner-related settings equal to null', $this->assertEquals($null_vals, $user->attributes));
69
70
    $form->send_email = 'not_a_boolean';
71
    expect('saveProfile should return null if the form validation fails', $this->assertNull($form->saveProfile()));
72
  }
73
74
  private function getUser() {
75
    $user = $this->getmockbuilder('\common\models\User')
76
      ->disableoriginalconstructor()
77
      ->setmethods(['getisnewrecord', 'attributes', 'save', 'findbypasswordresettoken', 'removepasswordresettoken', 'getid'])
78
      ->getmock();
79
    $user->method('attributes')->willReturn([
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

79
    $user->/** @scrutinizer ignore-call */ 
80
           method('attributes')->willReturn([

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...
80
      'timezone',
81
      'partner_email1',
82
      'partner_email2',
83
      'partner_email3',
84
      'send_email',
85
      'expose_graph',
86
    ]);
87
    $user->timezone = 'America/Los_Angeles';
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
88
    $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...
89
    $user->partner_email2 = '[email protected]';
0 ignored issues
show
Bug introduced by
Accessing partner_email2 on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
90
    $user->partner_email3 = '[email protected]';
0 ignored issues
show
Bug introduced by
Accessing partner_email3 on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
91
    $user->send_email = true;
0 ignored issues
show
Bug introduced by
Accessing send_email on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
92
    $user->expose_graph = true;
0 ignored issues
show
Bug introduced by
Accessing expose_graph on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
93
    return $user;
94
  }
95
}
96
97
class FakeUserBehavior {
98
  public $scores = true;
99
  public function getCheckInBreakdown() { return $this->scores; }
100
}
101