Skip to content

Commit

Permalink
test: fix the test cases error.
Browse files Browse the repository at this point in the history
  • Loading branch information
ping-yee committed Oct 11, 2023
1 parent 29b7f8c commit b3f30cc
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 17 deletions.
21 changes: 21 additions & 0 deletions tests/system/Router/Controllers/Admin_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Router\Controllers;

use CodeIgniter\Controller;

class Admin_user extends Controller
{
public function show_list(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/system/Router/Controllers/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Router\Controllers;

use CodeIgniter\Controller;

class Test extends Controller
{
public function test(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/system/Router/Controllers/foo/bar/baz/Some_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Router\Controllers\foo\bar\baz;

use CodeIgniter\Controller;

class Some_controller extends Controller
{
public function some_method($first = ''): void
{
}
}
21 changes: 21 additions & 0 deletions tests/system/Router/Controllers/Φ.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Router\Controllers;

use CodeIgniter\Controller;

class Φ extends Controller
{
public function getIndex(): void
{
}
}
21 changes: 21 additions & 0 deletions tests/system/Router/Controllers/Φ/Home.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Router\Controllers\Φ;

use CodeIgniter\Controller;

class Home extends Controller
{
public function getIndex(): void
{
}
}
62 changes: 45 additions & 17 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,12 @@ public function testAutoRouteFindsDefaultControllerAndMethod(): void
$this->collection->setDefaultMethod('test');
$router = new Router($this->collection, $this->request);

copy(TESTPATH . 'system/Router/Controllers/Test.php', APPPATH . 'Controllers/Test.php');

$router->autoRoute('/');

unlink(APPPATH . 'Controllers/Test.php');

$this->assertSame('Test', $router->controllerName());
$this->assertSame('test', $router->methodName());
}
Expand All @@ -212,9 +216,9 @@ public function testAutoRouteFindsControllerWithFileAndMethod(): void
$this->collection->setAutoRoute(true);
$router = new Router($this->collection, $this->request);

copy(TESTPATH . 'system/Router/Controllers/MyController.php', APPPATH . 'Controllers/Mycontroller.php');
copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php');

$router->autoRoute('Mycontroller/getSomemethod');
$router->autoRoute('mycontroller/getSomemethod');

unlink(APPPATH . 'Controllers/Mycontroller.php');

Expand All @@ -227,7 +231,7 @@ public function testAutoRouteFindsControllerWithFile(): void
$this->collection->setAutoRoute(true);
$router = new Router($this->collection, $this->request);

copy(TESTPATH . 'system/Router/Controllers/MyController.php', APPPATH . 'Controllers/Mycontroller.php');
copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php');

$router->autoRoute('mycontroller');

Expand All @@ -244,9 +248,9 @@ public function testAutoRouteFindsControllerWithSubfolder(): void

mkdir(APPPATH . 'Controllers/Subfolder');

copy(TESTPATH . 'system/Router/Controllers/Subfolder/MyController.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php');
copy(TESTPATH . 'system/Router/Controllers/Subfolder/Mycontroller.php', APPPATH . 'Controllers/Subfolder/Mycontroller.php');

$router->autoRoute('subfolder/Mycontroller/getSomemethod');
$router->autoRoute('subfolder/mycontroller/getSomemethod');

unlink(APPPATH . 'Controllers/Subfolder/Mycontroller.php');

Expand All @@ -263,9 +267,11 @@ public function testAutoRouteFindsDashedSubfolder(): void
$router->setTranslateURIDashes(true);

mkdir(APPPATH . 'Controllers/Dash_folder');
copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Mycontroller.php', APPPATH . 'Controllers/Dash_folder/Mycontroller.php');

$router->autoRoute('dash-folder/mycontroller/somemethod');

unlink(APPPATH . 'Controllers/Dash_folder/Mycontroller.php');
rmdir(APPPATH . 'Controllers/Dash_folder');

$this->assertSame('Dash_folder/', $router->directory());
Expand All @@ -280,16 +286,16 @@ public function testAutoRouteFindsDashedController(): void
$router->setTranslateURIDashes(true);

mkdir(APPPATH . 'Controllers/Dash_folder');
file_put_contents(APPPATH . 'Controllers/Dash_folder/Dash_controller.php', '');
copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php');

$router->autoRoute('dash-folder/dash-controller/somemethod');
$router->autoRoute('dash-folder/dash-controller/getSomemethod');

unlink(APPPATH . 'Controllers/Dash_folder/Dash_controller.php');
rmdir(APPPATH . 'Controllers/Dash_folder');

$this->assertSame('Dash_folder/', $router->directory());
$this->assertSame('Dash_controller', $router->controllerName());
$this->assertSame('somemethod', $router->methodName());
$this->assertSame('getSomemethod', $router->methodName());
}

public function testAutoRouteFindsDashedMethod(): void
Expand All @@ -299,16 +305,16 @@ public function testAutoRouteFindsDashedMethod(): void
$router->setTranslateURIDashes(true);

mkdir(APPPATH . 'Controllers/Dash_folder');
file_put_contents(APPPATH . 'Controllers/Dash_folder/Dash_controller.php', '');
copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Dash_controller.php', APPPATH . 'Controllers/Dash_folder/Dash_controller.php');

$router->autoRoute('dash-folder/dash-controller/dash-method');
$router->autoRoute('dash-folder/dash-controller/getDash_method');

unlink(APPPATH . 'Controllers/Dash_folder/Dash_controller.php');
rmdir(APPPATH . 'Controllers/Dash_folder');

$this->assertSame('Dash_folder/', $router->directory());
$this->assertSame('Dash_controller', $router->controllerName());
$this->assertSame('dash_method', $router->methodName());
$this->assertSame('getDash_method', $router->methodName());
}

public function testAutoRouteFindsDefaultDashFolder(): void
Expand All @@ -318,9 +324,11 @@ public function testAutoRouteFindsDefaultDashFolder(): void
$router->setTranslateURIDashes(true);

mkdir(APPPATH . 'Controllers/Dash_folder');
copy(TESTPATH . 'system/Router/Controllers/Dash_folder/Home.php', APPPATH . 'Controllers/Dash_folder/Home.php');

$router->autoRoute('dash-folder');

unlink(APPPATH . 'Controllers/Dash_folder/Home.php');
rmdir(APPPATH . 'Controllers/Dash_folder');

$this->assertSame('Dash_folder/', $router->directory());
Expand All @@ -335,9 +343,11 @@ public function testAutoRouteFindsMByteDir(): void
$router->setTranslateURIDashes(true);

mkdir(APPPATH . 'Controllers/Φ');
copy(TESTPATH . 'system/Router/Controllers/Φ/Home.php', APPPATH . 'Controllers/Φ/Home.php');

$router->autoRoute('Φ');

unlink(APPPATH . 'Controllers/Φ/Home.php');
rmdir(APPPATH . 'Controllers/Φ');

$this->assertSame('Φ/', $router->directory());
Expand All @@ -351,11 +361,11 @@ public function testAutoRouteFindsMByteController(): void
$router = new Router($this->collection, $this->request);
$router->setTranslateURIDashes(true);

file_put_contents(APPPATH . 'Controllers/Φ', '');
copy(TESTPATH . 'system/Router/Controllers/Φ.php', APPPATH . 'Controllers/Φ.php');

$router->autoRoute('Φ');

unlink(APPPATH . 'Controllers/Φ');
unlink(APPPATH . 'Controllers/Φ.php');

$this->assertSame('Φ', $router->controllerName());
$this->assertSame('index', $router->methodName());
Expand Down Expand Up @@ -749,7 +759,11 @@ public function testTranslateURIDashesForAutoRoute(): void
$router = new Router($this->collection, $this->request);
$router->setTranslateURIDashes(true);

$router->autoRoute('admin-user/show-list');
copy(TESTPATH . 'system/Router/Controllers/Admin_user.php', APPPATH . 'Controllers/Admin_user.php');

$router->autoRoute('admin_user/show_list');

unlink(APPPATH . 'Controllers/Admin_user.php');

$this->assertSame('Admin_user', $router->controllerName());
$this->assertSame('show_list', $router->methodName());
Expand All @@ -763,10 +777,14 @@ public function testAutoRouteMatchesZeroParams(): void
$this->collection->setAutoRoute(true);
$router = new Router($this->collection, $this->request);

$router->autoRoute('myController/someMethod/0/abc');
copy(TESTPATH . 'system/Router/Controllers/Mycontroller.php', APPPATH . 'Controllers/Mycontroller.php');

$this->assertSame('MyController', $router->controllerName());
$this->assertSame('someMethod', $router->methodName());
$router->autoRoute('mycontroller/getSomemethod/0/abc');

$this->assertSame('Mycontroller', $router->controllerName());
$this->assertSame('getSomemethod', $router->methodName());

unlink(APPPATH . 'Controllers/Mycontroller.php');

$expected = [
'0',
Expand Down Expand Up @@ -834,9 +852,19 @@ public function testRouterPriorDirectory(): void
$this->collection->setAutoRoute(true);
$router = new Router($this->collection, $this->request);

mkdir(APPPATH . 'Controllers/foo');
mkdir(APPPATH . 'Controllers/foo/bar');
mkdir(APPPATH . 'Controllers/foo/bar/baz');
copy(TESTPATH . 'system/Router/Controllers/foo/bar/baz/Some_controller.php', APPPATH . 'Controllers/foo/bar/baz/Some_controller.php');

$router->setDirectory('foo/bar/baz', false, true);
$router->handle('Some_controller/some_method/param1/param2/param3');

unlink(APPPATH . 'Controllers/foo/bar/baz/Some_controller.php');
rmdir(APPPATH . 'Controllers/foo/bar/baz');
rmdir(APPPATH . 'Controllers/foo/bar');
rmdir(APPPATH . 'Controllers/foo');

$this->assertSame('foo/bar/baz/', $router->directory());
$this->assertSame('Some_controller', $router->controllerName());
$this->assertSame('some_method', $router->methodName());
Expand Down

0 comments on commit b3f30cc

Please sign in to comment.