GraphQLTestCase.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Webkul\BagistoApi\Tests;
  3. use Illuminate\Testing\TestResponse;
  4. use Webkul\Customer\Models\Customer;
  5. /**
  6. * Base test case for GraphQL API tests.
  7. *
  8. * Provides convenience methods for public and authenticated
  9. * GraphQL requests with storefront key handling.
  10. */
  11. abstract class GraphQLTestCase extends BagistoApiTestCase
  12. {
  13. /** GraphQL endpoint URL */
  14. protected string $graphqlUrl = '/api/graphql';
  15. /**
  16. * Execute a public GraphQL query (storefront key only, no auth)
  17. */
  18. protected function graphQL(string $query, array $variables = [], array $headers = []): TestResponse
  19. {
  20. $payload = ['query' => $query];
  21. if (! empty($variables)) {
  22. $payload['variables'] = $variables;
  23. }
  24. $headers = array_merge($this->storefrontHeaders(), $headers);
  25. return $this->postJson($this->graphqlUrl, $payload, $headers);
  26. }
  27. /**
  28. * Execute an authenticated GraphQL query (storefront key + customer token)
  29. */
  30. protected function authenticatedGraphQL(Customer $customer, string $query, array $variables = []): TestResponse
  31. {
  32. $payload = ['query' => $query];
  33. if (! empty($variables)) {
  34. $payload['variables'] = $variables;
  35. }
  36. return $this->actingAs($customer)
  37. ->withHeaders($this->authHeaders($customer))
  38. ->postJson($this->graphqlUrl, $payload);
  39. }
  40. }