Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Parser/Middleware/CacheMiddlewareTest.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-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\StrategyContainer;
21
use Stash\Item;
22
use Stash\Pool;
23
24
/**
25
 * @coversDefaultClass phpDocumentor\parser\Middleware\CacheMiddleware
26
 * @covers ::<private>
27
 * @covers ::__construct
28
 */
29
final class CacheMiddlewareTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
30
{
31
    /**
32
     * @covers ::execute
33
     * @uses phpDocumentor\Reflection\Php\Factory\File\CreateCommand
34
     * @uses phpDocumentor\Reflection\Php\File
35
     */
36
    public function testCacheIsUsed()
37
    {
38
        $file = new File('hash', 'myFile.php');
39
        $item = new Item();
40
        $poolMock = m::mock(Pool::class);
41
        $poolMock->shouldReceive('getItem')
42
            ->andReturnSelf();
43
44
        $poolMock->shouldReceive('getItem->isMiss')
45
            ->once()
46
            ->andReturn(true);
47
48
        $poolMock->shouldReceive('getItem->lock')
49
            ->once();
50
51
        $poolMock->shouldReceive('getItem->set')
52
            ->once()
53
            ->andReturn($item)
54
            ->with($file);
55
56
        $poolMock->shouldReceive('getItem->get')
57
            ->never();
58
59
        $poolMock->shouldReceive('save')->with($item);
60
61
        $sourceFile = m::mock(SourceFile::class);
62
        $sourceFile->shouldReceive('path')->andReturn('myFile.php');
0 ignored issues
show
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...
63
        $stategies = m::mock(StrategyContainer::class);
64
        $command = new CreateCommand($sourceFile, $stategies);
65
        $fixture = new CacheMiddleware($poolMock, m::mock(Parser::class));
66
67
        $result = $fixture->execute($command, function () use ($file) {
68
            return $file;
69
        });
70
71
        $this->assertSame($file, $result);
72
    }
73
74
    /**
75
     * @covers ::execute
76
     * @uses phpDocumentor\Reflection\Php\Factory\File\CreateCommand
77
     * @uses phpDocumentor\Reflection\Php\File
78
     */
79
    public function testChecksHash()
80
    {
81
        $cachedFile = new File('OldHash', 'myFile.php');
82
        $freshFile = new File('NewHash', 'myFile.php');
83
        $item = new Item();
84
        $poolMock = m::mock(Pool::class);
85
        $poolMock->shouldReceive('getItem')
86
            ->andReturnSelf();
87
88
        $poolMock->shouldReceive('getItem->isMiss')
89
            ->once()
90
            ->andReturn(false);
91
92
        $poolMock->shouldReceive('getItem->lock')
93
            ->once();
94
95
        $poolMock->shouldReceive('getItem->set')
96
            ->once()
97
            ->with($freshFile)
98
            ->andReturn($item);
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');
0 ignored issues
show
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...
108
        $sourceFile->shouldReceive('md5')
109
            ->andReturn('NewHash');
110
        $stategies = m::mock(StrategyContainer::class);
111
        $parser = m::mock(Parser::class);
112
        $parser->shouldReceive('isForced')->andReturn(false);
113
114
        $command = new CreateCommand($sourceFile, $stategies);
115
        $fixture = new CacheMiddleware($poolMock, $parser);
116
117
        $result = $fixture->execute($command, function () use ($freshFile) {
118
            return $freshFile;
119
        });
120
121
        $this->assertSame($freshFile, $result);
122
    }
123
}
124