Skip to content

Commit

Permalink
refactor: fix HTTP verbs for FeatureTestTrait::withRoutes()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 10, 2023
1 parent 8a0ced0 commit 92bab56
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
9 changes: 6 additions & 3 deletions system/Test/FeatureTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trait FeatureTestTrait
*
* Example routes:
* [
* ['get', 'home', 'Home::index']
* ['GET', 'home', 'Home::index'],
* ]
*
* @param array|null $routes Array to set routes
Expand All @@ -51,10 +51,13 @@ protected function withRoutes(?array $routes = null)
$collection->resetRoutes();

foreach ($routes as $route) {
// @TODO For backward compatibility. Remove strtolower() in the future.
$method = strtolower($route[0]);

if (isset($route[3])) {
$collection->{$route[0]}($route[1], $route[2], $route[3]);
$collection->{$method}($route[1], $route[2], $route[3]);
} else {
$collection->{$route[0]}($route[1], $route[2]);
$collection->{$method}($route[1], $route[2]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/system/HomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testPageLoadsSuccessfully(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
'\App\Controllers\Home::index',
],
Expand Down
52 changes: 26 additions & 26 deletions tests/system/Test/FeatureTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testCallGet(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
static fn () => 'Hello World',
],
Expand All @@ -69,7 +69,7 @@ public function testCallGetAndUriString(): void
{
$this->withRoutes([
[
'get',
'GET',
'foo/bar/1/2/3',
static fn () => 'Hello World',
],
Expand All @@ -85,7 +85,7 @@ public function testCallGetAndFilterReturnsResponse(): void
{
$this->withRoutes([
[
'get',
'GET',
'admin',
static fn () => 'Admin Area',
['filter' => 'test-redirectfilter'],
Expand All @@ -100,7 +100,7 @@ public function testClosureWithEcho()
{
$this->withRoutes([
[
'get',
'GET',
'home',
static function () { echo 'test echo'; },
],
Expand All @@ -118,7 +118,7 @@ public function testCallPost(): void
{
$this->withRoutes([
[
'post',
'POST',
'home',
static fn () => 'Hello Mars',
],
Expand All @@ -132,7 +132,7 @@ public function testCallPostWithBody(): void
{
$this->withRoutes([
[
'post',
'POST',
'home',
static fn () => 'Hello ' . service('request')->getPost('foo') . '!',
],
Expand All @@ -146,7 +146,7 @@ public function testCallValidationTwice(): void
{
$this->withRoutes([
[
'post',
'POST',
'section/create',
static function () {
$validation = Services::validation();
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testCallPut(): void
{
$this->withRoutes([
[
'put',
'PUT',
'home',
static fn () => 'Hello Pluto',
],
Expand All @@ -190,7 +190,7 @@ public function testCallPatch(): void
{
$this->withRoutes([
[
'patch',
'PATCH',
'home',
static fn () => 'Hello Jupiter',
],
Expand All @@ -204,7 +204,7 @@ public function testCallOptions(): void
{
$this->withRoutes([
[
'options',
'OPTIONS',
'home',
static fn () => 'Hello George',
],
Expand All @@ -218,7 +218,7 @@ public function testCallDelete(): void
{
$this->withRoutes([
[
'delete',
'DELETE',
'home',
static fn () => 'Hello Wonka',
],
Expand All @@ -232,7 +232,7 @@ public function testSession(): void
{
$response = $this->withRoutes([
[
'get',
'GET',
'home',
static fn () => 'Home',
],
Expand All @@ -254,7 +254,7 @@ public function testWithSessionNull(): void

$response = $this->withRoutes([
[
'get',
'GET',
'home',
static fn () => 'Home',
],
Expand All @@ -268,7 +268,7 @@ public function testReturns(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
'\Tests\Support\Controllers\Popcorn::index',
],
Expand All @@ -281,7 +281,7 @@ public function testIgnores(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
'\Tests\Support\Controllers\Popcorn::cat',
],
Expand All @@ -294,7 +294,7 @@ public function testEchoesWithParams(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
'\Tests\Support\Controllers\Popcorn::canyon',
],
Expand All @@ -308,7 +308,7 @@ public function testEchoesWithQuery(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
'\Tests\Support\Controllers\Popcorn::canyon',
],
Expand Down Expand Up @@ -373,7 +373,7 @@ public function testOpenCliRoutesFromHttpGot404($from, $to, $httpGet): void

$this->withRoutes([
[
'cli',
'CLI',
$from,
$to,
],
Expand All @@ -388,7 +388,7 @@ public function testIsOkWithRedirects(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
'\Tests\Support\Controllers\Popcorn::goaway',
],
Expand All @@ -402,7 +402,7 @@ public function testCallGetWithParams(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
static fn () => json_encode(Services::request()->getGet()),
],
Expand All @@ -429,7 +429,7 @@ public function testCallGetWithParamsAndREQUEST(): void
{
$this->withRoutes([
[
'get',
'GET',
'home',
static fn () => json_encode(Services::request()->fetchGlobal('request')),
],
Expand All @@ -456,7 +456,7 @@ public function testCallPostWithParams(): void
{
$this->withRoutes([
[
'post',
'POST',
'home',
static fn () => json_encode(Services::request()->getPost()),
],
Expand All @@ -483,7 +483,7 @@ public function testCallPostWithParamsAndREQUEST(): void
{
$this->withRoutes([
[
'post',
'POST',
'home',
static fn () => json_encode(Services::request()->fetchGlobal('request')),
],
Expand All @@ -510,7 +510,7 @@ public function testCallPutWithJsonRequest(): void
{
$this->withRoutes([
[
'put',
'PUT',
'home',
'\Tests\Support\Controllers\Popcorn::echoJson',
],
Expand All @@ -534,7 +534,7 @@ public function testCallPutWithJsonRequestAndREQUEST(): void
{
$this->withRoutes([
[
'put',
'PUT',
'home',
static fn () => json_encode(Services::request()->fetchGlobal('request')),
],
Expand All @@ -558,7 +558,7 @@ public function testCallWithJsonRequest(): void
{
$this->withRoutes([
[
'post',
'POST',
'home',
'\Tests\Support\Controllers\Popcorn::echoJson',
],
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/testing/feature/002.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Get a simple page
$result = $this->call('get', '/');
$result = $this->call('GET', '/');

// Submit a form
$result = $this->call('post', 'contact', [
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/testing/feature/004.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$routes = [
['get', 'users', 'UserController::list'],
['GET', 'users', 'UserController::list'],
];

$result = $this->withRoutes($routes)->get('users');

0 comments on commit 92bab56

Please sign in to comment.