This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocTest.php
More file actions
157 lines (132 loc) · 6.49 KB
/
DocTest.php
File metadata and controls
157 lines (132 loc) · 6.49 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
<?php
/*
* UserFrosting Uniform Resource Locator (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/UniformResourceLocator
* @copyright Copyright (c) 2013-2019 Alexander Weissman, Louis Charette
* @license https://github.com/userfrosting/UniformResourceLocator/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\UniformResourceLocator\Tests;
use PHPUnit\Framework\TestCase;
use UserFrosting\UniformResourceLocator\Normalizer;
use UserFrosting\UniformResourceLocator\ResourceInterface;
use UserFrosting\UniformResourceLocator\ResourceLocationInterface;
use UserFrosting\UniformResourceLocator\ResourceLocator;
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->registerLocation('Floor1', 'floors/Floor1/');
$locator->registerLocation('Floor2', 'floors/Floor2/');
// Register Streams
$locator->registerStream('config');
$locator->registerStream('upload', '', 'uploads/', true);
// Finding Files
// 1) Find Resource
$default = $locator->findResource('config://default.json');
$this->assertSame($this->getBasePath().'app/floors/Floor2/config/default.json', $default);
// 2) getRerouce
$defaultResource = $locator->getResource('config://default.json');
$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());
// 3) GetLocation
$defaultResourceLocation = $defaultResource->getLocation();
$this->assertInstanceOf(ResourceLocationInterface::class, $defaultResourceLocation);
$this->assertSame('Floor2', $defaultResourceLocation->getName());
$this->assertSame('floors/Floor2/', $defaultResourceLocation->getPath());
// 4) GetStream
$defaultResourceStream = $defaultResource->getStream();
$this->assertInstanceOf(ResourceStreamInterface::class, $defaultResourceStream);
$this->assertSame('config/', $defaultResourceStream->getPath());
$this->assertSame('', $defaultResourceStream->getPrefix());
$this->assertSame('config', $defaultResourceStream->getScheme());
$this->assertSame(false, $defaultResourceStream->isShared());
// 5) FindResources
$defaults = $locator->findResources('config://default.json');
$this->assertSame([
$this->getBasePath().'app/floors/Floor2/config/default.json',
$this->getBasePath().'app/floors/Floor1/config/default.json',
], $defaults);
// Finding Files - upload://profile
// 1) Find Resource
$upload = $locator->findResource('upload://profile');
$this->assertSame($this->getBasePath().'app/uploads/profile', $upload);
// 2) getRerouce
$uploadResource = $locator->getResource('upload://profile');
$this->assertInstanceOf(ResourceInterface::class, $uploadResource);
$this->assertSame($this->getBasePath().'app/uploads/profile', $uploadResource->getAbsolutePath());
$this->assertSame('uploads/profile', $uploadResource->getPath()); // N.B.: No `/` at the end, because we didn't ask for it in `findResource`
$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());
// 3) FindResources
$defaults = $locator->findResources('upload://profile');
$this->assertSame([
$this->getBasePath().'app/uploads/profile',
], $defaults);
// 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://', 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://', false, 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);
// ListReources - Folder
$list = $locator->listResources('config://foo/', true);
$this->assertEquals([
$this->getBasePath().'app/floors/Floor2/config/foo/bar.json',
], $list);
// listStreams
$streams = $locator->listStreams();
$this->assertSame([
'config',
'upload',
], $streams);
// listLocations
$locations = $locator->listLocations();
$this->assertSame([
'Floor2',
'Floor1',
], $locations);
}
protected function getBasePath(): string
{
return Normalizer::normalizePath(__DIR__);
}
}