/
DumpCommandTest.php
121 lines (93 loc) · 5.81 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
<?php
/*
* 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 PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
class DumpCommandTest extends TestCase
{
protected $extractor;
protected $router;
private $serializer;
public function setUp(): void
{
$this->extractor = $this->getMockBuilder('FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor')
->disableOriginalConstructor()
->getMock();
$this->router = $this->getMockBuilder('Symfony\Component\Routing\Router')
->disableOriginalConstructor()
->getMock();
$this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\SerializerInterface')
->disableOriginalConstructor()
->getMock();
}
public function testExecute()
{
$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->extractor, $this->serializer, '/root/dir');
$tester = new CommandTester($command);
$tester->execute(array('--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()
{
$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->extractor, $this->serializer, '/root/dir');
$tester = new CommandTester($command);
$tester->execute(array(
'--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()
{
$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->extractor, $this->serializer, '/root/dir');
$tester = new CommandTester($command);
$tester->execute(array(
'--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()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Unable to create directory /root/dir/web/js');
$command = new DumpCommand($this->extractor, $this->serializer, '/root/dir');
$tester = new CommandTester($command);
$tester->execute(array());
}
public function testExecuteUnableToWriteFile()
{
$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->extractor, $this->serializer, '/root/dir');
$tester = new CommandTester($command);
$tester->execute(array('--target' => '/tmp'));
}
}