CustomerReviewRestTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\Rest;
  3. use Webkul\BagistoApi\Tests\RestApiTestCase;
  4. use Webkul\Product\Models\Product;
  5. use Webkul\Product\Models\ProductReview;
  6. class CustomerReviewRestTest extends RestApiTestCase
  7. {
  8. /**
  9. * Create test data — customer with reviews on multiple products
  10. */
  11. private function createTestData(): array
  12. {
  13. $this->seedRequiredData();
  14. $customer = $this->createCustomer();
  15. $product1 = Product::factory()->create();
  16. $product2 = Product::factory()->create();
  17. $review1 = ProductReview::factory()->create([
  18. 'customer_id' => $customer->id,
  19. 'product_id' => $product1->id,
  20. 'title' => 'Great product',
  21. 'comment' => 'Really enjoyed using this product.',
  22. 'rating' => 5,
  23. 'status' => 'approved',
  24. 'name' => $customer->first_name,
  25. ]);
  26. $review2 = ProductReview::factory()->create([
  27. 'customer_id' => $customer->id,
  28. 'product_id' => $product2->id,
  29. 'title' => 'Average product',
  30. 'comment' => 'It was okay, nothing special.',
  31. 'rating' => 3,
  32. 'status' => 'pending',
  33. 'name' => $customer->first_name,
  34. ]);
  35. return compact('customer', 'product1', 'product2', 'review1', 'review2');
  36. }
  37. // ── Collection ────────────────────────────────────────────
  38. /**
  39. * Test: GET /api/shop/customer-reviews returns collection
  40. */
  41. public function test_get_customer_reviews_collection(): void
  42. {
  43. $testData = $this->createTestData();
  44. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-reviews');
  45. $response->assertOk();
  46. $json = $response->json();
  47. expect($json)->toBeArray();
  48. expect(count($json))->toBeGreaterThanOrEqual(2);
  49. }
  50. /**
  51. * Test: GET /api/shop/customer-reviews without auth returns error
  52. */
  53. public function test_get_customer_reviews_requires_auth(): void
  54. {
  55. $this->seedRequiredData();
  56. $response = $this->publicGet('/api/shop/customer-reviews');
  57. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  58. }
  59. /**
  60. * Test: Customer only sees own reviews
  61. */
  62. public function test_customer_only_sees_own_reviews(): void
  63. {
  64. $testData = $this->createTestData();
  65. /** Create another customer with their own review */
  66. $otherCustomer = $this->createCustomer();
  67. $product = Product::factory()->create();
  68. ProductReview::factory()->create([
  69. 'customer_id' => $otherCustomer->id,
  70. 'product_id' => $product->id,
  71. 'status' => 'approved',
  72. ]);
  73. $response = $this->authenticatedGet($testData['customer'], '/api/shop/customer-reviews');
  74. $response->assertOk();
  75. $json = $response->json();
  76. /** Should only see the 2 reviews belonging to testData customer */
  77. expect(count($json))->toBe(2);
  78. }
  79. /**
  80. * Test: Customer with no reviews returns empty collection
  81. */
  82. public function test_customer_with_no_reviews_returns_empty(): void
  83. {
  84. $this->seedRequiredData();
  85. $customer = $this->createCustomer();
  86. $response = $this->authenticatedGet($customer, '/api/shop/customer-reviews');
  87. $response->assertOk();
  88. $json = $response->json();
  89. expect($json)->toBeArray();
  90. expect(count($json))->toBe(0);
  91. }
  92. // ── Single Item ───────────────────────────────────────────
  93. /**
  94. * Test: GET /api/shop/customer-reviews/{id} returns single review
  95. */
  96. public function test_get_single_customer_review(): void
  97. {
  98. $testData = $this->createTestData();
  99. $response = $this->authenticatedGet(
  100. $testData['customer'],
  101. '/api/shop/customer-reviews/'.$testData['review1']->id
  102. );
  103. $response->assertOk();
  104. $json = $response->json();
  105. expect($json)->toHaveKey('id');
  106. expect($json)->toHaveKey('title');
  107. expect($json)->toHaveKey('comment');
  108. expect($json)->toHaveKey('rating');
  109. expect($json)->toHaveKey('status');
  110. expect($json)->toHaveKey('product');
  111. expect($json)->toHaveKey('customer');
  112. expect($json)->toHaveKey('createdAt');
  113. expect($json)->toHaveKey('updatedAt');
  114. expect($json['id'])->toBe($testData['review1']->id);
  115. expect($json['title'])->toBe('Great product');
  116. expect($json['rating'])->toBe(5);
  117. }
  118. /**
  119. * Test: GET /api/shop/customer-reviews/{id} with invalid id returns 404
  120. */
  121. public function test_get_customer_review_not_found(): void
  122. {
  123. $this->seedRequiredData();
  124. $customer = $this->createCustomer();
  125. $response = $this->authenticatedGet($customer, '/api/shop/customer-reviews/999999');
  126. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  127. }
  128. /**
  129. * Test: Cannot access another customer's review by ID
  130. */
  131. public function test_cannot_access_other_customers_review(): void
  132. {
  133. $testData = $this->createTestData();
  134. $otherCustomer = $this->createCustomer();
  135. $response = $this->authenticatedGet(
  136. $otherCustomer,
  137. '/api/shop/customer-reviews/'.$testData['review1']->id
  138. );
  139. /** Should return 404/500 because the review doesn't belong to otherCustomer */
  140. expect(in_array($response->getStatusCode(), [404, 500]))->toBeTrue();
  141. }
  142. /**
  143. * Test: Single review without auth returns error
  144. */
  145. public function test_get_single_review_requires_auth(): void
  146. {
  147. $testData = $this->createTestData();
  148. $response = $this->publicGet(
  149. '/api/shop/customer-reviews/'.$testData['review1']->id
  150. );
  151. expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
  152. }
  153. // ── Filtering (via query params) ──────────────────────────
  154. /**
  155. * Test: Filter reviews by status via query parameter
  156. */
  157. public function test_filter_reviews_by_status(): void
  158. {
  159. $testData = $this->createTestData();
  160. $response = $this->authenticatedGet(
  161. $testData['customer'],
  162. '/api/shop/customer-reviews?status=approved'
  163. );
  164. $response->assertOk();
  165. $json = $response->json();
  166. /** Only the approved review should be returned */
  167. foreach ($json as $review) {
  168. expect($review['status'])->toBe('approved');
  169. }
  170. }
  171. /**
  172. * Test: Filter reviews by rating via query parameter
  173. */
  174. public function test_filter_reviews_by_rating(): void
  175. {
  176. $testData = $this->createTestData();
  177. $response = $this->authenticatedGet(
  178. $testData['customer'],
  179. '/api/shop/customer-reviews?rating=5'
  180. );
  181. $response->assertOk();
  182. $json = $response->json();
  183. /** Only the 5-star review should be returned */
  184. foreach ($json as $review) {
  185. expect($review['rating'])->toBe(5);
  186. }
  187. }
  188. // ── Response format assertions ────────────────────────────
  189. /**
  190. * Test: Review includes product relationship data
  191. */
  192. public function test_review_includes_product_relationship(): void
  193. {
  194. $testData = $this->createTestData();
  195. $response = $this->authenticatedGet(
  196. $testData['customer'],
  197. '/api/shop/customer-reviews/'.$testData['review1']->id
  198. );
  199. $response->assertOk();
  200. $json = $response->json();
  201. expect($json)->toHaveKey('product');
  202. expect($json['product'])->not()->toBeNull();
  203. }
  204. /**
  205. * Test: Review includes customer relationship data
  206. */
  207. public function test_review_includes_customer_relationship(): void
  208. {
  209. $testData = $this->createTestData();
  210. $response = $this->authenticatedGet(
  211. $testData['customer'],
  212. '/api/shop/customer-reviews/'.$testData['review1']->id
  213. );
  214. $response->assertOk();
  215. $json = $response->json();
  216. expect($json)->toHaveKey('customer');
  217. expect($json['customer'])->not()->toBeNull();
  218. }
  219. }