Completed
Push — develop ( 8f34f3...3bf98d )
by Jaap
13:27 queued 03:31
created

FlySystemAssetsTest::itShouldReturnAFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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-2016 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\Infrastructure\Renderer;
14
15
use League\Flysystem\AdapterInterface;
16
use League\Flysystem\File;
17
use League\Flysystem\Filesystem;
18
use League\Flysystem\FilesystemInterface;
19
use Mockery as m;
20
use phpDocumentor\DomainModel\Path;
21
use phpDocumentor\DomainModel\Renderer\Asset;
22
use phpDocumentor\DomainModel\Renderer\AssetNotFoundException;
23
24
/**
25
 * @coversDefaultClass phpDocumentor\Infrastructure\Renderer\FlySystemAssets
26
 * @covers ::__construct
27
 * @covers ::<private>
28
 */
29
final class FlySystemAssetsTest extends \PHPUnit_Framework_TestCase
30
{
31
    /** @var FilesystemInterface|m\MockInterface */
32
    private $filesystem;
33
34
    /** @var FlySystemAssets */
35
    private $assets;
36
37
    public function setUp()
38
    {
39
        $this->filesystem = m::mock(FilesystemInterface::class);
40
41
        $this->assets = new FlySystemAssets($this->filesystem);
42
    }
43
44
    /**
45
     * @test
46
     * @covers ::get
47
     */
48
    public function itShouldReturnAnAsset()
49
    {
50
        $location = new Path('image.jpg');
51
        $file = m::mock(File::class);
52
        $file->shouldReceive('isDir')->andReturn(false);
53
        $file->shouldReceive('read')->andReturn('data');
54
55
        $this->filesystem->shouldReceive('has')->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in League\Flysystem\FilesystemInterface.

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...
56
        $this->filesystem->shouldReceive('get')->andReturn($file);
57
58
        $asset = $this->assets->get($location);
59
60
        $this->assertInstanceOf(Asset::class, $asset);
61
        $this->assertSame('data', $asset->content());
0 ignored issues
show
Bug introduced by
The method content does only exist in phpDocumentor\DomainModel\Renderer\Asset, but not in phpDocumentor\DomainModel\Renderer\Asset\Folder.

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...
62
    }
63
64
    /**
65
     * @test
66
     * @covers ::get
67
     */
68
    public function itShouldReturnAFolder()
69
    {
70
        $location = new Path('images');
71
        $file = m::mock(File::class);
72
        $file->shouldReceive('isDir')->andReturn(true);
73
74
        $this->filesystem->shouldReceive('has')->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in League\Flysystem\FilesystemInterface.

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...
75
        $this->filesystem->shouldReceive('get')->andReturn($file);
76
        $this->filesystem->shouldReceive('listContents')
77
            ->with((string)$location, true)
78
            ->andReturn([['type' => 'file', 'path' => 'images/image.jpg']]);
79
80
        $asset = $this->assets->get($location);
81
82
        $this->assertInstanceOf(Asset\Folder::class, $asset);
83
        $this->assertEquals([new Path('images/image.jpg')], $asset->getArrayCopy());
0 ignored issues
show
Bug introduced by
The method getArrayCopy does only exist in phpDocumentor\DomainModel\Renderer\Asset\Folder, but not in phpDocumentor\DomainModel\Renderer\Asset.

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...
84
    }
85
86
    /**
87
     * @test
88
     * @covers ::get
89
     */
90
    public function itShouldNotReturnSubFoldersWhenRetrievingAFolder()
91
    {
92
        $location = new Path('images');
93
        $file = m::mock(File::class);
94
        $file->shouldReceive('isDir')->andReturn(true);
95
96
        $this->filesystem->shouldReceive('has')->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in League\Flysystem\FilesystemInterface.

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...
97
        $this->filesystem->shouldReceive('get')->andReturn($file);
98
        $this->filesystem->shouldReceive('listContents')
99
            ->with((string)$location, true)
100
            ->andReturn(
101
                [
102
                    ['type' => 'dir', 'path' => 'images/cats'],
103
                    ['type' => 'file', 'path' => 'images/cats/cute.png'],
104
                    ['type' => 'file', 'path' => 'images/image.jpg']
105
                ]
106
            );
107
108
        $asset = $this->assets->get($location);
109
110
        $this->assertInstanceOf(Asset\Folder::class, $asset);
111
        $this->assertCount(2, $asset->getArrayCopy());
0 ignored issues
show
Bug introduced by
The method getArrayCopy does only exist in phpDocumentor\DomainModel\Renderer\Asset\Folder, but not in phpDocumentor\DomainModel\Renderer\Asset.

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...
112
        $this->assertEquals(new Path('images/cats/cute.png'), $asset->getArrayCopy()[0]);
113
        $this->assertEquals(new Path('images/image.jpg'), $asset->getArrayCopy()[1]);
114
    }
115
116
    /**
117
     * @test
118
     * @covers ::get
119
     */
120
    public function itShouldErrorWhenFetchingAnAssetAndItDoesntExist()
121
    {
122
        $this->setExpectedException(AssetNotFoundException::class);
123
        $this->filesystem->shouldReceive('has')->andReturn(false);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in League\Flysystem\FilesystemInterface.

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...
124
        $this->filesystem->shouldReceive('get')->never();
125
126
        $this->assets->get(new Path('image.jpg'));
127
    }
128
129
    /**
130
     * @test
131
     * @covers ::has
132
     */
133
    public function itShouldCheckWithFlysystemIfAnAssetExists()
134
    {
135
        $this->filesystem->shouldReceive('has')->with('image.jpg')->andReturn(true);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\MockInterface, but not in League\Flysystem\FilesystemInterface.

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...
136
        $this->filesystem->shouldReceive('has')->with('does-not-exist.jpg')->andReturn(false);
137
138
        $this->assertTrue($this->assets->has(new Path('image.jpg')));
139
        $this->assertFalse($this->assets->has(new Path('does-not-exist.jpg')));
140
    }
141
}
142