|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @see https://github.com/zendframework/zend-expressive for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ZendTest\Expressive\Template\ZendView; |
| 11 | + |
| 12 | +use ArrayObject; |
| 13 | +use PHPUnit_Framework_TestCase as TestCase; |
| 14 | +use Zend\Expressive\Exception; |
| 15 | +use Zend\Expressive\Router\RouterInterface; |
| 16 | +use Zend\Expressive\Router\RouteResult; |
| 17 | +use Zend\Expressive\Template\ZendView\UrlHelper; |
| 18 | + |
| 19 | +class UrlHelperTest extends TestCase |
| 20 | +{ |
| 21 | + public function setUp() |
| 22 | + { |
| 23 | + $this->router = $this->prophesize(RouterInterface::class); |
| 24 | + } |
| 25 | + |
| 26 | + public function createHelper() |
| 27 | + { |
| 28 | + return new UrlHelper($this->router->reveal()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testRaisesExceptionOnInvocationIfNoRouteProvidedAndNoResultPresent() |
| 32 | + { |
| 33 | + $helper = $this->createHelper(); |
| 34 | + $this->setExpectedException(Exception\RenderingException::class, 'use matched result'); |
| 35 | + $helper(); |
| 36 | + } |
| 37 | + |
| 38 | + public function testRaisesExceptionOnInvocationIfNoRouteProvidedAndResultIndicatesFailure() |
| 39 | + { |
| 40 | + $result = $this->prophesize(RouteResult::class); |
| 41 | + $result->isFailure()->willReturn(true); |
| 42 | + $helper = $this->createHelper(); |
| 43 | + $helper->setRouteResult($result->reveal()); |
| 44 | + $this->setExpectedException(Exception\RenderingException::class, 'routing failed'); |
| 45 | + $helper(); |
| 46 | + } |
| 47 | + |
| 48 | + public function testRaisesExceptionOnInvocationIfRouterCannotGenerateUriForRouteProvided() |
| 49 | + { |
| 50 | + $this->router->generateUri('foo', [])->willThrow(Exception\RuntimeException::class); |
| 51 | + $helper = $this->createHelper(); |
| 52 | + $this->setExpectedException(Exception\RuntimeException::class); |
| 53 | + $helper('foo'); |
| 54 | + } |
| 55 | + |
| 56 | + public function testWhenNoRouteProvidedTheHelperUsesComposedResultToGenerateUrl() |
| 57 | + { |
| 58 | + $result = $this->prophesize(RouteResult::class); |
| 59 | + $result->isFailure()->willReturn(false); |
| 60 | + $result->getMatchedRouteName()->willReturn('foo'); |
| 61 | + $result->getMatchedParams()->willReturn(['bar' => 'baz']); |
| 62 | + |
| 63 | + $this->router->generateUri('foo', ['bar' => 'baz'])->willReturn('URL'); |
| 64 | + |
| 65 | + $helper = $this->createHelper(); |
| 66 | + $helper->setRouteResult($result->reveal()); |
| 67 | + |
| 68 | + $this->assertEquals('URL', $helper()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testWhenNoRouteProvidedTheHelperMergesPassedParametersWithResultParametersToGenerateUrl() |
| 72 | + { |
| 73 | + $result = $this->prophesize(RouteResult::class); |
| 74 | + $result->isFailure()->willReturn(false); |
| 75 | + $result->getMatchedRouteName()->willReturn('foo'); |
| 76 | + $result->getMatchedParams()->willReturn(['bar' => 'baz']); |
| 77 | + |
| 78 | + $this->router->generateUri('foo', ['bar' => 'baz', 'baz' => 'bat'])->willReturn('URL'); |
| 79 | + |
| 80 | + $helper = $this->createHelper(); |
| 81 | + $helper->setRouteResult($result->reveal()); |
| 82 | + |
| 83 | + $this->assertEquals('URL', $helper(null, ['baz' => 'bat'])); |
| 84 | + } |
| 85 | + |
| 86 | + public function testWhenRouteProvidedTheHelperDelegatesToTheRouterToGenerateUrl() |
| 87 | + { |
| 88 | + $this->router->generateUri('foo', ['bar' => 'baz'])->willReturn('URL'); |
| 89 | + $helper = $this->createHelper(); |
| 90 | + $this->assertEquals('URL', $helper('foo', ['bar' => 'baz'])); |
| 91 | + } |
| 92 | +} |
0 commit comments