Skip to content

Commit

Permalink
Move RoutesShell test to use the integration test case.
Browse files Browse the repository at this point in the history
This new test case class lets us use less code to test shell commands.
  • Loading branch information
markstory committed Aug 25, 2017
1 parent 11b109a commit 6c9b96a
Showing 1 changed file with 82 additions and 95 deletions.
177 changes: 82 additions & 95 deletions tests/TestCase/Shell/RoutesShellTest.php
Expand Up @@ -16,12 +16,12 @@

use Cake\Routing\Router;
use Cake\Shell\RoutesShell;
use Cake\TestSuite\TestCase;
use Cake\TestSuite\ConsoleIntegrationTestCase;

/**
* RoutesShellTest
*/
class RoutesShellTest extends TestCase
class RoutesShellTest extends ConsoleIntegrationTestCase
{

/**
Expand All @@ -32,18 +32,6 @@ class RoutesShellTest extends TestCase
public function setUp()
{
parent::setUp();
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')
->setMethods(['helper', 'out', 'err'])
->getMock();
$this->table = $this->getMockBuilder('Cake\Shell\Helper\TableHelper')
->setConstructorArgs([$this->io])
->getMock();
$this->io->expects($this->any())
->method('helper')
->with('table')
->will($this->returnValue($this->table));

$this->shell = new RoutesShell($this->io);
Router::connect('/articles/:action/*', ['controller' => 'Articles']);
Router::connect('/bake/:controller/:action', ['plugin' => 'Bake']);
Router::connect('/tests/:action/*', ['controller' => 'Tests'], ['_name' => 'testName']);
Expand All @@ -58,7 +46,22 @@ public function tearDown()
{
parent::tearDown();
Router::reload();
unset($this->io, $this->shell);
}

/**
* Check that a row of cells exists in the output.
*
* @param array $row The row of cells to check
* @return void
*/
protected function assertOutputContainsRow(array $row)
{
$row = array_map(function ($cell) {
return preg_quote($cell, '/');
}, $row);
$cells = implode('\s+\|\s+', $row);
$pattern = '/' . $cells . '/';
$this->assertOutputRegexp($pattern);
}

/**
Expand All @@ -68,29 +71,27 @@ public function tearDown()
*/
public function testMain()
{
$this->table->expects($this->once())
->method('output')
->with(
$this->logicalAnd(
$this->contains(['Route name', 'URI template', 'Defaults']),
$this->contains([
'articles:_action',
'/articles/:action/*',
'{"controller":"Articles","action":"index","plugin":null}'
]),
$this->contains([
'bake._controller:_action',
'/bake/:controller/:action',
'{"plugin":"Bake","action":"index"}',
]),
$this->contains([
'testName',
'/tests/:action/*',
'{"controller":"Tests","action":"index","plugin":null}'
])
)
);
$this->shell->main();
$this->exec('routes');
$this->assertOutputContainsRow([
'<info>Route name</info>',
'<info>URI template</info>',
'<info>Defaults</info>'
]);
$this->assertOutputContainsRow([
'articles:_action',
'/articles/:action/*',
'{"controller":"Articles","action":"index","plugin":null}'
]);
$this->assertOutputContainsRow([
'bake._controller:_action',
'/bake/:controller/:action',
'{"plugin":"Bake","action":"index"}'
]);
$this->assertOutputContainsRow([
'testName',
'/tests/:action/*',
'{"controller":"Tests","action":"index","plugin":null}'
]);
}

/**
Expand All @@ -100,19 +101,17 @@ public function testMain()
*/
public function testCheck()
{
$this->table->expects($this->once())
->method('output')
->with(
$this->logicalAnd(
$this->contains(['Route name', 'URI template', 'Defaults']),
$this->contains([
'articles:_action',
'/articles/index',
'{"action":"index","pass":[],"controller":"Articles","plugin":null}'
])
)
);
$this->shell->check('/articles/index');
$this->exec('routes check /articles/check');
$this->assertOutputContainsRow([
'<info>Route name</info>',
'<info>URI template</info>',
'<info>Defaults</info>'
]);
$this->assertOutputContainsRow([
'articles:_action',
'/articles/check',
'{"action":"check","pass":[],"controller":"Articles","plugin":null}'
]);
}

/**
Expand All @@ -122,19 +121,17 @@ public function testCheck()
*/
public function testCheckWithNamedRoute()
{
$this->table->expects($this->once())
->method('output')
->with(
$this->logicalAnd(
$this->contains(['Route name', 'URI template', 'Defaults']),
$this->contains([
'testName',
'/tests/index',
'{"action":"index","pass":[],"controller":"Tests","plugin":null}'
])
)
);
$this->shell->check('/tests/index');
$this->exec('routes check /tests/index');
$this->assertOutputContainsRow([
'<info>Route name</info>',
'<info>URI template</info>',
'<info>Defaults</info>'
]);
$this->assertOutputContainsRow([
'testName',
'/tests/index',
'{"action":"index","pass":[],"controller":"Tests","plugin":null}'
]);
}

/**
Expand All @@ -144,33 +141,32 @@ public function testCheckWithNamedRoute()
*/
public function testCheckNotFound()
{
$this->io->expects($this->at(0))
->method('err')
->with($this->stringContains('did not match'));
$this->shell->check('/nope');
$this->exec('routes check /nope');
$this->assertErrorContains('did not match');
}

/**
* Test generating URLs
*
* @return void
*/
public function testGenerate()
public function testGenerateNoPassArgs()
{
$this->io->expects($this->never())
->method('err');
$this->io->expects($this->at(0))
->method('out')
->with($this->stringContains('> /articles/index'));
$this->io->expects($this->at(2))
->method('out')
->with($this->stringContains('> /articles/view/2/3'));

$this->shell->args = ['controller:Articles', 'action:index'];
$this->shell->generate();
$this->exec('routes generate controller:Articles action:index');
$this->assertOutputContains('> /articles/index');
$this->assertErrorEmpty();
}

$this->shell->args = ['controller:Articles', 'action:view', '2', '3'];
$this->shell->generate();
/**
* Test generating URLs with passed arguments
*
* @return void
*/
public function testGeneratePassedArguments()
{
$this->exec('routes generate controller:Articles action:view 2 3');
$this->assertOutputContains('> /articles/view/2/3');
$this->assertErrorEmpty();
}

/**
Expand All @@ -180,14 +176,8 @@ public function testGenerate()
*/
public function testGenerateBoolParams()
{
$this->io->expects($this->never())
->method('err');
$this->io->expects($this->at(0))
->method('out')
->with($this->stringContains('> https://example.com/articles/index'));

$this->shell->args = ['_ssl:true', '_host:example.com', 'controller:Articles', 'action:index'];
$this->shell->generate();
$this->exec('routes generate controller:Articles action:index _ssl:true _host:example.com');
$this->assertOutputContains('> https://example.com/articles/index');
}

/**
Expand All @@ -197,10 +187,7 @@ public function testGenerateBoolParams()
*/
public function testGenerateMissing()
{
$this->io->expects($this->at(0))
->method('err')
->with($this->stringContains('do not match'));
$this->shell->args = ['controller:Derp'];
$this->shell->generate();
$this->exec('routes generate controller:Derp');
$this->assertErrorContains('do not match');
}
}

0 comments on commit 6c9b96a

Please sign in to comment.