|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) Tyler Sommer |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE file |
| 7 | + * that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Nice\Tests\Router\UrlGenerator; |
| 11 | + |
| 12 | +use Nice\Router\UrlGenerator\SimpleUrlGenerator; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | + |
| 15 | +class SimpleUrlGeneratorTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Test basic functionality |
| 19 | + */ |
| 20 | + public function testFunctionality() |
| 21 | + { |
| 22 | + $generator = $this->getGenerator(); |
| 23 | + |
| 24 | + $this->assertEquals('/', $generator->generate('home')); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Test base URL functionality |
| 29 | + */ |
| 30 | + public function testBaseUrl() |
| 31 | + { |
| 32 | + $generator = $this->getGenerator(); |
| 33 | + |
| 34 | + $request = Request::create('https://www.example.com/subdirectory/somepage', 'GET', array(), array(), array(), array( |
| 35 | + 'SCRIPT_FILENAME' => 'index.php', |
| 36 | + 'PHP_SELF' => '/subdirectory/index.php' |
| 37 | + )); |
| 38 | + $generator->setRequest($request); |
| 39 | + |
| 40 | + $this->assertEquals('/subdirectory/', $generator->generate('home')); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test absolute URL functionality |
| 45 | + */ |
| 46 | + public function testAbsoluteUrl() |
| 47 | + { |
| 48 | + $generator = $this->getGenerator(); |
| 49 | + |
| 50 | + $request = Request::create('https://www.example.com/subdirectory/somepage', 'GET', array(), array(), array(), array( |
| 51 | + 'SCRIPT_FILENAME' => 'index.php', |
| 52 | + 'PHP_SELF' => '/subdirectory/index.php' |
| 53 | + )); |
| 54 | + $generator->setRequest($request); |
| 55 | + |
| 56 | + $this->assertEquals('https://www.example.com/subdirectory/', $generator->generate('home', array(), true)); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test a dynamic route |
| 61 | + */ |
| 62 | + public function testDynamicRoute() |
| 63 | + { |
| 64 | + $generator = $this->getGenerator(array( |
| 65 | + 'user_edit' => array( |
| 66 | + 'params' => array( |
| 67 | + 'id' |
| 68 | + ), |
| 69 | + 'path' => '/user/{id}/edit' |
| 70 | + ) |
| 71 | + )); |
| 72 | + |
| 73 | + $this->assertEquals('/user/123/edit', $generator->generate('user_edit', array('id' => 123))); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Test a dynamic route with a missing parameter |
| 78 | + */ |
| 79 | + public function testDynamicRouteWithMissingParameter() |
| 80 | + { |
| 81 | + $generator = $this->getGenerator(array( |
| 82 | + 'user_edit' => array( |
| 83 | + 'params' => array( |
| 84 | + 'id' |
| 85 | + ), |
| 86 | + 'path' => '/user/{id}/edit' |
| 87 | + ) |
| 88 | + )); |
| 89 | + |
| 90 | + $this->setExpectedException('RuntimeException', 'Missing required parameter'); |
| 91 | + |
| 92 | + $this->assertEquals('/user/123/edit', $generator->generate('user_edit')); |
| 93 | + } |
| 94 | + |
| 95 | + private function getGenerator(array $routes = array('home' => '/')) |
| 96 | + { |
| 97 | + $dataGenerator = $this->getMockForAbstractClass('Nice\Router\UrlGenerator\DataGeneratorInterface'); |
| 98 | + $dataGenerator->expects($this->once()) |
| 99 | + ->method('getData') |
| 100 | + ->will($this->returnValue($routes)); |
| 101 | + |
| 102 | + return new SimpleUrlGenerator($dataGenerator); |
| 103 | + } |
| 104 | +} |
0 commit comments