1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2017 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Parser\Middleware; |
14
|
|
|
|
15
|
|
|
use Mockery as m; |
16
|
|
|
use phpDocumentor\Reflection\File as SourceFile; |
17
|
|
|
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand; |
18
|
|
|
use phpDocumentor\Reflection\Php\File; |
19
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
20
|
|
|
use Stash\Item; |
21
|
|
|
use Stash\Pool; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @coversDefaultClass phpDocumentor\parser\Middleware\CacheMiddleware |
25
|
|
|
* @covers ::<private> |
26
|
|
|
* @covers ::__construct |
27
|
|
|
*/ |
28
|
|
|
final class CacheMiddlewareTest extends \PHPUnit_Framework_TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @covers ::execute |
32
|
|
|
* @uses phpDocumentor\Reflection\Php\Factory\File\CreateCommand |
33
|
|
|
* @uses phpDocumentor\Reflection\Php\File |
34
|
|
|
*/ |
35
|
|
|
public function testCacheIsUsed() |
36
|
|
|
{ |
37
|
|
|
$file = new File('hash', 'myFile.php'); |
38
|
|
|
$item = new Item(); |
39
|
|
|
$poolMock = m::mock(Pool::class); |
40
|
|
|
$poolMock->shouldReceive('getItem') |
41
|
|
|
->andReturnSelf(); |
42
|
|
|
|
43
|
|
|
$poolMock->shouldReceive('getItem->isMiss') |
44
|
|
|
->once() |
45
|
|
|
->andReturn(true); |
46
|
|
|
|
47
|
|
|
$poolMock->shouldReceive('getItem->lock') |
48
|
|
|
->once(); |
49
|
|
|
|
50
|
|
|
$poolMock->shouldReceive('getItem->set') |
51
|
|
|
->once() |
52
|
|
|
->andReturn($item) |
53
|
|
|
->with($file); |
54
|
|
|
|
55
|
|
|
$poolMock->shouldReceive('getItem->get') |
56
|
|
|
->never(); |
57
|
|
|
|
58
|
|
|
$poolMock->shouldReceive('save')->with($item); |
59
|
|
|
|
60
|
|
|
$sourceFile = m::mock(SourceFile::class); |
61
|
|
|
$sourceFile->shouldReceive('path')->andReturn('myFile.php'); |
62
|
|
|
$stategies = m::mock(StrategyContainer::class); |
63
|
|
|
$command = new CreateCommand($sourceFile, $stategies); |
64
|
|
|
$fixture = new CacheMiddleware($poolMock); |
65
|
|
|
|
66
|
|
|
$result = $fixture->execute($command, function () use ($file) { |
67
|
|
|
return $file; |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
$this->assertSame($file, $result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @covers ::execute |
75
|
|
|
* @uses phpDocumentor\Reflection\Php\Factory\File\CreateCommand |
76
|
|
|
* @uses phpDocumentor\Reflection\Php\File |
77
|
|
|
*/ |
78
|
|
|
public function testChecksHash() |
79
|
|
|
{ |
80
|
|
|
$cachedFile = new File('OldHash', 'myFile.php'); |
81
|
|
|
$freshFile = new File('NewHash', 'myFile.php'); |
82
|
|
|
$item = new Item(); |
83
|
|
|
$poolMock = m::mock(Pool::class); |
84
|
|
|
$poolMock->shouldReceive('getItem') |
85
|
|
|
->andReturnSelf(); |
86
|
|
|
|
87
|
|
|
$poolMock->shouldReceive('getItem->isMiss') |
88
|
|
|
->once() |
89
|
|
|
->andReturn(false); |
90
|
|
|
|
91
|
|
|
$poolMock->shouldReceive('getItem->lock') |
92
|
|
|
->once(); |
93
|
|
|
|
94
|
|
|
$poolMock->shouldReceive('getItem->set') |
95
|
|
|
->once() |
96
|
|
|
->with($freshFile) |
97
|
|
|
->andReturn($item); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
$poolMock->shouldReceive('getItem->get') |
101
|
|
|
->once() |
102
|
|
|
->andReturn($cachedFile); |
103
|
|
|
|
104
|
|
|
$poolMock->shouldReceive('save')->with($item); |
105
|
|
|
|
106
|
|
|
$sourceFile = m::mock(SourceFile::class); |
107
|
|
|
$sourceFile->shouldReceive('path')->andReturn('myFile.php'); |
108
|
|
|
$sourceFile->shouldReceive('md5') |
109
|
|
|
->andReturn('NewHash'); |
110
|
|
|
$stategies = m::mock(StrategyContainer::class); |
111
|
|
|
$command = new CreateCommand($sourceFile, $stategies); |
112
|
|
|
$fixture = new CacheMiddleware($poolMock); |
113
|
|
|
|
114
|
|
|
$result = $fixture->execute($command, function () use ($freshFile) { |
115
|
|
|
return $freshFile; |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$this->assertSame($freshFile, $result); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|