LocaleTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
  3. use Webkul\BagistoApi\Tests\GraphQLTestCase;
  4. use Webkul\Core\Models\Locale;
  5. class LocaleTest extends GraphQLTestCase
  6. {
  7. /**
  8. * Test: Query all locales collection
  9. */
  10. public function test_get_locales_collection(): void
  11. {
  12. $query = <<<'GQL'
  13. query getLocales {
  14. locales {
  15. edges {
  16. cursor
  17. node {
  18. id
  19. _id
  20. code
  21. name
  22. direction
  23. logoPath
  24. logoUrl
  25. createdAt
  26. updatedAt
  27. }
  28. }
  29. pageInfo {
  30. endCursor
  31. startCursor
  32. hasNextPage
  33. hasPreviousPage
  34. }
  35. totalCount
  36. }
  37. }
  38. GQL;
  39. $response = $this->graphQL($query);
  40. $response->assertOk();
  41. $response->assertJsonStructure([
  42. 'data' => [
  43. 'locales' => [
  44. 'edges' => [
  45. '*' => [
  46. 'cursor',
  47. 'node' => [
  48. 'id',
  49. '_id',
  50. 'code',
  51. 'name',
  52. 'direction',
  53. 'logoPath',
  54. 'logoUrl',
  55. 'createdAt',
  56. 'updatedAt',
  57. ],
  58. ],
  59. ],
  60. 'pageInfo' => [
  61. 'endCursor',
  62. 'startCursor',
  63. 'hasNextPage',
  64. 'hasPreviousPage',
  65. ],
  66. 'totalCount',
  67. ],
  68. ],
  69. ]);
  70. $data = $response->json('data.locales');
  71. expect($data['totalCount'])->toBe(Locale::count());
  72. expect($data['edges'])->not()->toBeEmpty();
  73. }
  74. /**
  75. * Test: Query single locale by ID
  76. */
  77. public function test_get_locale_by_id(): void
  78. {
  79. $locale = Locale::first();
  80. $localeId = "/api/shop/locales/{$locale->id}";
  81. $query = <<<GQL
  82. query getLocale {
  83. locale(id: "{$localeId}") {
  84. id
  85. _id
  86. code
  87. name
  88. direction
  89. logoPath
  90. logoUrl
  91. createdAt
  92. updatedAt
  93. }
  94. }
  95. GQL;
  96. $response = $this->graphQL($query);
  97. $response->assertOk();
  98. $data = $response->json('data.locale');
  99. expect($data['_id'])->toBe($locale->id);
  100. expect($data['code'])->toBe($locale->code);
  101. expect($data['name'])->toBe($locale->name);
  102. expect($data['direction'])->toBe($locale->direction);
  103. }
  104. /**
  105. * Test: Timestamps are returned in ISO8601 format
  106. */
  107. public function test_locale_timestamps_are_iso8601_format(): void
  108. {
  109. $query = <<<'GQL'
  110. query getLocales {
  111. locales(first: 1) {
  112. edges {
  113. node {
  114. code
  115. createdAt
  116. updatedAt
  117. }
  118. }
  119. }
  120. }
  121. GQL;
  122. $response = $this->graphQL($query);
  123. $response->assertOk();
  124. $locale = $response->json('data.locales.edges.0.node');
  125. // Verify ISO8601 format (should contain 'T' and 'Z' or timezone)
  126. expect($locale['createdAt'])->toMatch('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/');
  127. expect($locale['updatedAt'])->toMatch('/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/');
  128. }
  129. /**
  130. * Test: Query locales with pagination (first)
  131. */
  132. public function test_locales_pagination_first(): void
  133. {
  134. $query = <<<'GQL'
  135. query getLocales {
  136. locales(first: 1) {
  137. edges {
  138. node {
  139. id
  140. code
  141. }
  142. }
  143. pageInfo {
  144. hasNextPage
  145. }
  146. }
  147. }
  148. GQL;
  149. $response = $this->graphQL($query);
  150. $response->assertOk();
  151. $data = $response->json('data.locales');
  152. expect($data['edges'])->toHaveCount(1);
  153. }
  154. /**
  155. * Test: Query locales with pagination (after cursor)
  156. */
  157. public function test_locales_pagination_after_cursor(): void
  158. {
  159. // Get first page
  160. $firstQuery = <<<'GQL'
  161. query getLocales {
  162. locales(first: 1) {
  163. edges {
  164. cursor
  165. node {
  166. code
  167. }
  168. }
  169. }
  170. }
  171. GQL;
  172. $firstResponse = $this->graphQL($firstQuery);
  173. $firstCursor = $firstResponse->json('data.locales.edges.0.cursor');
  174. // Get second page using cursor
  175. $secondQuery = <<<GQL
  176. query getLocales {
  177. locales(first: 1, after: "{$firstCursor}") {
  178. edges {
  179. node {
  180. code
  181. }
  182. }
  183. }
  184. }
  185. GQL;
  186. $secondResponse = $this->graphQL($secondQuery);
  187. $secondResponse->assertOk();
  188. }
  189. /**
  190. * Test: Query locales with introspection (schema exploration)
  191. */
  192. public function test_locale_introspection_query(): void
  193. {
  194. $query = <<<'GQL'
  195. {
  196. __type(name: "Locale") {
  197. name
  198. kind
  199. fields {
  200. name
  201. type {
  202. name
  203. kind
  204. }
  205. }
  206. }
  207. }
  208. GQL;
  209. $response = $this->graphQL($query);
  210. $response->assertOk();
  211. $type = $response->json('data.__type');
  212. expect($type['name'])->toBe('Locale');
  213. expect($type['kind'])->toBe('OBJECT');
  214. $fieldNames = collect($type['fields'])->pluck('name')->toArray();
  215. expect($fieldNames)->toContain('id', 'code', 'name', 'direction', 'createdAt', 'updatedAt');
  216. }
  217. /**
  218. * Test: Query returns appropriate error for invalid ID
  219. */
  220. public function test_invalid_locale_id_returns_error(): void
  221. {
  222. $query = <<<'GQL'
  223. query getLocale {
  224. locale(id: "/api/shop/locales/99999") {
  225. id
  226. code
  227. }
  228. }
  229. GQL;
  230. $response = $this->graphQL($query);
  231. $response->assertOk();
  232. expect($response->json('data.locale'))->toBeNull();
  233. }
  234. /**
  235. * Test: Logo URL is properly formatted
  236. */
  237. public function test_locale_logo_url_format(): void
  238. {
  239. $locale = Locale::first();
  240. $query = <<<GQL
  241. query getLocale {
  242. locale(id: "/api/shop/locales/{$locale->id}") {
  243. logoUrl
  244. }
  245. }
  246. GQL;
  247. $response = $this->graphQL($query);
  248. $response->assertOk();
  249. $logoUrl = $response->json('data.locale.logoUrl');
  250. // Should be either null or a valid URL
  251. if ($logoUrl) {
  252. expect($logoUrl)->toMatch('/http(s)?:\/\/.+/');
  253. }
  254. }
  255. /**
  256. * Test: Multiple fields can be queried together
  257. */
  258. public function test_query_multiple_locale_fields(): void
  259. {
  260. $query = <<<'GQL'
  261. query getLocales {
  262. locales(first: 5) {
  263. edges {
  264. node {
  265. id
  266. _id
  267. code
  268. name
  269. direction
  270. logoPath
  271. logoUrl
  272. createdAt
  273. updatedAt
  274. }
  275. }
  276. totalCount
  277. }
  278. }
  279. GQL;
  280. $response = $this->graphQL($query);
  281. $response->assertOk();
  282. $data = $response->json('data.locales');
  283. expect($data['totalCount'])->toBeGreaterThan(0);
  284. $node = $data['edges'][0]['node'];
  285. expect($node)->toHaveKeys(['id', '_id', 'code', 'name', 'direction', 'logoPath', 'logoUrl', 'createdAt', 'updatedAt']);
  286. }
  287. /**
  288. * Test: Direction field validation (should be 'ltr' or 'rtl')
  289. */
  290. public function test_locale_direction_values(): void
  291. {
  292. $query = <<<'GQL'
  293. query getLocales {
  294. locales {
  295. edges {
  296. node {
  297. direction
  298. }
  299. }
  300. }
  301. }
  302. GQL;
  303. $response = $this->graphQL($query);
  304. $response->assertOk();
  305. $edges = $response->json('data.locales.edges');
  306. foreach ($edges as $edge) {
  307. expect($edge['node']['direction'])->toBeIn(['ltr', 'rtl']);
  308. }
  309. }
  310. }