2014-08-29 11:20:08 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Router;
|
|
|
|
use Szurubooru\Tests\AbstractTestCase;
|
|
|
|
use Szurubooru\Tests\TestController;
|
2014-08-29 11:20:08 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
final class PostDaoTest extends AbstractTestCase
|
2014-08-29 11:20:08 +02:00
|
|
|
{
|
|
|
|
public function testParameterlessHandling()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$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);
|
|
|
|
}
|
2014-08-30 12:52:44 +02:00
|
|
|
|
|
|
|
public function testOutputHandling()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$router = new Router;
|
2014-08-30 12:52:44 +02:00
|
|
|
$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()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$router = new Router;
|
2014-08-30 13:02:03 +02:00
|
|
|
$testController = new TestController();
|
|
|
|
$router->get('/normal', [$testController, 'normalRoute']);
|
2014-10-08 14:47:47 +02:00
|
|
|
$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
|
|
|
}
|