Skip to content

Commit 6c9b96a

Browse files
committed
Move RoutesShell test to use the integration test case.
This new test case class lets us use less code to test shell commands.
1 parent 11b109a commit 6c9b96a

File tree

1 file changed

+82
-95
lines changed

1 file changed

+82
-95
lines changed

tests/TestCase/Shell/RoutesShellTest.php

Lines changed: 82 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
use Cake\Routing\Router;
1818
use Cake\Shell\RoutesShell;
19-
use Cake\TestSuite\TestCase;
19+
use Cake\TestSuite\ConsoleIntegrationTestCase;
2020

2121
/**
2222
* RoutesShellTest
2323
*/
24-
class RoutesShellTest extends TestCase
24+
class RoutesShellTest extends ConsoleIntegrationTestCase
2525
{
2626

2727
/**
@@ -32,18 +32,6 @@ class RoutesShellTest extends TestCase
3232
public function setUp()
3333
{
3434
parent::setUp();
35-
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
36-
->setMethods(['helper', 'out', 'err'])
37-
->getMock();
38-
$this->table = $this->getMockBuilder('Cake\Shell\Helper\TableHelper')
39-
->setConstructorArgs([$this->io])
40-
->getMock();
41-
$this->io->expects($this->any())
42-
->method('helper')
43-
->with('table')
44-
->will($this->returnValue($this->table));
45-
46-
$this->shell = new RoutesShell($this->io);
4735
Router::connect('/articles/:action/*', ['controller' => 'Articles']);
4836
Router::connect('/bake/:controller/:action', ['plugin' => 'Bake']);
4937
Router::connect('/tests/:action/*', ['controller' => 'Tests'], ['_name' => 'testName']);
@@ -58,7 +46,22 @@ public function tearDown()
5846
{
5947
parent::tearDown();
6048
Router::reload();
61-
unset($this->io, $this->shell);
49+
}
50+
51+
/**
52+
* Check that a row of cells exists in the output.
53+
*
54+
* @param array $row The row of cells to check
55+
* @return void
56+
*/
57+
protected function assertOutputContainsRow(array $row)
58+
{
59+
$row = array_map(function ($cell) {
60+
return preg_quote($cell, '/');
61+
}, $row);
62+
$cells = implode('\s+\|\s+', $row);
63+
$pattern = '/' . $cells . '/';
64+
$this->assertOutputRegexp($pattern);
6265
}
6366

6467
/**
@@ -68,29 +71,27 @@ public function tearDown()
6871
*/
6972
public function testMain()
7073
{
71-
$this->table->expects($this->once())
72-
->method('output')
73-
->with(
74-
$this->logicalAnd(
75-
$this->contains(['Route name', 'URI template', 'Defaults']),
76-
$this->contains([
77-
'articles:_action',
78-
'/articles/:action/*',
79-
'{"controller":"Articles","action":"index","plugin":null}'
80-
]),
81-
$this->contains([
82-
'bake._controller:_action',
83-
'/bake/:controller/:action',
84-
'{"plugin":"Bake","action":"index"}',
85-
]),
86-
$this->contains([
87-
'testName',
88-
'/tests/:action/*',
89-
'{"controller":"Tests","action":"index","plugin":null}'
90-
])
91-
)
92-
);
93-
$this->shell->main();
74+
$this->exec('routes');
75+
$this->assertOutputContainsRow([
76+
'<info>Route name</info>',
77+
'<info>URI template</info>',
78+
'<info>Defaults</info>'
79+
]);
80+
$this->assertOutputContainsRow([
81+
'articles:_action',
82+
'/articles/:action/*',
83+
'{"controller":"Articles","action":"index","plugin":null}'
84+
]);
85+
$this->assertOutputContainsRow([
86+
'bake._controller:_action',
87+
'/bake/:controller/:action',
88+
'{"plugin":"Bake","action":"index"}'
89+
]);
90+
$this->assertOutputContainsRow([
91+
'testName',
92+
'/tests/:action/*',
93+
'{"controller":"Tests","action":"index","plugin":null}'
94+
]);
9495
}
9596

9697
/**
@@ -100,19 +101,17 @@ public function testMain()
100101
*/
101102
public function testCheck()
102103
{
103-
$this->table->expects($this->once())
104-
->method('output')
105-
->with(
106-
$this->logicalAnd(
107-
$this->contains(['Route name', 'URI template', 'Defaults']),
108-
$this->contains([
109-
'articles:_action',
110-
'/articles/index',
111-
'{"action":"index","pass":[],"controller":"Articles","plugin":null}'
112-
])
113-
)
114-
);
115-
$this->shell->check('/articles/index');
104+
$this->exec('routes check /articles/check');
105+
$this->assertOutputContainsRow([
106+
'<info>Route name</info>',
107+
'<info>URI template</info>',
108+
'<info>Defaults</info>'
109+
]);
110+
$this->assertOutputContainsRow([
111+
'articles:_action',
112+
'/articles/check',
113+
'{"action":"check","pass":[],"controller":"Articles","plugin":null}'
114+
]);
116115
}
117116

118117
/**
@@ -122,19 +121,17 @@ public function testCheck()
122121
*/
123122
public function testCheckWithNamedRoute()
124123
{
125-
$this->table->expects($this->once())
126-
->method('output')
127-
->with(
128-
$this->logicalAnd(
129-
$this->contains(['Route name', 'URI template', 'Defaults']),
130-
$this->contains([
131-
'testName',
132-
'/tests/index',
133-
'{"action":"index","pass":[],"controller":"Tests","plugin":null}'
134-
])
135-
)
136-
);
137-
$this->shell->check('/tests/index');
124+
$this->exec('routes check /tests/index');
125+
$this->assertOutputContainsRow([
126+
'<info>Route name</info>',
127+
'<info>URI template</info>',
128+
'<info>Defaults</info>'
129+
]);
130+
$this->assertOutputContainsRow([
131+
'testName',
132+
'/tests/index',
133+
'{"action":"index","pass":[],"controller":"Tests","plugin":null}'
134+
]);
138135
}
139136

140137
/**
@@ -144,33 +141,32 @@ public function testCheckWithNamedRoute()
144141
*/
145142
public function testCheckNotFound()
146143
{
147-
$this->io->expects($this->at(0))
148-
->method('err')
149-
->with($this->stringContains('did not match'));
150-
$this->shell->check('/nope');
144+
$this->exec('routes check /nope');
145+
$this->assertErrorContains('did not match');
151146
}
152147

153148
/**
154149
* Test generating URLs
155150
*
156151
* @return void
157152
*/
158-
public function testGenerate()
153+
public function testGenerateNoPassArgs()
159154
{
160-
$this->io->expects($this->never())
161-
->method('err');
162-
$this->io->expects($this->at(0))
163-
->method('out')
164-
->with($this->stringContains('> /articles/index'));
165-
$this->io->expects($this->at(2))
166-
->method('out')
167-
->with($this->stringContains('> /articles/view/2/3'));
168-
169-
$this->shell->args = ['controller:Articles', 'action:index'];
170-
$this->shell->generate();
155+
$this->exec('routes generate controller:Articles action:index');
156+
$this->assertOutputContains('> /articles/index');
157+
$this->assertErrorEmpty();
158+
}
171159

172-
$this->shell->args = ['controller:Articles', 'action:view', '2', '3'];
173-
$this->shell->generate();
160+
/**
161+
* Test generating URLs with passed arguments
162+
*
163+
* @return void
164+
*/
165+
public function testGeneratePassedArguments()
166+
{
167+
$this->exec('routes generate controller:Articles action:view 2 3');
168+
$this->assertOutputContains('> /articles/view/2/3');
169+
$this->assertErrorEmpty();
174170
}
175171

176172
/**
@@ -180,14 +176,8 @@ public function testGenerate()
180176
*/
181177
public function testGenerateBoolParams()
182178
{
183-
$this->io->expects($this->never())
184-
->method('err');
185-
$this->io->expects($this->at(0))
186-
->method('out')
187-
->with($this->stringContains('> https://example.com/articles/index'));
188-
189-
$this->shell->args = ['_ssl:true', '_host:example.com', 'controller:Articles', 'action:index'];
190-
$this->shell->generate();
179+
$this->exec('routes generate controller:Articles action:index _ssl:true _host:example.com');
180+
$this->assertOutputContains('> https://example.com/articles/index');
191181
}
192182

193183
/**
@@ -197,10 +187,7 @@ public function testGenerateBoolParams()
197187
*/
198188
public function testGenerateMissing()
199189
{
200-
$this->io->expects($this->at(0))
201-
->method('err')
202-
->with($this->stringContains('do not match'));
203-
$this->shell->args = ['controller:Derp'];
204-
$this->shell->generate();
190+
$this->exec('routes generate controller:Derp');
191+
$this->assertErrorContains('do not match');
205192
}
206193
}

0 commit comments

Comments
 (0)