szurubooru/tests/RouterTest.php

125 lines
3.2 KiB
PHP
Raw Normal View History

2014-08-29 11:20:08 +02:00
<?php
namespace Szurubooru\Tests;
use Szurubooru\Router;
use Szurubooru\Tests\AbstractTestCase;
use Szurubooru\Tests\TestController;
2014-08-29 11:20:08 +02:00
final class PostDaoTest extends AbstractTestCase
2014-08-29 11:20:08 +02:00
{
public function testParameterlessHandling()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$testOk = false;
$router->get('/test', function() use (&$testOk) { $testOk = true; });
$router->handle('GET', '/test');
$this->assertTrue($testOk);
}
public function testTrailingSlashes()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$testOk = false;
$router->get('/test', function() use (&$testOk) { $testOk = true; });
$router->handle('GET', '/test/');
$this->assertTrue($testOk);
}
public function testUnhandledMethod()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
2014-08-30 13:02:03 +02:00
$this->setExpectedException(\DomainException::class);
2014-08-29 11:20:08 +02:00
$router->handle('POST', '/test');
}
public function testUnhandledQuery()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
2014-08-30 13:02:03 +02:00
$this->setExpectedException(\DomainException::class);
2014-08-29 11:20:08 +02:00
$router->handle('GET', '/test2');
}
public function testTwoMethodsHandling()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$testOk = false;
$router->get('/test', function() { $this->fail('Route shouldn\'t be executed'); });
$router->post('/test', function() use (&$testOk) { $testOk = true; });
$router->handle('POST', '/test');
$this->assertTrue($testOk);
}
public function testParameterHandling()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$testOk = false;
2014-11-22 12:44:45 +01:00
$router->get('/tests/:id', function($args) use (&$testOk) {
extract($args);
2014-08-29 11:20:08 +02:00
$this->assertEquals($id, 'test_id');
$testOk = true; });
$router->handle('GET', '/tests/test_id');
$this->assertTrue($testOk);
}
public function testTwoParameterHandling()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$testOk = false;
2014-11-22 12:44:45 +01:00
$router->get('/tests/:id/:page', function($args) use (&$testOk) {
extract($args);
2014-08-29 11:20:08 +02:00
$this->assertEquals($id, 'test_id');
$this->assertEquals($page, 'test_page');
$testOk = true; });
$router->handle('GET', '/tests/test_id/test_page');
$this->assertTrue($testOk);
}
public function testMissingParameterHandling()
{
$router = new Router;
2014-08-29 11:20:08 +02:00
$testOk = false;
2014-11-22 12:44:45 +01:00
$router->get('/tests/:id', function($args) use (&$testOk) {
extract($args);
2014-08-29 11:20:08 +02:00
$this->assertEquals($id, 'test_id');
2014-11-22 12:44:45 +01:00
$this->assertFalse(isset($page));
2014-08-29 11:20:08 +02:00
$testOk = true; });
$router->handle('GET', '/tests/test_id');
$this->assertTrue($testOk);
}
public function testOutputHandling()
{
$router = new Router;
$router->get('/test', function() { return 'ok'; });
$output = $router->handle('GET', '/test');
$this->assertEquals('ok', $output);
}
2014-08-30 13:02:03 +02:00
public function testRoutingToClassMethods()
{
$router = new Router;
2014-08-30 13:02:03 +02:00
$testController = new TestController();
$router->get('/normal', [$testController, 'normalRoute']);
$router->get('/static', [TestController::class, 'staticRoute']);
2014-08-30 13:02:03 +02:00
$this->assertEquals('normal', $router->handle('GET', '/normal'));
$this->assertEquals('static', $router->handle('GET', '/static'));
}
}
class TestController
{
public function normalRoute()
{
return 'normal';
}
public static function staticRoute()
{
return 'static';
}
2014-08-29 11:20:08 +02:00
}