Skip to content

Commit

Permalink
[update] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmf-san committed Jan 8, 2019
1 parent 8beba8f commit b8707cf
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 159 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf # doesn't work yet
charset = utf-8
trim_trailing_whitespace = true # doesn't work yet
insert_final_newline = true # doesn't work yet
[*.php]
indent_size = 4
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/.circleci export-ignore
/phpunit.xml export-ignore
/tests export-ignore
/README.md export-ignore
/README.md export-ignore
/.editorconfig export-ignore
325 changes: 167 additions & 158 deletions tests/AhiRouterTest.php
Original file line number Diff line number Diff line change
@@ -1,167 +1,176 @@
<?php
<?php

namespace bmfsan\AhiRouterTest;

use PHPUnit\Framework\TestCase;
use bmfsan\AhiRouter\Router;

class AhiRouterTest extends TestCase
class AhiRouterTest extends TestCase
{
private $routes = [
'/' => [
'END_POINT' => [
'GET' => 'HomeController@index',
],
'users' => [
'END_POINT' => [
'GET' => 'UserController@index',
],
':user_id' => [
'END_POINT' => [
'GET' => 'UserController@getUser',
'POST' => 'UserController@postUser',
],
'events' => [
'END_POINT' => [
'GET' => 'UserController@getEventsByUser',
],
':event_id' => [
'END_POINT' => [
'GET' => 'UserController@getEventByUser',
'POST' => 'UserController@postEventByUser',
],
],
],
],
'config' => [
'END_POINT' => [
'GET' => 'UserController@getConfigByUser',
],
],
],
],
];

/**
* @test
* @dataProvider createPathArrayProvider
*/
public function testCreateArrayFromCurrentPath($currentPath, $expectedPathArray)
{
$router = new Router();

$arrayFromCurrentPath = $router->createArrayFromCurrentPath($currentPath);

$this->assertEquals($expectedPathArray, $arrayFromCurrentPath);
}

/**
* @test
* @dataProvider searchProvider
*/
public function testSearch($currentPath, $requestMethod, $targetParams, $expectedAction, $expectedParams)
{
$router = new Router();

$arrayFromCurrentPath = $router->createArrayFromCurrentPath($currentPath);

$result = $router->search($this->routes, $arrayFromCurrentPath, $requestMethod, $targetParams);

$this->assertSame($expectedAction, $result['action']);
$this->assertEquals($expectedParams, $result['params']);
}

/**
* DataProvider for testCreateArrayFromCurrentPath
*
* @return array
*/
public function createPathArrayProvider(): array
{
return [
'/' => [
'/', ['/']
],
'/users' => [
'/users', ['users']
],
'/user/1' => [
'/users/1', ['users', '1']
],
'/users/foo' => [
'/users/foo', ['users', 'foo']
],
'/users/1' => [
'/users/1', ['users', '1']
],
'/users/1/events' => [
'/users/1/events', ['users', '1', 'events']
],
'/users/foo/events' => [
'/users/foo/events', ['users', 'foo', 'events']
],
'/users/1/events/1' => [
'/users/1/events/1', ['users', '1', 'events', '1']
],
'/users/foo/events/bar' => [
'/users/foo/events/bar', ['users', 'foo', 'events', 'bar']
],
'/users/1/events/bar' => [
'/users/1/events/bar', ['users', '1', 'events', 'bar']
],
'/users/foo/events/1' => [
'/users/foo/events/1', ['users', 'foo', 'events', '1']
],
'/users/config' => [
'/users/config', ['users', 'config']
],
];
}

/**
* DataProvider for testSearch
* *
* @return array
*/
public function searchProvider(): array
{
return [
'/' => [
'/', 'GET', [], 'HomeController@index', []
],
'/users' => [
'/users', 'GET', [], 'UserController@index', []
],
'/user/1' => [
'/users/1', 'GET', [':user_id'], 'UserController@getUser', [':user_id' => 1]
],
'/users/foo' => [
'/users/foo', 'GET', [':user_id'], 'UserController@getUser', [':user_id' => 'foo']
],
'/users/1' => [
'/users/1', 'POST', [':user_id'], 'UserController@postUser', [':user_id' => 1]
],
'/users/1/events' => [
'/users/1/events', 'GET', [':user_id'], 'UserController@getEventsByUser', [':user_id' => 1]
],
'/users/foo/events' => [
'/users/foo/events', 'GET', [':user_id'], 'UserController@getEventsByUser', [':user_id' => 'foo']
],
'/users/1/events/1' => [
'/users/1/events/1', 'GET', [':user_id', ':event_id'], 'UserController@getEventByUser', [':user_id' => 1, ':event_id' => 1]
],
'/users/foo/events/bar' => [
'/users/foo/events/bar', 'GET', [':user_id', ':event_id'], 'UserController@getEventByUser', [':user_id' => 'foo', ':event_id' => 'bar']
],
'/users/1/events/bar' => [
'/users/1/events/bar', 'GET', [':user_id', ':event_id'], 'UserController@getEventByUser', [':user_id' => 1, ':event_id' => 'bar']
],
'/users/foo/events/1' => [
'/users/foo/events/1', 'GET', [':user_id', ':event_id'], 'UserController@getEventByUser', [':user_id' => 'foo', ':event_id' => 1]
],
'/users/config' => [
'/users/config', 'GET', [], 'UserController@getConfigByUser', []
],
/**
* Route definition
* @var array
*
* GET / IndexController@getIndex
* GET /posts PostController@getPosts
* GET /posts/:title PostController@getPostByPostTitle
* POST /posts/:title PostController@getPostByPostTitle
* GET /posts/:title/:token PostController@getPostByToken
* GET /posts/:category_name PostController@getPostsByCategoryName
*/
private $routes = [
'/' => [
'END_POINT' => [
'GET' => 'IndexController@getIndex',
],
'posts' => [
'END_POINT' => [
'GET' => 'PostController@getPosts',
],
':title' => [
'END_POINT' => [
'GET' => 'PostController@getPostByPostTitle',
'POST' => 'PostController@postPostByPostTitle',
],
':token' => [
'END_POINT' => [
'GET' => 'PostController@getPostByToken',
],
],
],
':category_name' => [
'END_POINT' => [
'GET' => 'PostController@getPostsByCategoryName',
],
],
],
],
];
}

/**
* @test
* @dataProvider createPathArrayProvider
*/
public function testCreateArrayFromCurrentPath($currentPath, $expectedPathArray)
{
$router = new Router();

$arrayFromCurrentPath = $router->createArrayFromCurrentPath($currentPath);

$this->assertEquals($expectedPathArray, $arrayFromCurrentPath);
}

/**
* @test
* @dataProvider searchProvider
*/
public function testSearch($currentPath, $requestMethod, $targetParams, $expectedAction, $expectedParams)
{
$router = new Router();

$arrayFromCurrentPath = $router->createArrayFromCurrentPath($currentPath);

$result = $router->search($this->routes, $arrayFromCurrentPath, $requestMethod, $targetParams);

$this->assertSame($expectedAction, $result['action']);
$this->assertEquals($expectedParams, $result['params']);
}

/**
* DataProvider for testCreateArrayFromCurrentPath
*
* @return array
*/
public function createPathArrayProvider(): array
{
/**
* dataset name => [
* route, [expected]
* ]
*/
return [
'Route:/' => [
'/', ['/']
],
'Route:/posts' => [
'/posts', ['posts']
],
'Route:/posts/:title[int]' => [
'/posts/1', ['posts', 1]
],
'Route:/posts/:title[string]' => [
'/posts/foo', ['posts', 'foo']
],
'Route:/posts/:title[int]/:token[int]' => [
'/posts/1/123', ['posts', '1', '123']
],
'Route:/posts/:title[string]/:token[string]' => [
'/posts/foo/bar', ['posts', 'foo', 'bar']
],
'Route:/posts/:title[int]/:token[string]' => [
'/posts/1/foo', ['posts', 1, 'foo']
],
'Route:/posts/:title[string]/:token[int]' => [
'/posts/foo/123', ['posts', 'foo', 123]
],
'Route:/posts/:category_name[int]' => [
'/posts/1', ['posts', 1]
],
'Route:/posts/:category_name[string]' => [
'/posts/foo', ['posts', 'foo']
],
];
}

/**
* DataProvider for testSearch
* *
* @return array
*/
public function searchProvider(): array
{
/**
* dataset name => [
* route, method, [params], expected action, expected params
* ]
*/
return [
'Route:/' => [
'/', 'GET', [], 'IndexController@getIndex', []
],
'Route:/posts' => [
'/posts', 'GET', [], 'PostController@getPosts', []
],
'Route:/posts/:title[int]' => [
'/posts/1', 'GET', [':title'], 'PostController@getPostByPostTitle', [':title' => 1]
],
'Route:/posts/:title[string]' => [
'/posts/foo', 'GET', [':title'], 'PostController@getPostByPostTitle', [':title' => 'foo']
],
'Route:/posts/:title[int]' => [
'/posts/1', 'POST', [':title'], 'PostController@postPostByPostTitle', [':title' => 1]
],
'Route:/posts/:title[string]' => [
'/posts/foo', 'POST', [':title'], 'PostController@postPostByPostTitle', [':title' => 'foo']
],
'Route:/posts/:title[int]/:token[int]' => [
'/posts/1/123', 'GET', [':title', ':token'], 'PostController@getPostByToken', [':title' => 1, ':token' => 123]
],
'Route:/posts/:title[string]/:token[string]' => [
'/posts/foo/bar', 'GET', [':title', ':token'], 'PostController@getPostByToken', [':title' => 'foo', ':token' => 'bar']
],
'Route:/posts/:title[int]/:token[string]' => [
'/posts/1/foo', 'GET', [':title', ':token'], 'PostController@getPostByToken', [':title' => 1, ':token' => 'foo']
],
'Route:/posts/:title[string]/:token[int]' => [
'/posts/foo/123', 'GET', [':title', ':token'], 'PostController@getPostByToken', [':title' => 'foo', ':token' => 123]
],
'Route:/posts/:category_name[int]' => [
'/posts/1', 'GET', [':category_name'], 'PostController@getPostsByCategoryName', [':category_name' => 1]
],
'Route:/posts/:category_name[string]' => [
'/posts/foo', 'GET', [':category_name'], 'PostController@getPostsByCategoryName', [':category_name' => 'foo']
]
];
}
}

0 comments on commit b8707cf

Please sign in to comment.