Files
majestic/tests/app/router/RouteTest.php

93 lines
3.0 KiB
PHP
Raw Normal View History

2011-10-31 19:25:22 +04:00
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
2011-11-01 19:00:57 +04:00
* @since 2011-10-31
2011-10-31 19:25:22 +04:00
*
2011-11-01 19:00:57 +04:00
* Unit tests for Router class
2011-10-31 19:25:22 +04:00
*/
require_once dirname(__FILE__) . '/../../../app/router/Route.php';
class RouteTest extends PHPUnit_Framework_TestCase
{
public function testConstructNoParams()
{
$route = new Route('index', 'user');
$this->assertSame('userAction', $route->getAction());
$this->assertSame('Layout', $route->getLayout());
2011-10-31 19:25:22 +04:00
$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->assertSame(array('name' => 'tony', 'role' => 'user'), $route->getParams());
2011-10-31 19:25:22 +04:00
}
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->assertSame(array('id' => '142'), $route->getParams());
2011-10-31 19:25:22 +04:00
}
public function testMatchDiffPregParts()
{
$route = new Route('user/account/(?<key>value)', 'user');
$this->assertFalse($route->match(array('user', 'account', 'wrong')));
$this->assertSame(0, count($route->getParams()));
2011-10-31 19:25:22 +04:00
}
public function testMatchPregPasses()
{
$route = new Route('user/account/(?<key>[a-z]+)', 'user');
$this->assertTrue($route->match(array('user', 'account', 'value')));
$this->assertSame(array('key' => 'value'), $route->getParams());
$this->assertSame(1, count($route->getParams()));
2011-10-31 19:25:22 +04:00
}
/**
* @TODO Need check empty parameters from constructor Route
2011-10-31 19:25:22 +04:00
*/
public function testMatchEmptyRequest()
{
$route = new Route('', 'user', array('name' => 'tony', 'role' => 'user'));
2011-12-02 12:25:47 +04:00
$this->setExpectedException('PHPUnit_Framework_Error');
2011-10-31 19:25:22 +04:00
$route->match('');
}
2012-05-25 16:31:08 +04:00
public function testGetUri()
{
$route = 'myroute';
$route_mock = $this->getMockBuilder('Route')
->disableOriginalConstructor()
->setMethods(array('__construct'))
->getMock();
$reflection = new ReflectionProperty('Route', 'route');
$reflection->setAccessible(true);
$reflection->setValue($route_mock, $route);
$this->assertEquals('/' . $route, $route_mock->getUri());
}
2011-10-31 19:25:22 +04:00
}