Skip to content

Commit 5c7a11d

Browse files
committed
Added tests and workflow. Also, fixed some little bugs.
1 parent 2fa8fd5 commit 5c7a11d

13 files changed

+128
-101
lines changed

.github/workflows/run-tests.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
os: [ubuntu-latest, windows-latest]
12+
php: [7.2.5, 8.0]
13+
stability: [prefer-stable]
14+
15+
name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: ${{ matrix.php }}
25+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
26+
coverage: none
27+
28+
- name: Setup problem matchers
29+
run: |
30+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
31+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
32+
33+
- name: Install dependencies
34+
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction
35+
36+
- name: Execute tests
37+
run: composer test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/index.php
66
/.htaccess
77
.idea
8+
.phpunit.result.cache
89

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
"Buki\\Tests\\": "tests/"
3636
}
3737
},
38+
"scripts": {
39+
"test": "vendor/bin/phpunit",
40+
"coverage": "vendor/bin/phpunit --coverage-html coverage",
41+
"dev": "cd tests/Example && php -S 127.0.0.1:8000 -t ./"
42+
},
3843
"minimum-stability": "dev",
3944
"prefer-stable": true
4045
}

src/RouterCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ protected function resolveMiddleware(string $middleware)
360360
*/
361361
public function sendResponse($response)
362362
{
363-
if (is_array($response) || strpos($this->request->headers->get('Accept'), 'application/json') !== false) {
363+
if (is_array($response) || strpos($this->request->headers->get('Accept') ?? '', 'application/json') !== false) {
364364
$this->response->headers->set('Content-Type', 'application/json');
365365
return $this->response
366366
->setContent($response instanceof Response ? $response->getContent() : json_encode($response))
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RewriteEngine On
2-
RewriteBase /tests
2+
RewriteBase /tests/Example
33
RewriteCond %{REQUEST_FILENAME} !-f
44
RewriteCond %{REQUEST_FILENAME} !-d
55
RewriteRule ^ index.php [QSA,L]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Buki\Tests\Example\Controllers;
4+
5+
use Buki\Router\Http\Controller;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
class TestController extends Controller
9+
{
10+
public function main(): string
11+
{
12+
return 'controller route';
13+
}
14+
15+
public function foo(Response $response): Response
16+
{
17+
$response->setContent('Foo in TestController!');
18+
19+
return $response;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Buki\Tests\Example\Middlewares;
4+
5+
use Buki\Router\Http\Middleware;
6+
7+
class TestMiddleware extends Middleware
8+
{
9+
public function handle(): bool
10+
{
11+
return true;
12+
}
13+
}

tests/Example/index.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require __DIR__ . '/../../vendor/autoload.php';
4+
5+
$params = [
6+
'paths' => [
7+
'controllers' => __DIR__ . '/Controllers',
8+
'middlewares' => __DIR__ . '/Middlewares',
9+
],
10+
'namespaces' => [
11+
'controllers' => 'Buki\\Tests\\Example\\Controllers',
12+
'middlewares' => 'Buki\\Tests\\Example\\Middlewares',
13+
],
14+
'base_folder' => __DIR__,
15+
'main_method' => 'main',
16+
];
17+
18+
$router = new \Buki\Router\Router($params);
19+
20+
$router->get('/', function() {
21+
return 'Hello World!';
22+
});
23+
24+
$router->get('/test', 'TestController@main');
25+
26+
$router->controller('/controller', 'TestController');
27+
28+
$router->run();
File renamed without changes.
File renamed without changes.

tests/RouterTest.php

+21-62
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,52 @@
22

33
namespace Buki\Tests;
44

5-
use Buki\Router;
6-
use GuzzleHttp\Client;
5+
use Buki\Router\Router;
76
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\HttpFoundation\Request;
88

99
class RouterTest extends TestCase
1010
{
1111
protected $router;
1212

13-
protected $client;
13+
protected $request;
1414

15-
protected function setUp()
15+
protected function setUp(): void
1616
{
17-
$this->router = new Router();
18-
19-
$this->client = new Client();
17+
error_reporting(E_ALL);
18+
$this->request = Request::createFromGlobals();
19+
$this->router = new Router(
20+
[],
21+
$this->request
22+
);
2023

2124
// Clear SCRIPT_NAME because bramus/router tries to guess the subfolder the script is run in
22-
$_SERVER['SCRIPT_NAME'] = '/index.php';
25+
$this->request->server->set('SCRIPT_NAME', '/index.php');
26+
$this->request->server->set('SCRIPT_FILENAME', '/index.php');
2327

2428
// Default request method to GET
25-
$_SERVER['REQUEST_METHOD'] = 'GET';
29+
$this->request->server->set('REQUEST_METHOD', 'GET');
2630

2731
// Default SERVER_PROTOCOL method to HTTP/1.1
28-
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
32+
$this->request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
2933
}
3034

31-
protected function tearDown()
35+
protected function tearDown(): void
3236
{
3337
// nothing
3438
}
3539

36-
public function testGetIndexRoute()
37-
{
38-
$request = $this->client->createRequest('GET', 'http://localhost/tests/fixtures/');
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/tests/fixtures/not/found');
50-
$response = $this->client->send($request);
51-
}
52-
53-
public function testGetControllerRoute()
54-
{
55-
$request = $this->client->createRequest('GET', 'http://localhost/tests/fixtures/controller');
56-
$response = $this->client->send($request);
57-
58-
$this->assertSame('controller route', (string) $response->getBody());
59-
}
60-
6140
public function testInit()
6241
{
63-
$this->assertInstanceOf('\\Buki\\Router', new Router());
42+
$this->assertInstanceOf('\Buki\Router\Router', $this->router);
6443
}
6544

66-
public function testGetRoutes()
45+
public function testRouteCount()
6746
{
68-
$params = [
69-
'paths' => [
70-
'controllers' => 'controllers/',
71-
],
72-
'namespaces' => [
73-
'controllers' => 'Controllers\\',
74-
],
75-
'base_folder' => __DIR__,
76-
'main_method' => 'main',
77-
];
78-
$router = new Router($params);
79-
80-
$router->get('/', function() {
81-
return 'Hello World!';
82-
});
83-
84-
$router->get('/controllers', 'TestController@main');
85-
86-
$routes = $router->getRoutes();
47+
$this->router->get('/', 'HomeController@main');
48+
$this->router->get('/contact', 'HomeController@contact');
49+
$this->router->get('/about', 'HomeController@about');
8750

88-
$this->assertCount(2, $routes);
89-
$this->assertInstanceOf('\\Closure', $routes[0]['callback']);
90-
$this->assertSame('TestController@main', $routes[1]['callback']);
91-
$this->assertSame('GET', $routes[0]['method']);
92-
$this->assertSame('GET', $routes[1]['method']);
51+
$this->assertCount(3, $this->router->getRoutes(), "doesn't contains 2 routes");
9352
}
9453
}

tests/example/Controllers/TestController.php

-11
This file was deleted.

tests/example/index.php

-26
This file was deleted.

0 commit comments

Comments
 (0)