-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDocTest.php
More file actions
177 lines (152 loc) · 7.96 KB
/
Copy pathDocTest.php
File metadata and controls
177 lines (152 loc) · 7.96 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Tests\UniformResourceLocator;
use PHPUnit\Framework\TestCase;
use UserFrosting\UniformResourceLocator\Normalizer;
use UserFrosting\UniformResourceLocator\ResourceInterface;
use UserFrosting\UniformResourceLocator\ResourceLocation;
use UserFrosting\UniformResourceLocator\ResourceLocationInterface;
use UserFrosting\UniformResourceLocator\ResourceLocator;
use UserFrosting\UniformResourceLocator\ResourceStream;
use UserFrosting\UniformResourceLocator\ResourceStreamInterface;
/**
* Tests for the example code in the docs/Readme.md.
*/
class DocTest extends TestCase
{
/**
* Setup the shared locator.
*/
public function testDocExample(): void
{
// Create Locator
$locator = new ResourceLocator(__DIR__.'/app/');
// Register Locations
$locator->addLocation(new ResourceLocation('Floor1', 'floors/Floor1/'));
$locator->addLocation(new ResourceLocation('Floor2', 'floors/Floor2/'));
// Register Streams
$locator->addStream(new ResourceStream('config'));
$locator->addStream(new ResourceStream('upload', 'uploads/', true));
// Finding Files
$defaultResource = $locator->getResource('config://default.json');
$this->assertEquals($this->getBasePath().'app/floors/Floor2/config/default.json', $defaultResource);
$this->assertInstanceOf(ResourceInterface::class, $defaultResource);
$this->assertSame($this->getBasePath().'app/floors/Floor2/config/default.json', $defaultResource->getAbsolutePath());
$this->assertSame('floors/Floor2/config/default.json', $defaultResource->getPath());
$this->assertSame('default.json', $defaultResource->getBasePath());
$this->assertSame('default.json', $defaultResource->getBasename());
$this->assertSame('json', $defaultResource->getExtension());
$this->assertSame('default', $defaultResource->getFilename());
$this->assertSame('config://default.json', $defaultResource->getUri());
// GetLocation
$defaultResourceLocation = $defaultResource->getLocation();
$this->assertInstanceOf(ResourceLocationInterface::class, $defaultResourceLocation);
$this->assertSame('Floor2', $defaultResourceLocation->getName());
$this->assertSame('floors/Floor2/', $defaultResourceLocation->getPath());
// GetStream
$defaultResourceStream = $defaultResource->getStream();
$this->assertInstanceOf(ResourceStreamInterface::class, $defaultResourceStream); // @phpstan-ignore-line
$this->assertSame('config/', $defaultResourceStream->getPath());
$this->assertSame('config', $defaultResourceStream->getScheme());
$this->assertSame(false, $defaultResourceStream->isShared());
$this->assertSame(false, $defaultResourceStream->isReadonly());
// GetResources
$defaults = $locator->getResources('config://default.json');
$this->assertContainsOnlyInstancesOf(ResourceInterface::class, $defaults);
$this->assertSame([
$this->getBasePath().'app/floors/Floor2/config/default.json',
$this->getBasePath().'app/floors/Floor1/config/default.json',
], array_map('strval', $defaults)); // N.B.: array_map is not required if `assertEquals` is used. But we do as the doc.
// Finding Files - upload://profile
// GetResource
$uploadResource = $locator->getResource('upload://profile/');
$this->assertInstanceOf(ResourceInterface::class, $uploadResource);
$this->assertEquals($this->getBasePath().'app/uploads/profile', $uploadResource);
$this->assertSame($this->getBasePath().'app/uploads/profile', $uploadResource->getAbsolutePath());
$this->assertSame('uploads/profile', $uploadResource->getPath());
$this->assertSame('profile', $uploadResource->getBasePath());
$this->assertSame('profile', $uploadResource->getBasename());
$this->assertSame('', $uploadResource->getExtension());
$this->assertSame('profile', $uploadResource->getFilename());
$this->assertSame('upload://profile', $uploadResource->getUri());
// Side note, `getPath` doesn't add a `/` at the end, because NormalizePath normalize this behavior.
// In any case, `upload://profile` and `upload://profile/` are equivalent.
$this->assertSame('uploads/profile', $locator->getResource('upload://profile/')?->getPath());
// GetResources
$defaults = $locator->getResources('upload://profile');
$this->assertSame([
$this->getBasePath().'app/uploads/profile',
], array_map('strval', $defaults)); // N.B.: array_map is not required if `assertEquals` is used. But we do as the doc.
// ListResources
$list = $locator->listResources('config://');
$this->assertEquals([
$this->getBasePath().'app/floors/Floor1/config/debug.json',
$this->getBasePath().'app/floors/Floor2/config/default.json',
$this->getBasePath().'app/floors/Floor2/config/foo/bar.json',
$this->getBasePath().'app/floors/Floor2/config/production.json',
], $list);
// ListResources - All
$list = $locator->listResources('config://', all: true);
$this->assertEquals([
$this->getBasePath().'app/floors/Floor1/config/debug.json',
$this->getBasePath().'app/floors/Floor1/config/default.json',
$this->getBasePath().'app/floors/Floor2/config/default.json',
$this->getBasePath().'app/floors/Floor2/config/foo/bar.json',
$this->getBasePath().'app/floors/Floor2/config/production.json',
], $list);
// ListResources - Sort
$list = $locator->listResources('config://', sort: false);
$this->assertEquals([
$this->getBasePath().'app/floors/Floor2/config/default.json',
$this->getBasePath().'app/floors/Floor2/config/foo/bar.json',
$this->getBasePath().'app/floors/Floor2/config/production.json',
$this->getBasePath().'app/floors/Floor1/config/debug.json',
], $list);
// ListResources - Folder
$list = $locator->listResources('config://foo/', all: true);
$this->assertEquals([
$this->getBasePath().'app/floors/Floor2/config/foo/bar.json',
], $list);
// GetStreams
$streams = $locator->getStreams();
$this->assertCount(2, $streams);
$this->assertContainsOnlyInstancesOf(ResourceStreamInterface::class, $streams['config']);
$this->assertContainsOnlyInstancesOf(ResourceStreamInterface::class, $streams['upload']);
// listSchemes
$streams = $locator->listSchemes();
$this->assertSame([
'config',
'upload',
], $streams);
// getStream
$streams = $locator->getStream('upload');
$this->assertContainsOnlyInstancesOf(ResourceStreamInterface::class, $streams);
$this->assertCount(1, $streams);
$this->assertSame('uploads/', $streams[0]->getPath());
// GetLocations
$locations = $locator->getLocations();
$this->assertContainsOnlyInstancesOf(ResourceLocationInterface::class, $locations);
$this->assertCount(2, $locations);
$this->assertSame('floors/Floor2/', $locations['Floor2']->getPath());
// listLocations
$locations = $locator->listLocations();
$this->assertSame([
'Floor2',
'Floor1',
], $locations);
// GetLocation
$location = $locator->getLocation('Floor1');
$this->assertSame('floors/Floor1/', $location->getPath());
}
protected function getBasePath(): string
{
return Normalizer::normalizePath(__DIR__);
}
}