| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP Project
- * @since 3.1.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Command;
- use Cake\Console\CommandInterface;
- use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
- use Cake\Core\Configure;
- use Cake\Routing\Route\Route;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- /**
- * RoutesCommandTest
- */
- class RoutesCommandTest extends TestCase
- {
- use ConsoleIntegrationTestTrait;
- /**
- * setUp method
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->setAppNamespace();
- }
- /**
- * tearDown
- */
- public function tearDown(): void
- {
- parent::tearDown();
- Router::reload();
- }
- /**
- * Ensure help for `routes` works
- */
- public function testRouteListHelp(): void
- {
- $this->exec('routes -h');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('list of routes');
- $this->assertErrorEmpty();
- }
- /**
- * Test checking an nonexistent route.
- */
- public function testRouteList(): void
- {
- $this->exec('routes');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- '<info>Route name</info>',
- '<info>URI template</info>',
- '<info>Plugin</info>',
- '<info>Prefix</info>',
- '<info>Controller</info>',
- '<info>Action</info>',
- '<info>Valid Action</info>',
- '<info>Method(s)</info>',
- ]);
- $this->assertOutputContainsRow([
- 'articles:_action',
- '/app/articles/{action}/*',
- '',
- '',
- 'Articles',
- 'index',
- 'X',
- '',
- ]);
- $this->assertOutputContainsRow([
- 'bake._controller:_action',
- '/bake/{controller}/{action}',
- 'Bake',
- '',
- '',
- 'index',
- '',
- '',
- ]);
- $this->assertOutputContainsRow([
- 'testName',
- '/app/tests/{action}/*',
- '',
- '',
- 'Tests',
- 'index',
- '',
- '',
- ]);
- }
- /**
- * Test routes with --verbose option
- */
- public function testRouteListVerbose(): void
- {
- $this->exec('routes -v');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- '<info>Route name</info>',
- '<info>URI template</info>',
- '<info>Plugin</info>',
- '<info>Prefix</info>',
- '<info>Controller</info>',
- '<info>Action</info>',
- '<info>Valid Action</info>',
- '<info>Method(s)</info>',
- '<info>Middlewares</info>',
- '<info>Defaults</info>',
- ]);
- $this->assertOutputContainsRow([
- 'articles:_action',
- '/app/articles/{action}/*',
- '',
- '',
- 'Articles',
- 'index',
- 'X',
- '',
- 'dumb, sample',
- '{"action":"index","controller":"Articles","plugin":null}',
- ]);
- }
- /**
- * Test routes with --sort option
- */
- public function testRouteListSorted(): void
- {
- Configure::write('TestApp.routes', function ($routes): void {
- $routes->connect(
- new Route('/a/route/sorted', [], ['_name' => '_aRoute'])
- );
- });
- $this->exec('routes -s');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('_aRoute', $this->_out->messages()[3]);
- }
- /**
- * Test routes with --with-middlewares option
- */
- public function testRouteWithMiddlewares(): void
- {
- $this->exec('routes -m');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- 'articles:_action',
- '/app/articles/{action}/*',
- '',
- '',
- 'Articles',
- 'index',
- 'X',
- '',
- 'dumb, sample',
- ]);
- }
- /**
- * Ensure help for `routes` works
- */
- public function testCheckHelp(): void
- {
- $this->exec('routes check -h');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('Check a URL');
- $this->assertErrorEmpty();
- }
- /**
- * Ensure routes check with no input
- */
- public function testCheckNoInput(): void
- {
- $this->exec('routes check');
- $this->assertExitCode(CommandInterface::CODE_ERROR);
- $this->assertErrorContains('`url` argument is required');
- }
- /**
- * Test checking an existing route.
- */
- public function testCheck(): void
- {
- $this->exec('routes check /app/articles/check');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- '<info>Route name</info>',
- '<info>URI template</info>',
- '<info>Defaults</info>',
- ]);
- $this->assertOutputContainsRow([
- 'articles:_action',
- '/app/articles/check',
- '{"_middleware":["dumb","sample"],"action":"check","controller":"Articles","pass":[],"plugin":null}',
- ]);
- }
- /**
- * Test checking an existing route with named route.
- */
- public function testCheckWithNamedRoute(): void
- {
- $this->exec('routes check /app/tests/index');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- '<info>Route name</info>',
- '<info>URI template</info>',
- '<info>Defaults</info>',
- ]);
- $this->assertOutputContainsRow([
- 'testName',
- '/app/tests/index',
- '{"_middleware":["dumb","sample"],"_name":"testName","action":"index","controller":"Tests","pass":[],"plugin":null}',
- ]);
- }
- /**
- * Test checking an existing route with redirect route.
- */
- public function testCheckWithRedirectRoute(): void
- {
- $this->exec('routes check /app/redirect');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- '<info>URI template</info>',
- '<info>Redirect</info>',
- ]);
- $this->assertOutputContainsRow([
- '/app/redirect',
- 'http://example.com/test.html',
- ]);
- }
- /**
- * Test checking an nonexistent route.
- */
- public function testCheckNotFound(): void
- {
- $this->exec('routes check /nope');
- $this->assertExitCode(CommandInterface::CODE_ERROR);
- $this->assertErrorContains('did not match');
- }
- /**
- * Ensure help for `routes` works
- */
- public function testGenerareHelp(): void
- {
- $this->exec('routes generate -h');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('Check a routing array');
- $this->assertErrorEmpty();
- }
- /**
- * Test generating URLs
- */
- public function testGenerateNoPassArgs(): void
- {
- $this->exec('routes generate controller:Articles action:index');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('> /app/articles');
- $this->assertErrorEmpty();
- }
- /**
- * Test generating URLs with passed arguments
- */
- public function testGeneratePassedArguments(): void
- {
- $this->exec('routes generate controller:Articles action:view 2 3');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('> /app/articles/view/2/3');
- $this->assertErrorEmpty();
- }
- /**
- * Test generating URLs with bool params
- */
- public function testGenerateBoolParams(): void
- {
- $this->exec('routes generate controller:Articles action:index _https:true _host:example.com');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContains('> https://example.com/app/articles');
- }
- /**
- * Test generating URLs
- */
- public function testGenerateMissing(): void
- {
- $this->exec('routes generate plugin:Derp controller:Derp');
- $this->assertExitCode(CommandInterface::CODE_ERROR);
- $this->assertErrorContains('do not match');
- }
- /**
- * Test routes duplicate warning
- */
- public function testRouteDuplicateWarning(): void
- {
- Configure::write('TestApp.routes', function ($builder): void {
- $builder->connect(
- new Route('/unique-path', [], ['_name' => '_aRoute'])
- );
- $builder->connect(
- new Route('/unique-path', [], ['_name' => '_bRoute'])
- );
- $builder->connect(
- new Route('/blog', ['_method' => 'GET'], ['_name' => 'blog-get'])
- );
- $builder->connect(
- new Route('/blog', [], ['_name' => 'blog-all'])
- );
- $builder->connect(
- new Route('/events', ['_method' => ['POST', 'PUT']], ['_name' => 'events-post'])
- );
- $builder->connect(
- new Route('/events', ['_method' => 'GET'], ['_name' => 'events-get'])
- );
- });
- $this->exec('routes');
- $this->assertExitCode(CommandInterface::CODE_SUCCESS);
- $this->assertOutputContainsRow([
- '<info>Route name</info>',
- '<info>URI template</info>',
- '<info>Plugin</info>',
- '<info>Prefix</info>',
- '<info>Controller</info>',
- '<info>Action</info>',
- '<info>Valid Action</info>',
- '<info>Method(s)</info>',
- ]);
- $this->assertOutputContainsRow([
- '_aRoute',
- '/unique-path',
- '',
- '',
- '',
- '',
- '',
- ]);
- $this->assertOutputContainsRow([
- '_bRoute',
- '/unique-path',
- '',
- '',
- '',
- '',
- '',
- ]);
- $this->assertOutputContainsRow([
- 'blog-get',
- '/blog',
- '',
- '',
- '',
- '',
- '',
- ]);
- $this->assertOutputContainsRow([
- 'blog-all',
- '/blog',
- '',
- '',
- '',
- '',
- '',
- ]);
- }
- }
|