/usr/share/php/kohana3.2/system/tests/kohana/RouteTest.php is in libkohana3.2-core-php 3.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 83 84 85 86 87 88 89 90 91 92 93 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 275 276 277 278 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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 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 379 380 381 382 383 384 385 386 387 388 389 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 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 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 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | <?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Description of RouteTest
*
* @group kohana
* @group kohana.core
* @group kohana.core.route
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @author BRMatt <matthew@sigswitch.com>
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
include Kohana::find_file('tests', 'test_data/callback_routes');
class Kohana_RouteTest extends Unittest_TestCase
{
/**
* Remove all caches
*/
public function setUp()
{
parent::setUp();
$this->cleanCacheDir();
}
/**
* Removes cache files created during tests
*/
public function tearDown()
{
parent::tearDown();
$this->cleanCacheDir();
}
/**
* If Route::get() is asked for a route that does not exist then
* it should throw a Kohana_Exception
*
* Note use of @expectedException
*
* @test
* @covers Route::get
* @expectedException Kohana_Exception
*/
public function test_get_throws_exception_if_route_dnx()
{
Route::get('HAHAHAHAHAHAHAHAHA');
}
/**
* Route::all() should return all routes defined via Route::set()
* and not through new Route()
*
* @test
* @covers Route::all
*/
public function test_all_returns_all_defined_routes()
{
$defined_routes = self::readAttribute('Route', '_routes');
$this->assertSame($defined_routes, Route::all());
}
/**
* Route::name() should fetch the name of a passed route
* If route is not found then it should return FALSE
*
* @TODO: This test needs to segregate the Route::$_routes singleton
* @test
* @covers Route::name
*/
public function test_name_returns_routes_name_or_false_if_dnx()
{
$route = Route::set('flamingo_people', 'flamingo/dance');
$this->assertSame('flamingo_people', Route::name($route));
$route = new Route('dance/dance');
$this->assertFalse(Route::name($route));
}
/**
* If Route::cache() was able to restore routes from the cache then
* it should return TRUE and load the cached routes
*
* @test
* @covers Route::cache
*/
public function test_cache_stores_route_objects()
{
$routes = Route::all();
// First we create the cache
Route::cache(TRUE);
// Now lets modify the "current" routes
Route::set('nonsensical_route', 'flabbadaga/ding_dong');
// Then try and load said cache
$this->assertTrue(Route::cache());
// Check the route cache flag
$this->assertTrue(Route::$cache);
// And if all went ok the nonsensical route should be gone...
$this->assertEquals($routes, Route::all());
}
/**
* Route::cache() should return FALSE if cached routes could not be found
*
* The cache is cleared before and after each test in setUp tearDown
* by cleanCacheDir()
*
* @test
* @covers Route::cache
*/
public function test_cache_returns_false_if_cache_dnx()
{
$this->assertSame(FALSE, Route::cache(), 'Route cache was not empty');
// Check the route cache flag
$this->assertFalse(Route::$cache);
}
/**
* If the constructor is passed a NULL uri then it should assume it's
* being loaded from the cache & therefore shouldn't override the cached attributes
*
* @test
* @covers Route::__construct
*/
public function test_constructor_returns_if_uri_is_null()
{
// We use a mock object to make sure that the route wasn't recompiled
$route = $this->getMock('Route', array('_compile'), array(), '', FALSE);
$route
->expects($this->never())
->method('_compile');
$route->__construct(NULL,NULL);
$this->assertAttributeSame('', '_uri', $route);
$this->assertAttributeSame(array(), '_regex', $route);
$this->assertAttributeSame(array('action' => 'index', 'host' => FALSE), '_defaults', $route);
$this->assertAttributeSame(NULL, '_route_regex', $route);
}
/**
* Provider for test_constructor_only_changes_custom_regex_if_passed
*
* @return array
*/
public function provider_constructor_only_changes_custom_regex_if_passed()
{
return array(
array('<controller>/<action>', '<controller>/<action>'),
array(array('Route_Holder', 'default_callback'), array('Route_Holder', 'default_callback')),
);
}
/**
* The constructor should only use custom regex if passed a non-empty array
*
* Technically we can't "test" this as the default regex is an empty array, this
* is purely for improving test coverage
*
* @dataProvider provider_constructor_only_changes_custom_regex_if_passed
*
* @test
* @covers Route::__construct
*/
public function test_constructor_only_changes_custom_regex_if_passed($uri, $uri2)
{
$route = new Route($uri, array());
$this->assertAttributeSame(array(), '_regex', $route);
$route = new Route($uri2, NULL);
$this->assertAttributeSame(array(), '_regex', $route);
}
/**
* When we pass custom regex to the route's constructor it should it
* in leu of the default. This does not apply to callback/lambda routes
*
* @test
* @covers Route::__construct
* @covers Route::compile
*/
public function test_route_uses_custom_regex_passed_to_constructor()
{
$regex = array('id' => '[0-9]{1,2}');
$route = new Route('<controller>(/<action>(/<id>))', $regex);
$this->assertAttributeSame($regex, '_regex', $route);
$this->assertAttributeContains(
$regex['id'],
'_route_regex',
$route
);
}
/**
* Provider for test_matches_returns_false_on_failure
*
* @return array
*/
public function provider_matches_returns_false_on_failure()
{
return array(
array('projects/(<project_id>/(<controller>(/<action>(/<id>))))', 'apple/pie'),
array(array('Route_Holder', 'default_callback'), 'apple/pie'),
);
}
/**
* Route::matches() should return false if the route doesn't match against a uri
*
* @dataProvider provider_matches_returns_false_on_failure
*
* @test
* @covers Route::matches
*/
public function test_matches_returns_false_on_failure($uri, $match)
{
$route = new Route($uri);
$this->assertSame(FALSE, $route->matches($match));
}
/**
* Provider for test_matches_returns_array_of_parameters_on_successful_match
*
* @return array
*/
public function provider_matches_returns_array_of_parameters_on_successful_match()
{
return array(
array(
'(<controller>(/<action>(/<id>)))',
'welcome/index',
'welcome',
'index',
),
array(
array('Route_Holder', 'matches_returns_array_of_parameters_on_successful_match'),
'apple/pie',
'welcome',
'index',
),
);
}
/**
* Route::matches() should return an array of parameters when a match is made
* An parameters that are not matched should not be present in the array of matches
*
* @dataProvider provider_matches_returns_array_of_parameters_on_successful_match
*
* @test
* @covers Route::matches
*/
public function test_matches_returns_array_of_parameters_on_successful_match($uri, $m, $c, $a)
{
$route = new Route($uri);
$matches = $route->matches($m);
$this->assertInternalType('array', $matches);
$this->assertArrayHasKey('controller', $matches);
$this->assertArrayHasKey('action', $matches);
$this->assertArrayNotHasKey('id', $matches);
// $this->assertSame(5, count($matches));
$this->assertSame($c, $matches['controller']);
$this->assertSame($a, $matches['action']);
}
/**
* Provider for test_matches_returns_array_of_parameters_on_successful_match
*
* @return array
*/
public function provider_defaults_are_used_if_params_arent_specified()
{
return array(
array(
'<controller>(/<action>(/<id>))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'index',
'unit/test/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'welcome',
),
array(
'(<controller>(/<action>(/<id>)))',
NULL,
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'index',
'unit/test/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'',
),
array(
array('Route_Holder', 'default_return_callback'),
'(<controller>(/<action>(/<id>)))',
array('controller' => 'welcome', 'action' => 'index'),
'welcome',
'index',
'unit/test/1',
array(
'controller' => 'unit',
'action' => 'test',
'id' => '1'
),
'',
),
);
}
/**
* Defaults specified with defaults() should be used if their values aren't
* present in the uri
*
* @dataProvider provider_defaults_are_used_if_params_arent_specified
*
* @test
* @covers Route::matches
*/
public function test_defaults_are_used_if_params_arent_specified($uri, $regex, $defaults, $c, $a, $test_uri, $test_uri_array, $default_uri)
{
$route = new Route($uri, $regex);
$route->defaults($defaults);
$this->assertSame($defaults, $route->defaults());
$matches = $route->matches($default_uri);
$this->assertInternalType('array', $matches);
$this->assertArrayHasKey('controller', $matches);
$this->assertArrayHasKey('action', $matches);
$this->assertArrayNotHasKey('id', $matches);
// $this->assertSame(4, count($matches));
$this->assertSame($c, $matches['controller']);
$this->assertSame($a, $matches['action']);
$this->assertSame($test_uri, $route->uri($test_uri_array));
$this->assertSame($default_uri, $route->uri());
}
/**
* Provider for test_required_parameters_are_needed
*
* @return array
*/
public function provider_required_parameters_are_needed()
{
return array(
array(
'admin(/<controller>(/<action>(/<id>)))',
'admin',
'admin/users/add',
),
array(
array('Route_Holder', 'required_parameters_are_needed'),
'admin',
'admin/users/add',
),
);
}
/**
* This tests that routes with required parameters will not match uris without them present
*
* @dataProvider provider_required_parameters_are_needed
*
* @test
* @covers Route::matches
*/
public function test_required_parameters_are_needed($uri, $matches_route1, $matches_route2)
{
$route = new Route($uri);
$this->assertFalse($route->matches(''));
$matches = $route->matches($matches_route1);
$this->assertInternalType('array', $matches);
$matches = $route->matches($matches_route2);
$this->assertInternalType('array', $matches);
// $this->assertSame(5, count($matches));
$this->assertArrayHasKey('controller', $matches);
$this->assertArrayHasKey('action', $matches);
}
/**
* Provider for test_required_parameters_are_needed
*
* @return array
*/
public function provider_reverse_routing_returns_routes_uri_if_route_is_static()
{
return array(
array(
'info/about_us',
NULL,
'info/about_us',
array('some' => 'random', 'params' => 'to confuse'),
),
array(
array('Route_Holder', 'reverse_routing_returns_routes_uri_if_route_is_static'),
'info/about_us',
'info/about_us',
array('some' => 'random', 'params' => 'to confuse'),
),
);
}
/**
* This tests the reverse routing returns the uri specified in the route
* if it's a static route
*
* A static route is a route without any parameters
*
* @dataProvider provider_reverse_routing_returns_routes_uri_if_route_is_static
*
* @test
* @covers Route::uri
*/
public function test_reverse_routing_returns_routes_uri_if_route_is_static($uri, $regex, $target_uri, $uri_params)
{
$route = new Route($uri, $regex);
$this->assertSame($target_uri, $route->uri($uri_params));
}
/**
* Provider for test_uri_throws_exception_if_required_params_are_missing
*
* @return array
*/
public function provider_uri_throws_exception_if_required_params_are_missing()
{
return array(
array(
'<controller>(/<action)',
NULL,
array('action' => 'awesome-action'),
),
array(
array('Route_Holder', 'default_return_callback'),
'<controller>(/<action)',
array('action' => 'awesome-action'),
),
);
}
/**
* When Route::uri is working on a uri that requires certain parameters to be present
* (i.e. <controller> in '<controller(/<action)') then it should throw an exception
* if the param was not provided
*
* @dataProvider provider_uri_throws_exception_if_required_params_are_missing
*
* @test
* @covers Route::uri
*/
public function test_uri_throws_exception_if_required_params_are_missing($uri, $regex, $uri_array)
{
$route = new Route($uri, $regex);
try
{
$route->uri($uri_array);
$this->fail('Route::uri should throw exception if required param is not provided');
}
catch(Exception $e)
{
$this->assertInstanceOf('Kohana_Exception', $e);
// Check that the error in question is about the controller param
$this->assertContains('controller', $e->getMessage());
}
}
/**
* Provider for test_uri_fills_required_uri_segments_from_params
*
* @return array
*/
public function provider_uri_fills_required_uri_segments_from_params()
{
return array(
array(
'<controller>/<action>(/<id>)',
NULL,
'users/edit',
array(
'controller' => 'users',
'action' => 'edit',
),
'users/edit/god',
array(
'controller' => 'users',
'action' => 'edit',
'id' => 'god',
),
),
array(
array('Route_Holder', 'default_return_callback'),
'<controller>/<action>(/<id>)',
'users/edit',
array(
'controller' => 'users',
'action' => 'edit',
),
'users/edit/god',
array(
'controller' => 'users',
'action' => 'edit',
'id' => 'god',
),
),
);
}
/**
* The logic for replacing required segments is separate (but similar) to that for
* replacing optional segments.
*
* This test asserts that Route::uri will replace required segments with provided
* params
*
* @dataProvider provider_uri_fills_required_uri_segments_from_params
*
* @test
* @covers Route::uri
*/
public function test_uri_fills_required_uri_segments_from_params($uri, $regex, $uri_string1, $uri_array1, $uri_string2, $uri_array2)
{
$route = new Route($uri, $regex);
$this->assertSame(
$uri_string1,
$route->uri($uri_array1)
);
$this->assertSame(
$uri_string2,
$route->uri($uri_array2)
);
}
/**
* Provides test data for test_composing_url_from_route()
* @return array
*/
public function provider_composing_url_from_route()
{
return array(
array('/'),
array('/news/view/42', array('controller' => 'news', 'action' => 'view', 'id' => 42)),
array('http://kohanaframework.org/news', array('controller' => 'news'), 'http')
);
}
/**
* Tests Route::url()
*
* Checks the url composing from specific route via Route::url() shortcut
*
* @test
* @dataProvider provider_composing_url_from_route
* @param string $expected
* @param array $params
* @param boolean $protocol
*/
public function test_composing_url_from_route($expected, $params = NULL, $protocol = NULL)
{
Route::set('foobar', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
)
);
$this->setEnvironment(array(
'_SERVER' => array('HTTP_HOST' => 'kohanaframework.org'),
'Kohana::$base_url' => '/',
'Kohana::$index_file' => '',
));
$this->assertSame($expected, Route::url('foobar', $params, $protocol));
}
/**
* Tests Route::compile()
*
* Makes sure that compile will use custom regex if specified
*
* @test
* @covers Route::compile
*/
public function test_compile_uses_custom_regex_if_specificed()
{
$compiled = Route::compile(
'<controller>(/<action>(/<id>))',
array(
'controller' => '[a-z]+',
'id' => '\d+',
)
);
$this->assertSame('#^(?P<controller>[a-z]+)(?:/(?P<action>[^/.,;?\n]++)(?:/(?P<id>\d+))?)?$#uD', $compiled);
}
/**
* Tests Route::is_external(), ensuring the host can return
* whether internal or external host
*/
public function test_is_external_route_from_host()
{
// Setup local route
Route::set('internal', 'local/test/route')
->defaults(array(
'controller' => 'foo',
'action' => 'bar'
)
);
// Setup external route
Route::set('external', 'local/test/route')
->defaults(array(
'controller' => 'foo',
'action' => 'bar',
'host' => 'http://kohanaframework.org'
)
);
// Test internal route
$this->assertFalse(Route::get('internal')->is_external());
// Test external route
$this->assertTrue(Route::get('external')->is_external());
}
/**
* Provider for test_external_route_includes_params_in_uri
*
* @return array
*/
public function provider_external_route_includes_params_in_uri()
{
return array(
array(
'<controller>/<action>',
array(
'controller' => 'foo',
'action' => 'bar',
'host' => 'kohanaframework.org'
),
'http://kohanaframework.org/foo/bar'
),
array(
'<controller>/<action>',
array(
'controller' => 'foo',
'action' => 'bar',
'host' => 'http://kohanaframework.org'
),
'http://kohanaframework.org/foo/bar'
),
array(
'foo/bar',
array(
'controller' => 'foo',
'host' => 'http://kohanaframework.org'
),
'http://kohanaframework.org/foo/bar'
),
);
}
/**
* Tests the external route include route parameters
*
* @dataProvider provider_external_route_includes_params_in_uri
*/
public function test_external_route_includes_params_in_uri($route, $defaults, $expected_uri)
{
Route::set('test', $route)
->defaults($defaults);
$this->assertSame($expected_uri, Route::get('test')->uri());
}
}
|