Skip to content

Commit

Permalink
Added tests and workflow. Also, fixed some little bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
izniburak committed Jan 22, 2022
1 parent 2fa8fd5 commit 5c7a11d
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 101 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests

on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.2.5, 8.0]
stability: [prefer-stable]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Execute tests
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
/index.php
/.htaccess
.idea
.phpunit.result.cache

5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"Buki\\Tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"coverage": "vendor/bin/phpunit --coverage-html coverage",
"dev": "cd tests/Example && php -S 127.0.0.1:8000 -t ./"
},
"minimum-stability": "dev",
"prefer-stable": true
}
2 changes: 1 addition & 1 deletion src/RouterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ protected function resolveMiddleware(string $middleware)
*/
public function sendResponse($response)
{
if (is_array($response) || strpos($this->request->headers->get('Accept'), 'application/json') !== false) {
if (is_array($response) || strpos($this->request->headers->get('Accept') ?? '', 'application/json') !== false) {
$this->response->headers->set('Content-Type', 'application/json');
return $this->response
->setContent($response instanceof Response ? $response->getContent() : json_encode($response))
Expand Down
2 changes: 1 addition & 1 deletion tests/example/.htaccess → tests/Example/.htaccess
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RewriteEngine On
RewriteBase /tests
RewriteBase /tests/Example
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
21 changes: 21 additions & 0 deletions tests/Example/Controllers/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Buki\Tests\Example\Controllers;

use Buki\Router\Http\Controller;
use Symfony\Component\HttpFoundation\Response;

class TestController extends Controller
{
public function main(): string
{
return 'controller route';
}

public function foo(Response $response): Response
{
$response->setContent('Foo in TestController!');

return $response;
}
}
13 changes: 13 additions & 0 deletions tests/Example/Middlewares/TestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Buki\Tests\Example\Middlewares;

use Buki\Router\Http\Middleware;

class TestMiddleware extends Middleware
{
public function handle(): bool
{
return true;
}
}
28 changes: 28 additions & 0 deletions tests/Example/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';

$params = [
'paths' => [
'controllers' => __DIR__ . '/Controllers',
'middlewares' => __DIR__ . '/Middlewares',
],
'namespaces' => [
'controllers' => 'Buki\\Tests\\Example\\Controllers',
'middlewares' => 'Buki\\Tests\\Example\\Middlewares',
],
'base_folder' => __DIR__,
'main_method' => 'main',
];

$router = new \Buki\Router\Router($params);

$router->get('/', function() {
return 'Hello World!';
});

$router->get('/test', 'TestController@main');

$router->controller('/controller', 'TestController');

$router->run();
File renamed without changes.
File renamed without changes.
83 changes: 21 additions & 62 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,52 @@

namespace Buki\Tests;

use Buki\Router;
use GuzzleHttp\Client;
use Buki\Router\Router;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

class RouterTest extends TestCase
{
protected $router;

protected $client;
protected $request;

protected function setUp()
protected function setUp(): void
{
$this->router = new Router();

$this->client = new Client();
error_reporting(E_ALL);
$this->request = Request::createFromGlobals();
$this->router = new Router(
[],
$this->request
);

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

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

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

protected function tearDown()
protected function tearDown(): void
{
// nothing
}

public function testGetIndexRoute()
{
$request = $this->client->createRequest('GET', 'http://localhost/tests/fixtures/');
$response = $this->client->send($request);

$this->assertSame('Hello World!', (string) $response->getBody());
}

/**
* @expectedException GuzzleHttp\Exception\ClientException
*/
public function testGetNotFoundRoute()
{
$request = $this->client->createRequest('GET', 'http://localhost/tests/fixtures/not/found');
$response = $this->client->send($request);
}

public function testGetControllerRoute()
{
$request = $this->client->createRequest('GET', 'http://localhost/tests/fixtures/controller');
$response = $this->client->send($request);

$this->assertSame('controller route', (string) $response->getBody());
}

public function testInit()
{
$this->assertInstanceOf('\\Buki\\Router', new Router());
$this->assertInstanceOf('\Buki\Router\Router', $this->router);
}

public function testGetRoutes()
public function testRouteCount()
{
$params = [
'paths' => [
'controllers' => 'controllers/',
],
'namespaces' => [
'controllers' => 'Controllers\\',
],
'base_folder' => __DIR__,
'main_method' => 'main',
];
$router = new Router($params);

$router->get('/', function() {
return 'Hello World!';
});

$router->get('/controllers', 'TestController@main');

$routes = $router->getRoutes();
$this->router->get('/', 'HomeController@main');
$this->router->get('/contact', 'HomeController@contact');
$this->router->get('/about', 'HomeController@about');

$this->assertCount(2, $routes);
$this->assertInstanceOf('\\Closure', $routes[0]['callback']);
$this->assertSame('TestController@main', $routes[1]['callback']);
$this->assertSame('GET', $routes[0]['method']);
$this->assertSame('GET', $routes[1]['method']);
$this->assertCount(3, $this->router->getRoutes(), "doesn't contains 2 routes");
}
}
11 changes: 0 additions & 11 deletions tests/example/Controllers/TestController.php

This file was deleted.

26 changes: 0 additions & 26 deletions tests/example/index.php

This file was deleted.

0 comments on commit 5c7a11d

Please sign in to comment.