PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/silex/silex/tests/Silex/Tests/RouterTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site
PHP | 273 lines | 197 code | 60 blank | 16 comment | 1 complexity | d59b06f39e7d62ef6f94847f08604615 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Silex framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Silex\Tests;
  11. use Silex\Application;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. /**
  17. * Router test cases.
  18. *
  19. * @author Igor Wiedler <igor@wiedler.ch>
  20. */
  21. class RouterTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testMapRouting()
  24. {
  25. $app = new Application();
  26. $app->match('/foo', function () {
  27. return 'foo';
  28. });
  29. $app->match('/bar', function () {
  30. return 'bar';
  31. });
  32. $app->match('/', function () {
  33. return 'root';
  34. });
  35. $this->checkRouteResponse($app, '/foo', 'foo');
  36. $this->checkRouteResponse($app, '/bar', 'bar');
  37. $this->checkRouteResponse($app, '/', 'root');
  38. }
  39. public function testStatusCode()
  40. {
  41. $app = new Application();
  42. $app->put('/created', function () {
  43. return new Response('', 201);
  44. });
  45. $app->match('/forbidden', function () {
  46. return new Response('', 403);
  47. });
  48. $app->match('/not_found', function () {
  49. return new Response('', 404);
  50. });
  51. $request = Request::create('/created', 'put');
  52. $response = $app->handle($request);
  53. $this->assertEquals(201, $response->getStatusCode());
  54. $request = Request::create('/forbidden');
  55. $response = $app->handle($request);
  56. $this->assertEquals(403, $response->getStatusCode());
  57. $request = Request::create('/not_found');
  58. $response = $app->handle($request);
  59. $this->assertEquals(404, $response->getStatusCode());
  60. }
  61. public function testRedirect()
  62. {
  63. $app = new Application();
  64. $app->match('/redirect', function () {
  65. return new RedirectResponse('/target');
  66. });
  67. $app->match('/redirect2', function () use ($app) {
  68. return $app->redirect('/target2');
  69. });
  70. $request = Request::create('/redirect');
  71. $response = $app->handle($request);
  72. $this->assertTrue($response->isRedirect('/target'));
  73. $request = Request::create('/redirect2');
  74. $response = $app->handle($request);
  75. $this->assertTrue($response->isRedirect('/target2'));
  76. }
  77. /**
  78. * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  79. */
  80. public function testMissingRoute()
  81. {
  82. $app = new Application();
  83. $app['exception_handler']->disable();
  84. $request = Request::create('/baz');
  85. $app->handle($request);
  86. }
  87. public function testMethodRouting()
  88. {
  89. $app = new Application();
  90. $app->match('/foo', function () {
  91. return 'foo';
  92. });
  93. $app->match('/bar', function () {
  94. return 'bar';
  95. })->method('GET|POST');
  96. $app->get('/resource', function () {
  97. return 'get resource';
  98. });
  99. $app->post('/resource', function () {
  100. return 'post resource';
  101. });
  102. $app->put('/resource', function () {
  103. return 'put resource';
  104. });
  105. $app->delete('/resource', function () {
  106. return 'delete resource';
  107. });
  108. $this->checkRouteResponse($app, '/foo', 'foo');
  109. $this->checkRouteResponse($app, '/bar', 'bar');
  110. $this->checkRouteResponse($app, '/bar', 'bar', 'post');
  111. $this->checkRouteResponse($app, '/resource', 'get resource');
  112. $this->checkRouteResponse($app, '/resource', 'post resource', 'post');
  113. $this->checkRouteResponse($app, '/resource', 'put resource', 'put');
  114. $this->checkRouteResponse($app, '/resource', 'delete resource', 'delete');
  115. }
  116. public function testRequestShouldBeStoredRegardlessOfRouting()
  117. {
  118. $app = new Application();
  119. $app->get('/foo', function () use ($app) {
  120. return new Response($app['request']->getRequestUri());
  121. });
  122. $app->error(function ($e) use ($app) {
  123. return new Response($app['request']->getRequestUri());
  124. });
  125. foreach (array('/foo', '/bar') as $path) {
  126. $request = Request::create($path);
  127. $response = $app->handle($request);
  128. $this->assertContains($path, $response->getContent());
  129. }
  130. }
  131. public function testTrailingSlashBehavior()
  132. {
  133. $app = new Application();
  134. $app->get('/foo/', function () use ($app) {
  135. return new Response('ok');
  136. });
  137. $request = Request::create('/foo');
  138. $response = $app->handle($request);
  139. $this->assertEquals(301, $response->getStatusCode());
  140. $this->assertEquals('/foo/', $response->getTargetUrl());
  141. }
  142. public function testHostSpecification()
  143. {
  144. if (!method_exists('Symfony\Component\Routing\Route', 'setHost')) {
  145. $this->markTestSkipped('host() is only supported in the Symfony Routing 2.2+');
  146. }
  147. $route = new \Silex\Route();
  148. $this->assertSame($route, $route->host('{locale}.example.com'));
  149. $this->assertEquals('{locale}.example.com', $route->getHost());
  150. }
  151. public function testRequireHttpRedirect()
  152. {
  153. $app = new Application();
  154. $app->match('/secured', function () {
  155. return 'secured content';
  156. })
  157. ->requireHttp();
  158. $request = Request::create('https://example.com/secured');
  159. $response = $app->handle($request);
  160. $this->assertTrue($response->isRedirect('http://example.com/secured'));
  161. }
  162. public function testRequireHttpsRedirect()
  163. {
  164. $app = new Application();
  165. $app->match('/secured', function () {
  166. return 'secured content';
  167. })
  168. ->requireHttps();
  169. $request = Request::create('http://example.com/secured');
  170. $response = $app->handle($request);
  171. $this->assertTrue($response->isRedirect('https://example.com/secured'));
  172. }
  173. public function testRequireHttpsRedirectIncludesQueryString()
  174. {
  175. $app = new Application();
  176. $app->match('/secured', function () {
  177. return 'secured content';
  178. })
  179. ->requireHttps();
  180. $request = Request::create('http://example.com/secured?query=string');
  181. $response = $app->handle($request);
  182. $this->assertTrue($response->isRedirect('https://example.com/secured?query=string'));
  183. }
  184. public function testClassNameControllerSyntax()
  185. {
  186. $app = new Application();
  187. $app->get('/foo', 'Silex\Tests\MyController::getFoo');
  188. $this->checkRouteResponse($app, '/foo', 'foo');
  189. }
  190. public function testClassNameControllerSyntaxWithStaticMethod()
  191. {
  192. $app = new Application();
  193. $app->get('/bar', 'Silex\Tests\MyController::getBar');
  194. $this->checkRouteResponse($app, '/bar', 'bar');
  195. }
  196. protected function checkRouteResponse($app, $path, $expectedContent, $method = 'get', $message = null)
  197. {
  198. $request = Request::create($path, $method);
  199. $response = $app->handle($request);
  200. $this->assertEquals($expectedContent, $response->getContent(), $message);
  201. }
  202. }
  203. class MyController
  204. {
  205. public function getFoo()
  206. {
  207. return 'foo';
  208. }
  209. public static function getBar()
  210. {
  211. return 'bar';
  212. }
  213. }