Skip to content

Commit 5c7a11d

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

File tree

13 files changed

+128
-101
lines changed

13 files changed

+128
-101
lines changed

.github/workflows/run-tests.yml

Lines changed: 37 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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))
Lines changed: 1 addition & 1 deletion
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]
Lines changed: 21 additions & 0 deletions
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+
}
Lines changed: 13 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
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.

0 commit comments

Comments
 (0)