diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..9e620fd --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index ed19770..e8aa1af 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ /index.php /.htaccess .idea +.phpunit.result.cache diff --git a/composer.json b/composer.json index b6e4775..4d3c39a 100644 --- a/composer.json +++ b/composer.json @@ -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 } diff --git a/src/RouterCommand.php b/src/RouterCommand.php index abfa2b4..02c9ba8 100644 --- a/src/RouterCommand.php +++ b/src/RouterCommand.php @@ -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)) diff --git a/tests/example/.htaccess b/tests/Example/.htaccess similarity index 81% rename from tests/example/.htaccess rename to tests/Example/.htaccess index b48ebbd..48ca1ae 100644 --- a/tests/example/.htaccess +++ b/tests/Example/.htaccess @@ -1,5 +1,5 @@ RewriteEngine On -RewriteBase /tests +RewriteBase /tests/Example RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L] diff --git a/tests/Example/Controllers/TestController.php b/tests/Example/Controllers/TestController.php new file mode 100644 index 0000000..5cefbac --- /dev/null +++ b/tests/Example/Controllers/TestController.php @@ -0,0 +1,21 @@ +setContent('Foo in TestController!'); + + return $response; + } +} diff --git a/tests/Example/Middlewares/TestMiddleware.php b/tests/Example/Middlewares/TestMiddleware.php new file mode 100644 index 0000000..bff20d4 --- /dev/null +++ b/tests/Example/Middlewares/TestMiddleware.php @@ -0,0 +1,13 @@ + [ + '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(); diff --git a/tests/example/travis-ci-apache b/tests/Example/travis-ci-apache similarity index 100% rename from tests/example/travis-ci-apache rename to tests/Example/travis-ci-apache diff --git a/tests/example/www.conf.default b/tests/Example/www.conf.default similarity index 100% rename from tests/example/www.conf.default rename to tests/Example/www.conf.default diff --git a/tests/RouterTest.php b/tests/RouterTest.php index d3c753d..6edae06 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -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"); } } diff --git a/tests/example/Controllers/TestController.php b/tests/example/Controllers/TestController.php deleted file mode 100644 index bd78f72..0000000 --- a/tests/example/Controllers/TestController.php +++ /dev/null @@ -1,11 +0,0 @@ - [ - 'Controllers' => 'Controllers', - 'middlewares' => 'Middlewares', - ], - 'namespaces' => [ - 'Controllers' => 'Controllers\\', - 'middlewares' => 'Middlewares\\', - ], - 'base_folder' => __DIR__, - 'main_method' => 'main', -]; - -$router = new \Buki\Router\Router($params); - -$router->get('/', function() { - return 'Hello World!'; -}); - -$router->get('/controller', 'TestController@main'); - -$router->run();