akester/storehouse

Fork Project

fea3e83bbbaefd7c9daa4e05569f7391a54c83d4

Authored By: Andrew Kester Date: July 7, 2017 3:27 pm

Update cache settings to not cache things that change frequently.

src/StorehouseBundle/Tests/Utils/VCS/AbstractWrapperTest.php
15 15
 use StorehouseBundle\Entity\VCS\Commit;
16 16
 use StorehouseBundle\Repository\User\AbstractUserRepository;
17 17
 use StorehouseBundle\Entity\VCS\Diff;
18
+use StorehouseBundle\Entity\VCS\LargeDiff;
19 18
 
20 19
 /*
21 20
  * The Storehouse - Project Storage for Big Ideas
82 83
     }
83 84
 
84 85
     /**
85
+     * Get a command wrapper with the getBranches command mocked.
86
+     *
87
+     * @param unknown $return
88
+     * @return PHPUnit_Framework_MockObject_MockObject
89
+     */
90
+    private function getWrapper($return)
91
+    {
92
+        $wrapper = $this->getMockBuilder(GitWrapper::class)
93
+            ->setConstructorArgs(
94
+            array(
95
+                $this->getCommand($return),
96
+                $this->entityManager,
97
+                $this->project
98
+            ))
99
+            ->setMethods(array(
100
+            'getBranches'
101
+        ))
102
+            ->getMock();
103
+
104
+        $wrapper->method('getBranches')->will($this->returnValue(array(
105
+            'test'
106
+        )));
107
+
108
+        return $wrapper;
109
+    }
110
+
111
+    /**
112 86
      *
113 87
      * {@inheritdoc}
114 88
      *
132 160
                 'getTags'
133 161
             ),
134 162
             array(
163
-                'getBranches'
164
-            ),
165
-            array(
135 166
                 'init'
136 167
             )
137 168
         );
150 175
      */
151 176
     public function testUnsupportedCommands($command)
152 177
     {
178
-        $wrapper = new GitWrapper($this->commandProcessor, $this->entityManager, $this->project);
153
+        $wrapper = $this->getWrapper(true);
154 179
 
155 180
         $property = new \ReflectionProperty(AbstractWrapper::class, 'commandObjects');
156 181
         $property->setAccessible(true);
162 187
     }
163 188
 
164 189
     /**
165
+     * Test getBranches when it's not supported
166
+     *
167
+     * @param string $command
168
+     *            Command to test
169
+     */
170
+    public function testGetBranchesUnsupported()
171
+    {
172
+        $wrapper = new GitWrapper($this->commandProcessor, $this->entityManager, $this->project);
173
+
174
+        $property = new \ReflectionProperty(AbstractWrapper::class, 'commandObjects');
175
+        $property->setAccessible(true);
176
+        $property->setValue($wrapper, array(
177
+            'getBranches' => new NotSupportedCommand(true)
178
+        ));
179
+
180
+        $this->assertTrue($wrapper->getBranches());
181
+    }
182
+
183
+    /**
184 190
      * Get test conditions for getTree
185 191
      *
186 192
      * @return NULL[][]|string[][]
204 248
      */
205 249
     public function testGetTree($path, $commit)
206 250
     {
251
-        $wrapper = new GitWrapper($this->getCommand(true), $this->entityManager, $this->project);
207
+        $wrapper = $this->getWrapper(true);
208 252
 
209 253
         $this->assertTrue($wrapper->getTree($path, $commit));
210 254
     }
222 266
     public function testGetFile($path, $commit)
223 267
     {
224 268
         $file = $this->createMock(File::class);
269
-        $wrapper = new GitWrapper($this->getCommand($file), $this->entityManager, $this->project);
225
+        $wrapper = $this->getWrapper($file);
226 270
 
227 271
         $this->assertInstanceOf(File::class, $wrapper->getFile($path, $commit));
228 272
     }
240 284
     public function testGetFileSkel($path, $commit)
241 285
     {
242 286
         $file = $this->createMock(File::class);
287
-        $wrapper = new GitWrapper($this->getCommand($file), $this->entityManager, $this->project);
243
+        $wrapper = $this->getWrapper($file);
244 288
 
245 289
         $file = $wrapper->getFileSkel($path, $commit);
246 290
         $this->assertInstanceOf(File::class, $file);
260 304
     public function testGetRawFile($path, $commit)
261 305
     {
262 306
         $file = $this->createMock(File::class);
307
-        $wrapper = new GitWrapper($this->getCommand($file), $this->entityManager, $this->project);
263
+        $wrapper = $this->getWrapper($file);
264 308
 
265 309
         $this->assertInstanceOf(File::class, $wrapper->getRawFile($path, $commit));
266 310
     }
282 326
         $tree = $this->createMock(Tree::class);
283 327
         $tree->method('contains')->will($this->returnValue(true));
284 328
 
329
-        $wrapper = new GitWrapper($this->getCommand($tree), $this->entityManager, $this->project);
285
+        $wrapper = $this->getMockBuilder(GitWrapper::class)
286
+            ->setConstructorArgs(
287
+            array(
288
+                $this->getCommand($tree),
289
+                $this->entityManager,
290
+                $this->project
291
+            ))
292
+            ->setMethods(array(
293
+            'getBranches'
294
+        ))
295
+            ->getMock();
296
+        $wrapper->method('getBranches')->will($this->returnValue(array(
297
+            'test'
298
+        )));
299 330
 
300 331
         $this->assertSame($output, $wrapper->isFile($path, $commit));
301 332
     }
308 365
                     )
309 366
                 )));
310 367
 
368
-        $wrapper = new GitWrapper($this->getCommand($tree), $this->entityManager, $this->project);
311
+        $wrapper = $this->getWrapper($tree);
312 369
 
313 370
         $this->assertFalse($wrapper->isFile('test', 'test'));
314 371
     }
337 394
         $tree = $this->createMock(Tree::class);
338 395
         $tree->method('contains')->will($this->returnValue(false));
339 396
 
397
-        $wrapper = new GitWrapper($this->getCommand($tree), $this->entityManager, $this->project);
340
+        $wrapper = $this->getWrapper($tree);
341 398
 
342 399
         $wrapper->isFile('test', 'test');
343 400
     }
363 420
         $commitObject->author = 'Test User <test@sthse.co>';
364 421
         $commitObject->message = 'Test Message';
365 422
 
423
-        $wrapper = new GitWrapper(
424
-            $this->getCommand(array(
425
-                $commitObject,
426
-                $commitObject
427
-            )), $this->entityManager, $this->project);
366
+        $wrapper = $this->getWrapper(array(
367
+            $commitObject,
368
+            $commitObject
369
+        ));
370 428
 
371 429
         $result = $wrapper->getLog($commit, $path);
372 430
         $this->assertInternalType('array', $result);
405 461
             $diff
406 462
         );
407 463
 
464
-        $wrapper = new GitWrapper($this->getCommand($commitObject), $this->entityManager, $this->project);
408
+        $wrapper = $this->getWrapper($commitObject);
409
+
410
+        $result = $wrapper->getCommit($commit);
411
+        $this->assertInstanceOf(Commit::class, $result);
412
+    }
413
+
414
+    /**
415
+     * Test getLog with large diffs returned.
416
+     *
417
+     * @dataProvider getPaths
418
+     *
419
+     * @param string $path
420
+     *            Path to get log for
421
+     * @param string $commit
422
+     *            Commit to get log at
423
+     */
424
+    public function testGetCommitLargeDiff($path, $commit)
425
+    {
426
+        $repo = $this->createMock(AbstractUserRepository::class);
427
+        $repo->method('searchForUser')->will($this->returnValue(null));
428
+        $this->entityManager->method('getRepository')->will($this->returnValue($repo));
429
+
430
+        $diff = new Diff();
431
+        $diff->file1 = new File('README');
432
+        $diff->file2 = new File('README');
433
+
434
+        $commitObject = new UnresolvedCommit();
435
+        $commitObject->diff = array(
436
+            $this->createMock(LargeDiff::class)
437
+        );
438
+
439
+        $wrapper = $this->getWrapper($commitObject);
440 465
 
441 466
         $result = $wrapper->getCommit($commit);
442 467
         $this->assertInstanceOf(Commit::class, $result);
src/StorehouseBundle/Utils/VCS/AbstractWrapper.php
216 216
 
217 217
         $command = $this->commandObjects['getTree'];
218 218
 
219
+        if (in_array($commit, $this->getBranches())) {
220
+            $command->cacheDuration = 0;
221
+        }
222
+
223 219
         if ($command instanceof NotSupportedCommand) {
224 220
             return $command->processOutput(array());
225 221
         }
264 268
 
265 269
         $command = $this->commandObjects['getFile'];
266 270
 
267
+        if (in_array($commit, $this->getBranches())) {
268
+            $command->cacheDuration = 0;
269
+        }
270
+
271 271
         if ($command instanceof NotSupportedCommand) {
272 272
             return $command->processOutput(array());
273 273
         }
298 306
 
299 307
         $command = $this->commandObjects['getRawFile'];
300 308
 
301
+        if (in_array($commit, $this->getBranches())) {
302
+            $command->cacheDuration = 0;
303
+        }
304
+
305 309
         if ($command instanceof NotSupportedCommand) {
306 310
             return $command->processOutput(array());
307 311
         }
334 346
 
335 347
         $command = $this->commandObjects['getLog'];
336 348
 
337
+        if (in_array($commit, $this->getBranches())) {
338
+            $command->cacheDuration = 0;
339
+        }
340
+
341 349
         if ($command instanceof NotSupportedCommand) {
342 350
             return $command->processOutput(array());
343 351
         }
391 407
 
392 408
         $command = $this->commandObjects['getCommit'];
393 409
 
394
+        if (in_array($commit, $this->getBranches())) {
395
+            $command->cacheDuration = 0;
396
+        }
397
+
398 410
         if ($command instanceof NotSupportedCommand) {
399 411
             return $command->processOutput(array());
400 412
         }
src/StorehouseBundle/Utils/VCS/Commands/AbstractCommand.php
28 28
      *
29 29
      * @var integer
30 30
      */
31
-    public $cacheDuration = 10;
31
+    public $cacheDuration = 30;
32 32
 
33 33
     /**
34 34
      * Array of arguments to pass into the command.
35 35