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); |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
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()); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: