UserBehaviorTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 26
c 0
b 0
f 0
rs 9.6
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' => null,
193
      'category_id' => 6,
194
      'date' => '2016-09-10 19:26:04',
195
      'custom_behavior' => 'some other custom behavior',
196
      'category' => [
197
        'id' => 6,
198
        'name' => 'Exhausted',
199
      ],
200
    ], [
201
      'id' => 831,
202
      'user_id' => 2,
203
      'behavior_id' => null,
204
      'category_id' => 7,
205
      'date' => '2016-09-10 19:26:04',
206
      'custom_behavior' => 'some_custom_behavior',
207
      'category' => [
208
        'id' => 7,
209
        'name' => 'Relapse/Moral Failure',
210
      ],
211
    ],
212
  ];
213
214
  public $userBehaviors = [
215
    1 => [
216
        'making eye contact' => [
217
          'id' => 7,
218
          'name' => 'making eye contact',
219
        ],
220
    ],
221
    2 => [
222
        'less time/energy for God, meetings, and church' => [
223
          'id' => 13,
224
          'name' => 'less time/energy for God, meetings, and church',
225
        ],
226
        'changes in goals' => [
227
          'id' => 18,
228
          'name' => 'changes in goals',
229
        ],
230
    ],
231
    3 => [
232
        'using profanity' => [
233
          'id' => 29,
234
          'name' => 'using profanity',
235
        ],
236
        'co-dependent rescuing' => [
237
          'id' => 41,
238
          'name' => 'co-dependent rescuing',
239
        ],
240
    ],
241
    4 => [
242
        'workaholic' => [
243
          'id' => 48,
244
          'name' => 'workaholic',
245
        ],
246
    ],
247
    5 => [
248
        'black and white, all or nothing thinking' => [
249
          'id' => 72,
250
          'name' => 'black and white, all or nothing thinking',
251
        ],
252
        'blaming' => [
253
          'id' => 79,
254
          'name' => 'blaming',
255
        ],
256
        'obsessive (stuck) thoughts' => [
257
          'id' => 89,
258
          'name' => 'obsessive (stuck) thoughts',
259
        ],
260
    ],
261
    6 => [
262
        'seeking out old unhealthy people and places' => [
263
          'id' => 111,
264
          'name' => 'seeking out old unhealthy people and places',
265
        ],
266
        'some other custom behavior' => [
267
          'id' => null,
268
          'name' => 'some other custom behavior',
269
        ],
270
    ],
271
    7 => [
272
        'some_custom_behavior' => [
273
          'id' => null,
274
          'name' => 'some_custom_behavior',
275
        ],
276
    ],
277
  ];
278
279
280
  public function setUp() {
281
    // pull in test data
282
    $data = require(__DIR__.'/../data/checkinData.php');
283
    $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...
284
    $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...
285
    $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...
286
    $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...
287
288
    $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...
289
    $this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser');
290
    $this->container->set('common\interfaces\QuestionInterface', '\site\tests\_support\MockQuestion');
291
    $this->container->set('common\interfaces\BehaviorInterface', 'common\models\Behavior');
292
    $this->container->set('common\interfaces\TimeInterface', function () {
293
        return new \common\components\Time('America/Los_Angeles');
294
      });
295
296
    $time = $this->container->get('common\interfaces\TimeInterface');
297
    $behavior = $this->container->get('common\interfaces\BehaviorInterface');
298
    $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...
299
300
    $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...
301
                            ->getMockBuilder('common\models\UserBehavior')
302
                            ->setConstructorArgs([$behavior, $time])
303
                            ->setMethods(['getIsNewRecord', 'save', 'getBehaviorsWithCounts'])
304
                            ->getMock();
305
    parent::setUp();
306
  }
307
308
  protected function tearDown() {
309
    $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...
310
    parent::tearDown();
311
  }
312
313
  public function testRules() {
314
    expect('rules', $this->assertEquals($this->user_behavior->rules(), [
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi..._behavior', 'string'))) 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...
315
      [['user_id', 'behavior_id', 'category_id', 'date'], 'required'],
316
      [['user_id', 'behavior_id', 'category_id'], 'integer'],
317
      ['custom_behavior', 'string'],
318
    ]));
319
  }
320
321
  public function testAttributeLabels() {
322
    expect('attributeLabels', $this->assertEquals($this->user_behavior->attributelabels(), [
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi...rsonal Behavior Name')) 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
      'id'        => 'ID',
324
      'date'      => 'Date',
325
      'user_id'   => 'User ID',
326
      'behavior_id' => 'Behavior ID',
327
      'category_id' => 'Category ID',
328
      'custom_behavior' => 'Personal Behavior Name',
329
    ]));
330
  }
331
332
  public function testDecorate() {
333
    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...
334
                    [['id' => 396,
335
                      'user_id' => 2,
336
                      'behavior_id' => 107,
337
                      'category_id' => 6,
338
                      'date' => '2016-06-17 04:12:43',
339
                      'behavior' => [
340
                        'id' => 107,
341
                        'name' => 'numb',
342
                        'category_id' => 6,
343
                      ],
344
                      'category' => [
345
                        'id' => 6,
346
                        'name' => 'Exhausted',
347
                      ]]]));
348
349
    expect('decorate should add Category data but no Behavior data when the category_id is valid and the behavior_id is invalid',
350
      $this->assertEquals(
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...
351
        $this->user_behavior->decorate($this->badSingleSimpleBehaviorNoBehavior),
352
        [[
353
          'id' => 396,
354
          'user_id' => 2,
355
          'behavior_id' => 99999,
356
          'category_id' => 6,
357
          'date' => '2016-06-17 04:12:43',
358
          'category' => [
359
            'id' => 6,
360
            'name' => 'Exhausted',
361
          ],
362
        ]]));
363
  }
364
365
  public function testParseBehaviorData() {
366
    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...
367
    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...
368
  }
369
370
  public function testGetBehaviorsByCategory() {
371
    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...
372
    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...
373
      1 => [
374
        'name' => 'Restoration',
375
        'count' => 5,
376
        'color' => '#008000',
377
        'highlight' => '#199919'
378
      ],
379
      2 => [
380
        'name' => 'Forgetting Priorities',
381
        'count' => 4,
382
        'color' => '#4CA100',
383
        'highlight' => '#61B219'
384
      ],
385
      4 => [
386
        'name' => 'Speeding Up',
387
        'count' => 3,
388
        'color' => '#E5E500',
389
        'highlight' => '#E5E533'
390
      ]
391
    ], $this->user_behavior->getBehaviorsByCategory($this->user_behavior::decorate([
392
        [
393
          'user_id' => 1,
394
          'behavior_id' => 1,
395
          'category_id' => 1,
396
          'count' => 5
397
        ], [
398
          'user_id' => 1,
399
          'behavior_id' => 20,
400
          'category_id' => 2,
401
          'count' => 4
402
        ], [
403
          'user_id' => 1,
404
          'behavior_id' => 50,
405
          'category_id' => 4,
406
          'count' => 3
407
        ]
408
      ]))));
409
  }
410
411
  public function testGetCheckinBreakdown() {
412
    Yii::configure(Yii::$app, [
413
      'components' => [
414
        'user' => [
415
          'class' => 'yii\web\User',
416
          'identityClass' => 'common\tests\unit\FakeUser',
417
        ],
418
      ],
419
    ]);
420
    $identity = new \common\tests\unit\FakeUser();
421
    $tz = 'America/Los_Angeles';
422
    $identity->timezone = $tz;
423
    // logs in the user -- we use Yii::$app->user->id in getCheckinBreakdown()
424
    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

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