TestCase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Barryvdh\Cors\Tests;
  3. use Illuminate\Routing\Router;
  4. abstract class TestCase extends \Orchestra\Testbench\TestCase
  5. {
  6. use \Illuminate\Foundation\Validation\ValidatesRequests;
  7. protected function resolveApplicationConfiguration($app)
  8. {
  9. parent::resolveApplicationConfiguration($app);
  10. $app['config']['cors'] = [
  11. 'supportsCredentials' => false,
  12. 'allowedOrigins' => ['localhost'],
  13. 'allowedHeaders' => ['X-Custom-1', 'X-Custom-2'],
  14. 'allowedMethods' => ['GET', 'POST'],
  15. 'exposedHeaders' => [],
  16. 'maxAge' => 0,
  17. ];
  18. }
  19. protected function getPackageProviders($app)
  20. {
  21. return [\Barryvdh\Cors\ServiceProvider::class];
  22. }
  23. /**
  24. * Define environment setup.
  25. *
  26. * @param Illuminate\Foundation\Application $app
  27. *
  28. * @return void
  29. */
  30. protected function getEnvironmentSetUp($app)
  31. {
  32. $router = $app['router'];
  33. $this->addWebRoutes($router);
  34. $this->addApiRoutes($router);
  35. }
  36. /**
  37. * @param Router $router
  38. */
  39. protected function addWebRoutes(Router $router)
  40. {
  41. $router->get('web/ping', [
  42. 'as' => 'web.ping',
  43. 'uses' => function () {
  44. return 'pong';
  45. }
  46. ]);
  47. $router->post('web/ping', [
  48. 'uses' => function () {
  49. return 'PONG';
  50. }
  51. ]);
  52. $router->post('web/error', [
  53. 'uses' => function () {
  54. abort(500);
  55. }
  56. ]);
  57. $router->post('web/validation', [
  58. 'uses' => function (\Illuminate\Http\Request $request) {
  59. $this->validate($request, [
  60. 'name' => 'required',
  61. ]);
  62. return 'ok';
  63. }
  64. ]);
  65. }
  66. /**
  67. * @param Router $router
  68. */
  69. protected function addApiRoutes($router)
  70. {
  71. $router->group(['middleware' => \Barryvdh\Cors\HandleCors::class], function () use ($router) {
  72. $router->get('api/ping', [
  73. 'as' => 'api.ping',
  74. 'uses' => function () {
  75. return 'pong';
  76. }
  77. ]);
  78. $router->post('api/ping', [
  79. 'uses' => function () {
  80. return 'PONG';
  81. }
  82. ]);
  83. $router->put('api/ping', [
  84. 'uses' => function () {
  85. return 'PONG';
  86. }
  87. ]);
  88. $router->post('api/error', [
  89. 'uses' => function () {
  90. abort(500);
  91. }
  92. ]);
  93. $router->post('api/validation', [
  94. 'uses' => function (\Illuminate\Http\Request $request) {
  95. $this->validate($request, [
  96. 'name' => 'required',
  97. ]);
  98. return 'ok';
  99. }
  100. ]);
  101. });
  102. }
  103. protected function checkVersion($version, $operator = ">=")
  104. {
  105. return version_compare($this->app->version(), $version, $operator);
  106. }
  107. }