-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDirectoryTest.php
132 lines (107 loc) · 4.54 KB
/
DirectoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward\Test\Resolver;
use Laminas\Http\Response;
use Magento\Upward\Definition;
use Magento\Upward\DefinitionIterator;
use Magento\Upward\Resolver\Directory;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use function BeBat\Verify\verify;
class DirectoryTest extends TestCase
{
use MockeryPHPUnitIntegration;
/**
* @var DefinitionIterator|Mockery\MockInterface
*/
private $mockIterator;
/**
* @var Directory
*/
private $resolver;
protected function setUp(): void
{
$this->mockIterator = Mockery::mock(DefinitionIterator::class);
$this->resolver = new Directory();
$this->resolver->setIterator($this->mockIterator);
$this->mockIterator->shouldReceive('getRootDefinition->getBasepath')
->andReturn(__DIR__);
$this->mockIterator->shouldReceive('get')
->with(Mockery::type('string'), Mockery::type(Definition::class))
->andReturnUsing(function (string $key, Definition $definition) {
return $definition->get($key);
});
}
public function dataProviderFor404(): array
{
return [
'Missing File' => ['directory' => './_data', 'filename' => '/not-real-file.txt'],
'Missing Directory' => ['directory' => './not-real', 'filename' => '/sample.txt'],
'Not Directory' => ['directory' => basename(__FILE__), 'filename' => '/sample.txt'],
'Outside Directory' => ['directory' => '../_data', 'filename' => '/sample.txt'],
'File is Root' => ['directory' => './_data', 'filename' => '/'],
'File is Directory' => ['directory' => './_data', 'filename' => '/test'],
];
}
public function testIndicator(): void
{
verify($this->resolver->getIndicator())->is()->sameAs('directory');
}
public function testIsShorthand(): void
{
verify($this->resolver->isShortHand('anything'))->is()->false();
}
public function testIsValid(): void
{
$validDefinition = new Definition(['directory' => '_data']);
$missingDirectory = new Definition(['somekey' => 'somevalue']);
$notADirectory = new Definition(['directory' => basename(__FILE__)]);
$doesNotExist = new Definition(['directory' => 'not_real']);
$validDefinition->setBasepath(__DIR__);
$missingDirectory->setBasepath(__DIR__);
$notADirectory->setBasepath(__DIR__);
$doesNotExist->setBasepath(__DIR__);
verify($this->resolver->isValid($validDefinition))->is()->true();
verify($this->resolver->isValid($missingDirectory))->is()->false();
verify($this->resolver->isValid($notADirectory))->is()->false();
verify($this->resolver->isValid($doesNotExist))->is()->false();
}
public function testResolve(): void
{
$definition = new Definition(['directory' => './_data']);
$this->mockIterator->shouldReceive('get')
->with('request.url.pathname')
->andReturn('/sample.txt');
$result = $this->resolver->resolve($definition);
verify($result)->is()->instanceOf(Response::class);
verify($result->getStatusCode())->is()->sameAs(200);
verify($result->getHeaders()->get('Content-Type')->getFieldValue())->is()->sameAs('text/plain');
verify($result->getHeaders()->get('Cache-Control')->getFieldValue())->is()->sameAs('max-age=31557600');
verify($result->getBody())->is()->equalToFile(__DIR__ . '/_data/sample.txt');
}
/**
* @dataProvider dataProviderFor404
*/
public function testResolve404(string $directory, string $filename): void
{
$definition = new Definition(compact('directory'));
$this->mockIterator->shouldReceive('get')
->with('request.url.pathname')
->andReturn($filename);
$result = $this->resolver->resolve($definition);
verify($result)->is()->instanceOf(Response::class);
verify($result->getStatusCode())->is()->sameAs(404);
verify($result->getBody())->is()->empty();
}
public function testResolveException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$definition must be an instance of Magento\\Upward\\Definition');
$this->resolver->resolve('_data');
}
}