-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRoutesDelegatorTest.php
More file actions
243 lines (205 loc) · 8.87 KB
/
Copy pathRoutesDelegatorTest.php
File metadata and controls
243 lines (205 loc) · 8.87 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare(strict_types=1);
namespace DotTest\Maker\Type;
use Dot\Maker\ColorEnum;
use Dot\Maker\Config;
use Dot\Maker\Context;
use Dot\Maker\FileSystem;
use Dot\Maker\IO\Output;
use Dot\Maker\Type\RoutesDelegator;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use function fclose;
use function fopen;
use function rewind;
use function sprintf;
use function stream_get_contents;
use const PHP_EOL;
class RoutesDelegatorTest extends TestCase
{
private string $moduleName = 'ModuleName';
private string $resourceName = 'BookStore';
/**
* @dataProvider dataProvider
*/
public function testCallToCreateWhenProjectTypeIsNotApiWillCreateFile(string $expected): void
{
$stream = fopen('php://memory', 'w+');
Output::setOutputStream($stream);
$root = vfsStream::setup('root', 0644, [
'composer.json' => '{
"autoload": {
"psr-4": {
"Admin\\\\App\\\\": "src/App/src/",
"Core\\\\App\\\\": "src/Core/src/App/src/"
}
}
}',
]);
$config = new Config($root->url());
$context = new Context($root->url());
$fileSystem = (new FileSystem($context))->setModuleName($this->moduleName);
$fileSystem->getCreateResourceHandler($this->resourceName)->create('...');
$fileSystem->postCreateResourceHandler($this->resourceName)->create('...');
$fileSystem->getDeleteResourceHandler($this->resourceName)->create('...');
$fileSystem->postDeleteResourceHandler($this->resourceName)->create('...');
$fileSystem->getEditResourceHandler($this->resourceName)->create('...');
$fileSystem->postEditResourceHandler($this->resourceName)->create('...');
$fileSystem->getListResourcesHandler($this->resourceName)->create('...');
$fileSystem->getViewResourceHandler($this->resourceName)->create('...');
$routesDelegator = new RoutesDelegator($fileSystem, $context, $config);
$routesDelegator = $routesDelegator->create($this->resourceName);
rewind($stream);
$this->assertSame(
ColorEnum::colorize(
sprintf('Created RoutesDelegator: %s', $routesDelegator->getPath()),
ColorEnum::ForegroundBrightGreen
) . PHP_EOL,
stream_get_contents($stream)
);
$this->assertSame($expected, $routesDelegator->read());
fclose($stream);
}
/**
* @dataProvider dataProviderApi
*/
public function testCallToCreateWhenProjectTypeIsApiWillCreateFile(string $expected): void
{
$stream = fopen('php://memory', 'w+');
Output::setOutputStream($stream);
$root = vfsStream::setup('root', 0644, [
'composer.json' => '{
"autoload": {
"psr-4": {
"Api\\\\App\\\\": "src/App/src/",
"Core\\\\App\\\\": "src/Core/src/App/src/"
}
}
}',
]);
$config = new Config($root->url());
$context = new Context($root->url());
$fileSystem = (new FileSystem($context))->setModuleName($this->moduleName);
$fileSystem->entity($this->resourceName)->create('...');
$fileSystem->apiDeleteResourceHandler($this->resourceName)->create('...');
$fileSystem->apiGetResourceHandler($this->resourceName)->create('...');
$fileSystem->apiGetCollectionHandler($this->resourceName)->create('...');
$fileSystem->apiPatchResourceHandler($this->resourceName)->create('...');
$fileSystem->apiPostResourceHandler($this->resourceName)->create('...');
$fileSystem->apiPutResourceHandler($this->resourceName)->create('...');
$routesDelegator = new RoutesDelegator($fileSystem, $context, $config);
$routesDelegator = $routesDelegator->create($this->resourceName);
rewind($stream);
$this->assertSame(
ColorEnum::colorize(
sprintf('Created RoutesDelegator: %s', $routesDelegator->getPath()),
ColorEnum::ForegroundBrightGreen
) . PHP_EOL,
stream_get_contents($stream)
);
$this->assertSame($expected, $routesDelegator->read());
fclose($stream);
}
public static function dataProvider(): array
{
// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<BODY
<?php
declare(strict_types=1);
namespace Admin\ModuleName;
use Admin\ModuleName\Handler\BookStore\GetCreateBookStoreFormHandler;
use Admin\ModuleName\Handler\BookStore\GetDeleteBookStoreFormHandler;
use Admin\ModuleName\Handler\BookStore\GetEditBookStoreFormHandler;
use Admin\ModuleName\Handler\BookStore\GetListBookStoreHandler;
use Admin\ModuleName\Handler\BookStore\GetViewBookStoreHandler;
use Admin\ModuleName\Handler\BookStore\PostCreateBookStoreHandler;
use Admin\ModuleName\Handler\BookStore\PostDeleteBookStoreHandler;
use Admin\ModuleName\Handler\BookStore\PostEditBookStoreHandler;
use Core\App\ConfigProvider;
use Dot\Router\RouteCollectorInterface;
use Mezzio\Application;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class RoutesDelegator
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(
ContainerInterface \$container,
string \$serviceName,
callable \$callback,
): Application {
\$uuid = ConfigProvider::REGEXP_UUID;
/** @var RouteCollectorInterface \$routeCollector */
\$routeCollector = \$container->get(RouteCollectorInterface::class);
\$routeCollector
->get('/create-book-store', GetCreateBookStoreFormHandler::class, 'book-store::create-book-store-form')
->post('/create-book-store', PostCreateBookStoreHandler::class, 'book-store::create-book-store')
->get('/delete-book-store/' . \$uuid, GetDeleteBookStoreFormHandler::class, 'book-store::delete-book-store-form')
->post('/delete-book-store/' . \$uuid, PostDeleteBookStoreHandler::class, 'book-store::delete-book-store')
->get('/edit-book-store/' . \$uuid, GetEditBookStoreFormHandler::class, 'book-store::edit-book-store-form')
->post('/edit-book-store/' . \$uuid, PostEditBookStoreHandler::class, 'book-store::edit-book-store')
->get('/list-book-store', GetListBookStoreHandler::class, 'book-store::list-book-store')
->get('/view-book-store/' . \$uuid, GetViewBookStoreHandler::class, 'book-store::view-book-store-form');
return \$callback();
}
}
BODY;
// phpcs:enable Generic.Files.LineLength.TooLong
return [
[$expected],
];
}
public static function dataProviderApi(): array
{
// phpcs:disable Generic.Files.LineLength.TooLong
$expected = <<<BODY
<?php
declare(strict_types=1);
namespace Api\ModuleName;
use Api\ModuleName\Handler\BookStore\DeleteBookStoreResourceHandler;
use Api\ModuleName\Handler\BookStore\GetBookStoreCollectionHandler;
use Api\ModuleName\Handler\BookStore\GetBookStoreResourceHandler;
use Api\ModuleName\Handler\BookStore\PatchBookStoreResourceHandler;
use Api\ModuleName\Handler\BookStore\PostBookStoreResourceHandler;
use Api\ModuleName\Handler\BookStore\PutBookStoreResourceHandler;
use Core\App\ConfigProvider;
use Dot\Router\RouteCollectorInterface;
use Mezzio\Application;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
class RoutesDelegator
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(
ContainerInterface \$container,
string \$serviceName,
callable \$callback,
): Application {
\$uuid = ConfigProvider::REGEXP_UUID;
/** @var RouteCollectorInterface \$routeCollector */
\$routeCollector = \$container->get(RouteCollectorInterface::class);
\$routeCollector
->delete('/book-store/' . \$uuid, DeleteBookStoreResourceHandler::class, 'book-store::delete-book-store')
->get('/book-store/' . \$uuid, GetBookStoreResourceHandler::class, 'book-store::view-book-store')
->get('/book-store', GetBookStoreCollectionHandler::class, 'book-store::list-book-store')
->patch('/book-store/' . \$uuid, PatchBookStoreResourceHandler::class, 'book-store::update-book-store')
->post('/book-store', PostBookStoreResourceHandler::class, 'book-store::create-book-store')
->put('/book-store/' . \$uuid, PutBookStoreResourceHandler::class, 'book-store::replace-book-store');
return \$callback();
}
}
BODY;
// phpcs:enable Generic.Files.LineLength.TooLong
return [
[$expected],
];
}
}