Completed
Pull Request — master (#163)
by Corey
02:33
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 testDecorate() {
68
    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...
69
                    [['id' => 396,
70
                      'user_id' => 2,
71
                      'behavior_id' => 107,
72
                      'date' => '2016-06-17 04:12:43',
73
                      'behavior' => [
74
                        'id' => 107,
75
                        'name' => 'numb',
76
                        'category_id' => 6,
77
                      ]]]));
78
79
    expect('decorate SHOULD NOT add Behavior data when the provided behavior_id is invalid',
80
      $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...
81
        $this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior),
82
        [['id' => 396,
83
          'user_id' => 2,
84
          'behavior_id' => 99999,
85
          'date' => '2016-06-17 04:12:43']]));
86
  }
87
88
  public function testDecorateWithCategory() {
89
    expect('decorate should add Behavior data and Category data to an array of UserBehaviors',
90
      $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...
91
        $this->user_behavior->decorateWithCategory($this->singleSimpleBehaviorNoBehavior),
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
             'category' => [
101
               'id' => 6,
102
               'name' => 'Exhausted',
103
             ]
104
           ]]]));
105
  }
106
107
  public function testGetTopBehaviors() {
108
    $this
109
      ->user_behavior
110
      ->method('getBehaviorsWithCounts')
111
      ->willReturnOnConsecutiveCalls([], [
112
        [
113
          'user_id' => 1,
114
          'behavior_id' => 1,
115
          'count' => 5
116
        ], [
117
          'user_id' => 1,
118
          'behavior_id' => 2,
119
          'count' => 4
120
        ], [
121
          'user_id' => 1,
122
          'behavior_id' => 3,
123
          'count' => 3
124
        ]
125
      ]);
126
127
    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...
128
    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...
129
      [[
130
        'user_id' => 1,
131
        'behavior_id' => 1,
132
        'count' => 5,
133
        'behavior' => [
134
          'id' => 1,
135
          'name' => 'no current secrets',
136
          'category_id' => 1,
137
          'category' => [
138
            'id' => 1,
139
            'name' => 'Restoration',
140
          ],
141
        ],
142
      ], [
143
        'user_id' => 1,
144
        'behavior_id' => 2,
145
        'count' => 4,
146
        'behavior' => [
147
          'id' => 2,
148
          'name' => 'resolving problems',
149
          'category_id' => 1,
150
          'category' => [
151
            'id' => 1,
152
            'name' => 'Restoration',
153
          ],
154
        ],
155
      ], [
156
        'user_id' => 1,
157
        'behavior_id' => 3,
158
        'count' => 3,
159
        'behavior' => [
160
          'id' => 3,
161
          'name' => 'identifying fears and feelings',
162
          'category_id' => 1,
163
          'category' => [
164
            'id' => 1,
165
            'name' => 'Restoration',
166
          ],
167
        ],
168
      ]], $this->user_behavior->getTopBehaviors()));
169
  }
170
171
  public function testGetBehaviorsByCategory() {
172
    $this
173
      ->user_behavior
174
      ->method('getBehaviorsWithCounts')
175
      ->willReturnOnConsecutiveCalls([], [
176
        [
177
          'user_id' => 1,
178
          'behavior_id' => 1,
179
          'count' => 5
180
        ], [
181
          'user_id' => 1,
182
          'behavior_id' => 20,
183
          'count' => 4
184
        ], [
185
          'user_id' => 1,
186
          'behavior_id' => 50,
187
          'count' => 3
188
        ]
189
      ]);
190
191
    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...
192
    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...
193
      1 => [
194
        'name' => 'Restoration',
195
        'count' => 5,
196
        'color' => '#008000',
197
        'highlight' => '#199919'
198
      ],
199
      2 => [
200
        'name' => 'Forgetting Priorities',
201
        'count' => 4,
202
        'color' => '#4CA100',
203
        'highlight' => '#61B219'
204
      ],
205
      4 => [
206
        'name' => 'Speeding Up',
207
        'count' => 3,
208
        'color' => '#E5E500',
209
        'highlight' => '#E5E533'
210
      ]
211
    ], $this->user_behavior->getBehaviorsByCategory()));
212
  }
213
}
214