| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
echo "testing Router\n"; |
|---|
| 15 |
require_once 'testenv.php'; |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
if (!defined("PHPUnit2_MAIN_METHOD")) { |
|---|
| 19 |
define("PHPUnit2_MAIN_METHOD", "RouterTest::main"); |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
require_once "PHPUnit2/Framework/TestCase.php"; |
|---|
| 23 |
require_once "PHPUnit2/Framework/TestSuite.php"; |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
require_once "PHPUnit2/Framework/IncompleteTestError.php"; |
|---|
| 27 |
|
|---|
| 28 |
require_once "router.php"; |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
class RouterTest extends PHPUnit2_Framework_TestCase { |
|---|
| 35 |
|
|---|
| 36 |
* Runs the test methods of this class. |
|---|
| 37 |
* |
|---|
| 38 |
* @access public |
|---|
| 39 |
* @static |
|---|
| 40 |
*/ |
|---|
| 41 |
public static function main() { |
|---|
| 42 |
require_once "PHPUnit2/TextUI/TestRunner.php"; |
|---|
| 43 |
|
|---|
| 44 |
$suite = new PHPUnit2_Framework_TestSuite("RouterTest"); |
|---|
| 45 |
$result = PHPUnit2_TextUI_TestRunner::run($suite); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* Sets up the fixture, for example, open a network connection. |
|---|
| 50 |
* This method is called before a test is executed. |
|---|
| 51 |
* |
|---|
| 52 |
* @access protected |
|---|
| 53 |
*/ |
|---|
| 54 |
protected function setUp() { |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* Tears down the fixture, for example, close a network connection. |
|---|
| 59 |
* This method is called after a test is executed. |
|---|
| 60 |
* |
|---|
| 61 |
* @access protected |
|---|
| 62 |
*/ |
|---|
| 63 |
protected function tearDown() { |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Test build_route_regexp(). |
|---|
| 68 |
*/ |
|---|
| 69 |
public function testBuild_route_regexp() { |
|---|
| 70 |
$r = new Router; |
|---|
| 71 |
|
|---|
| 72 |
$regexp = $r->build_route_regexp(':foo/:bar'); |
|---|
| 73 |
$this->assertEquals($regexp, |
|---|
| 74 |
'^[a-z0-9_\-]+\/[a-z0-9_\-]+$'); |
|---|
| 75 |
|
|---|
| 76 |
$regexp = $r->build_route_regexp(':foo/:bar/:mumble'); |
|---|
| 77 |
$this->assertEquals($regexp, |
|---|
| 78 |
'^[a-z0-9_\-]+\/[a-z0-9_\-]+\/[a-z0-9_\-]+$'); |
|---|
| 79 |
|
|---|
| 80 |
$regexp = $r->build_route_regexp(':foo/bar/:mumble'); |
|---|
| 81 |
$this->assertEquals($regexp, |
|---|
| 82 |
'^[a-z0-9_\-]+\/bar\/[a-z0-9_\-]+$'); |
|---|
| 83 |
|
|---|
| 84 |
$regexp = $r->build_route_regexp('foo/bar'); |
|---|
| 85 |
$this->assertEquals($regexp, |
|---|
| 86 |
'^foo\/bar$'); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
* Test default route table |
|---|
| 91 |
*/ |
|---|
| 92 |
public function testDefault_route() { |
|---|
| 93 |
$r = new Router; |
|---|
| 94 |
|
|---|
| 95 |
$route = $r->find_route('a/b/mumble'); |
|---|
| 96 |
$this->assertEquals(':controller/:action/:id', $route['path']); |
|---|
| 97 |
$this->assertNull($route['params']); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
* Test route table with one simple entry besides default |
|---|
| 102 |
*/ |
|---|
| 103 |
public function testSimple_route() { |
|---|
| 104 |
$r = new Router; |
|---|
| 105 |
|
|---|
| 106 |
$r->connect(':foo/:bar/mumble', array('mumble route')); |
|---|
| 107 |
|
|---|
| 108 |
$r->connect(':controller/:action/:id', 'not-an-array'); |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
$route = $r->find_route('a/b/mumble'); |
|---|
| 112 |
$this->assertEquals(':foo/:bar/mumble', $route['path']); |
|---|
| 113 |
$this->assertEquals(array('mumble route'), $route['params']); |
|---|
| 114 |
$selected = $r->get_selected_route(); |
|---|
| 115 |
$this->assertEquals(':foo/:bar/mumble', $selected['path']); |
|---|
| 116 |
$this->assertEquals(array('mumble route'), $selected['params']); |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
$route = $r->find_route('a/b/c'); |
|---|
| 120 |
$this->assertEquals(':controller/:action/:id', $route['path']); |
|---|
| 121 |
$this->assertNull($route['params']); |
|---|
| 122 |
$selected = $r->get_selected_route(); |
|---|
| 123 |
$this->assertEquals(':controller/:action/:id', $selected['path']); |
|---|
| 124 |
$this->assertNull($selected['params']); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* Test route table with one regexp entry besides default |
|---|
| 129 |
*/ |
|---|
| 130 |
public function testRegexp_route() { |
|---|
| 131 |
$r = new Router; |
|---|
| 132 |
|
|---|
| 133 |
$r->connect(':foo/:bar/\?(catalog|part)number=.*', |
|---|
| 134 |
array('number route')); |
|---|
| 135 |
$r->connect(':controller/:action/:id', array('default route')); |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
$route = $r->find_route('a/b/?catalognumber=17'); |
|---|
| 139 |
$this->assertEquals(':foo/:bar/\?(catalog|part)number=.*', |
|---|
| 140 |
$route['path']); |
|---|
| 141 |
$this->assertEquals(array('number route'), $route['params']); |
|---|
| 142 |
$route = $r->find_route('a/b/?partnumber=123-456'); |
|---|
| 143 |
$this->assertEquals(':foo/:bar/\?(catalog|part)number=.*', |
|---|
| 144 |
$route['path']); |
|---|
| 145 |
$this->assertEquals(array('number route'), $route['params']); |
|---|
| 146 |
$route = $r->find_route('a/b/?personnumber=156'); |
|---|
| 147 |
$this->assertEquals(':controller/:action/:id', $route['path']); |
|---|
| 148 |
$this->assertEquals(array('default route'), $route['params']); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
* Test route table with route with empty path |
|---|
| 153 |
*/ |
|---|
| 154 |
public function testEmpty_route() { |
|---|
| 155 |
$r = new Router; |
|---|
| 156 |
|
|---|
| 157 |
$r->connect('', array('empty route')); |
|---|
| 158 |
$route = $r->find_route(''); |
|---|
| 159 |
$this->assertEquals('', $route['path']); |
|---|
| 160 |
$this->assertEquals(array('empty route'), $route['params']); |
|---|
| 161 |
|
|---|
| 162 |
$route = $r->find_route('mumble/foo'); |
|---|
| 163 |
$this->assertNull($route); |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
if (PHPUnit2_MAIN_METHOD == "RouterTest::main") { |
|---|
| 169 |
RouterTest::main(); |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
?> |
|---|
| 180 |
|
|---|