RestApiTestCase.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Webkul\BagistoApi\Tests;
  3. use Illuminate\Testing\TestResponse;
  4. use Webkul\Customer\Models\Customer;
  5. /**
  6. * Base test case for REST API tests.
  7. *
  8. * Provides convenience methods for public and authenticated
  9. * REST requests with storefront key handling.
  10. */
  11. abstract class RestApiTestCase extends BagistoApiTestCase
  12. {
  13. /**
  14. * Execute a public GET request (storefront key only)
  15. */
  16. protected function publicGet(string $url): TestResponse
  17. {
  18. return $this->getJson($url, $this->storefrontHeaders());
  19. }
  20. /**
  21. * Execute a public POST request (storefront key only)
  22. */
  23. protected function publicPost(string $url, array $data = []): TestResponse
  24. {
  25. return $this->postJson($url, $data, $this->storefrontHeaders());
  26. }
  27. /**
  28. * Execute an authenticated GET request (storefront key + customer token)
  29. */
  30. protected function authenticatedGet(Customer $customer, string $url): TestResponse
  31. {
  32. return $this->actingAs($customer)
  33. ->withHeaders($this->authHeaders($customer))
  34. ->getJson($url);
  35. }
  36. /**
  37. * Execute an authenticated POST request (storefront key + customer token)
  38. */
  39. protected function authenticatedPost(Customer $customer, string $url, array $data = []): TestResponse
  40. {
  41. return $this->actingAs($customer)
  42. ->withHeaders($this->authHeaders($customer))
  43. ->postJson($url, $data);
  44. }
  45. /**
  46. * Execute an authenticated DELETE request (storefront key + customer token)
  47. */
  48. protected function authenticatedDelete(Customer $customer, string $url): TestResponse
  49. {
  50. return $this->actingAs($customer)
  51. ->withHeaders($this->authHeaders($customer))
  52. ->deleteJson($url);
  53. }
  54. /**
  55. * Execute a public DELETE request (storefront key only)
  56. */
  57. protected function publicDelete(string $url): TestResponse
  58. {
  59. return $this->deleteJson($url, [], $this->storefrontHeaders());
  60. }
  61. }