| Conditions | 1 |
| Paths | 1 |
| Total Lines | 65 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | public function testGetCheckinBreakdown() { |
||
| 162 | Yii::configure(Yii::$app, [ |
||
| 163 | 'components' => [ |
||
| 164 | 'user' => [ |
||
| 165 | 'class' => 'yii\web\User', |
||
| 166 | 'identityClass' => 'common\tests\unit\FakeUser', |
||
| 167 | ], |
||
| 168 | ], |
||
| 169 | ]); |
||
| 170 | $identity = new \common\tests\unit\FakeUser(); |
||
| 171 | $tz = 'America/Los_Angeles'; |
||
| 172 | $identity->timezone = $tz; |
||
| 173 | // logs in the user -- we use Yii::$app->user->id in getCheckinBreakdown() |
||
| 174 | Yii::$app->user->setIdentity($identity); |
||
| 175 | |||
| 176 | /* get mocked Time */ |
||
| 177 | $tz = new \DateTimeZone($tz); |
||
| 178 | $date_range = new \DatePeriod(new \DateTime('2019-02-03', $tz), |
||
| 179 | new \DateInterval('P1D'), |
||
| 180 | new \DateTime('2019-03-05', $tz)); |
||
| 181 | $time = $this |
||
| 182 | ->getMockBuilder("common\components\Time") |
||
| 183 | ->setConstructorArgs(['America/Los_Angeles']) |
||
| 184 | ->setMethods(['getDateTimesInPeriod']) |
||
| 185 | ->getMock(); |
||
| 186 | $time |
||
| 187 | ->method('getDateTimesInPeriod') |
||
| 188 | ->willReturn($date_range); |
||
| 189 | |||
| 190 | $behavior = new \common\models\Behavior(); |
||
| 191 | |||
| 192 | $this->user_behavior = $this |
||
| 193 | ->getMockBuilder("common\models\UserBehavior") |
||
| 194 | ->setConstructorArgs([$behavior, $time]) |
||
| 195 | ->setMethods(['getIsNewRecord', 'save', 'getBehaviorsByDate', 'getBehaviorsWithCounts']) |
||
| 196 | ->getMock(); |
||
| 197 | |||
| 198 | $bhvrs = require(__DIR__.'/../data/behaviorsWithCounts.php'); |
||
| 199 | $expected = require(__DIR__.'/../data/expected_getCheckinBreakdown.php'); |
||
| 200 | $this->user_behavior->method('getBehaviorsWithCounts')->willReturn(...$bhvrs); |
||
| 201 | expect('asdf', $this->assertEquals($expected, $this->user_behavior->getCheckinBreakdown())); |
||
| 202 | } |
||
| 203 | } |
||
| 204 |