3 namespace PHPRouterTest\Test;
6 use PHPRouter\Test\Fixtures\InvokableController;
7 use PHPUnit_Framework_TestCase;
9 class RouteTest extends PHPUnit_Framework_TestCase
11 private $routeUsing__invoke;
12 private $routeWithParameters;
13 private $routeInvalid;
15 protected function setUp()
17 $this->routeUsing__invoke = new Route(
20 '_controller' => '\PHPRouter\Test\Fixtures\InvokableController',
27 $this->routeWithParameters = new Route(
28 '/page/:page_id/:page_size',
30 '_controller' => '\PHPRouter\Test\Fixtures\SomeController::page',
34 'target' => 'thisIsAString',
39 $this->routeInvalid = new Route(
42 '_controller' => '\PHPRouter\Test\Fixtures\TestController::page',
46 'target' => 'thisIsAString',
52 public function testGetUrl()
54 $this->assertEquals('/page/:page_id/:page_size', $this->routeWithParameters->getUrl());
57 public function testSetUrl()
59 $this->routeWithParameters->setUrl('/pages/:page_name/');
60 $this->assertEquals('/pages/:page_name/', $this->routeWithParameters->getUrl());
62 $this->routeWithParameters->setUrl('/pages/:page_name');
63 $this->assertEquals('/pages/:page_name/', $this->routeWithParameters->getUrl());
66 public function testGetMethods()
68 $this->assertEquals(array('GET'), $this->routeWithParameters->getMethods());
71 public function testSetMethods()
73 $this->routeWithParameters->setMethods(array('POST'));
74 $this->assertEquals(array('POST'), $this->routeWithParameters->getMethods());
76 $this->routeWithParameters->setMethods(array('GET', 'POST', 'PUT', 'DELETE'));
77 $this->assertEquals(array('GET', 'POST', 'PUT', 'DELETE'), $this->routeWithParameters->getMethods());
80 public function testGetTarget()
82 $this->assertEquals('thisIsAString', $this->routeWithParameters->getTarget());
85 public function testSetTarget()
87 $this->routeWithParameters->setTarget('ThisIsAnotherString');
88 $this->assertEquals('ThisIsAnotherString', $this->routeWithParameters->getTarget());
91 public function testGetName()
93 $this->assertEquals('page', $this->routeWithParameters->getName());
96 public function testSetName()
98 $this->routeWithParameters->setName('pageroute');
99 $this->assertEquals('pageroute', $this->routeWithParameters->getName());
102 public function testGetAction()
104 $this->assertEquals('page', $this->routeWithParameters->getAction());
105 $this->assertEquals(null, $this->routeUsing__invoke->getAction());
108 public function testGetClass()
110 $this->assertEquals('\PHPRouter\Test\Fixtures\SomeController', $this->routeWithParameters->getClass());
111 $this->assertEquals('\PHPRouter\Test\Fixtures\InvokableController', $this->routeUsing__invoke->getClass());
114 public function testGetValidController()
116 $this->assertEquals("\PHPRouter\Test\Fixtures\SomeController::page",
117 $this->routeWithParameters->getValidController());
118 $this->assertNull($this->routeInvalid->getValidController());
121 public function testSetGetParameters()
123 $param = array("page_id" => 123);
124 $this->routeWithParameters->setParameters($param);
126 $this->assertEquals($param, $this->routeWithParameters->getParameters());
129 public function testGetRegex()
131 $regex = $this->routeWithParameters->getRegex();
133 $this->assertEquals("/page/([\\w-%]+)/([\\w-%]+)", $regex);
136 public function testGetFilterRegex()
139 ":user" => "([A-Z][a-z]+)",
140 ":id" => "([1-9]\\d)",
143 $this->routeUsing__invoke->setFilters($filters);
145 $regex = $this->routeUsing__invoke->getRegex();
147 $this->assertEquals("/home/([A-Z][a-z]+)/([1-9]\\d)", $regex);
150 public function testDispatch()
152 $message = "welcome";
158 $this->routeUsing__invoke->setParameters($param);
160 $this->assertEquals(" {$param['id']}:{$param['user']}", $this->routeUsing__invoke->dispatch());
162 $instance = new InvokableController();
164 $instance->message = $message;
166 $this->assertEquals("$message {$param['id']}:{$param['user']}", $this->routeUsing__invoke->dispatch($instance));