CustomerReviewTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
  3. use Webkul\BagistoApi\Tests\GraphQLTestCase;
  4. use Webkul\Core\Models\Channel;
  5. use Webkul\Customer\Models\Customer;
  6. use Webkul\Product\Models\Product;
  7. use Webkul\Product\Models\ProductReview;
  8. class CustomerReviewTest extends GraphQLTestCase
  9. {
  10. /**
  11. * Create test data — customer with reviews on multiple products
  12. */
  13. private function createTestData(): array
  14. {
  15. $this->seedRequiredData();
  16. $customer = $this->createCustomer();
  17. $product1 = Product::factory()->create();
  18. $product2 = Product::factory()->create();
  19. $review1 = ProductReview::factory()->create([
  20. 'customer_id' => $customer->id,
  21. 'product_id' => $product1->id,
  22. 'title' => 'Great product',
  23. 'comment' => 'Really enjoyed using this product.',
  24. 'rating' => 5,
  25. 'status' => 'approved',
  26. 'name' => $customer->first_name,
  27. ]);
  28. $review2 = ProductReview::factory()->create([
  29. 'customer_id' => $customer->id,
  30. 'product_id' => $product2->id,
  31. 'title' => 'Average product',
  32. 'comment' => 'It was okay, nothing special.',
  33. 'rating' => 3,
  34. 'status' => 'pending',
  35. 'name' => $customer->first_name,
  36. ]);
  37. return compact('customer', 'product1', 'product2', 'review1', 'review2');
  38. }
  39. // ── Collection Queries ────────────────────────────────────
  40. /**
  41. * Test: Query all customer reviews collection
  42. */
  43. public function test_get_customer_reviews_collection(): void
  44. {
  45. $testData = $this->createTestData();
  46. $query = <<<'GQL'
  47. query getCustomerReviews {
  48. customerReviews {
  49. edges {
  50. cursor
  51. node {
  52. id
  53. _id
  54. title
  55. comment
  56. rating
  57. status
  58. name
  59. product {
  60. id
  61. }
  62. customer {
  63. id
  64. }
  65. createdAt
  66. updatedAt
  67. }
  68. }
  69. pageInfo {
  70. endCursor
  71. startCursor
  72. hasNextPage
  73. hasPreviousPage
  74. }
  75. totalCount
  76. }
  77. }
  78. GQL;
  79. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  80. $response->assertOk();
  81. $data = $response->json('data.customerReviews');
  82. expect($data['totalCount'])->toBeGreaterThanOrEqual(2);
  83. expect($data['edges'])->not()->toBeEmpty();
  84. }
  85. /**
  86. * Test: Unauthenticated request returns error
  87. */
  88. public function test_get_customer_reviews_requires_authentication(): void
  89. {
  90. $query = <<<'GQL'
  91. query getCustomerReviews {
  92. customerReviews(first: 5) {
  93. edges {
  94. node {
  95. _id
  96. }
  97. }
  98. }
  99. }
  100. GQL;
  101. $response = $this->graphQL($query);
  102. $response->assertOk();
  103. $errors = $response->json('errors');
  104. expect($errors)->not()->toBeEmpty();
  105. }
  106. /**
  107. * Test: Customer only sees their own reviews
  108. */
  109. public function test_customer_only_sees_own_reviews(): void
  110. {
  111. $testData = $this->createTestData();
  112. /** Create another customer with their own review */
  113. $otherCustomer = $this->createCustomer();
  114. $product = Product::factory()->create();
  115. ProductReview::factory()->create([
  116. 'customer_id' => $otherCustomer->id,
  117. 'product_id' => $product->id,
  118. 'status' => 'approved',
  119. ]);
  120. $query = <<<'GQL'
  121. query getCustomerReviews {
  122. customerReviews(first: 50) {
  123. edges {
  124. node {
  125. _id
  126. }
  127. }
  128. totalCount
  129. }
  130. }
  131. GQL;
  132. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  133. $response->assertOk();
  134. $data = $response->json('data.customerReviews');
  135. /** Should only see the 2 reviews belonging to testData customer */
  136. expect($data['totalCount'])->toBe(2);
  137. }
  138. // ── Single Item Query ─────────────────────────────────────
  139. /**
  140. * Test: Query single customer review by ID
  141. */
  142. public function test_get_customer_review_by_id(): void
  143. {
  144. $testData = $this->createTestData();
  145. $reviewId = "/api/shop/customer-reviews/{$testData['review1']->id}";
  146. $query = <<<GQL
  147. query getCustomerReview {
  148. customerReview(id: "{$reviewId}") {
  149. id
  150. _id
  151. title
  152. comment
  153. rating
  154. status
  155. name
  156. product {
  157. id
  158. }
  159. customer {
  160. id
  161. }
  162. createdAt
  163. updatedAt
  164. }
  165. }
  166. GQL;
  167. $response = $this->graphQL($query);
  168. $response->assertOk();
  169. $data = $response->json('data.customerReview');
  170. expect($data['_id'])->toBe($testData['review1']->id);
  171. expect($data['title'])->toBe('Great product');
  172. expect($data['rating'])->toBe(5);
  173. expect($data['product'])->toHaveKey('id');
  174. expect($data['customer'])->toHaveKey('id');
  175. }
  176. /**
  177. * Test: Query with invalid ID returns null/error
  178. */
  179. public function test_invalid_customer_review_id_returns_error(): void
  180. {
  181. $query = <<<'GQL'
  182. query getCustomerReview {
  183. customerReview(id: "/api/shop/customer-reviews/99999") {
  184. id
  185. }
  186. }
  187. GQL;
  188. $response = $this->graphQL($query);
  189. $response->assertOk();
  190. expect($response->json('data.customerReview'))->toBeNull();
  191. }
  192. // ── Filtering ─────────────────────────────────────────────
  193. /**
  194. * Test: Filter reviews by status
  195. */
  196. public function test_filter_reviews_by_status(): void
  197. {
  198. $testData = $this->createTestData();
  199. $query = <<<'GQL'
  200. query getCustomerReviews {
  201. customerReviews(first: 10, status: "approved") {
  202. edges {
  203. node {
  204. _id
  205. status
  206. }
  207. }
  208. totalCount
  209. }
  210. }
  211. GQL;
  212. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  213. $response->assertOk();
  214. $data = $response->json('data.customerReviews');
  215. expect($data['totalCount'])->toBe(1);
  216. $statuses = collect($data['edges'])->pluck('node.status')->unique()->toArray();
  217. expect($statuses)->toBe(['approved']);
  218. }
  219. /**
  220. * Test: Filter reviews by rating
  221. */
  222. public function test_filter_reviews_by_rating(): void
  223. {
  224. $testData = $this->createTestData();
  225. $query = <<<'GQL'
  226. query getCustomerReviews {
  227. customerReviews(first: 10, rating: 5) {
  228. edges {
  229. node {
  230. _id
  231. rating
  232. }
  233. }
  234. totalCount
  235. }
  236. }
  237. GQL;
  238. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  239. $response->assertOk();
  240. $data = $response->json('data.customerReviews');
  241. expect($data['totalCount'])->toBe(1);
  242. $ratings = collect($data['edges'])->pluck('node.rating')->unique()->toArray();
  243. expect($ratings)->toBe([5]);
  244. }
  245. /**
  246. * Test: Filter with no matching results returns empty collection
  247. */
  248. public function test_filter_with_no_matches_returns_empty(): void
  249. {
  250. $testData = $this->createTestData();
  251. $query = <<<'GQL'
  252. query getCustomerReviews {
  253. customerReviews(first: 10, status: "rejected") {
  254. edges {
  255. node {
  256. _id
  257. }
  258. }
  259. totalCount
  260. }
  261. }
  262. GQL;
  263. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  264. $response->assertOk();
  265. $data = $response->json('data.customerReviews');
  266. expect($data['totalCount'])->toBe(0);
  267. expect($data['edges'])->toBeEmpty();
  268. }
  269. // ── Pagination ────────────────────────────────────────────
  270. /**
  271. * Test: Pagination with first parameter
  272. */
  273. public function test_pagination_first(): void
  274. {
  275. $testData = $this->createTestData();
  276. $query = <<<'GQL'
  277. query getCustomerReviews {
  278. customerReviews(first: 1) {
  279. edges {
  280. node {
  281. _id
  282. }
  283. }
  284. pageInfo {
  285. hasNextPage
  286. }
  287. }
  288. }
  289. GQL;
  290. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  291. $response->assertOk();
  292. $data = $response->json('data.customerReviews');
  293. expect($data['edges'])->toHaveCount(1);
  294. }
  295. /**
  296. * Test: Cursor-based pagination with after parameter
  297. */
  298. public function test_pagination_with_cursor(): void
  299. {
  300. $testData = $this->createTestData();
  301. /** Fetch first page to get cursor */
  302. $firstQuery = <<<'GQL'
  303. query getCustomerReviews {
  304. customerReviews(first: 1) {
  305. edges {
  306. cursor
  307. node {
  308. _id
  309. }
  310. }
  311. }
  312. }
  313. GQL;
  314. $firstResponse = $this->authenticatedGraphQL($testData['customer'], $firstQuery);
  315. $cursor = $firstResponse->json('data.customerReviews.edges.0.cursor');
  316. /** Fetch second page using cursor */
  317. $secondQuery = <<<GQL
  318. query getCustomerReviews {
  319. customerReviews(first: 1, after: "{$cursor}") {
  320. edges {
  321. node {
  322. _id
  323. }
  324. }
  325. }
  326. }
  327. GQL;
  328. $secondResponse = $this->authenticatedGraphQL($testData['customer'], $secondQuery);
  329. $secondResponse->assertOk();
  330. }
  331. // ── Field & Format Assertions ─────────────────────────────
  332. /**
  333. * Test: All expected fields are present on the review node
  334. */
  335. public function test_query_all_fields(): void
  336. {
  337. $testData = $this->createTestData();
  338. $query = <<<'GQL'
  339. query getCustomerReviews {
  340. customerReviews(first: 1) {
  341. edges {
  342. node {
  343. id
  344. _id
  345. title
  346. comment
  347. rating
  348. status
  349. name
  350. product {
  351. id
  352. }
  353. customer {
  354. id
  355. }
  356. createdAt
  357. updatedAt
  358. }
  359. }
  360. }
  361. }
  362. GQL;
  363. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  364. $response->assertOk();
  365. $node = $response->json('data.customerReviews.edges.0.node');
  366. expect($node)->toHaveKeys([
  367. 'id', '_id', 'title', 'comment', 'rating',
  368. 'status', 'name', 'product', 'customer',
  369. 'createdAt', 'updatedAt',
  370. ]);
  371. }
  372. /**
  373. * Test: Timestamps are returned in ISO8601 format
  374. */
  375. public function test_timestamps_are_iso8601_format(): void
  376. {
  377. $testData = $this->createTestData();
  378. $query = <<<'GQL'
  379. query getCustomerReviews {
  380. customerReviews(first: 1) {
  381. edges {
  382. node {
  383. _id
  384. createdAt
  385. updatedAt
  386. }
  387. }
  388. }
  389. }
  390. GQL;
  391. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  392. $response->assertOk();
  393. $node = $response->json('data.customerReviews.edges.0.node');
  394. expect($node['createdAt'])->toMatch('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/');
  395. expect($node['updatedAt'])->toMatch('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/');
  396. }
  397. /**
  398. * Test: Numeric ID (_id) is an integer
  399. */
  400. public function test_numeric_id_is_integer(): void
  401. {
  402. $testData = $this->createTestData();
  403. $query = <<<'GQL'
  404. query getCustomerReviews {
  405. customerReviews(first: 1) {
  406. edges {
  407. node {
  408. _id
  409. }
  410. }
  411. }
  412. }
  413. GQL;
  414. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  415. $response->assertOk();
  416. $node = $response->json('data.customerReviews.edges.0.node');
  417. expect($node['_id'])->toBeInt();
  418. }
  419. // ── Schema Introspection ──────────────────────────────────
  420. /**
  421. * Test: CustomerReview type exists in GraphQL schema
  422. */
  423. public function test_introspection_query(): void
  424. {
  425. $query = <<<'GQL'
  426. {
  427. __type(name: "CustomerReview") {
  428. name
  429. kind
  430. fields {
  431. name
  432. }
  433. }
  434. }
  435. GQL;
  436. $response = $this->graphQL($query);
  437. $response->assertOk();
  438. $type = $response->json('data.__type');
  439. expect($type['name'])->toBe('CustomerReview');
  440. expect($type['kind'])->toBe('OBJECT');
  441. $fieldNames = collect($type['fields'])->pluck('name')->toArray();
  442. expect($fieldNames)->toContain('id', '_id', 'title', 'comment', 'rating', 'status', 'name');
  443. }
  444. // ── Edge Cases ────────────────────────────────────────────
  445. /**
  446. * Test: Customer with no reviews returns empty collection
  447. */
  448. public function test_customer_with_no_reviews_returns_empty(): void
  449. {
  450. $this->seedRequiredData();
  451. $customer = $this->createCustomer();
  452. $query = <<<'GQL'
  453. query getCustomerReviews {
  454. customerReviews(first: 10) {
  455. edges {
  456. node {
  457. _id
  458. }
  459. }
  460. totalCount
  461. }
  462. }
  463. GQL;
  464. $response = $this->authenticatedGraphQL($customer, $query);
  465. $response->assertOk();
  466. $data = $response->json('data.customerReviews');
  467. expect($data['totalCount'])->toBe(0);
  468. expect($data['edges'])->toBeEmpty();
  469. }
  470. /**
  471. * Test: Reviews include product relationship data
  472. */
  473. public function test_reviews_include_product_relationship(): void
  474. {
  475. $testData = $this->createTestData();
  476. $query = <<<'GQL'
  477. query getCustomerReviews {
  478. customerReviews(first: 1) {
  479. edges {
  480. node {
  481. _id
  482. product {
  483. id
  484. }
  485. }
  486. }
  487. }
  488. }
  489. GQL;
  490. $response = $this->authenticatedGraphQL($testData['customer'], $query);
  491. $response->assertOk();
  492. $node = $response->json('data.customerReviews.edges.0.node');
  493. expect($node)->toHaveKey('product');
  494. expect($node['product'])->toHaveKey('id');
  495. }
  496. }