16
16
17
17
use Cake \Routing \Router ;
18
18
use Cake \Shell \RoutesShell ;
19
- use Cake \TestSuite \TestCase ;
19
+ use Cake \TestSuite \ConsoleIntegrationTestCase ;
20
20
21
21
/**
22
22
* RoutesShellTest
23
23
*/
24
- class RoutesShellTest extends TestCase
24
+ class RoutesShellTest extends ConsoleIntegrationTestCase
25
25
{
26
26
27
27
/**
@@ -32,18 +32,6 @@ class RoutesShellTest extends TestCase
32
32
public function setUp ()
33
33
{
34
34
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 );
47
35
Router::connect ('/articles/:action/* ' , ['controller ' => 'Articles ' ]);
48
36
Router::connect ('/bake/:controller/:action ' , ['plugin ' => 'Bake ' ]);
49
37
Router::connect ('/tests/:action/* ' , ['controller ' => 'Tests ' ], ['_name ' => 'testName ' ]);
@@ -58,7 +46,22 @@ public function tearDown()
58
46
{
59
47
parent ::tearDown ();
60
48
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 );
62
65
}
63
66
64
67
/**
@@ -68,29 +71,27 @@ public function tearDown()
68
71
*/
69
72
public function testMain ()
70
73
{
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
+ ]);
94
95
}
95
96
96
97
/**
@@ -100,19 +101,17 @@ public function testMain()
100
101
*/
101
102
public function testCheck ()
102
103
{
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
+ ]);
116
115
}
117
116
118
117
/**
@@ -122,19 +121,17 @@ public function testCheck()
122
121
*/
123
122
public function testCheckWithNamedRoute ()
124
123
{
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
+ ]);
138
135
}
139
136
140
137
/**
@@ -144,33 +141,32 @@ public function testCheckWithNamedRoute()
144
141
*/
145
142
public function testCheckNotFound ()
146
143
{
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 ' );
151
146
}
152
147
153
148
/**
154
149
* Test generating URLs
155
150
*
156
151
* @return void
157
152
*/
158
- public function testGenerate ()
153
+ public function testGenerateNoPassArgs ()
159
154
{
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
+ }
171
159
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 ();
174
170
}
175
171
176
172
/**
@@ -180,14 +176,8 @@ public function testGenerate()
180
176
*/
181
177
public function testGenerateBoolParams ()
182
178
{
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 ' );
191
181
}
192
182
193
183
/**
@@ -197,10 +187,7 @@ public function testGenerateBoolParams()
197
187
*/
198
188
public function testGenerateMissing ()
199
189
{
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 ' );
205
192
}
206
193
}
0 commit comments