| 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']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     $this->manyBhvrs = $data['manyBhvrs']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     $this->allBhvrs = $data['allBhvrs']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     $this->multipleDates = $data['multipleDates']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     $this->container = new \yii\di\Container; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     $this->user_behavior = $this | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |   public function testRules() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     expect('rules', $this->assertEquals($this->user_behavior->rules(), [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(), [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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([])))); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     expect('getBehaviorsByCategory to return the empty array if the user has not logged any behaviors', $this->assertEquals([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  |