RouteApiResourceTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Illuminate\Tests\Integration\Routing;
  3. use Illuminate\Support\Facades\Route;
  4. use Illuminate\Tests\Integration\Routing\Fixtures\ApiResourceTaskController;
  5. use Illuminate\Tests\Integration\Routing\Fixtures\ApiResourceTestController;
  6. use Orchestra\Testbench\TestCase;
  7. class RouteApiResourceTest extends TestCase
  8. {
  9. public function testApiResource()
  10. {
  11. Route::apiResource('tests', ApiResourceTestController::class);
  12. $response = $this->get('/tests');
  13. $this->assertEquals(200, $response->getStatusCode());
  14. $this->assertSame('I`m index', $response->getContent());
  15. $response = $this->post('/tests');
  16. $this->assertEquals(200, $response->getStatusCode());
  17. $this->assertSame('I`m store', $response->getContent());
  18. $response = $this->get('/tests/1');
  19. $this->assertEquals(200, $response->getStatusCode());
  20. $this->assertSame('I`m show', $response->getContent());
  21. $response = $this->put('/tests/1');
  22. $this->assertEquals(200, $response->getStatusCode());
  23. $this->assertSame('I`m update', $response->getContent());
  24. $response = $this->patch('/tests/1');
  25. $this->assertEquals(200, $response->getStatusCode());
  26. $this->assertSame('I`m update', $response->getContent());
  27. $response = $this->delete('/tests/1');
  28. $this->assertEquals(200, $response->getStatusCode());
  29. $this->assertSame('I`m destroy', $response->getContent());
  30. }
  31. public function testApiResourceWithOnly()
  32. {
  33. Route::apiResource('tests', ApiResourceTestController::class)->only(['index', 'store']);
  34. $response = $this->get('/tests');
  35. $this->assertEquals(200, $response->getStatusCode());
  36. $this->assertSame('I`m index', $response->getContent());
  37. $response = $this->post('/tests');
  38. $this->assertEquals(200, $response->getStatusCode());
  39. $this->assertSame('I`m store', $response->getContent());
  40. $this->assertEquals(404, $this->get('/tests/1')->getStatusCode());
  41. $this->assertEquals(404, $this->put('/tests/1')->getStatusCode());
  42. $this->assertEquals(404, $this->patch('/tests/1')->getStatusCode());
  43. $this->assertEquals(404, $this->delete('/tests/1')->getStatusCode());
  44. }
  45. public function testApiResources()
  46. {
  47. Route::apiResources([
  48. 'tests' => ApiResourceTestController::class,
  49. 'tasks' => ApiResourceTaskController::class,
  50. ]);
  51. $response = $this->get('/tests');
  52. $this->assertEquals(200, $response->getStatusCode());
  53. $this->assertSame('I`m index', $response->getContent());
  54. $response = $this->post('/tests');
  55. $this->assertEquals(200, $response->getStatusCode());
  56. $this->assertSame('I`m store', $response->getContent());
  57. $response = $this->get('/tests/1');
  58. $this->assertEquals(200, $response->getStatusCode());
  59. $this->assertSame('I`m show', $response->getContent());
  60. $response = $this->put('/tests/1');
  61. $this->assertEquals(200, $response->getStatusCode());
  62. $this->assertSame('I`m update', $response->getContent());
  63. $response = $this->patch('/tests/1');
  64. $this->assertEquals(200, $response->getStatusCode());
  65. $this->assertSame('I`m update', $response->getContent());
  66. $response = $this->delete('/tests/1');
  67. $this->assertEquals(200, $response->getStatusCode());
  68. $this->assertSame('I`m destroy', $response->getContent());
  69. /////////////////////
  70. $response = $this->get('/tasks');
  71. $this->assertEquals(200, $response->getStatusCode());
  72. $this->assertSame('I`m index tasks', $response->getContent());
  73. $response = $this->post('/tasks');
  74. $this->assertEquals(200, $response->getStatusCode());
  75. $this->assertSame('I`m store tasks', $response->getContent());
  76. $response = $this->get('/tasks/1');
  77. $this->assertEquals(200, $response->getStatusCode());
  78. $this->assertSame('I`m show tasks', $response->getContent());
  79. $response = $this->put('/tasks/1');
  80. $this->assertEquals(200, $response->getStatusCode());
  81. $this->assertSame('I`m update tasks', $response->getContent());
  82. $response = $this->patch('/tasks/1');
  83. $this->assertEquals(200, $response->getStatusCode());
  84. $this->assertSame('I`m update tasks', $response->getContent());
  85. $response = $this->delete('/tasks/1');
  86. $this->assertEquals(200, $response->getStatusCode());
  87. $this->assertSame('I`m destroy tasks', $response->getContent());
  88. }
  89. }