Skip to content

Commit a2eeab2

Browse files
committed
Initialize route request tests
1 parent 1e8d9ea commit a2eeab2

8 files changed

+134
-47
lines changed

.travis.yml

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
language: php
2-
php:
3-
- "7.1"
4-
- "7.0"
5-
- "5.6"
6-
- "5.5"
7-
- "5.4"
2+
3+
matrix:
4+
include:
5+
- php: 5.4
6+
- php: 5.5
7+
- php: 5.6
8+
- php: 7.0
9+
- php: 7.1
10+
- php: 7.2
11+
- php: 7.3
12+
13+
sudo: false
14+
15+
install:
16+
- composer install
17+
18+
before_script:
19+
- php -S localhost:5000 tests/fixtures/server.php &
20+
21+
script:
22+
- vendor/bin/phpunit

composer.json

+9
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@
1515
"require": {
1616
"php": ">=5.4.0"
1717
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^4.8 || ^5.7 || ^6.5 || ^7.0",
20+
"guzzlehttp/guzzle": "^5.3"
21+
},
1822
"autoload": {
1923
"psr-4": {
2024
"Buki\\": "src"
2125
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Buki\\Tests\\": "tests/"
30+
}
2231
}
2332
}

phpunit.xml.dist

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/bootstrap.php">
3+
<phpunit bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
verbose="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false">
13+
414
<testsuites>
515
<testsuite name="Router Tests">
616
<directory>tests/</directory>
717
</testsuite>
818
</testsuites>
19+
920
<filter>
1021
<whitelist>
1122
<directory suffix=".php">src/</directory>
1223
</whitelist>
1324
</filter>
25+
1426
</phpunit>

src/Router.php

+23-23
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Router
7575

7676
/**
7777
* Router constructor method.
78-
*
78+
*
7979
* @param array $params
8080
*
8181
* @return void
@@ -105,24 +105,24 @@ function __construct(array $params = [])
105105
protected function setPaths($params)
106106
{
107107
if (isset($params['paths']) && $paths = $params['paths']) {
108-
$this->paths['controllers'] =
108+
$this->paths['controllers'] =
109109
isset($paths['controllers'])
110110
? trim($paths['controllers'], '/')
111111
: $this->paths['controllers'];
112112

113-
$this->paths['middlewares'] =
113+
$this->paths['middlewares'] =
114114
isset($paths['middlewares'])
115115
? trim($paths['middlewares'], '/')
116116
: $this->paths['middlewares'];
117117
}
118118

119119
if (isset($params['namespaces']) && $namespaces = $params['namespaces']) {
120-
$this->namespaces['controllers'] =
120+
$this->namespaces['controllers'] =
121121
isset($namespaces['controllers'])
122122
? trim($namespaces['controllers'], '\\') . '\\'
123123
: '';
124124

125-
$this->namespaces['middlewares'] =
125+
$this->namespaces['middlewares'] =
126126
isset($namespaces['middlewares'])
127127
? trim($namespaces['middlewares'], '\\') . '\\'
128128
: '';
@@ -197,10 +197,10 @@ public function __call($method, $params)
197197
* Add new route method one or more http methods.
198198
*
199199
* @param string $methods
200-
* @param string $route
200+
* @param string $route
201201
* @param array|string|closure $settings
202202
* @param string|closure $callback
203-
*
203+
*
204204
* @return void
205205
*/
206206
public function add($methods, $route, $settings, $callback = null)
@@ -228,7 +228,7 @@ public function add($methods, $route, $settings, $callback = null)
228228
*
229229
* @param string|array $pattern
230230
* @param null|string $attr
231-
*
231+
*
232232
* @return void
233233
* @throws
234234
*/
@@ -350,10 +350,10 @@ public function run()
350350
/**
351351
* Routes Group
352352
*
353-
* @param string $name
353+
* @param string $name
354354
* @param closure|array $settings
355355
* @param null|closure $callback
356-
*
356+
*
357357
* @return void
358358
*/
359359
public function group($name, $settings = null, $callback = null)
@@ -376,12 +376,12 @@ public function group($name, $settings = null, $callback = null)
376376
foreach ($this->groups as $key => $value) {
377377
if (is_array($value['before'])) {
378378
foreach ($value['before'] as $k => $v) {
379-
$list['before'][] = $v;
379+
$list['before'][] = $v;
380380
}
381381
foreach ($value['after'] as $k => $v) {
382-
$list['after'][] = $v;
382+
$list['after'][] = $v;
383383
}
384-
}
384+
}
385385
}
386386

387387
if (! is_null($group['before'])) {
@@ -414,7 +414,7 @@ public function group($name, $settings = null, $callback = null)
414414
* @param string $route
415415
* @param string|array $settings
416416
* @param null|string $controller
417-
*
417+
*
418418
* @return void
419419
* @throws
420420
*/
@@ -429,7 +429,7 @@ public function controller($route, $settings, $controller = null)
429429
$controllerFile = realpath(
430430
rtrim($this->paths['controllers'], '/') . '/' . $controller . '.php'
431431
);
432-
432+
433433
if (! file_exists($controllerFile)) {
434434
return $this->exception($controller . ' class is not found!');
435435
}
@@ -474,7 +474,7 @@ public function controller($route, $settings, $controller = null)
474474
* Routes error function. (Closure)
475475
*
476476
* @param $callback
477-
*
477+
*
478478
* @return void
479479
*/
480480
public function error($callback)
@@ -486,10 +486,10 @@ public function error($callback)
486486
* Add new Route and it's settings
487487
*
488488
* @param $uri
489-
* @param $method
489+
* @param $method
490490
* @param $callback
491491
* @param $settings
492-
*
492+
*
493493
* @return void
494494
*/
495495
private function addRoute($uri, $method, $callback, $settings)
@@ -523,11 +523,11 @@ private function addRoute($uri, $method, $callback, $settings)
523523
? $settings['name']
524524
: null
525525
),
526-
'before' => (isset($settings['before'])
526+
'before' => (isset($settings['before'])
527527
? $settings['before']
528528
: null
529529
),
530-
'after' => (isset($settings['after'])
530+
'after' => (isset($settings['after'])
531531
? $settings['after']
532532
: null
533533
),
@@ -556,9 +556,9 @@ private function runRouteCommand($command, $params = null)
556556
/**
557557
* Detect Routes Middleware; before or after
558558
*
559-
* @param $middleware
559+
* @param $middleware
560560
* @param $type
561-
*
561+
*
562562
* @return void
563563
*/
564564
public function runRouteMiddleware($middleware, $type)
@@ -621,7 +621,7 @@ public function getRoutes()
621621

622622
/**
623623
* Throw new Exception for Router Error
624-
*
624+
*
625625
* @param $message
626626
*
627627
* @return RouterException

src/Router/RouterCommand.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public function exception($message = '')
4747
* Run Route Middlewares
4848
*
4949
* @param $command
50-
* @param $path
50+
* @param $path
5151
* @param $namespace
52-
*
52+
*
5353
* @return mixed|void
5454
* @throws
5555
*/
@@ -69,18 +69,18 @@ public function beforeAfter($command, $path = '', $namespace = '')
6969
return $this->exception('handle() method is not found in <b>'.$command.'</b> class.');
7070
}
7171
}
72-
72+
7373
return;
7474
}
7575

7676
/**
7777
* Run Route Command; Controller or Closure
7878
*
79-
* @param $command
80-
* @param $params
81-
* @param $path
82-
* @param $namespace
83-
*
79+
* @param $command
80+
* @param $params
81+
* @param $path
82+
* @param $namespace
83+
*
8484
* @return void
8585
* @throws
8686
*/
@@ -99,13 +99,13 @@ public function runRoute($command, $params = null, $path = '', $namespace = '')
9999
);
100100
return;
101101
}
102-
102+
103103
return $this->exception($controllerMethod . ' method is not found in '.$controllerClass.' class.');
104104
} else {
105105
if (! is_null($params)) {
106106
echo call_user_func_array($command, $params);
107107
return;
108-
}
108+
}
109109

110110
echo call_user_func($command);
111111
}
@@ -114,10 +114,10 @@ public function runRoute($command, $params = null, $path = '', $namespace = '')
114114
/**
115115
* Resolve Controller or Middleware class.
116116
*
117-
* @param $class
118-
* @param $path
119-
* @param $namespace
120-
*
117+
* @param $class
118+
* @param $path
119+
* @param $namespace
120+
*
121121
* @return object
122122
* @throws
123123
*/
@@ -130,7 +130,7 @@ protected function resolveClass($class, $path, $namespace)
130130

131131
require_once($file);
132132
$class = $namespace . str_replace('/', '\\', $class);
133-
133+
134134
return new $class();
135135
}
136136
}

tests/RouterTest.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
<?php
22

3-
class RouterTest extends PHPUnit_Framework_TestCase
3+
namespace Buki\Tests;
4+
5+
use Buki\Router;
6+
use GuzzleHttp\Client;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class RouterTest extends TestCase
410
{
11+
protected $router;
12+
13+
protected $client;
14+
515
protected function setUp()
616
{
17+
$this->router = new Router();
18+
19+
$this->client = new Client();
20+
721
// Clear SCRIPT_NAME because bramus/router tries to guess the subfolder the script is run in
822
$_SERVER['SCRIPT_NAME'] = '/index.php';
923

@@ -19,8 +33,25 @@ protected function tearDown()
1933
// nothing
2034
}
2135

36+
public function testGetIndexRoute()
37+
{
38+
$request = $this->client->createRequest('GET', 'http://localhost:5000/');
39+
$response = $this->client->send($request);
40+
41+
$this->assertSame('Hello World!', (string) $response->getBody());
42+
}
43+
44+
/**
45+
* @expectedException GuzzleHttp\Exception\ClientException
46+
*/
47+
public function testGetNotFoundRoute()
48+
{
49+
$request = $this->client->createRequest('GET', 'http://localhost:5000/not/found');
50+
$response = $this->client->send($request);
51+
}
52+
2253
public function testInit()
2354
{
24-
$this->assertInstanceOf('\Buki\Router', new \Buki\Router());
55+
$this->assertInstanceOf('\Buki\Router', new Router());
2556
}
2657
}

tests/fixtures/TestController.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class TestController
4+
{
5+
public function main(Request $request)
6+
{
7+
return 'Hello World!';
8+
}
9+
}

tests/fixtures/server.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
require __DIR__ . '/../../vendor/autoload.php';
4+
5+
$router = new Buki\Router();
6+
7+
$router->get('/', function() {
8+
return 'Hello World!';
9+
});
10+
11+
$router->run();

0 commit comments

Comments
 (0)