|
11 | 11 |
|
12 | 12 | namespace CodeIgniter\Commands;
|
13 | 13 |
|
| 14 | +use CodeIgniter\Router\RouteCollection; |
14 | 15 | use CodeIgniter\Test\CIUnitTestCase;
|
15 | 16 | use CodeIgniter\Test\StreamFilterTrait;
|
16 | 17 | use Config\Services;
|
@@ -39,29 +40,120 @@ protected function getBuffer()
|
39 | 40 | return $this->getStreamFilterBuffer();
|
40 | 41 | }
|
41 | 42 |
|
| 43 | + private function getCleanRoutes(): RouteCollection |
| 44 | + { |
| 45 | + $routes = Services::routes(); |
| 46 | + $routes->resetRoutes(); |
| 47 | + $routes->loadRoutes(); |
| 48 | + |
| 49 | + return $routes; |
| 50 | + } |
| 51 | + |
42 | 52 | public function testRoutesCommand()
|
43 | 53 | {
|
| 54 | + $this->getCleanRoutes(); |
| 55 | + |
44 | 56 | command('routes');
|
45 | 57 |
|
46 |
| - $this->assertStringContainsString('| (Closure)', $this->getBuffer()); |
47 |
| - $this->assertStringContainsString('| Route', $this->getBuffer()); |
48 |
| - $this->assertStringContainsString('| testing', $this->getBuffer()); |
49 |
| - $this->assertStringContainsString('\\TestController::index', $this->getBuffer()); |
| 58 | + $expected = <<<'EOL' |
| 59 | + +---------+---------+---------------+----------------------------------------+----------------+---------------+ |
| 60 | + | Method | Route | Name | Handler | Before Filters | After Filters | |
| 61 | + +---------+---------+---------------+----------------------------------------+----------------+---------------+ |
| 62 | + | GET | / | » | \App\Controllers\Home::index | | toolbar | |
| 63 | + | GET | closure | » | (Closure) | | toolbar | |
| 64 | + | GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 65 | + | HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 66 | + | POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 67 | + | PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 68 | + | DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 69 | + | OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 70 | + | TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 71 | + | CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 72 | + | CLI | testing | testing-index | \App\Controllers\TestController::index | | | |
| 73 | + +---------+---------+---------------+----------------------------------------+----------------+---------------+ |
| 74 | + EOL; |
| 75 | + $this->assertStringContainsString($expected, $this->getBuffer()); |
50 | 76 | }
|
51 | 77 |
|
52 |
| - public function testRoutesCommandRouteFilterAndAutoRoute() |
| 78 | + public function testRoutesCommandAutoRouteImproved() |
53 | 79 | {
|
54 |
| - $routes = Services::routes(); |
55 |
| - $routes->setDefaultNamespace('App\Controllers'); |
56 |
| - $routes->resetRoutes(); |
57 |
| - $routes->get('/', 'Home::index', ['filter' => 'csrf']); |
| 80 | + $routes = $this->getCleanRoutes(); |
| 81 | + |
| 82 | + $routes->setAutoRoute(true); |
| 83 | + config('Feature')->autoRoutesImproved = true; |
| 84 | + $namespace = 'Tests\Support\Controllers'; |
| 85 | + $routes->setDefaultNamespace($namespace); |
| 86 | + |
| 87 | + command('routes'); |
| 88 | + |
| 89 | + $expected = <<<'EOL' |
| 90 | + +------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+ |
| 91 | + | Method | Route | Name | Handler | Before Filters | After Filters | |
| 92 | + +------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+ |
| 93 | + | GET | / | » | \App\Controllers\Home::index | | toolbar | |
| 94 | + | GET | closure | » | (Closure) | | toolbar | |
| 95 | + | GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 96 | + | HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 97 | + | POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 98 | + | PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 99 | + | DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 100 | + | OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 101 | + | TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 102 | + | CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 103 | + | CLI | testing | testing-index | \App\Controllers\TestController::index | | | |
| 104 | + | GET(auto) | newautorouting | | \Tests\Support\Controllers\Newautorouting::getIndex | | toolbar | |
| 105 | + | POST(auto) | newautorouting/save/../..[/..] | | \Tests\Support\Controllers\Newautorouting::postSave | | toolbar | |
| 106 | + +------------+--------------------------------+---------------+-----------------------------------------------------+----------------+---------------+ |
| 107 | + EOL; |
| 108 | + $this->assertStringContainsString($expected, $this->getBuffer()); |
| 109 | + } |
| 110 | + |
| 111 | + public function testRoutesCommandRouteLegacy() |
| 112 | + { |
| 113 | + $routes = $this->getCleanRoutes(); |
| 114 | + |
58 | 115 | $routes->setAutoRoute(true);
|
| 116 | + $namespace = 'Tests\Support\Controllers'; |
| 117 | + $routes->setDefaultNamespace($namespace); |
59 | 118 |
|
60 | 119 | command('routes');
|
61 | 120 |
|
62 |
| - $this->assertStringContainsString( |
63 |
| - '|auto|/|\App\Controllers\Home::index||toolbar|', |
64 |
| - str_replace(' ', '', $this->getBuffer()) |
65 |
| - ); |
| 121 | + $expected = <<<'EOL' |
| 122 | + +---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+ |
| 123 | + | Method | Route | Name | Handler | Before Filters | After Filters | |
| 124 | + +---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+ |
| 125 | + | GET | / | » | \App\Controllers\Home::index | | toolbar | |
| 126 | + | GET | closure | » | (Closure) | | toolbar | |
| 127 | + | GET | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 128 | + | HEAD | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 129 | + | POST | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 130 | + | PUT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 131 | + | DELETE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 132 | + | OPTIONS | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 133 | + | TRACE | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 134 | + | CONNECT | testing | testing-index | \App\Controllers\TestController::index | | toolbar | |
| 135 | + | CLI | testing | testing-index | \App\Controllers\TestController::index | | | |
| 136 | + | auto | hello | | \Tests\Support\Controllers\Hello::index | | toolbar | |
| 137 | + | auto | hello/index[/...] | | \Tests\Support\Controllers\Hello::index | | toolbar | |
| 138 | + | auto | newautorouting/getIndex[/...] | | \Tests\Support\Controllers\Newautorouting::getIndex | | toolbar | |
| 139 | + | auto | newautorouting/postSave[/...] | | \Tests\Support\Controllers\Newautorouting::postSave | | toolbar | |
| 140 | + | auto | popcorn | | \Tests\Support\Controllers\Popcorn::index | | toolbar | |
| 141 | + | auto | popcorn/index[/...] | | \Tests\Support\Controllers\Popcorn::index | | toolbar | |
| 142 | + | auto | popcorn/pop[/...] | | \Tests\Support\Controllers\Popcorn::pop | | toolbar | |
| 143 | + | auto | popcorn/popper[/...] | | \Tests\Support\Controllers\Popcorn::popper | | toolbar | |
| 144 | + | auto | popcorn/weasel[/...] | | \Tests\Support\Controllers\Popcorn::weasel | | toolbar | |
| 145 | + | auto | popcorn/oops[/...] | | \Tests\Support\Controllers\Popcorn::oops | | toolbar | |
| 146 | + | auto | popcorn/goaway[/...] | | \Tests\Support\Controllers\Popcorn::goaway | | toolbar | |
| 147 | + | auto | popcorn/index3[/...] | | \Tests\Support\Controllers\Popcorn::index3 | | toolbar | |
| 148 | + | auto | popcorn/canyon[/...] | | \Tests\Support\Controllers\Popcorn::canyon | | toolbar | |
| 149 | + | auto | popcorn/cat[/...] | | \Tests\Support\Controllers\Popcorn::cat | | toolbar | |
| 150 | + | auto | popcorn/json[/...] | | \Tests\Support\Controllers\Popcorn::json | | toolbar | |
| 151 | + | auto | popcorn/xml[/...] | | \Tests\Support\Controllers\Popcorn::xml | | toolbar | |
| 152 | + | auto | popcorn/toindex[/...] | | \Tests\Support\Controllers\Popcorn::toindex | | toolbar | |
| 153 | + | auto | popcorn/echoJson[/...] | | \Tests\Support\Controllers\Popcorn::echoJson | | toolbar | |
| 154 | + | auto | remap[/...] | | \Tests\Support\Controllers\Remap::_remap | | toolbar | |
| 155 | + +---------+-------------------------------+---------------+-----------------------------------------------------+----------------+---------------+ |
| 156 | + EOL; |
| 157 | + $this->assertStringContainsString($expected, $this->getBuffer()); |
66 | 158 | }
|
67 | 159 | }
|
0 commit comments