Completed
Pull Request — master (#155)
by Corey
02:53
created

UserBehaviorTest::testDecorate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 16
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 testCalculateScore() {
68
    $this->specify('calculateScore should function correctly', function () {
69
      expect('calculateScore should return the empty set when null is passed', $this->assertEmpty($this->user_behavior->calculateScore(null)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEmpty($this...->calculateScore(null)) targeting PHPUnit\Framework\Assert::assertEmpty() 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...
70
71
      expect('calculateScore should return the empty set with no selected behaviors', $this->assertEmpty($this->user_behavior->calculateScore([])));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEmpty($this...alculateScore(array())) targeting PHPUnit\Framework\Assert::assertEmpty() 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...
72
      
73
      expect('calculateScore should work with a single date item and simple behaviors', $this->assertEquals(['2016-06-16T21:12:43-07:00' => 1], $this->user_behavior->calculateScore($this->singleBhvr)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...ore($this->singleBhvr)) 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...
74
      
75
      expect('calculateScore should work with a single date item and complex behaviors', $this->assertEquals(['2016-06-20T21:08:36-07:00' => 43], $this->user_behavior->calculateScore($this->manyBhvrs)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...core($this->manyBhvrs)) 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
77
      expect('calculateScore should max out at 100', $this->assertEquals(['2016-08-03T22:57:53-07:00' => 100], $this->user_behavior->calculateScore($this->allBhvrs)));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...Score($this->allBhvrs)) 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...
78
79
      
80
      expect('calculateScore should work with multiple dates', $this->assertEquals([
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...($this->multipleDates)) 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...
81
          '2016-08-03T22:57:53-07:00' => 100,
82
          '2016-07-15T21:16:58-07:00' => 0,
83
          '2016-07-16T20:24:35-07:00' => 0,
84
          '2016-07-17T15:15:26-07:00' => 0,
85
          '2016-07-29T19:49:41-07:00' => 97
86
        ], $this->user_behavior->calculateScore($this->multipleDates)));
87
    });
88
  }
89
90
  public function testDecorate() {
91
    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...
92
                    [['id' => 396,
93
                      'user_id' => 2,
94
                      'behavior_id' => 107,
95
                      'date' => '2016-06-17 04:12:43',
96
                      'behavior' => [
97
                        'id' => 107,
98
                        'name' => 'numb',
99
                        'category_id' => 6,
100
                      ]]]));
101
102
    expect('decorate SHOULD NOT add Behavior data when the provided behavior_id is invalid',
103
      $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...
104
        $this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior),
105
        [['id' => 396,
106
          'user_id' => 2,
107
          'behavior_id' => 99999,
108
          'date' => '2016-06-17 04:12:43']]));
109
  }
110
111
  public function testDecorateWithCategory() {
112
    expect('decorate should add Behavior data and Category data to an array of UserBehaviors',
113
      $this->assertEquals(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...ed', 'weight' => 8))))) 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...
114
        $this->user_behavior->decorateWithCategory($this->singleSimpleBehaviorNoBehavior),
115
         [['id' => 396,
116
           'user_id' => 2,
117
           'behavior_id' => 107,
118
           'date' => '2016-06-17 04:12:43',
119
           'behavior' => [
120
             'id' => 107,
121
             'name' => 'numb',
122
             'category_id' => 6,
123
             'category' => [
124
               'id' => 6,
125
               'name' => 'Exhausted',
126
               'weight' => 8
127
             ]
128
           ]]]));
129
  }
130
131
  public function testGetDailyScore() {
132
    // getBehaviorsByDate() is called internally by getDailyScore()
133
    $this
134
      ->user_behavior
135
      ->method('getBehaviorsByDate')
136
      ->willReturnOnConsecutiveCalls([], $this->manyBhvrs);
137
138
    expect('getDailyScore should return the score for the day given',
139
      $this->assertEquals($this->user_behavior->getDailyScore(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...or->getDailyScore(), 0) 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...
140
      0));
141
    
142
    expect('getDailyScore should return the score for the day given',
143
      $this->assertEquals($this->user_behavior->getDailyScore('2017-08-01'),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...core('2017-08-01'), 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...
144
      43));
145
  }
146
147
  public function testGetTopBehaviors() {
148
    $this
149
      ->user_behavior
150
      ->method('getBehaviorsWithCounts')
151
      ->willReturnOnConsecutiveCalls([], [
152
        [
153
          'user_id' => 1,
154
          'behavior_id' => 1,
155
          'count' => 5
156
        ], [
157
          'user_id' => 1,
158
          'behavior_id' => 2,
159
          'count' => 4
160
        ], [
161
          'user_id' => 1,
162
          'behavior_id' => 3,
163
          'count' => 3
164
        ]
165
      ]);
166
167
    expect('getTopBehaviors to return the empty array if the user has not logged any behaviors', $this->assertEquals([], $this->user_behavior->getTopBehaviors()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...ior->getTopBehaviors()) 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...
168
    expect('getTopBehaviors to return embelished data corresponding to the most commonly selected behaviors', $this->assertEquals(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...ior->getTopBehaviors()) 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...
169
      [[
170
        'user_id' => 1,
171
        'behavior_id' => 1,
172
        'count' => 5,
173
        'behavior' => [
174
          'id' => 1,
175
          'name' => 'no current secrets',
176
          'category_id' => 1,
177
          'category' => [
178
            'id' => 1,
179
            'weight' => 0,
180
            'name' => 'Restoration',
181
          ],
182
        ],
183
      ], [
184
        'user_id' => 1,
185
        'behavior_id' => 2,
186
        'count' => 4,
187
        'behavior' => [
188
          'id' => 2,
189
          'name' => 'resolving problems',
190
          'category_id' => 1,
191
          'category' => [
192
            'id' => 1,
193
            'weight' => 0,
194
            'name' => 'Restoration',
195
          ],
196
        ],
197
      ], [
198
        'user_id' => 1,
199
        'behavior_id' => 3,
200
        'count' => 3,
201
        'behavior' => [
202
          'id' => 3,
203
          'name' => 'identifying fears and feelings',
204
          'category_id' => 1,
205
          'category' => [
206
            'id' => 1,
207
            'weight' => 0,
208
            'name' => 'Restoration',
209
          ],
210
        ],
211
      ]], $this->user_behavior->getTopBehaviors()));
212
  }
213
214
  public function testGetBehaviorsByCategory() {
215
    $this
216
      ->user_behavior
217
      ->method('getBehaviorsWithCounts')
218
      ->willReturnOnConsecutiveCalls([], [
219
        [
220
          'user_id' => 1,
221
          'behavior_id' => 1,
222
          'count' => 5
223
        ], [
224
          'user_id' => 1,
225
          'behavior_id' => 20,
226
          'count' => 4
227
        ], [
228
          'user_id' => 1,
229
          'behavior_id' => 50,
230
          'count' => 3
231
        ]
232
      ]);
233
234
    expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([], $this->user_behavior->getBehaviorsByCategory()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...tBehaviorsByCategory()) 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...
235
    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...tBehaviorsByCategory()) 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...
236
      [[
237
        'name' => 'Restoration',
238
        'count' => 5,
239
      ], [
240
        'name' => 'Forgetting Priorities',
241
        'count' => 4,
242
      ], [
243
        'name' => 'Speeding Up',
244
        'count' => 3,
245
      ]], $this->user_behavior->getBehaviorsByCategory()));
246
  }
247
}
248