| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- namespace Webkul\BagistoApi\Tests\Feature\RestApi;
- use Webkul\BagistoApi\Tests\RestApiTestCase;
- use Webkul\Customer\Models\CompareItem;
- use Webkul\Customer\Models\Customer;
- use Webkul\Product\Models\Product;
- class CompareItemTest extends RestApiTestCase
- {
- private string $apiUrl = '/api/shop/compare_items';
- /**
- * Create test data - customer, products and compare items
- */
- private function createTestData(): array
- {
- $this->seedRequiredData();
- $customer = $this->createCustomer();
- $product1 = Product::factory()->create();
- $product2 = Product::factory()->create();
- $compareItem1 = CompareItem::factory()->create([
- 'customer_id' => $customer->id,
- 'product_id' => $product1->id,
- ]);
- $compareItem2 = CompareItem::factory()->create([
- 'customer_id' => $customer->id,
- 'product_id' => $product2->id,
- ]);
- return compact('customer', 'product1', 'product2', 'compareItem1', 'compareItem2');
- }
- /**
- * Test: GET all compare items
- */
- public function test_get_all_compare_items(): void
- {
- $this->createTestData();
- $response = $this->publicGet($this->apiUrl);
- $response->assertOk();
- $data = $response->json();
- // API Platform Collection Format
- if (isset($data['hydra:member'])) {
- expect($data['hydra:member'])->not()->toBeEmpty();
- } else if (isset($data['@type'])) {
- // Alternative collection format
- expect($data)->toHaveKey('@type');
- } else if (is_array($data)) {
- // Fallback: array of items
- expect(count($data))->toBeGreaterThanOrEqual(0);
- }
- }
- /**
- * Test: GET single compare item by ID
- */
- public function test_get_single_compare_item(): void
- {
- $testData = $this->createTestData();
- $response = $this->publicGet(
- "{$this->apiUrl}/{$testData['compareItem1']->id}"
- );
- $response->assertOk();
- $data = $response->json();
- // Check for compare item ID in response
- expect($data)->toHaveKey('id');
- expect($data['id'])->toBe($testData['compareItem1']->id);
- }
- /**
- * Test: GET compare item with embedded relationships
- */
- public function test_get_compare_item_with_relationships(): void
- {
- $testData = $this->createTestData();
- $response = $this->publicGet(
- "{$this->apiUrl}/{$testData['compareItem1']->id}"
- );
- $response->assertOk();
- $data = $response->json();
- // Product and customer might be IRI references or embedded objects
- if (is_array($data['product'])) {
- expect($data['product'])->toHaveKey('id');
- }
- if (is_array($data['customer'])) {
- expect($data['customer'])->toHaveKey('id');
- }
- }
- /**
- * Test: GET compare item with timestamps
- */
- public function test_get_compare_item_with_timestamps(): void
- {
- $testData = $this->createTestData();
- $response = $this->publicGet($this->apiUrl . '?itemsPerPage=1');
- $response->assertOk();
- $data = $response->json();
-
- // Handle both Hydra format and plain array format
- $compareItem = $data['hydra:member'][0] ?? $data[0] ?? null;
-
- if ($compareItem) {
- expect($compareItem)->toHaveKey('createdAt');
- expect($compareItem)->toHaveKey('updatedAt');
- }
- }
- /**
- * Test: POST create new compare item
- */
- public function test_create_compare_item(): void
- {
- $testData = $this->createTestData();
- $product3 = Product::factory()->create();
- $payload = [
- 'customer_id' => $testData['customer']->id,
- 'product_id' => $product3->id,
- ];
- $response = $this->publicPost($this->apiUrl, $payload);
- $response->assertCreated();
- $data = $response->json();
-
- // Verify the created item has the expected IDs
- expect($data)->toHaveKey('id');
- expect($data['id'])->toBeGreaterThan(0);
- }
- /**
- * Test: DELETE compare item
- */
- public function test_delete_compare_item(): void
- {
- $testData = $this->createTestData();
- $response = $this->publicDelete(
- "{$this->apiUrl}/{$testData['compareItem1']->id}"
- );
- $response->assertNoContent();
- // Verify deletion
- $checkResponse = $this->publicGet(
- "{$this->apiUrl}/{$testData['compareItem1']->id}"
- );
- $checkResponse->assertNotFound();
- }
- /**
- * Test: GET non-existent compare item returns 404
- */
- public function test_get_non_existent_compare_item(): void
- {
- $response = $this->publicGet("{$this->apiUrl}/99999");
- $response->assertNotFound();
- }
- /**
- * Test: GET compare items with pagination
- */
- public function test_get_compare_items_with_pagination(): void
- {
- $this->createTestData();
- $response = $this->publicGet(
- "{$this->apiUrl}?itemsPerPage=1&page=1"
- );
- $response->assertOk();
- $data = $response->json();
- // Handle both Hydra and plain response formats
- if (isset($data['hydra:member'])) {
- expect(count($data['hydra:member']))->toBeGreaterThanOrEqual(0);
- }
- }
- /**
- * Test: GET compare items with multiple pages
- */
- public function test_get_compare_items_with_multiple_pages(): void
- {
- $this->createTestData();
- $firstPageResponse = $this->publicGet(
- "{$this->apiUrl}?itemsPerPage=1&page=1"
- );
- $firstPageResponse->assertOk();
- }
- /**
- * Test: Invalid compare item cannot be deleted
- */
- public function test_delete_non_existent_compare_item(): void
- {
- $response = $this->publicDelete(
- "{$this->apiUrl}/99999"
- );
- $response->assertNotFound();
- }
- /**
- * Test: Compare items have proper API resource type
- */
- public function test_compare_item_has_proper_ld_jsonld_type(): void
- {
- $this->createTestData();
- $response = $this->publicGet($this->apiUrl . '?itemsPerPage=1');
- $response->assertOk();
- $data = $response->json();
- // Verify we got a valid response
- expect($data)->toBeArray();
- }
- /**
- * Test: All required fields are present
- */
- public function test_compare_item_has_all_required_fields(): void
- {
- $this->createTestData();
- $response = $this->publicGet($this->apiUrl . '?itemsPerPage=1');
- $response->assertOk();
- $data = $response->json();
- // Verify we got a valid response
- expect($data)->toBeArray();
- }
- }
|