/tests/TwigRuntimeExtensionTest.php

https://github.com/slimphp/Twig-View · PHP · 300 lines · 181 code · 49 blank · 70 comment · 2 complexity · 5b8899acfb2a84ab5552515aec199291 MD5 · raw file

  1. <?php
  2. /**
  3. * Slim Framework (http://slimframework.com)
  4. *
  5. * @license https://github.com/slimphp/Twig-View/blob/master/LICENSE.md (MIT License)
  6. */
  7. declare(strict_types=1);
  8. namespace Slim\Tests;
  9. use Psr\Http\Message\ResponseFactoryInterface;
  10. use Psr\Http\Message\UriInterface;
  11. use Slim\Interfaces\CallableResolverInterface;
  12. use Slim\Interfaces\RouteInterface;
  13. use Slim\Routing\RouteCollector;
  14. use Slim\Views\TwigRuntimeExtension;
  15. class TwigRuntimeExtensionTest extends TestCase
  16. {
  17. /**
  18. * Create a route collector using the given base path.
  19. *
  20. * Note that this method would create mocks for the ResponseFactoryInterface
  21. * and the CallableResolverInterface injected into the constructor of the
  22. * RouteCollector.
  23. *
  24. * @param string $basePath
  25. *
  26. * @return RouteCollector
  27. */
  28. protected function createRouteCollector(string $basePath): RouteCollector
  29. {
  30. $routeCollector = new RouteCollector(
  31. $this->createMock(ResponseFactoryInterface::class),
  32. $this->createMock(CallableResolverInterface::class)
  33. );
  34. if ($basePath) {
  35. $routeCollector->setBasePath($basePath);
  36. }
  37. return $routeCollector;
  38. }
  39. /**
  40. * Map a route to the given route collector.
  41. *
  42. * @param RouteCollector $routeCollector
  43. * @param array $methods
  44. * @param string $pattern
  45. * @param string $routeName
  46. *
  47. * @return RouteInterface
  48. */
  49. protected function mapRouteCollectorRoute(
  50. RouteCollector $routeCollector,
  51. array $methods,
  52. string $pattern,
  53. string $routeName
  54. ): RouteInterface {
  55. $route = $routeCollector->map($methods, $pattern, 'handler');
  56. $route->setName($routeName);
  57. return $route;
  58. }
  59. public function isCurrentUrlProvider()
  60. {
  61. return [
  62. ['/hello/{name}', ['name' => 'world'], '/hello/world', '/base-path', true],
  63. ['/hello/{name}', ['name' => 'world'], '/hello/world', '', true],
  64. ['/hello/{name}', ['name' => 'world'], '/hello/john', '/base-path', false],
  65. ['/hello/{name}', ['name' => 'world'], '/hello/john', '', false],
  66. ];
  67. }
  68. /**
  69. * @dataProvider isCurrentUrlProvider
  70. *
  71. * @param string $pattern
  72. * @param array $data
  73. * @param string $path
  74. * @param string|null $basePath
  75. * @param bool $expected
  76. */
  77. public function testIsCurrentUrl(string $pattern, array $data, string $path, ?string $basePath, bool $expected)
  78. {
  79. $routeCollector = $this->createRouteCollector($basePath);
  80. $routeParser = $routeCollector->getRouteParser();
  81. $routeName = 'route';
  82. $this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);
  83. $uriProphecy = $this->prophesize(UriInterface::class);
  84. /** @noinspection PhpUndefinedMethodInspection */
  85. $uriProphecy
  86. ->getPath()
  87. ->willReturn($path)
  88. ->shouldBeCalledOnce();
  89. /** @var UriInterface $uri */
  90. $uri = $uriProphecy->reveal();
  91. $extension = new TwigRuntimeExtension($routeParser, $uri, $basePath);
  92. $result = $extension->isCurrentUrl($routeName, $data);
  93. $this->assertEquals($expected, $result);
  94. }
  95. public function currentUrlProvider()
  96. {
  97. return [
  98. ['/hello/{name}', 'http://example.com/hello/world?a=b', '', true],
  99. ['/hello/{name}', 'http://example.com/hello/world', '', false],
  100. ['/base-path/hello/{name}', 'http://example.com/base-path/hello/world?a=b', '/base-path', true],
  101. ['/base-path/hello/{name}', 'http://example.com/base-path/hello/world', '/base-path', false],
  102. ];
  103. }
  104. /**
  105. * @dataProvider currentUrlProvider
  106. *
  107. * @param string $pattern
  108. * @param string $url
  109. * @param string $basePath
  110. * @param bool $withQueryString
  111. */
  112. public function testCurrentUrl(string $pattern, string $url, string $basePath, bool $withQueryString)
  113. {
  114. $routeCollector = $this->createRouteCollector($basePath);
  115. $routeParser = $routeCollector->getRouteParser();
  116. $routeName = 'route';
  117. $this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);
  118. $uriProphecy = $this->prophesize(UriInterface::class);
  119. $path = parse_url($url, PHP_URL_PATH);
  120. $query = parse_url($url, PHP_URL_QUERY);
  121. /** @noinspection PhpUndefinedMethodInspection */
  122. $uriProphecy
  123. ->getPath()
  124. ->willReturn($path)
  125. ->shouldBeCalledOnce();
  126. /** @noinspection PhpUndefinedMethodInspection */
  127. $uriProphecy
  128. ->getQuery()
  129. ->willReturn($query)
  130. ->shouldBeCalledOnce();
  131. $expected = $basePath . $path;
  132. if ($withQueryString) {
  133. $expected .= '?' . $query;
  134. }
  135. /** @var UriInterface $uri */
  136. $uri = $uriProphecy->reveal();
  137. $extension = new TwigRuntimeExtension($routeParser, $uri, $basePath);
  138. $result = $extension->getCurrentUrl($withQueryString);
  139. $this->assertEquals($expected, $result);
  140. }
  141. public function urlForProvider()
  142. {
  143. return [
  144. ['/hello/{name}', ['name' => 'world'], [], '', '/hello/world'],
  145. ['/hello/{name}', ['name' => 'world'], [], '/base-path', '/base-path/hello/world'],
  146. ['/hello/{name}', ['name' => 'world'], ['foo' => 'bar'], '', '/hello/world?foo=bar'],
  147. ['/hello/{name}', ['name' => 'world'], ['foo' => 'bar'], '/base-path', '/base-path/hello/world?foo=bar'],
  148. ];
  149. }
  150. /**
  151. * @dataProvider urlForProvider
  152. *
  153. * @param string $pattern
  154. * @param array $routeData
  155. * @param array $queryParams
  156. * @param string $basePath
  157. * @param string $expectedUrl
  158. */
  159. public function testUrlFor(
  160. string $pattern,
  161. array $routeData,
  162. array $queryParams,
  163. string $basePath,
  164. string $expectedUrl
  165. ) {
  166. $routeName = 'route';
  167. $routeCollector = $this->createRouteCollector($basePath);
  168. $this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);
  169. $uriProphecy = $this->prophesize(UriInterface::class);
  170. /** @var UriInterface $uri */
  171. $uri = $uriProphecy->reveal();
  172. $extension = new TwigRuntimeExtension($routeCollector->getRouteParser(), $uri, $routeCollector->getBasePath());
  173. $this->assertEquals($expectedUrl, $extension->urlFor($routeName, $routeData, $queryParams));
  174. }
  175. public function fullUrlForProvider()
  176. {
  177. return [
  178. ['/hello/{name}', ['name' => 'world'], [], '', 'http://localhost/hello/world'],
  179. ['/hello/{name}', ['name' => 'world'], [], '/base-path', 'http://localhost/base-path/hello/world'],
  180. ['/hello/{name}', ['name' => 'world'], ['foo' => 'bar'], '', 'http://localhost/hello/world?foo=bar'],
  181. [
  182. '/hello/{name}',
  183. ['name' => 'world'],
  184. ['foo' => 'bar'],
  185. '/base-path',
  186. 'http://localhost/base-path/hello/world?foo=bar',
  187. ],
  188. ];
  189. }
  190. /**
  191. * @dataProvider fullUrlForProvider
  192. *
  193. * @param string $pattern
  194. * @param array $routeData
  195. * @param array $queryParams
  196. * @param string $basePath
  197. * @param string $expectedFullUrl
  198. */
  199. public function testFullUrlFor(
  200. string $pattern,
  201. array $routeData,
  202. array $queryParams,
  203. string $basePath,
  204. string $expectedFullUrl
  205. ) {
  206. $routeName = 'route';
  207. $routeCollector = $this->createRouteCollector($basePath);
  208. $this->mapRouteCollectorRoute($routeCollector, ['GET'], $pattern, $routeName);
  209. $uriProphecy = $this->prophesize(UriInterface::class);
  210. /** @noinspection PhpUndefinedMethodInspection */
  211. $uriProphecy->getScheme()
  212. ->willReturn('http')
  213. ->shouldBeCalledOnce();
  214. /** @noinspection PhpUndefinedMethodInspection */
  215. $uriProphecy->getAuthority()
  216. ->willReturn('localhost')
  217. ->shouldBeCalledOnce();
  218. /** @var UriInterface $uri */
  219. $uri = $uriProphecy->reveal();
  220. $extension = new TwigRuntimeExtension($routeCollector->getRouteParser(), $uri, $routeCollector->getBasePath());
  221. $this->assertEquals($expectedFullUrl, $extension->fullUrlFor($routeName, $routeData, $queryParams));
  222. }
  223. public function testSetUri()
  224. {
  225. $basePath = '';
  226. $routeCollector = $this->createRouteCollector($basePath);
  227. $uri = $this->createMock(UriInterface::class);
  228. $extension = new TwigRuntimeExtension(
  229. $routeCollector->getRouteParser(),
  230. $uri,
  231. $routeCollector->getBasePath()
  232. );
  233. $uri2 = $this->createMock(UriInterface::class);
  234. $extension->setUri($uri2);
  235. $this->assertEquals($uri2, $extension->getUri());
  236. }
  237. public function testSetBasePath()
  238. {
  239. $basePath = '';
  240. $routeCollector = $this->createRouteCollector($basePath);
  241. $uri = $this->createMock(UriInterface::class);
  242. $extension = new TwigRuntimeExtension(
  243. $routeCollector->getRouteParser(),
  244. $uri,
  245. $routeCollector->getBasePath()
  246. );
  247. $basePath = '/base-path';
  248. $extension->setBasePath($basePath);
  249. $this->assertEquals($basePath, $extension->getBasePath());
  250. }
  251. }