-
Notifications
You must be signed in to change notification settings - Fork 259
/
DumpCommandTest.php
158 lines (127 loc) · 6.56 KB
/
DumpCommandTest.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
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
<?php
declare(strict_types=1);
/*
* This file is part of the FOSJsRoutingBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\JsRoutingBundle\Tests\Command;
use FOS\JsRoutingBundle\Command\DumpCommand;
use FOS\JsRoutingBundle\Response\RoutesResponse;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Router;
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor;
class DumpCommandTest extends TestCase
{
protected RoutesResponse $routesResponse;
protected $extractor;
protected $router;
private $serializer;
public function setUp(): void
{
$this->routesResponse = $this->getMockBuilder(RoutesResponse::class)
->disableOriginalConstructor()
->getMock();
$this->extractor = $this->getMockBuilder(ExposedRoutesExtractor::class)
->disableOriginalConstructor()
->getMock();
$this->router = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->getMock();
$this->serializer = $this->getMockBuilder(SerializerInterface::class)
->disableOriginalConstructor()
->getMock();
}
public function testExecute(): void
{
$this->serializer->expects($this->once())
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);
$tester = new CommandTester($command);
$tester->execute(['--target' => '/tmp/dump-command-test']);
$this->assertStringContainsString('Dumping exposed routes.', $tester->getDisplay());
$this->assertStringContainsString('[file+] /tmp/dump-command-test', $tester->getDisplay());
$this->assertEquals('fos.Router.setData({"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""});', file_get_contents('/tmp/dump-command-test'));
}
public function testExecuteCallbackOption(): void
{
$this->serializer->expects($this->once())
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);
$tester = new CommandTester($command);
$tester->execute([
'--target' => '/tmp/dump-command-test',
'--callback' => 'test',
]);
$this->assertStringContainsString('Dumping exposed routes.', $tester->getDisplay());
$this->assertStringContainsString('[file+] /tmp/dump-command-test', $tester->getDisplay());
$this->assertEquals('test({"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""});', file_get_contents('/tmp/dump-command-test'));
}
public function testExecuteFormatOption(): void
{
$json = '{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}';
$this->serializer->expects($this->once())
->method('serialize')
->will($this->returnValue($json));
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);
$tester = new CommandTester($command);
$tester->execute([
'--target' => '/tmp/dump-command-test',
'--format' => 'json',
]);
$this->assertStringContainsString('Dumping exposed routes.', $tester->getDisplay());
$this->assertStringContainsString('[file+] /tmp/dump-command-test', $tester->getDisplay());
$this->assertEquals($json, file_get_contents('/tmp/dump-command-test'));
}
public function testExecuteUnableToCreateDirectory(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to create directory /root/dir/public/js');
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testExecuteUnableToWriteFile(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to write file /tmp');
$this->serializer->expects($this->once())
->method('serialize')
->will($this->returnValue('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]]}},"prefix":"","host":"","scheme":""}'));
$command = new DumpCommand(
$this->routesResponse,
$this->extractor,
$this->serializer,
'/root/dir',
);
$tester = new CommandTester($command);
$tester->execute(['--target' => '/tmp']);
}
}