Completed
Pull Request — master (#163)
by Corey
03:20
created

UserBehaviorTest::testGetDailyScore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
c 0
b 0
f 0
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace common\unit\models;
4
5
use Yii;
6
use Codeception\Specify;
7
8
date_default_timezone_set('UTC');
9
10
/**
11
 * Time test
12
 */
13
14
class UserBehaviorTest extends \Codeception\Test\Unit {
15
  use Specify;
16
17
  public $singleSimpleBehaviorNoBehavior = [
18
    [
19
      'id' => 396,
20
      'user_id' => 2,
21
      'behavior_id' => 107,
22
      'date' => '2016-06-17 04:12:43',
23
    ],
24
  ]; 
25
26
  public $badSingleSimpleBehaviorNoBehavior = [
27
    [
28
      'id' => 396,
29
      'user_id' => 2,
30
      'behavior_id' => 99999,
31
      'date' => '2016-06-17 04:12:43',
32
    ],
33
  ]; 
34
  public function setUp() {
35
    // pull in test data
36
    $data = require(__DIR__.'/../data/checkinData.php');
37
    $this->singleBhvr = $data['singleBhvr'];
0 ignored issues
show
Bug Best Practice introduced by
The property singleBhvr does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
    $this->manyBhvrs = $data['manyBhvrs'];
0 ignored issues
show
Bug Best Practice introduced by
The property manyBhvrs does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
    $this->allBhvrs = $data['allBhvrs'];
0 ignored issues
show
Bug Best Practice introduced by
The property allBhvrs does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
    $this->multipleDates = $data['multipleDates'];
0 ignored issues
show
Bug Best Practice introduced by
The property multipleDates does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
42
    $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...
43
    $this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser');
44
    $this->container->set('common\interfaces\QuestionInterface', '\site\tests\_support\MockQuestion');
45
    $this->container->set('common\interfaces\BehaviorInterface', 'common\models\Behavior');
46
    $this->container->set('common\interfaces\TimeInterface', function () {
47
        return new \common\components\Time('America/Los_Angeles');
48
      });
49
50
    $time = $this->container->get('common\interfaces\TimeInterface');
51
    $behavior = $this->container->get('common\interfaces\BehaviorInterface');
52
    $user = $this->container->get('common\interfaces\UserInterface');
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
53
54
    $this->user_behavior = $this
0 ignored issues
show
Bug Best Practice introduced by
The property user_behavior does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
                            ->getMockBuilder("common\models\UserBehavior")
56
                            ->setConstructorArgs([$behavior, $time])
57
                            ->setMethods(['getIsNewRecord', 'save', 'getBehaviorsByDate', 'getBehaviorsWithCounts'])
58
                            ->getMock();
59
    parent::setUp();
60
  }
61
62
  protected function tearDown() {
63
    $this->user_behavior = null;
0 ignored issues
show
Bug Best Practice introduced by
The property user_behavior does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
    parent::tearDown();
65
  }
66
67
  public function testRules() {
68
    expect('rules', $this->assertEquals($this->user_behavior->rules(), [
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...vior_id'), 'integer'))) 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...
69
      [['user_id', 'behavior_id', 'date'], 'required'],
70
      [['user_id', 'behavior_id'], 'integer'],
71
    ]));
72
  }
73
74
  public function testAttributeLabels() {
75
    expect('attributeLabels', $this->assertEquals($this->user_behavior->attributelabels(), [
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi..._id' => 'Behavior ID')) 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...
76
      'id'        => 'ID',
77
      'date'      => 'Date',
78
      'user_id'   => 'User ID',
79
      'behavior_id' => 'Behavior ID',
80
    ]));
81
  }
82
83
  public function testDecorate() {
84
    expect('decorate should add Behavior data to an array of UserBehaviors', $this->assertEquals($this->user_behavior->decorate($this->singleSimpleBehaviorNoBehavior),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi... 'category_id' => 6)))) 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...
85
                    [['id' => 396,
86
                      'user_id' => 2,
87
                      'behavior_id' => 107,
88
                      'date' => '2016-06-17 04:12:43',
89
                      'behavior' => [
90
                        'id' => 107,
91
                        'name' => 'numb',
92
                        'category_id' => 6,
93
                      ]]]));
94
95
    expect('decorate SHOULD NOT add Behavior data when the provided behavior_id is invalid',
96
      $this->assertEquals(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...2016-06-17 04:12:43'))) 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...
97
        $this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior),
98
        [['id' => 396,
99
          'user_id' => 2,
100
          'behavior_id' => 99999,
101
          'date' => '2016-06-17 04:12:43']]));
102
  }
103
104
  public function testDecorateWithCategory() {
105
    expect('decorate should add Behavior data and Category data to an array of UserBehaviors',
106
      $this->assertEquals(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...me' => 'Exhausted'))))) 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...
107
        $this->user_behavior->decorateWithCategory($this->singleSimpleBehaviorNoBehavior),
108
         [['id' => 396,
109
           'user_id' => 2,
110
           'behavior_id' => 107,
111
           'date' => '2016-06-17 04:12:43',
112
           'behavior' => [
113
             'id' => 107,
114
             'name' => 'numb',
115
             'category_id' => 6,
116
             'category' => [
117
               'id' => 6,
118
               'name' => 'Exhausted',
119
             ]
120
           ]]]));
121
  }
122
123
  public function testGetBehaviorsByCategory() {
124
    expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([], $this->user_behavior->getBehaviorsByCategory($this->user_behavior::decorateWithCategory([]))));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...WithCategory(array()))) 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...
125
    expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...> 50, 'count' => 3))))) 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
      1 => [
127
        'name' => 'Restoration',
128
        'count' => 5,
129
        'color' => '#008000',
130
        'highlight' => '#199919'
131
      ],
132
      2 => [
133
        'name' => 'Forgetting Priorities',
134
        'count' => 4,
135
        'color' => '#4CA100',
136
        'highlight' => '#61B219'
137
      ],
138
      4 => [
139
        'name' => 'Speeding Up',
140
        'count' => 3,
141
        'color' => '#E5E500',
142
        'highlight' => '#E5E533'
143
      ]
144
    ], $this->user_behavior->getBehaviorsByCategory($this->user_behavior::decorateWithCategory([
145
        [
146
          'user_id' => 1,
147
          'behavior_id' => 1,
148
          'count' => 5
149
        ], [
150
          'user_id' => 1,
151
          'behavior_id' => 20,
152
          'count' => 4
153
        ], [
154
          'user_id' => 1,
155
          'behavior_id' => 50,
156
          'count' => 3
157
        ]
158
      ]))));
159
  }
160
161
  public function testGetCheckinBreakdown() {
162
    Yii::configure(Yii::$app, [
163
      'components' => [
164
        'user' => [
165
          'class' => 'yii\web\User',
166
          'identityClass' => 'common\tests\unit\FakeUser',
167
        ],
168
      ],
169
    ]);
170
    $identity = new \common\tests\unit\FakeUser();
171
    $tz = 'America/Los_Angeles';
172
    $identity->timezone = $tz;
173
    // logs in the user -- we use Yii::$app->user->id in getCheckinBreakdown()
174
    Yii::$app->user->setIdentity($identity);
0 ignored issues
show
Bug introduced by
The method setIdentity() does not exist on null. ( Ignorable by Annotation )

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

174
    Yii::$app->user->/** @scrutinizer ignore-call */ 
175
                     setIdentity($identity);

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...
175
176
    /* get mocked Time */
177
    $tz = new \DateTimeZone($tz);
178
    $date_range = new \DatePeriod(new \DateTime('2019-02-03', $tz),
179
                                  new \DateInterval('P1D'),
180
                                  new \DateTime('2019-03-05', $tz));
181
    $time = $this
182
        ->getMockBuilder("common\components\Time")
183
        ->setConstructorArgs(['America/Los_Angeles'])
184
        ->setMethods(['getDateTimesInPeriod'])
185
        ->getMock();
186
    $time
187
      ->method('getDateTimesInPeriod')
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

187
      ->/** @scrutinizer ignore-call */ 
188
        method('getDateTimesInPeriod')

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...
188
      ->willReturn($date_range);
189
190
    $behavior = new \common\models\Behavior();
191
192
    $this->user_behavior = $this
0 ignored issues
show
Bug Best Practice introduced by
The property user_behavior does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
193
                            ->getMockBuilder("common\models\UserBehavior")
194
                            ->setConstructorArgs([$behavior, $time])
195
                            ->setMethods(['getIsNewRecord', 'save', 'getBehaviorsByDate', 'getBehaviorsWithCounts'])
196
                            ->getMock();
197
198
    $bhvrs = require(__DIR__.'/../data/behaviorsWithCounts.php');
199
    $expected = require(__DIR__.'/../data/expected_getCheckinBreakdown.php');
200
    $this->user_behavior->method('getBehaviorsWithCounts')->willReturn(...$bhvrs);
201
		expect('asdf', $this->assertEquals($expected, $this->user_behavior->getCheckinBreakdown()));
0 ignored issues
show
Bug introduced by
The method getCheckinBreakdown() 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

201
		expect('asdf', $this->assertEquals($expected, $this->user_behavior->/** @scrutinizer ignore-call */ getCheckinBreakdown()));

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...
Bug introduced by
Are you sure the usage of $this->assertEquals($exp...>getCheckinBreakdown()) 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...
202
  }
203
}
204