From 21e194dc22f7da0c9000593796f616c8a1ba8fe6 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Mon, 31 Oct 2011 19:25:22 +0400 Subject: [PATCH] Route class tested --- tests/app/router/RouteTest.php | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/app/router/RouteTest.php diff --git a/tests/app/router/RouteTest.php b/tests/app/router/RouteTest.php new file mode 100644 index 0000000..4605c84 --- /dev/null +++ b/tests/app/router/RouteTest.php @@ -0,0 +1,79 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage UnitTests + * @since 2011-10-11 + * + * Unit tests for Route class + */ + +require_once dirname(__FILE__) . '/../../../app/router/Route.php'; + +class RouteTest extends PHPUnit_Framework_TestCase +{ + + public function testConstructNoParams() + { + $route = new Route('index', 'user'); + $this->assertEquals('userAction', $route->getAction()); + $this->assertEquals('Layout', $route->getLayout()); + $this->assertEmpty($route->getParams()); + $this->assertAttributeEquals('index', 'route', $route); + } + + public function testConstructWithParams() + { + $route = new Route('index', 'user', array('name' => 'tony', 'role' => 'user')); + $this->assertArrayHasKey('role', $route->getParams()); + $this->assertEquals(array('name' => 'tony', 'role' => 'user'), $route->getParams()); + } + + public function testMatchDiffCount() + { + $route = new Route('user/account/id/142', 'user', array('name' => 'tony', 'role' => 'user')); + $this->assertFalse($route->match(array('user', 'account'))); + } + + /** + * @TODO: Route::match() - need to trim left-right slashes + */ + public function testMatchDiffParts() + { + $route = new Route('user/account', 'user'); + $this->assertFalse($route->match(array('user', 'login'))); + } + + public function testMatchVariable() + { + $route = new Route('user/account/:id', 'user'); + $this->assertTrue($route->match(array('user', 'account', '142'))); + $this->assertEquals(array('id' => '142'), $route->getParams()); + } + + public function testMatchDiffPregParts() + { + $route = new Route('user/account/(?value)', 'user'); + $this->assertFalse($route->match(array('user', 'account', 'wrong'))); + $this->assertEquals(0, count($route->getParams())); + } + + public function testMatchPregPasses() + { + $route = new Route('user/account/(?[a-z]+)', 'user'); + $this->assertTrue($route->match(array('user', 'account', 'value'))); + $this->assertEquals(array('key' => 'value'), $route->getParams()); + $this->assertEquals(1, count($route->getParams())); + } + + /** + * @expectedException Exception + */ + public function testMatchEmptyRequest() + { + $route = new Route('', 'user', array('name' => 'tony', 'role' => 'user')); + $route->match(''); + } +} \ No newline at end of file