Completed
Push — develop ( 521632...eb9c4a )
by Mike
06:46
created

testCacheNewFileIfHashMismatches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 44
rs 9.216
c 0
b 0
f 0
1
<?php declare(strict_types=1);
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-2018 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\Parser\Parser;
17
use phpDocumentor\Reflection\File as SourceFile;
18
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
19
use phpDocumentor\Reflection\Php\File;
20
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
21
use phpDocumentor\Reflection\Php\StrategyContainer;
22
use Stash\Item;
23
use Stash\Pool;
24
25
/**
26
 * @coversDefaultClass \phpDocumentor\Parser\Middleware\CacheMiddleware
27
 * @covers ::<private>
28
 * @covers ::__construct
29
 */
30
final class CacheMiddlewareTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
31
{
32
    /**
33
     * @covers ::execute
34
     * @uses \phpDocumentor\Reflection\Php\Factory\File\CreateCommand
35
     * @uses \phpDocumentor\Reflection\Php\File
36
     */
37
    public function testCachedFileIsReturnedWhenValid()
38
    {
39
        $commandFile = new SourceFile\LocalFile(__FILE__);
40
        $file = new File($commandFile->md5(), __FILE__);
41
42
        $poolMock = m::mock(Pool::class);
43
        $poolMock->shouldReceive('getItem')->andReturnSelf();
44
        $poolMock->shouldReceive('getItem->isMiss')->andReturn(false);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
45
        $poolMock->shouldReceive('getItem->get')->andReturn($file);
46
        $poolMock->shouldReceive('getItem->lock')->never();
47
        $poolMock->shouldReceive('getItem->set')->never();
48
        $poolMock->shouldReceive('save')->never();
49
50
        $command = new CreateCommand($commandFile, new ProjectFactoryStrategies([]));
51
        $parserMock = m::mock(Parser::class);
52
        $parserMock->shouldReceive('isForced')->andReturn(false);
53
54
        $fixture = new CacheMiddleware($poolMock, $parserMock);
55
56
        $result = $fixture->execute($command, function () {
57
            $this->fail('Parsing should not be done, the cached item should be returned');
58
        });
59
60
        $this->assertSame($file, $result);
61
    }
62
63
    /**
64
     * @covers ::execute
65
     * @uses \phpDocumentor\Reflection\Php\Factory\File\CreateCommand
66
     * @uses \phpDocumentor\Reflection\Php\File
67
     */
68
    public function testCachedFileIsUpdatedWhenForced()
69
    {
70
        $commandFile = new SourceFile\LocalFile(__FILE__);
71
        $file = new File($commandFile->md5(), __FILE__);
72
        $item = new Item();
73
74
        $poolMock = m::mock(Pool::class);
75
        $poolMock->shouldReceive('getItem')->andReturnSelf();
76
        $poolMock->shouldReceive('getItem->isMiss')->andReturn(false);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
77
        $poolMock->shouldReceive('getItem->get')->andReturn($file);
78
        $poolMock->shouldReceive('getItem->lock')->once();
79
        $poolMock->shouldReceive('getItem->set')->andReturn($item)->with($file);
80
        $poolMock->shouldReceive('save')->with($item);
81
82
        $command = new CreateCommand($commandFile, new ProjectFactoryStrategies([]));
83
        $parserMock = m::mock(Parser::class);
84
        $parserMock->shouldReceive('isForced')->andReturn(true);
85
86
        $fixture = new CacheMiddleware($poolMock, $parserMock);
87
88
        $result = $fixture->execute($command, function () use ($file) {
89
            return $file;
90
        });
91
92
        $this->assertSame($file, $result);
93
    }
94
95
    /**
96
     * @covers ::execute
97
     * @uses \phpDocumentor\Reflection\Php\Factory\File\CreateCommand
98
     * @uses \phpDocumentor\Reflection\Php\File
99
     */
100
    public function testCacheIsUpdatedOnAMiss()
101
    {
102
        $file = new File('hash', 'myFile.php');
103
        $item = new Item();
104
        $poolMock = m::mock(Pool::class);
105
        $poolMock->shouldReceive('getItem')->andReturnSelf();
106
        $poolMock->shouldReceive('getItem->isMiss')->andReturn(true);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
107
        $poolMock->shouldReceive('getItem->lock')->once();
108
        $poolMock->shouldReceive('getItem->set')->andReturn($item)->with($file);
109
        $poolMock->shouldReceive('getItem->get')->never();
110
        $poolMock->shouldReceive('save')->with($item);
111
112
        $sourceFile = m::mock(SourceFile::class);
113
        $sourceFile->shouldReceive('path')->andReturn('myFile.php');
114
        $stategies = m::mock(StrategyContainer::class);
115
        $command = new CreateCommand($sourceFile, $stategies);
116
        $fixture = new CacheMiddleware($poolMock, m::mock(Parser::class));
117
118
        $result = $fixture->execute($command, function () use ($file) {
119
            return $file;
120
        });
121
122
        $this->assertSame($file, $result);
123
    }
124
125
    /**
126
     * @covers ::execute
127
     * @uses \phpDocumentor\Reflection\Php\Factory\File\CreateCommand
128
     * @uses \phpDocumentor\Reflection\Php\File
129
     */
130
    public function testCacheFileIfItIsNotInThePool()
131
    {
132
        $freshFile = new File('NewHash', 'myFile.php');
133
134
        $item = new Item();
135
        $poolMock = m::mock(Pool::class);
136
        $poolMock->shouldReceive('getItem')
137
            ->andReturnSelf();
138
139
        $poolMock->shouldReceive('getItem->isMiss')
140
            ->once()
141
            ->andReturn(false);
142
143
        $poolMock->shouldReceive('getItem->lock')
144
            ->once();
145
146
        $poolMock->shouldReceive('getItem->set')
147
            ->once()
148
            ->with($freshFile)
149
            ->andReturn($item);
150
151
        $poolMock->shouldReceive('getItem->get')
152
            ->once()
153
            ->andReturn(null);
154
155
        $poolMock->shouldReceive('save')->with($item);
156
157
        $sourceFile = m::mock(SourceFile::class);
158
        $sourceFile->shouldReceive('path')->andReturn('myFile.php');
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
159
        $sourceFile->shouldReceive('md5')
160
            ->andReturn('NewHash');
161
        $stategies = m::mock(StrategyContainer::class);
162
        $parser = m::mock(Parser::class);
163
        $parser->shouldReceive('isForced')->andReturn(false);
164
165
        $command = new CreateCommand($sourceFile, $stategies);
166
        $fixture = new CacheMiddleware($poolMock, $parser);
167
168
        $result = $fixture->execute($command, function () use ($freshFile) {
169
            return $freshFile;
170
        });
171
172
        $this->assertSame($freshFile, $result);
173
    }
174
175
    /**
176
     * @covers ::execute
177
     * @uses \phpDocumentor\Reflection\Php\Factory\File\CreateCommand
178
     * @uses \phpDocumentor\Reflection\Php\File
179
     */
180
    public function testCacheNewFileIfHashMismatches()
181
    {
182
        $cachedFile = new File('OldHash', 'myFile.php');
183
        $freshFile = new File('NewHash', 'myFile.php');
184
        $item = new Item();
185
        $poolMock = m::mock(Pool::class);
186
        $poolMock->shouldReceive('getItem')
187
            ->andReturnSelf();
188
189
        $poolMock->shouldReceive('getItem->isMiss')
190
            ->once()
191
            ->andReturn(false);
192
193
        $poolMock->shouldReceive('getItem->lock')
194
            ->once();
195
196
        $poolMock->shouldReceive('getItem->set')
197
            ->once()
198
            ->with($freshFile)
199
            ->andReturn($item);
200
201
        $poolMock->shouldReceive('getItem->get')
202
            ->once()
203
            ->andReturn($cachedFile);
204
205
        $poolMock->shouldReceive('save')->with($item);
206
207
        $sourceFile = m::mock(SourceFile::class);
208
        $sourceFile->shouldReceive('path')->andReturn('myFile.php');
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
209
        $sourceFile->shouldReceive('md5')
210
            ->andReturn('NewHash');
211
        $stategies = m::mock(StrategyContainer::class);
212
        $parser = m::mock(Parser::class);
213
        $parser->shouldReceive('isForced')->andReturn(false);
214
215
        $command = new CreateCommand($sourceFile, $stategies);
216
        $fixture = new CacheMiddleware($poolMock, $parser);
217
218
        $result = $fixture->execute($command, function () use ($freshFile) {
219
            return $freshFile;
220
        });
221
222
        $this->assertSame($freshFile, $result);
223
    }
224
}
225