Passed
Push — master ( 41f848...de078e )
by Corey
05:23 queued 02:26
created

UserBehaviorTest::testParseBehaviorData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
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
      'category_id' => 6,
23
      'date' => '2016-06-17 04:12:43',
24
    ],
25
  ]; 
26
27
  public $badSingleSimpleBehaviorNoBehavior = [
28
    [
29
      'id' => 396,
30
      'user_id' => 2,
31
      'behavior_id' => 99999,
32
      'category_id' => 6,
33
      'date' => '2016-06-17 04:12:43',
34
    ],
35
  ]; 
36
37
  public $behaviorData = [
38
    [
39
      'id' => 820,
40
      'user_id' => 2,
41
      'behavior_id' => 7,
42
      'category_id' => 1,
43
      'date' => '2016-09-10 19:26:04',
44
      'behavior' => [
45
        'id' => 7,
46
        'name' => 'making eye contact',
47
        'category_id' => 1,
48
      ],
49
      'category' => [
50
        'id' => 1,
51
        'name' => 'Restoration',
52
      ],
53
    ], [
54
      'id' => 821,
55
      'user_id' => 2,
56
      'behavior_id' => 13,
57
      'category_id' => 2,
58
      'date' => '2016-09-10 19:26:04',
59
      'behavior' => [
60
        'id' => 13,
61
        'name' => 'less time/energy for God, meetings, and church',
62
        'category_id' => 2,
63
      ],
64
      'category' => [
65
        'id' => 2,
66
        'name' => 'Forgetting Priorities',
67
      ],
68
    ], [
69
      'id' => 822,
70
      'user_id' => 2,
71
      'behavior_id' => 18,
72
      'category_id' => 2,
73
      'date' => '2016-09-10 19:26:04',
74
      'behavior' => [
75
        'id' => 18,
76
        'name' => 'changes in goals',
77
        'category_id' => 2,
78
      ],
79
      'category' => [
80
        'id' => 2,
81
        'name' => 'Forgetting Priorities',
82
      ],
83
    ], [
84
      'id' => 823,
85
      'user_id' => 2,
86
      'behavior_id' => 29,
87
      'category_id' => 3,
88
      'date' => '2016-09-10 19:26:04',
89
      'behavior' => [
90
        'id' => 29,
91
        'name' => 'using profanity',
92
        'category_id' => 3,
93
      ],
94
      'category' => [
95
        'id' => 3,
96
        'name' => 'Anxiety',
97
      ],
98
    ], [
99
      'id' => 824,
100
      'user_id' => 2,
101
      'behavior_id' => 41,
102
      'category_id' => 3,
103
      'date' => '2016-09-10 19:26:04',
104
      'behavior' => [
105
        'id' => 41,
106
        'name' => 'co-dependent rescuing',
107
        'category_id' => 3,
108
      ],
109
      'category' => [
110
        'id' => 3,
111
        'name' => 'Anxiety',
112
      ],
113
    ], [
114
      'id' => 825,
115
      'user_id' => 2,
116
      'behavior_id' => 48,
117
      'category_id' => 4,
118
      'date' => '2016-09-10 19:26:04',
119
      'behavior' => [
120
        'id' => 48,
121
        'name' => 'workaholic',
122
        'category_id' => 4,
123
      ],
124
      'category' => [
125
        'id' => 4,
126
        'name' => 'Speeding Up',
127
      ],
128
    ], [
129
      'id' => 826,
130
      'user_id' => 2,
131
      'behavior_id' => 72,
132
      'category_id' => 5,
133
      'date' => '2016-09-10 19:26:04',
134
      'behavior' => [
135
        'id' => 72,
136
        'name' => 'black and white, all or nothing thinking',
137
        'category_id' => 5,
138
      ],
139
      'category' => [
140
        'id' => 5,
141
        'name' => 'Ticked Off',
142
      ],
143
    ], [
144
      'id' => 827,
145
      'user_id' => 2,
146
      'behavior_id' => 79,
147
      'category_id' => 5,
148
      'date' => '2016-09-10 19:26:04',
149
      'behavior' => [
150
        'id' => 79,
151
        'name' => 'blaming',
152
        'category_id' => 5,
153
      ],
154
      'category' => [
155
        'id' => 5,
156
        'name' => 'Ticked Off',
157
      ],
158
      'date' => '2016-09-10 19:26:04',
159
    ], [
160
      'id' => 828,
161
      'user_id' => 2,
162
      'behavior_id' => 89,
163
      'category_id' => 5,
164
      'date' => '2016-09-10 19:26:04',
165
      'behavior' => [
166
        'id' => 89,
167
        'name' => 'obsessive (stuck) thoughts',
168
        'category_id' => 5,
169
      ],
170
      'category' => [
171
        'id' => 5,
172
        'name' => 'Ticked Off',
173
      ],
174
    ], [
175
      'id' => 829,
176
      'user_id' => 2,
177
      'behavior_id' => 111,
178
      'category_id' => 6,
179
      'date' => '2016-09-10 19:26:04',
180
      'behavior' => [
181
        'id' => 111,
182
        'name' => 'seeking out old unhealthy people and places',
183
        'category_id' => 6,
184
      ],
185
      'category' => [
186
        'id' => 6,
187
        'name' => 'Exhausted',
188
      ],
189
    ], [
190
      'id' => 830,
191
      'user_id' => 2,
192
      'behavior_id' => 118,
193
      'category_id' => 6,
194
      'date' => '2016-09-10 19:26:04',
195
      'behavior' => [
196
        'id' => 118,
197
        'name' => 'not returning phone calls',
198
        'category_id' => 6,
199
      ],
200
      'category' => [
201
        'id' => 6,
202
        'name' => 'Exhausted',
203
      ],
204
    ], [
205
      'id' => 831,
206
      'user_id' => 2,
207
      'behavior_id' => 122,
208
      'category_id' => 7,
209
      'date' => '2016-09-10 19:26:04',
210
      'behavior' => [
211
        'id' => 122,
212
        'name' => 'returning to the place you swore you would never go again',
213
        'category_id' => 7,
214
      ],
215
      'category' => [
216
        'id' => 7,
217
        'name' => 'Relapse/Moral Failure',
218
      ],
219
    ],
220
  ];
221
222
  public $userBehaviors = [
223
    1 => [
224
        'making eye contact' => [
225
          'id' => 7,
226
          'name' => 'making eye contact',
227
        ],
228
    ],
229
    2 => [
230
        'less time/energy for God, meetings, and church' => [
231
          'id' => 13,
232
          'name' => 'less time/energy for God, meetings, and church',
233
        ],
234
        'changes in goals' => [
235
          'id' => 18,
236
          'name' => 'changes in goals',
237
        ],
238
    ],
239
    3 => [
240
        'using profanity' => [
241
          'id' => 29,
242
          'name' => 'using profanity',
243
        ],
244
        'co-dependent rescuing' => [
245
          'id' => 41,
246
          'name' => 'co-dependent rescuing',
247
        ],
248
    ],
249
    4 => [
250
        'workaholic' => [
251
          'id' => 48,
252
          'name' => 'workaholic',
253
        ],
254
    ],
255
    5 => [
256
        'black and white, all or nothing thinking' => [
257
          'id' => 72,
258
          'name' => 'black and white, all or nothing thinking',
259
        ],
260
        'blaming' => [
261
          'id' => 79,
262
          'name' => 'blaming',
263
        ],
264
        'obsessive (stuck) thoughts' => [
265
          'id' => 89,
266
          'name' => 'obsessive (stuck) thoughts',
267
        ],
268
    ],
269
    6 => [
270
        'seeking out old unhealthy people and places' => [
271
          'id' => 111,
272
          'name' => 'seeking out old unhealthy people and places',
273
        ],
274
        'not returning phone calls' => [
275
          'id' => 118,
276
          'name' => 'not returning phone calls',
277
        ],
278
    ],
279
    7 => [
280
        'returning to the place you swore you would never go again' => [
281
          'id' => 122,
282
          'name' => 'returning to the place you swore you would never go again',
283
        ],
284
    ],
285
  ];
286
287
288
  public function setUp() {
289
    // pull in test data
290
    $data = require(__DIR__.'/../data/checkinData.php');
291
    $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...
292
    $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...
293
    $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...
294
    $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...
295
296
    $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...
297
    $this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser');
298
    $this->container->set('common\interfaces\QuestionInterface', '\site\tests\_support\MockQuestion');
299
    $this->container->set('common\interfaces\BehaviorInterface', 'common\models\Behavior');
300
    $this->container->set('common\interfaces\TimeInterface', function () {
301
        return new \common\components\Time('America/Los_Angeles');
302
      });
303
304
    $time = $this->container->get('common\interfaces\TimeInterface');
305
    $behavior = $this->container->get('common\interfaces\BehaviorInterface');
306
    $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...
307
308
    $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...
309
                            ->getMockBuilder('common\models\UserBehavior')
310
                            ->setConstructorArgs([$behavior, $time])
311
                            ->setMethods(['getIsNewRecord', 'save', 'getBehaviorsWithCounts'])
312
                            ->getMock();
313
    parent::setUp();
314
  }
315
316
  protected function tearDown() {
317
    $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...
318
    parent::tearDown();
319
  }
320
321
  public function testRules() {
322
    expect('rules', $this->assertEquals($this->user_behavior->rules(), [
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...gory_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...
323
      [['user_id', 'behavior_id', 'category_id', 'date'], 'required'],
324
      [['user_id', 'behavior_id', 'category_id'], 'integer'],
325
    ]));
326
  }
327
328
  public function testAttributeLabels() {
329
    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' => 'Category 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...
330
      'id'        => 'ID',
331
      'date'      => 'Date',
332
      'user_id'   => 'User ID',
333
      'behavior_id' => 'Behavior ID',
334
      'category_id' => 'Category ID',
335
    ]));
336
  }
337
338
  public function testDecorate() {
339
    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...ame' => '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...
340
                    [['id' => 396,
341
                      'user_id' => 2,
342
                      'behavior_id' => 107,
343
                      'category_id' => 6,
344
                      'date' => '2016-06-17 04:12:43',
345
                      'behavior' => [
346
                        'id' => 107,
347
                        'name' => 'numb',
348
                        'category_id' => 6,
349
                      ],
350
                      'category' => [
351
                        'id' => 6,
352
                        'name' => 'Exhausted',
353
                      ]]]));
354
355
    expect('decorate SHOULD NOT add additional Behavior or Category data when the provided behavior_id is invalid',
356
      $this->assertEquals(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...mpleBehaviorNoBehavior) 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...
357
        $this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior),
358
        $this->badSingleSimpleBehaviorNoBehavior));
359
360
    expect('decorate SHOULD NOT add Category data when the provided category_id is invalid',
361
      $this->assertEquals(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...mpleBehaviorNoBehavior) 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...
362
        $this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior),
363
        $this->badSingleSimpleBehaviorNoBehavior));
364
  }
365
366
  public function testParseBehaviorData() {
367
    expect('parseBehaviorData should return the correct structure with expected data', $this->assertEquals($this->user_behavior->parseBehaviorData($this->behaviorData), $this->userBehaviors));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi..., $this->userBehaviors) 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...
368
    expect('parseBehaviorData should return empty with the empty set', $this->assertEmpty($this->user_behavior->parseBehaviorData([])));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEmpty($this...eBehaviorData(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...
369
  }
370
371
  public function testGetBehaviorsByCategory() {
372
    expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([], $this->user_behavior->getBehaviorsByCategory($this->user_behavior::decorate([]))));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals(arra...or::decorate(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...
373
    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...=> 4, '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...
374
      1 => [
375
        'name' => 'Restoration',
376
        'count' => 5,
377
        'color' => '#008000',
378
        'highlight' => '#199919'
379
      ],
380
      2 => [
381
        'name' => 'Forgetting Priorities',
382
        'count' => 4,
383
        'color' => '#4CA100',
384
        'highlight' => '#61B219'
385
      ],
386
      4 => [
387
        'name' => 'Speeding Up',
388
        'count' => 3,
389
        'color' => '#E5E500',
390
        'highlight' => '#E5E533'
391
      ]
392
    ], $this->user_behavior->getBehaviorsByCategory($this->user_behavior::decorate([
393
        [
394
          'user_id' => 1,
395
          'behavior_id' => 1,
396
          'category_id' => 1,
397
          'count' => 5
398
        ], [
399
          'user_id' => 1,
400
          'behavior_id' => 20,
401
          'category_id' => 2,
402
          'count' => 4
403
        ], [
404
          'user_id' => 1,
405
          'behavior_id' => 50,
406
          'category_id' => 4,
407
          'count' => 3
408
        ]
409
      ]))));
410
  }
411
412
  public function testGetCheckinBreakdown() {
413
    Yii::configure(Yii::$app, [
414
      'components' => [
415
        'user' => [
416
          'class' => 'yii\web\User',
417
          'identityClass' => 'common\tests\unit\FakeUser',
418
        ],
419
      ],
420
    ]);
421
    $identity = new \common\tests\unit\FakeUser();
422
    $tz = 'America/Los_Angeles';
423
    $identity->timezone = $tz;
424
    // logs in the user -- we use Yii::$app->user->id in getCheckinBreakdown()
425
    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

425
    Yii::$app->user->/** @scrutinizer ignore-call */ 
426
                     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...
426
427
    /* get mocked Time */
428
    $tz = new \DateTimeZone($tz);
429
    $date_range = new \DatePeriod(new \DateTime('2019-02-03', $tz),
430
                                  new \DateInterval('P1D'),
431
                                  new \DateTime('2019-03-05', $tz));
432
    $time = $this
433
        ->getMockBuilder("common\components\Time")
434
        ->setConstructorArgs(['America/Los_Angeles'])
435
        ->setMethods(['getDateTimesInPeriod'])
436
        ->getMock();
437
    $time
438
      ->method('getDateTimesInPeriod')
439
      ->willReturn($date_range);
440
441
    $behavior = new \common\models\Behavior();
442
443
    $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...
444
                            ->getMockBuilder("common\models\UserBehavior")
445
                            ->setConstructorArgs([$behavior, $time])
446
                            ->setMethods(['getIsNewRecord', 'save', 'getBehaviorsWithCounts'])
447
                            ->getMock();
448
449
    $bhvrs = require(__DIR__.'/../data/behaviorsWithCounts.php');
450
    $expected = require(__DIR__.'/../data/expected_getCheckinBreakdown.php');
451
    $this->user_behavior->method('getBehaviorsWithCounts')->willReturn(...$bhvrs);
452
		expect('asdf', $this->assertEquals($expected, $this->user_behavior->getCheckinBreakdown()));
0 ignored issues
show
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...
453
  }
454
}
455