BaseApplicationTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Cake\Test\TestCase;
  3. use Cake\Http\Response;
  4. use Cake\Http\ServerRequestFactory;
  5. use Cake\Routing\Router;
  6. use Cake\TestSuite\TestCase;
  7. /**
  8. * Base application test.
  9. */
  10. class BaseApplicationTest extends TestCase
  11. {
  12. /**
  13. * Setup
  14. *
  15. * @return void
  16. */
  17. public function setUp()
  18. {
  19. parent::setUp();
  20. static::setAppNamespace();
  21. }
  22. /**
  23. * Integration test for a simple controller.
  24. *
  25. * @return void
  26. */
  27. public function testInvoke()
  28. {
  29. $next = function ($req, $res) {
  30. return $res;
  31. };
  32. $response = new Response();
  33. $request = ServerRequestFactory::fromGlobals(['REQUEST_URI' => '/cakes']);
  34. $request = $request->withAttribute('params', [
  35. 'controller' => 'Cakes',
  36. 'action' => 'index',
  37. 'plugin' => null,
  38. 'pass' => []
  39. ]);
  40. $path = dirname(dirname(__DIR__));
  41. $app = $this->getMockForAbstractClass('Cake\Http\BaseApplication', [$path]);
  42. $result = $app($request, $response, $next);
  43. $this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $result);
  44. $this->assertEquals('Hello Jane', '' . $result->getBody());
  45. }
  46. /**
  47. * Ensure that Router::$initialized is toggled even if the routes
  48. * file fails. This prevents the routes file from being re-parsed
  49. * during the error handling process.
  50. *
  51. * @return void
  52. */
  53. public function testRouteHookInitializesRouterOnError()
  54. {
  55. $app = $this->getMockForAbstractClass(
  56. 'Cake\Http\BaseApplication',
  57. [TEST_APP . 'invalid_routes' . DS]
  58. );
  59. $builder = Router::createRouteBuilder('/');
  60. try {
  61. $app->routes($builder);
  62. $this->fail('invalid_routes/routes.php file should raise an error.');
  63. } catch (\InvalidArgumentException $e) {
  64. $this->assertTrue(Router::$initialized, 'Should be toggled to prevent duplicate route errors');
  65. $this->assertContains('route class', $e->getMessage());
  66. }
  67. }
  68. }