UrlRuleTest.php 17.9 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php

namespace yiiunit\framework\web;

Qiang Xue committed
5
use yii\web\UrlManager;
Qiang Xue committed
6
use yii\web\UrlRule;
Qiang Xue committed
7
use yii\web\Request;
Alexander Makarov committed
8
use yiiunit\TestCase;
Qiang Xue committed
9

Alexander Makarov committed
10
class UrlRuleTest extends TestCase
Qiang Xue committed
11 12 13
{
	public function testCreateUrl()
	{
14
		$manager = new UrlManager(array('cache' => null));
Qiang Xue committed
15 16 17 18 19 20
		$suites = $this->getTestsForCreateUrl();
		foreach ($suites as $i => $suite) {
			list ($name, $config, $tests) = $suite;
			$rule = new UrlRule($config);
			foreach ($tests as $j => $test) {
				list ($route, $params, $expected) = $test;
Qiang Xue committed
21
				$url = $rule->createUrl($manager, $route, $params);
Qiang Xue committed
22 23 24 25 26
				$this->assertEquals($expected, $url, "Test#$i-$j: $name");
			}
		}
	}

Qiang Xue committed
27
	public function testParseRequest()
Qiang Xue committed
28
	{
29
		$manager = new UrlManager(array('cache' => null));
30
		$request = new Request(array('hostInfo' => 'http://en.example.com'));
Qiang Xue committed
31
		$suites = $this->getTestsForParseRequest();
Qiang Xue committed
32 33 34 35
		foreach ($suites as $i => $suite) {
			list ($name, $config, $tests) = $suite;
			$rule = new UrlRule($config);
			foreach ($tests as $j => $test) {
Qiang Xue committed
36
				$request->pathInfo = $test[0];
Qiang Xue committed
37 38
				$route = $test[1];
				$params = isset($test[2]) ? $test[2] : array();
Qiang Xue committed
39
				$result = $rule->parseRequest($manager, $request);
Qiang Xue committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
				if ($route === false) {
					$this->assertFalse($result, "Test#$i-$j: $name");
				} else {
					$this->assertEquals(array($route, $params), $result, "Test#$i-$j: $name");
				}
			}
		}
	}

	protected function getTestsForCreateUrl()
	{
		// structure of each test
		//   message for the test
		//   config for the URL rule
		//   list of inputs and outputs
		//     route
		//     params
		//     expected output
		return array(
			array(
				'empty pattern',
				array(
					'pattern' => '',
					'route' => 'post/index',
				),
				array(
					array('post/index', array(), ''),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), '?page=1'),
				),
			),
			array(
				'without param',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
				),
				array(
					array('post/index', array(), 'posts'),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), 'posts?page=1'),
				),
			),
Qiang Xue committed
83 84 85 86 87 88 89 90 91 92 93
			array(
				'parsing only',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
					'mode' => UrlRule::PARSING_ONLY,
				),
				array(
					array('post/index', array(), false),
				),
			),
Qiang Xue committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
			array(
				'with param',
				array(
					'pattern' => 'post/<page>',
					'route' => 'post/index',
				),
				array(
					array('post/index', array(), false),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), 'post/1'),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/1?tag=a'),
				),
			),
			array(
				'with param requirement',
				array(
					'pattern' => 'post/<page:\d+>',
					'route' => 'post/index',
				),
				array(
					array('post/index', array('page' => 'abc'), false),
					array('post/index', array('page' => 1), 'post/1'),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/1?tag=a'),
				),
			),
			array(
				'with multiple params',
				array(
					'pattern' => 'post/<page:\d+>-<tag>',
					'route' => 'post/index',
				),
				array(
					array('post/index', array('page' => '1abc'), false),
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/1-a'),
				),
			),
			array(
				'with optional param',
				array(
					'pattern' => 'post/<page:\d+>/<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/a'),
					array('post/index', array('page' => 2, 'tag' => 'a'), 'post/2/a'),
				),
			),
			array(
				'with optional param not in pattern',
				array(
					'pattern' => 'post/<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 2, 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/a'),
				),
			),
			array(
				'multiple optional params',
				array(
					'pattern' => 'post/<page:\d+>/<tag>/<sort:yes|no>',
					'route' => 'post/index',
					'defaults' => array('page' => 1, 'sort' => 'yes'),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a', 'sort' => 'YES'), false),
					array('post/index', array('page' => 1, 'tag' => 'a', 'sort' => 'yes'), 'post/a'),
					array('post/index', array('page' => 2, 'tag' => 'a', 'sort' => 'yes'), 'post/2/a'),
					array('post/index', array('page' => 2, 'tag' => 'a', 'sort' => 'no'), 'post/2/a/no'),
					array('post/index', array('page' => 1, 'tag' => 'a', 'sort' => 'no'), 'post/a/no'),
				),
			),
			array(
				'optional param and required param separated by dashes',
				array(
					'pattern' => 'post/<page:\d+>-<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/-a'),
					array('post/index', array('page' => 2, 'tag' => 'a'), 'post/2-a'),
				),
			),
			array(
				'optional param at the end',
				array(
					'pattern' => 'post/<tag>/<page:\d+>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/a'),
					array('post/index', array('page' => 2, 'tag' => 'a'), 'post/a/2'),
				),
			),
			array(
				'consecutive optional params',
				array(
					'pattern' => 'post/<page:\d+>/<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1, 'tag' => 'a'),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post'),
					array('post/index', array('page' => 2, 'tag' => 'a'), 'post/2'),
					array('post/index', array('page' => 1, 'tag' => 'b'), 'post/b'),
					array('post/index', array('page' => 2, 'tag' => 'b'), 'post/2/b'),
				),
			),
			array(
				'consecutive optional params separated by dash',
				array(
					'pattern' => 'post/<page:\d+>-<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1, 'tag' => 'a'),
				),
				array(
					array('post/index', array('page' => 1), false),
					array('post/index', array('page' => '1abc', 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a'), 'post/-'),
					array('post/index', array('page' => 1, 'tag' => 'b'), 'post/-b'),
					array('post/index', array('page' => 2, 'tag' => 'a'), 'post/2-'),
					array('post/index', array('page' => 2, 'tag' => 'b'), 'post/2-b'),
				),
			),
			array(
				'route has parameters',
				array(
					'pattern' => '<controller>/<action>',
					'route' => '<controller>/<action>',
					'defaults' => array(),
				),
				array(
					array('post/index', array('page' => 1), 'post/index?page=1'),
					array('module/post/index', array(), false),
				),
			),
			array(
				'route has parameters with regex',
				array(
					'pattern' => '<controller:post|comment>/<action>',
					'route' => '<controller>/<action>',
					'defaults' => array(),
				),
				array(
					array('post/index', array('page' => 1), 'post/index?page=1'),
					array('comment/index', array('page' => 1), 'comment/index?page=1'),
					array('test/index', array('page' => 1), false),
					array('post', array(), false),
					array('module/post/index', array(), false),
					array('post/index', array('controller' => 'comment'), 'post/index?controller=comment'),
				),
			),
			array(
				'route has default parameter',
				array(
					'pattern' => '<controller:post|comment>/<action>',
					'route' => '<controller>/<action>',
					'defaults' => array('action' => 'index'),
				),
				array(
					array('post/view', array('page' => 1), 'post/view?page=1'),
					array('comment/view', array('page' => 1), 'comment/view?page=1'),
					array('test/view', array('page' => 1), false),
Qiang Xue committed
275
					array('test/index', array('page' => 1), false),
Qiang Xue committed
276 277 278
					array('post/index', array('page' => 1), 'post?page=1'),
				),
			),
Qiang Xue committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
			array(
				'empty pattern with suffix',
				array(
					'pattern' => '',
					'route' => 'post/index',
					'suffix' => '.html',
				),
				array(
					array('post/index', array(), ''),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), '?page=1'),
				),
			),
			array(
				'regular pattern with suffix',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
					'suffix' => '.html',
				),
				array(
					array('post/index', array(), 'posts.html'),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), 'posts.html?page=1'),
				),
			),
			array(
				'empty pattern with slash suffix',
				array(
					'pattern' => '',
					'route' => 'post/index',
					'suffix' => '/',
				),
				array(
					array('post/index', array(), ''),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), '?page=1'),
				),
			),
			array(
				'regular pattern with slash suffix',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
					'suffix' => '/',
				),
				array(
					array('post/index', array(), 'posts/'),
					array('comment/index', array(), false),
					array('post/index', array('page' => 1), 'posts/?page=1'),
				),
			),
331 332 333
			array(
				'with host info',
				array(
Qiang Xue committed
334
					'pattern' => 'post/<page:\d+>/<tag>',
335 336
					'route' => 'post/index',
					'defaults' => array('page' => 1),
Qiang Xue committed
337
					'host' => 'http://<lang:en|fr>.example.com',
338 339 340 341 342 343
				),
				array(
					array('post/index', array('page' => 1, 'tag' => 'a'), false),
					array('post/index', array('page' => 1, 'tag' => 'a', 'lang' => 'en'), 'http://en.example.com/post/a'),
				),
			),
Qiang Xue committed
344 345 346
		);
	}

Qiang Xue committed
347
	protected function getTestsForParseRequest()
Qiang Xue committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	{
		// structure of each test
		//   message for the test
		//   config for the URL rule
		//   list of inputs and outputs
		//     pathInfo
		//     expected route, or false if the rule doesn't apply
		//     expected params, or not set if empty
		return array(
			array(
				'empty pattern',
				array(
					'pattern' => '',
					'route' => 'post/index',
				),
				array(
					array('', 'post/index'),
					array('a', false),
				),
			),
			array(
				'without param',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
				),
				array(
					array('posts', 'post/index'),
					array('a', false),
				),
			),
Qiang Xue committed
379 380 381 382 383 384 385 386 387 388 389
			array(
				'creation only',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
					'mode' => UrlRule::CREATION_ONLY,
				),
				array(
					array('posts', false),
				),
			),
Qiang Xue committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
			array(
				'with param',
				array(
					'pattern' => 'post/<page>',
					'route' => 'post/index',
				),
				array(
					array('post/1', 'post/index', array('page' => '1')),
					array('post/a', 'post/index', array('page' => 'a')),
					array('post', false),
					array('posts', false),
				),
			),
			array(
				'with param requirement',
				array(
					'pattern' => 'post/<page:\d+>',
					'route' => 'post/index',
				),
				array(
					array('post/1', 'post/index', array('page' => '1')),
					array('post/a', false),
					array('post/1/a', false),
				),
			),
			array(
				'with multiple params',
				array(
					'pattern' => 'post/<page:\d+>-<tag>',
					'route' => 'post/index',
				),
				array(
					array('post/1-a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/a', false),
					array('post/1', false),
					array('post/1/a', false),
				),
			),
			array(
				'with optional param',
				array(
					'pattern' => 'post/<page:\d+>/<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/1/a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/2/a', 'post/index', array('page' => '2', 'tag' => 'a')),
					array('post/a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/1', 'post/index', array('page' => '1', 'tag' => '1')),
				),
			),
			array(
				'with optional param not in pattern',
				array(
					'pattern' => 'post/<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/1', 'post/index', array('page' => '1', 'tag' => '1')),
					array('post', false),
				),
			),
			array(
				'multiple optional params',
				array(
					'pattern' => 'post/<page:\d+>/<tag>/<sort:yes|no>',
					'route' => 'post/index',
					'defaults' => array('page' => 1, 'sort' => 'yes'),
				),
				array(
					array('post/1/a/yes', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'yes')),
					array('post/2/a/no', 'post/index', array('page' => '2', 'tag' => 'a', 'sort' => 'no')),
					array('post/2/a', 'post/index', array('page' => '2', 'tag' => 'a', 'sort' => 'yes')),
					array('post/a/no', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'no')),
					array('post/a', 'post/index', array('page' => '1', 'tag' => 'a', 'sort' => 'yes')),
					array('post', false),
				),
			),
			array(
				'optional param and required param separated by dashes',
				array(
					'pattern' => 'post/<page:\d+>-<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/1-a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/2-a', 'post/index', array('page' => '2', 'tag' => 'a')),
					array('post/-a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/a', false),
					array('post-a', false),
				),
			),
			array(
				'optional param at the end',
				array(
					'pattern' => 'post/<tag>/<page:\d+>',
					'route' => 'post/index',
					'defaults' => array('page' => 1),
				),
				array(
					array('post/a/1', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/a/2', 'post/index', array('page' => '2', 'tag' => 'a')),
					array('post/a', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/2', 'post/index', array('page' => '1', 'tag' => '2')),
					array('post', false),
				),
			),
			array(
				'consecutive optional params',
				array(
					'pattern' => 'post/<page:\d+>/<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1, 'tag' => 'a'),
				),
				array(
					array('post/2/b', 'post/index', array('page' => '2', 'tag' => 'b')),
					array('post/2', 'post/index', array('page' => '2', 'tag' => 'a')),
					array('post', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post/b', 'post/index', array('page' => '1', 'tag' => 'b')),
					array('post//b', false),
				),
			),
			array(
				'consecutive optional params separated by dash',
				array(
					'pattern' => 'post/<page:\d+>-<tag>',
					'route' => 'post/index',
					'defaults' => array('page' => 1, 'tag' => 'a'),
				),
				array(
					array('post/2-b', 'post/index', array('page' => '2', 'tag' => 'b')),
					array('post/2-', 'post/index', array('page' => '2', 'tag' => 'a')),
					array('post/-b', 'post/index', array('page' => '1', 'tag' => 'b')),
					array('post/-', 'post/index', array('page' => '1', 'tag' => 'a')),
					array('post', false),
				),
			),
Qiang Xue committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
			array(
				'route has parameters',
				array(
					'pattern' => '<controller>/<action>',
					'route' => '<controller>/<action>',
					'defaults' => array(),
				),
				array(
					array('post/index', 'post/index'),
					array('module/post/index', false),
				),
			),
			array(
				'route has parameters with regex',
				array(
					'pattern' => '<controller:post|comment>/<action>',
					'route' => '<controller>/<action>',
					'defaults' => array(),
				),
				array(
					array('post/index', 'post/index'),
					array('comment/index', 'comment/index'),
					array('test/index', false),
					array('post', false),
					array('module/post/index', false),
				),
			),
			array(
				'route has default parameter',
				array(
					'pattern' => '<controller:post|comment>/<action>',
					'route' => '<controller>/<action>',
					'defaults' => array('action' => 'index'),
				),
				array(
					array('post/view', 'post/view'),
					array('comment/view', 'comment/view'),
					array('test/view', false),
					array('post', 'post/index'),
					array('posts', false),
					array('test', false),
					array('index', false),
				),
			),
Qiang Xue committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
			array(
				'empty pattern with suffix',
				array(
					'pattern' => '',
					'route' => 'post/index',
					'suffix' => '.html',
				),
				array(
					array('', 'post/index'),
					array('.html', false),
					array('a.html', false),
				),
			),
			array(
				'regular pattern with suffix',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
					'suffix' => '.html',
				),
				array(
					array('posts.html', 'post/index'),
					array('posts', false),
					array('posts.HTML', false),
					array('a.html', false),
					array('a', false),
				),
			),
			array(
				'empty pattern with slash suffix',
				array(
					'pattern' => '',
					'route' => 'post/index',
					'suffix' => '/',
				),
				array(
					array('', 'post/index'),
					array('a', false),
				),
			),
			array(
				'regular pattern with slash suffix',
				array(
					'pattern' => 'posts',
					'route' => 'post/index',
					'suffix' => '/',
				),
				array(
					array('posts', 'post/index'),
					array('a', false),
				),
			),
627 628 629
			array(
				'with host info',
				array(
Qiang Xue committed
630
					'pattern' => 'post/<page:\d+>',
631
					'route' => 'post/index',
Qiang Xue committed
632
					'host' => 'http://<lang:en|fr>.example.com',
633 634 635 636 637 638 639
				),
				array(
					array('post/1', 'post/index', array('page' => '1', 'lang' => 'en')),
					array('post/a', false),
					array('post/1/a', false),
				),
			),
Qiang Xue committed
640 641 642
		);
	}
}