CountryTest.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
  3. use Webkul\BagistoApi\Tests\GraphQLTestCase;
  4. class CountryTest extends GraphQLTestCase
  5. {
  6. /**
  7. * Get Country By ID - Basic
  8. */
  9. public function test_get_country_by_id_basic(): void
  10. {
  11. $query = <<<'GQL'
  12. query getSingleCountry($id: ID!) {
  13. country(id: $id) {
  14. id
  15. _id
  16. code
  17. name
  18. }
  19. }
  20. GQL;
  21. $variables = ['id' => '106'];
  22. $response = $this->graphQL($query, $variables);
  23. $response->assertSuccessful();
  24. $node = $response->json('data.country');
  25. $this->assertNotNull($node, 'country response is null');
  26. $this->assertArrayHasKey('id', $node);
  27. $this->assertArrayHasKey('_id', $node);
  28. $this->assertArrayHasKey('code', $node);
  29. $this->assertArrayHasKey('name', $node);
  30. }
  31. /**
  32. * Get Country with States
  33. */
  34. public function test_get_country_with_states(): void
  35. {
  36. $query = <<<'GQL'
  37. query getSingleCountry($id: ID!) {
  38. country(id: $id) {
  39. id
  40. _id
  41. code
  42. name
  43. states {
  44. edges {
  45. node {
  46. id
  47. _id
  48. code
  49. defaultName
  50. countryId
  51. countryCode
  52. translations {
  53. edges {
  54. node {
  55. id
  56. locale
  57. defaultName
  58. }
  59. }
  60. }
  61. }
  62. }
  63. pageInfo {
  64. hasNextPage
  65. endCursor
  66. }
  67. totalCount
  68. }
  69. }
  70. }
  71. GQL;
  72. $variables = ['id' => '1'];
  73. $response = $this->graphQL($query, $variables);
  74. $response->assertSuccessful();
  75. $node = $response->json('data.country');
  76. $this->assertNotNull($node, 'country response is null');
  77. $this->assertArrayHasKey('id', $node);
  78. $this->assertArrayHasKey('_id', $node);
  79. $this->assertArrayHasKey('code', $node);
  80. $this->assertArrayHasKey('name', $node);
  81. // Verify states
  82. $this->assertArrayHasKey('states', $node);
  83. $states = $node['states'];
  84. $this->assertIsArray($states['edges'] ?? []);
  85. $this->assertArrayHasKey('pageInfo', $states);
  86. $this->assertArrayHasKey('totalCount', $states);
  87. // pageInfo assertions
  88. $this->assertArrayHasKey('hasNextPage', $states['pageInfo']);
  89. $this->assertArrayHasKey('endCursor', $states['pageInfo']);
  90. // Verify state edges
  91. foreach ($states['edges'] ?? [] as $edge) {
  92. $this->assertArrayHasKey('node', $edge);
  93. $state = $edge['node'] ?? null;
  94. $this->assertNotNull($state, 'state node is null');
  95. $this->assertArrayHasKey('id', $state);
  96. $this->assertArrayHasKey('_id', $state);
  97. $this->assertArrayHasKey('code', $state);
  98. $this->assertArrayHasKey('defaultName', $state);
  99. $this->assertArrayHasKey('countryId', $state);
  100. $this->assertArrayHasKey('countryCode', $state);
  101. // Verify translations
  102. $this->assertArrayHasKey('translations', $state);
  103. $translations = $state['translations'];
  104. $this->assertIsArray($translations['edges'] ?? []);
  105. foreach ($translations['edges'] ?? [] as $tEdge) {
  106. $t = $tEdge['node'] ?? null;
  107. $this->assertNotNull($t, 'translation node is null');
  108. $this->assertArrayHasKey('id', $t);
  109. $this->assertArrayHasKey('locale', $t);
  110. $this->assertArrayHasKey('defaultName', $t);
  111. }
  112. }
  113. }
  114. /**
  115. * Get Country with Translations
  116. */
  117. public function test_get_country_with_translations(): void
  118. {
  119. $query = <<<'GQL'
  120. query getSingleCountry($id: ID!) {
  121. country(id: $id) {
  122. id
  123. _id
  124. code
  125. name
  126. translations {
  127. edges {
  128. node {
  129. id
  130. _id
  131. locale
  132. name
  133. }
  134. cursor
  135. }
  136. pageInfo {
  137. hasNextPage
  138. endCursor
  139. }
  140. totalCount
  141. }
  142. }
  143. }
  144. GQL;
  145. $variables = ['id' => '1'];
  146. $response = $this->graphQL($query, $variables);
  147. $response->assertSuccessful();
  148. $node = $response->json('data.country');
  149. $this->assertNotNull($node, 'country response is null');
  150. $this->assertArrayHasKey('id', $node);
  151. $this->assertArrayHasKey('_id', $node);
  152. $this->assertArrayHasKey('code', $node);
  153. $this->assertArrayHasKey('name', $node);
  154. // Verify translations
  155. $this->assertArrayHasKey('translations', $node);
  156. $translations = $node['translations'];
  157. $this->assertIsArray($translations['edges'] ?? []);
  158. $this->assertArrayHasKey('pageInfo', $translations);
  159. $this->assertArrayHasKey('totalCount', $translations);
  160. // pageInfo assertions
  161. $this->assertArrayHasKey('hasNextPage', $translations['pageInfo']);
  162. $this->assertArrayHasKey('endCursor', $translations['pageInfo']);
  163. // Verify translation edges
  164. foreach ($translations['edges'] ?? [] as $edge) {
  165. $this->assertArrayHasKey('node', $edge);
  166. $this->assertArrayHasKey('cursor', $edge);
  167. $t = $edge['node'] ?? null;
  168. $this->assertNotNull($t, 'translation node is null');
  169. $this->assertArrayHasKey('id', $t);
  170. $this->assertArrayHasKey('_id', $t);
  171. $this->assertArrayHasKey('locale', $t);
  172. $this->assertArrayHasKey('name', $t);
  173. }
  174. }
  175. /**
  176. * Get Country - Complete Details
  177. */
  178. public function test_get_country_complete_details(): void
  179. {
  180. $query = <<<'GQL'
  181. query getSingleCountry($id: ID!) {
  182. country(id: $id) {
  183. id
  184. _id
  185. code
  186. name
  187. states {
  188. edges {
  189. node {
  190. id
  191. _id
  192. code
  193. defaultName
  194. countryId
  195. countryCode
  196. translations {
  197. edges {
  198. node {
  199. id
  200. locale
  201. defaultName
  202. }
  203. }
  204. }
  205. }
  206. }
  207. pageInfo {
  208. hasNextPage
  209. endCursor
  210. }
  211. totalCount
  212. }
  213. translations {
  214. edges {
  215. node {
  216. id
  217. _id
  218. locale
  219. name
  220. }
  221. }
  222. pageInfo {
  223. hasNextPage
  224. endCursor
  225. }
  226. totalCount
  227. }
  228. }
  229. }
  230. GQL;
  231. $variables = ['id' => '106'];
  232. $response = $this->graphQL($query, $variables);
  233. $response->assertSuccessful();
  234. $node = $response->json('data.country');
  235. $this->assertNotNull($node, 'country response is null');
  236. $this->assertArrayHasKey('id', $node);
  237. $this->assertArrayHasKey('_id', $node);
  238. $this->assertArrayHasKey('code', $node);
  239. $this->assertArrayHasKey('name', $node);
  240. // Verify states
  241. $this->assertArrayHasKey('states', $node);
  242. $states = $node['states'];
  243. $this->assertIsArray($states['edges'] ?? []);
  244. $this->assertArrayHasKey('pageInfo', $states);
  245. $this->assertArrayHasKey('totalCount', $states);
  246. // pageInfo assertions for states
  247. $this->assertArrayHasKey('hasNextPage', $states['pageInfo']);
  248. $this->assertArrayHasKey('endCursor', $states['pageInfo']);
  249. // Verify state edges
  250. foreach ($states['edges'] ?? [] as $edge) {
  251. $this->assertArrayHasKey('node', $edge);
  252. $state = $edge['node'] ?? null;
  253. $this->assertNotNull($state, 'state node is null');
  254. $this->assertArrayHasKey('id', $state);
  255. $this->assertArrayHasKey('_id', $state);
  256. $this->assertArrayHasKey('code', $state);
  257. $this->assertArrayHasKey('defaultName', $state);
  258. $this->assertArrayHasKey('countryId', $state);
  259. $this->assertArrayHasKey('countryCode', $state);
  260. // Verify state translations
  261. $this->assertArrayHasKey('translations', $state);
  262. $stateTranslations = $state['translations'];
  263. $this->assertIsArray($stateTranslations['edges'] ?? []);
  264. foreach ($stateTranslations['edges'] ?? [] as $tEdge) {
  265. $t = $tEdge['node'] ?? null;
  266. $this->assertNotNull($t, 'state translation node is null');
  267. $this->assertArrayHasKey('id', $t);
  268. $this->assertArrayHasKey('locale', $t);
  269. $this->assertArrayHasKey('defaultName', $t);
  270. }
  271. }
  272. // Verify country translations
  273. $this->assertArrayHasKey('translations', $node);
  274. $countryTranslations = $node['translations'];
  275. $this->assertIsArray($countryTranslations['edges'] ?? []);
  276. $this->assertArrayHasKey('pageInfo', $countryTranslations);
  277. $this->assertArrayHasKey('totalCount', $countryTranslations);
  278. // pageInfo assertions for translations
  279. $this->assertArrayHasKey('hasNextPage', $countryTranslations['pageInfo']);
  280. $this->assertArrayHasKey('endCursor', $countryTranslations['pageInfo']);
  281. // Verify country translation edges
  282. foreach ($countryTranslations['edges'] ?? [] as $edge) {
  283. $this->assertArrayHasKey('node', $edge);
  284. $t = $edge['node'] ?? null;
  285. $this->assertNotNull($t, 'country translation node is null');
  286. $this->assertArrayHasKey('id', $t);
  287. $this->assertArrayHasKey('_id', $t);
  288. $this->assertArrayHasKey('locale', $t);
  289. $this->assertArrayHasKey('name', $t);
  290. }
  291. }
  292. /**
  293. * Get Countries - Basic
  294. */
  295. public function test_get_countries_basic(): void
  296. {
  297. $query = <<<'GQL'
  298. query getCountries {
  299. countries {
  300. edges {
  301. node {
  302. id
  303. _id
  304. code
  305. name
  306. }
  307. }
  308. pageInfo {
  309. hasNextPage
  310. endCursor
  311. }
  312. totalCount
  313. }
  314. }
  315. GQL;
  316. $response = $this->graphQL($query);
  317. $response->assertSuccessful();
  318. $data = $response->json('data.countries');
  319. $this->assertNotNull($data, 'countries response is null');
  320. $this->assertIsArray($data['edges'] ?? [], 'countries.edges is not an array');
  321. $this->assertArrayHasKey('pageInfo', $data);
  322. $this->assertArrayHasKey('totalCount', $data);
  323. // pageInfo assertions
  324. $this->assertArrayHasKey('hasNextPage', $data['pageInfo']);
  325. $this->assertArrayHasKey('endCursor', $data['pageInfo']);
  326. // Verify country edges
  327. if (! empty($data['edges'])) {
  328. foreach ($data['edges'] as $edge) {
  329. $this->assertArrayHasKey('node', $edge);
  330. $node = $edge['node'] ?? null;
  331. $this->assertNotNull($node, 'country node is null');
  332. $this->assertArrayHasKey('id', $node);
  333. $this->assertArrayHasKey('_id', $node);
  334. $this->assertArrayHasKey('code', $node);
  335. $this->assertArrayHasKey('name', $node);
  336. }
  337. }
  338. }
  339. /**
  340. * Get Countries with States
  341. */
  342. public function test_get_countries_with_states(): void
  343. {
  344. $query = <<<'GQL'
  345. query countries {
  346. countries {
  347. edges {
  348. node {
  349. id
  350. _id
  351. code
  352. name
  353. states {
  354. edges {
  355. node {
  356. id
  357. _id
  358. code
  359. defaultName
  360. countryId
  361. countryCode
  362. translation {
  363. id
  364. locale
  365. defaultName
  366. }
  367. }
  368. }
  369. pageInfo {
  370. hasNextPage
  371. endCursor
  372. }
  373. totalCount
  374. }
  375. translations {
  376. edges {
  377. node {
  378. id
  379. locale
  380. name
  381. }
  382. }
  383. totalCount
  384. }
  385. }
  386. }
  387. pageInfo {
  388. hasNextPage
  389. endCursor
  390. }
  391. totalCount
  392. }
  393. }
  394. GQL;
  395. $response = $this->graphQL($query);
  396. $response->assertSuccessful();
  397. $data = $response->json('data.countries');
  398. $this->assertNotNull($data, 'countries response is null');
  399. $this->assertIsArray($data['edges'] ?? [], 'countries.edges is not an array');
  400. $this->assertArrayHasKey('pageInfo', $data);
  401. $this->assertArrayHasKey('totalCount', $data);
  402. // pageInfo assertions
  403. $this->assertArrayHasKey('hasNextPage', $data['pageInfo']);
  404. $this->assertArrayHasKey('endCursor', $data['pageInfo']);
  405. // Verify country edges
  406. foreach ($data['edges'] ?? [] as $edge) {
  407. $this->assertArrayHasKey('node', $edge);
  408. $node = $edge['node'] ?? null;
  409. $this->assertNotNull($node, 'country node is null');
  410. $this->assertArrayHasKey('id', $node);
  411. $this->assertArrayHasKey('_id', $node);
  412. $this->assertArrayHasKey('code', $node);
  413. $this->assertArrayHasKey('name', $node);
  414. // Verify states
  415. $this->assertArrayHasKey('states', $node);
  416. $states = $node['states'];
  417. $this->assertIsArray($states['edges'] ?? []);
  418. $this->assertArrayHasKey('pageInfo', $states);
  419. $this->assertArrayHasKey('totalCount', $states);
  420. // pageInfo assertions for states
  421. $this->assertArrayHasKey('hasNextPage', $states['pageInfo']);
  422. $this->assertArrayHasKey('endCursor', $states['pageInfo']);
  423. // Verify state edges
  424. foreach ($states['edges'] ?? [] as $stateEdge) {
  425. $this->assertArrayHasKey('node', $stateEdge);
  426. $state = $stateEdge['node'] ?? null;
  427. $this->assertNotNull($state, 'state node is null');
  428. $this->assertArrayHasKey('id', $state);
  429. $this->assertArrayHasKey('_id', $state);
  430. $this->assertArrayHasKey('code', $state);
  431. $this->assertArrayHasKey('defaultName', $state);
  432. $this->assertArrayHasKey('countryId', $state);
  433. $this->assertArrayHasKey('countryCode', $state);
  434. // Verify state translation (singular)
  435. $this->assertArrayHasKey('translation', $state);
  436. $translation = $state['translation'];
  437. if ($translation) {
  438. $this->assertArrayHasKey('id', $translation);
  439. $this->assertArrayHasKey('locale', $translation);
  440. $this->assertArrayHasKey('defaultName', $translation);
  441. }
  442. }
  443. // Verify country translations
  444. $this->assertArrayHasKey('translations', $node);
  445. $translations = $node['translations'];
  446. $this->assertIsArray($translations['edges'] ?? []);
  447. $this->assertArrayHasKey('totalCount', $translations);
  448. // Verify translation edges
  449. foreach ($translations['edges'] ?? [] as $tEdge) {
  450. $t = $tEdge['node'] ?? null;
  451. $this->assertNotNull($t, 'translation node is null');
  452. $this->assertArrayHasKey('id', $t);
  453. $this->assertArrayHasKey('locale', $t);
  454. $this->assertArrayHasKey('name', $t);
  455. }
  456. }
  457. }
  458. /**
  459. * Get Countries with Pagination
  460. */
  461. public function test_get_countries_with_pagination(): void
  462. {
  463. $query = <<<'GQL'
  464. query countries($first: Int, $after: String) {
  465. countries(first: $first, after: $after) {
  466. edges {
  467. node {
  468. id
  469. _id
  470. code
  471. name
  472. }
  473. cursor
  474. }
  475. pageInfo {
  476. endCursor
  477. startCursor
  478. hasNextPage
  479. hasPreviousPage
  480. }
  481. totalCount
  482. }
  483. }
  484. GQL;
  485. $variables = ['first' => 10, 'after' => null];
  486. $response = $this->graphQL($query, $variables);
  487. $response->assertSuccessful();
  488. $data = $response->json('data.countries');
  489. $this->assertNotNull($data, 'countries response is null');
  490. $this->assertIsArray($data['edges'] ?? [], 'countries.edges is not an array');
  491. $this->assertArrayHasKey('pageInfo', $data);
  492. $this->assertArrayHasKey('totalCount', $data);
  493. // pageInfo assertions
  494. $this->assertArrayHasKey('endCursor', $data['pageInfo']);
  495. $this->assertArrayHasKey('startCursor', $data['pageInfo']);
  496. $this->assertArrayHasKey('hasNextPage', $data['pageInfo']);
  497. $this->assertArrayHasKey('hasPreviousPage', $data['pageInfo']);
  498. // Verify country edges with cursor
  499. foreach ($data['edges'] ?? [] as $edge) {
  500. $this->assertArrayHasKey('node', $edge);
  501. $this->assertArrayHasKey('cursor', $edge);
  502. $node = $edge['node'] ?? null;
  503. $this->assertNotNull($node, 'country node is null');
  504. $this->assertArrayHasKey('id', $node);
  505. $this->assertArrayHasKey('_id', $node);
  506. $this->assertArrayHasKey('code', $node);
  507. $this->assertArrayHasKey('name', $node);
  508. }
  509. }
  510. /**
  511. * Get Countries with All Translations
  512. */
  513. public function test_get_countries_with_all_translations(): void
  514. {
  515. $query = <<<'GQL'
  516. query countries {
  517. countries {
  518. edges {
  519. node {
  520. id
  521. _id
  522. code
  523. translations {
  524. edges {
  525. node {
  526. id
  527. locale
  528. name
  529. }
  530. }
  531. totalCount
  532. }
  533. }
  534. }
  535. totalCount
  536. }
  537. }
  538. GQL;
  539. $response = $this->graphQL($query);
  540. $response->assertSuccessful();
  541. $data = $response->json('data.countries');
  542. $this->assertNotNull($data, 'countries response is null');
  543. $this->assertIsArray($data['edges'] ?? [], 'countries.edges is not an array');
  544. $this->assertArrayHasKey('totalCount', $data);
  545. // Verify country edges
  546. foreach ($data['edges'] ?? [] as $edge) {
  547. $this->assertArrayHasKey('node', $edge);
  548. $node = $edge['node'] ?? null;
  549. $this->assertNotNull($node, 'country node is null');
  550. $this->assertArrayHasKey('id', $node);
  551. $this->assertArrayHasKey('_id', $node);
  552. $this->assertArrayHasKey('code', $node);
  553. // Verify translations
  554. $this->assertArrayHasKey('translations', $node);
  555. $translations = $node['translations'];
  556. $this->assertIsArray($translations['edges'] ?? []);
  557. $this->assertArrayHasKey('totalCount', $translations);
  558. // Verify translation edges
  559. foreach ($translations['edges'] ?? [] as $tEdge) {
  560. $t = $tEdge['node'] ?? null;
  561. $this->assertNotNull($t, 'translation node is null');
  562. $this->assertArrayHasKey('id', $t);
  563. $this->assertArrayHasKey('locale', $t);
  564. $this->assertArrayHasKey('name', $t);
  565. }
  566. }
  567. }
  568. /**
  569. * Get Countries for address form
  570. */
  571. public function test_get_countries_for_address_form(): void
  572. {
  573. $query = <<<'GQL'
  574. query countries {
  575. countries {
  576. edges {
  577. node {
  578. id
  579. _id
  580. code
  581. name
  582. states {
  583. edges {
  584. node {
  585. id
  586. _id
  587. code
  588. defaultName
  589. }
  590. }
  591. totalCount
  592. }
  593. }
  594. cursor
  595. }
  596. pageInfo {
  597. hasNextPage
  598. endCursor
  599. }
  600. totalCount
  601. }
  602. }
  603. GQL;
  604. $response = $this->graphQL($query);
  605. $response->assertSuccessful();
  606. $data = $response->json('data.countries');
  607. $this->assertNotNull($data, 'countries response is null');
  608. $this->assertIsArray($data['edges'] ?? [], 'countries.edges is not an array');
  609. $this->assertArrayHasKey('pageInfo', $data);
  610. $this->assertArrayHasKey('totalCount', $data);
  611. // pageInfo assertions
  612. $this->assertArrayHasKey('hasNextPage', $data['pageInfo']);
  613. $this->assertArrayHasKey('endCursor', $data['pageInfo']);
  614. // Verify country edges with cursor
  615. foreach ($data['edges'] ?? [] as $edge) {
  616. $this->assertArrayHasKey('node', $edge);
  617. $this->assertArrayHasKey('cursor', $edge);
  618. $node = $edge['node'] ?? null;
  619. $this->assertNotNull($node, 'country node is null');
  620. $this->assertArrayHasKey('id', $node);
  621. $this->assertArrayHasKey('_id', $node);
  622. $this->assertArrayHasKey('code', $node);
  623. $this->assertArrayHasKey('name', $node);
  624. // Verify states
  625. $this->assertArrayHasKey('states', $node);
  626. $states = $node['states'];
  627. $this->assertIsArray($states['edges'] ?? []);
  628. $this->assertArrayHasKey('totalCount', $states);
  629. // Verify state edges
  630. foreach ($states['edges'] ?? [] as $stateEdge) {
  631. $this->assertArrayHasKey('node', $stateEdge);
  632. $state = $stateEdge['node'] ?? null;
  633. $this->assertNotNull($state, 'state node is null');
  634. $this->assertArrayHasKey('id', $state);
  635. $this->assertArrayHasKey('_id', $state);
  636. $this->assertArrayHasKey('code', $state);
  637. $this->assertArrayHasKey('defaultName', $state);
  638. }
  639. }
  640. }
  641. /**
  642. * Get Country States - Basic
  643. */
  644. public function test_get_country_states_basic(): void
  645. {
  646. $query = <<<'GQL'
  647. query getCountryStates($countryId: Int!) {
  648. countryStates(countryId: $countryId) {
  649. edges {
  650. node {
  651. id
  652. _id
  653. code
  654. defaultName
  655. countryId
  656. countryCode
  657. }
  658. }
  659. totalCount
  660. }
  661. }
  662. GQL;
  663. $variables = ['countryId' => 16];
  664. $response = $this->graphQL($query, $variables);
  665. $response->assertSuccessful();
  666. $data = $response->json('data.countryStates');
  667. $this->assertNotNull($data, 'countryStates response is null');
  668. $this->assertIsArray($data['edges'] ?? [], 'countryStates.edges is not an array');
  669. $this->assertArrayHasKey('totalCount', $data);
  670. // Verify state edges
  671. foreach ($data['edges'] ?? [] as $edge) {
  672. $this->assertArrayHasKey('node', $edge);
  673. $state = $edge['node'] ?? null;
  674. $this->assertNotNull($state, 'state node is null');
  675. $this->assertArrayHasKey('id', $state);
  676. $this->assertArrayHasKey('_id', $state);
  677. $this->assertArrayHasKey('code', $state);
  678. $this->assertArrayHasKey('defaultName', $state);
  679. $this->assertArrayHasKey('countryId', $state);
  680. $this->assertArrayHasKey('countryCode', $state);
  681. }
  682. }
  683. /**
  684. * Get Country States with Translations
  685. */
  686. public function test_get_country_states_with_translations(): void
  687. {
  688. $query = <<<'GQL'
  689. query getCountryStates($countryId: Int!) {
  690. countryStates(countryId: $countryId) {
  691. edges {
  692. node {
  693. id
  694. _id
  695. code
  696. defaultName
  697. countryId
  698. countryCode
  699. translations {
  700. edges {
  701. node {
  702. id
  703. locale
  704. defaultName
  705. }
  706. }
  707. totalCount
  708. }
  709. }
  710. }
  711. totalCount
  712. }
  713. }
  714. GQL;
  715. $variables = ['countryId' => 16];
  716. $response = $this->graphQL($query, $variables);
  717. $response->assertSuccessful();
  718. $data = $response->json('data.countryStates');
  719. $this->assertNotNull($data, 'countryStates response is null');
  720. $this->assertIsArray($data['edges'] ?? [], 'countryStates.edges is not an array');
  721. $this->assertArrayHasKey('totalCount', $data);
  722. // Verify state edges
  723. foreach ($data['edges'] ?? [] as $edge) {
  724. $this->assertArrayHasKey('node', $edge);
  725. $state = $edge['node'] ?? null;
  726. $this->assertNotNull($state, 'state node is null');
  727. $this->assertArrayHasKey('id', $state);
  728. $this->assertArrayHasKey('_id', $state);
  729. $this->assertArrayHasKey('code', $state);
  730. $this->assertArrayHasKey('defaultName', $state);
  731. $this->assertArrayHasKey('countryId', $state);
  732. $this->assertArrayHasKey('countryCode', $state);
  733. // Verify translations
  734. $this->assertArrayHasKey('translations', $state);
  735. $translations = $state['translations'];
  736. $this->assertIsArray($translations['edges'] ?? []);
  737. $this->assertArrayHasKey('totalCount', $translations);
  738. // Verify translation edges
  739. foreach ($translations['edges'] ?? [] as $tEdge) {
  740. $t = $tEdge['node'] ?? null;
  741. $this->assertNotNull($t, 'translation node is null');
  742. $this->assertArrayHasKey('id', $t);
  743. $this->assertArrayHasKey('locale', $t);
  744. $this->assertArrayHasKey('defaultName', $t);
  745. }
  746. }
  747. }
  748. /**
  749. * Get Country States with Pagination
  750. */
  751. public function test_get_country_states_with_pagination(): void
  752. {
  753. $query = <<<'GQL'
  754. query getCountryStates($countryId: Int!, $first: Int, $after: String) {
  755. countryStates(countryId: $countryId, first: $first, after: $after) {
  756. edges {
  757. node {
  758. id
  759. _id
  760. code
  761. defaultName
  762. countryId
  763. countryCode
  764. }
  765. cursor
  766. }
  767. pageInfo {
  768. endCursor
  769. startCursor
  770. hasNextPage
  771. hasPreviousPage
  772. }
  773. totalCount
  774. }
  775. }
  776. GQL;
  777. $variables = ['countryId' => 16, 'first' => 10, 'after' => null];
  778. $response = $this->graphQL($query, $variables);
  779. $response->assertSuccessful();
  780. $data = $response->json('data.countryStates');
  781. $this->assertNotNull($data, 'countryStates response is null');
  782. $this->assertIsArray($data['edges'] ?? [], 'countryStates.edges is not an array');
  783. $this->assertArrayHasKey('pageInfo', $data);
  784. $this->assertArrayHasKey('totalCount', $data);
  785. // pageInfo assertions
  786. $this->assertArrayHasKey('endCursor', $data['pageInfo']);
  787. $this->assertArrayHasKey('startCursor', $data['pageInfo']);
  788. $this->assertArrayHasKey('hasNextPage', $data['pageInfo']);
  789. $this->assertArrayHasKey('hasPreviousPage', $data['pageInfo']);
  790. // Verify state edges with cursor
  791. foreach ($data['edges'] ?? [] as $edge) {
  792. $this->assertArrayHasKey('node', $edge);
  793. $this->assertArrayHasKey('cursor', $edge);
  794. $state = $edge['node'] ?? null;
  795. $this->assertNotNull($state, 'state node is null');
  796. $this->assertArrayHasKey('id', $state);
  797. $this->assertArrayHasKey('_id', $state);
  798. $this->assertArrayHasKey('code', $state);
  799. $this->assertArrayHasKey('defaultName', $state);
  800. $this->assertArrayHasKey('countryId', $state);
  801. $this->assertArrayHasKey('countryCode', $state);
  802. }
  803. }
  804. /**
  805. * Get Country States for dropdown form
  806. */
  807. public function test_get_country_states_for_dropdown_form(): void
  808. {
  809. $query = <<<'GQL'
  810. query getCountryStates($countryId: Int!) {
  811. countryStates(countryId: $countryId) {
  812. edges {
  813. node {
  814. id
  815. _id
  816. code
  817. defaultName
  818. }
  819. }
  820. totalCount
  821. }
  822. }
  823. GQL;
  824. $variables = ['countryId' => 106];
  825. $response = $this->graphQL($query, $variables);
  826. $response->assertSuccessful();
  827. $data = $response->json('data.countryStates');
  828. $this->assertNotNull($data, 'countryStates response is null');
  829. $this->assertIsArray($data['edges'] ?? [], 'countryStates.edges is not an array');
  830. $this->assertArrayHasKey('totalCount', $data);
  831. // Verify state edges
  832. foreach ($data['edges'] ?? [] as $edge) {
  833. $this->assertArrayHasKey('node', $edge);
  834. $state = $edge['node'] ?? null;
  835. $this->assertNotNull($state, 'state node is null');
  836. $this->assertArrayHasKey('id', $state);
  837. $this->assertArrayHasKey('_id', $state);
  838. $this->assertArrayHasKey('code', $state);
  839. $this->assertArrayHasKey('defaultName', $state);
  840. }
  841. }
  842. /**
  843. * Get Country State - Basic
  844. */
  845. public function test_get_country_state_basic(): void
  846. {
  847. $query = <<<'GQL'
  848. query getCountryState($id: ID!) {
  849. countryState(id: $id) {
  850. id
  851. _id
  852. code
  853. defaultName
  854. countryId
  855. countryCode
  856. }
  857. }
  858. GQL;
  859. $variables = ['id' => '16'];
  860. $response = $this->graphQL($query, $variables);
  861. $response->assertSuccessful();
  862. $node = $response->json('data.countryState');
  863. $this->assertNotNull($node, 'countryState response is null');
  864. $this->assertArrayHasKey('id', $node);
  865. $this->assertArrayHasKey('_id', $node);
  866. $this->assertArrayHasKey('code', $node);
  867. $this->assertArrayHasKey('defaultName', $node);
  868. $this->assertArrayHasKey('countryId', $node);
  869. $this->assertArrayHasKey('countryCode', $node);
  870. }
  871. /**
  872. * Get Country State with Translations
  873. */
  874. public function test_get_country_state_with_translations(): void
  875. {
  876. $query = <<<'GQL'
  877. query getCountryState($id: ID!) {
  878. countryState(id: $id) {
  879. id
  880. _id
  881. code
  882. defaultName
  883. countryId
  884. countryCode
  885. translations {
  886. edges {
  887. node {
  888. id
  889. locale
  890. defaultName
  891. }
  892. }
  893. totalCount
  894. }
  895. }
  896. }
  897. GQL;
  898. $variables = ['id' => '16'];
  899. $response = $this->graphQL($query, $variables);
  900. $response->assertSuccessful();
  901. $node = $response->json('data.countryState');
  902. $this->assertNotNull($node, 'countryState response is null');
  903. $this->assertArrayHasKey('id', $node);
  904. $this->assertArrayHasKey('_id', $node);
  905. $this->assertArrayHasKey('code', $node);
  906. $this->assertArrayHasKey('defaultName', $node);
  907. $this->assertArrayHasKey('countryId', $node);
  908. $this->assertArrayHasKey('countryCode', $node);
  909. // Verify translations
  910. $this->assertArrayHasKey('translations', $node);
  911. $translations = $node['translations'];
  912. $this->assertIsArray($translations['edges'] ?? []);
  913. $this->assertArrayHasKey('totalCount', $translations);
  914. // Verify translation edges
  915. foreach ($translations['edges'] ?? [] as $tEdge) {
  916. $t = $tEdge['node'] ?? null;
  917. $this->assertNotNull($t, 'translation node is null');
  918. $this->assertArrayHasKey('id', $t);
  919. $this->assertArrayHasKey('locale', $t);
  920. $this->assertArrayHasKey('defaultName', $t);
  921. }
  922. }
  923. /**
  924. * Get Country State for address validation
  925. */
  926. public function test_get_country_state_for_address_validation(): void
  927. {
  928. $query = <<<'GQL'
  929. query getCountryState($id: ID!) {
  930. countryState(id: $id) {
  931. id
  932. _id
  933. code
  934. defaultName
  935. countryId
  936. countryCode
  937. }
  938. }
  939. GQL;
  940. $variables = ['id' => '/api/shop/country-states/16'];
  941. $response = $this->graphQL($query, $variables);
  942. $response->assertSuccessful();
  943. $node = $response->json('data.countryState');
  944. $this->assertNotNull($node, 'countryState response is null');
  945. $this->assertArrayHasKey('id', $node);
  946. $this->assertArrayHasKey('_id', $node);
  947. $this->assertArrayHasKey('code', $node);
  948. $this->assertArrayHasKey('defaultName', $node);
  949. $this->assertArrayHasKey('countryId', $node);
  950. $this->assertArrayHasKey('countryCode', $node);
  951. }
  952. }