/packages/SDL/tests/Parser/Type/InterfaceTypeTestCase.php

https://github.com/railt/railt · PHP · 485 lines · 187 code · 71 blank · 227 comment · 0 complexity · ba0da047f6a4345cf394899db1161ac4 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of Railt package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. declare(strict_types=1);
  9. namespace Railt\SDL\Tests\Parser\Type;
  10. use Phplrt\Contracts\Parser\Exception\ParserRuntimeExceptionInterface;
  11. use PHPUnit\Framework\Exception;
  12. use PHPUnit\Framework\ExpectationFailedException;
  13. use Railt\SDL\Frontend\Ast\Definition\ArgumentDefinitionNode;
  14. use Railt\SDL\Frontend\Ast\Definition\Type\InterfaceTypeDefinitionNode;
  15. use Railt\SDL\Frontend\Ast\Node;
  16. use Railt\SDL\Frontend\Ast\Type\NamedTypeNode;
  17. use Railt\SDL\Tests\Parser\ParserTestCase;
  18. use Railt\TypeSystem\Value\IntValue;
  19. use Railt\TypeSystem\Value\StringValue;
  20. use SebastianBergmann\RecursionContext\InvalidArgumentException;
  21. /**
  22. * Class InterfaceTypeTestCase
  23. */
  24. class InterfaceTypeTestCase extends ParserTestCase
  25. {
  26. /**
  27. * @return void
  28. * @throws ParserRuntimeExceptionInterface
  29. * @throws ExpectationFailedException
  30. * @throws InvalidArgumentException
  31. * @throws \Throwable
  32. */
  33. public function testInterfaceDescription(): void
  34. {
  35. $type = $this->interfaceType();
  36. $this->assertSame('Type description', \trim($type->description->value));
  37. }
  38. /**
  39. * @return Node|InterfaceTypeDefinitionNode
  40. * @throws ParserRuntimeExceptionInterface
  41. * @throws \Throwable
  42. */
  43. protected function interfaceType(): Node
  44. {
  45. return $this->parseFirst(<<<'GraphQL'
  46. """
  47. Type description
  48. """
  49. interface Example implements Interface1 & Interface2 @typeDirective(a: 23) {
  50. "field description"
  51. field(
  52. "argument description"
  53. argument: ArgumentType = "argument default"
  54. @argumentDirective(b: 42)
  55. ): FieldType
  56. @fieldDirective(c: 666)
  57. }
  58. GraphQL);
  59. }
  60. /**
  61. * @return void
  62. * @throws ExpectationFailedException
  63. * @throws InvalidArgumentException
  64. * @throws ParserRuntimeExceptionInterface
  65. * @throws \Throwable
  66. */
  67. public function testInterfaceName(): void
  68. {
  69. $type = $this->interfaceType();
  70. $this->assertSame('Example', $type->name->value);
  71. }
  72. /**
  73. * @return void
  74. * @throws ExpectationFailedException
  75. * @throws InvalidArgumentException
  76. * @throws ParserRuntimeExceptionInterface
  77. * @throws Exception
  78. * @throws \Throwable
  79. */
  80. public function testInterfaceDirectivesCount(): void
  81. {
  82. $type = $this->interfaceType();
  83. $this->assertCount(1, $type->directives);
  84. }
  85. /**
  86. * @return void
  87. * @throws ExpectationFailedException
  88. * @throws InvalidArgumentException
  89. * @throws ParserRuntimeExceptionInterface
  90. * @throws Exception
  91. * @throws \Throwable
  92. */
  93. public function testInterfaceDirectiveName(): void
  94. {
  95. $type = $this->interfaceType();
  96. $this->assertSame('typeDirective', $type->directives[0]->name->name->value);
  97. }
  98. /**
  99. * @return void
  100. * @throws ExpectationFailedException
  101. * @throws InvalidArgumentException
  102. * @throws ParserRuntimeExceptionInterface
  103. * @throws Exception
  104. * @throws \Throwable
  105. */
  106. public function testInterfaceDirectivesArgumentsCount(): void
  107. {
  108. $type = $this->interfaceType();
  109. $this->assertCount(1, $type->directives[0]->arguments);
  110. }
  111. /**
  112. * @return void
  113. * @throws ExpectationFailedException
  114. * @throws InvalidArgumentException
  115. * @throws ParserRuntimeExceptionInterface
  116. * @throws Exception
  117. * @throws \Throwable
  118. */
  119. public function testInterfaceDirectivesArgumentName(): void
  120. {
  121. $type = $this->interfaceType();
  122. $this->assertSame('a', $type->directives[0]->arguments[0]->name->value);
  123. }
  124. /**
  125. * @return void
  126. * @throws ExpectationFailedException
  127. * @throws InvalidArgumentException
  128. * @throws ParserRuntimeExceptionInterface
  129. * @throws Exception
  130. * @throws \Throwable
  131. */
  132. public function testInterfaceDirectivesArgumentValue(): void
  133. {
  134. $type = $this->interfaceType();
  135. $this->assertEquals(new IntValue(23), $type->directives[0]->arguments[0]->value);
  136. }
  137. /**
  138. * @return void
  139. * @throws Exception
  140. * @throws ExpectationFailedException
  141. * @throws InvalidArgumentException
  142. * @throws ParserRuntimeExceptionInterface
  143. * @throws \Throwable
  144. */
  145. public function testInterfaceImplementsInterfacesCount(): void
  146. {
  147. $type = $this->interfaceType();
  148. $this->assertCount(2, $type->interfaces);
  149. }
  150. /**
  151. * @return void
  152. * @throws Exception
  153. * @throws ExpectationFailedException
  154. * @throws InvalidArgumentException
  155. * @throws ParserRuntimeExceptionInterface
  156. * @throws \Throwable
  157. */
  158. public function testInterfaceImplementsInterfaceNames(): void
  159. {
  160. $type = $this->interfaceType();
  161. $this->assertSame('Interface1', $type->interfaces[0]->interface->name->value);
  162. $this->assertSame('Interface2', $type->interfaces[1]->interface->name->value);
  163. }
  164. /**
  165. * @return void
  166. * @throws Exception
  167. * @throws ExpectationFailedException
  168. * @throws InvalidArgumentException
  169. * @throws ParserRuntimeExceptionInterface
  170. * @throws \Throwable
  171. */
  172. public function testFieldsCount(): void
  173. {
  174. $type = $this->interfaceType();
  175. $this->assertCount(1, $type->fields);
  176. }
  177. /**
  178. * @return void
  179. * @throws ExpectationFailedException
  180. * @throws InvalidArgumentException
  181. * @throws ParserRuntimeExceptionInterface
  182. * @throws \Throwable
  183. */
  184. public function testFieldName(): void
  185. {
  186. $type = $this->interfaceType();
  187. $this->assertSame('field', $type->fields[0]->name->value);
  188. }
  189. /**
  190. * @return void
  191. * @throws ExpectationFailedException
  192. * @throws InvalidArgumentException
  193. * @throws ParserRuntimeExceptionInterface
  194. * @throws \Throwable
  195. */
  196. public function testFieldDescription(): void
  197. {
  198. $type = $this->interfaceType();
  199. $this->assertSame('field description', $type->fields[0]->description->value);
  200. }
  201. /**
  202. * @return void
  203. * @throws ExpectationFailedException
  204. * @throws InvalidArgumentException
  205. * @throws ParserRuntimeExceptionInterface
  206. * @throws \Throwable
  207. */
  208. public function testFieldDirectivesCount(): void
  209. {
  210. $type = $this->interfaceType();
  211. $this->assertCount(1, $type->fields[0]->directives);
  212. }
  213. /**
  214. * @return void
  215. * @throws ExpectationFailedException
  216. * @throws InvalidArgumentException
  217. * @throws ParserRuntimeExceptionInterface
  218. * @throws \Throwable
  219. */
  220. public function testFieldDirectiveName(): void
  221. {
  222. $type = $this->interfaceType();
  223. $this->assertSame('fieldDirective', $type->fields[0]->directives[0]->name->name->value);
  224. }
  225. /**
  226. * @return void
  227. * @throws ExpectationFailedException
  228. * @throws InvalidArgumentException
  229. * @throws ParserRuntimeExceptionInterface
  230. * @throws \Throwable
  231. */
  232. public function testFieldDirectiveArgumentsCount(): void
  233. {
  234. $type = $this->interfaceType();
  235. $this->assertCount(1, $type->fields[0]->directives[0]->arguments);
  236. }
  237. /**
  238. * @return void
  239. * @throws ExpectationFailedException
  240. * @throws InvalidArgumentException
  241. * @throws ParserRuntimeExceptionInterface
  242. * @throws \Throwable
  243. */
  244. public function testFieldType(): void
  245. {
  246. $type = $this->interfaceType();
  247. $ofType = $type->fields[0]->type;
  248. $this->assertInstanceOf(NamedTypeNode::class, $ofType);
  249. $this->assertSame('FieldType', $ofType->name->value);
  250. }
  251. /**
  252. * @return void
  253. * @throws ExpectationFailedException
  254. * @throws InvalidArgumentException
  255. * @throws ParserRuntimeExceptionInterface
  256. * @throws \Throwable
  257. */
  258. public function testFieldDirectiveArgumentName(): void
  259. {
  260. $type = $this->interfaceType();
  261. $this->assertSame('c', $type->fields[0]->directives[0]->arguments[0]->name->value);
  262. }
  263. /**
  264. * @return void
  265. * @throws ExpectationFailedException
  266. * @throws InvalidArgumentException
  267. * @throws ParserRuntimeExceptionInterface
  268. * @throws \Throwable
  269. */
  270. public function testFieldDirectiveArgumentValue(): void
  271. {
  272. $type = $this->interfaceType();
  273. $this->assertEquals(new IntValue(666), $type->fields[0]->directives[0]->arguments[0]->value);
  274. }
  275. /**
  276. * @return void
  277. * @throws ExpectationFailedException
  278. * @throws InvalidArgumentException
  279. * @throws ParserRuntimeExceptionInterface
  280. * @throws \Throwable
  281. */
  282. public function testFieldArgumentsCount(): void
  283. {
  284. $type = $this->interfaceType();
  285. $this->assertCount(1, $type->fields[0]->arguments);
  286. }
  287. /**
  288. * @return void
  289. * @throws ExpectationFailedException
  290. * @throws InvalidArgumentException
  291. * @throws ParserRuntimeExceptionInterface
  292. * @throws \Throwable
  293. */
  294. public function testFieldArgumentName(): void
  295. {
  296. $type = $this->interfaceType();
  297. /** @var ArgumentDefinitionNode $argument */
  298. $argument = $type->fields[0]->arguments[0];
  299. $this->assertSame('argument', $argument->name->value);
  300. }
  301. /**
  302. * @return void
  303. * @throws ExpectationFailedException
  304. * @throws InvalidArgumentException
  305. * @throws ParserRuntimeExceptionInterface
  306. * @throws \Throwable
  307. */
  308. public function testFieldArgumentType(): void
  309. {
  310. $type = $this->interfaceType();
  311. /** @var ArgumentDefinitionNode $argument */
  312. $argument = $type->fields[0]->arguments[0];
  313. $this->assertInstanceOf(NamedTypeNode::class, $argument->type);
  314. $this->assertSame('ArgumentType', $argument->type->name->value);
  315. }
  316. /**
  317. * @return void
  318. * @throws ExpectationFailedException
  319. * @throws InvalidArgumentException
  320. * @throws ParserRuntimeExceptionInterface
  321. * @throws \Throwable
  322. */
  323. public function testFieldArgumentDefaultValue(): void
  324. {
  325. $type = $this->interfaceType();
  326. /** @var ArgumentDefinitionNode $argument */
  327. $argument = $type->fields[0]->arguments[0];
  328. $this->assertEquals(new StringValue('argument default'), $argument->defaultValue);
  329. }
  330. /**
  331. * @return void
  332. * @throws ExpectationFailedException
  333. * @throws InvalidArgumentException
  334. * @throws ParserRuntimeExceptionInterface
  335. * @throws \Throwable
  336. */
  337. public function testFieldArgumentDescription(): void
  338. {
  339. $type = $this->interfaceType();
  340. /** @var ArgumentDefinitionNode $argument */
  341. $argument = $type->fields[0]->arguments[0];
  342. $this->assertEquals('argument description', $argument->description->value);
  343. }
  344. /**
  345. * @return void
  346. * @throws ExpectationFailedException
  347. * @throws InvalidArgumentException
  348. * @throws ParserRuntimeExceptionInterface
  349. * @throws \Throwable
  350. */
  351. public function testFieldArgumentDirectivesCount(): void
  352. {
  353. $type = $this->interfaceType();
  354. /** @var ArgumentDefinitionNode $argument */
  355. $argument = $type->fields[0]->arguments[0];
  356. $this->assertCount(1, $argument->directives);
  357. }
  358. /**
  359. * @return void
  360. * @throws ExpectationFailedException
  361. * @throws InvalidArgumentException
  362. * @throws ParserRuntimeExceptionInterface
  363. * @throws \Throwable
  364. */
  365. public function testFieldArgumentDirectiveName(): void
  366. {
  367. $type = $this->interfaceType();
  368. /** @var ArgumentDefinitionNode $argument */
  369. $argument = $type->fields[0]->arguments[0];
  370. $this->assertSame('argumentDirective', $argument->directives[0]->name->name->value);
  371. }
  372. /**
  373. * @return void
  374. * @throws ExpectationFailedException
  375. * @throws InvalidArgumentException
  376. * @throws ParserRuntimeExceptionInterface
  377. * @throws \Throwable
  378. */
  379. public function testFieldArgumentDirectiveArgumentsCount(): void
  380. {
  381. $type = $this->interfaceType();
  382. /** @var ArgumentDefinitionNode $argument */
  383. $argument = $type->fields[0]->arguments[0];
  384. $this->assertCount(1, $argument->directives[0]->arguments);
  385. }
  386. /**
  387. * @return void
  388. * @throws ExpectationFailedException
  389. * @throws InvalidArgumentException
  390. * @throws ParserRuntimeExceptionInterface
  391. * @throws \Throwable
  392. */
  393. public function testFieldArgumentDirectiveArgumentName(): void
  394. {
  395. $type = $this->interfaceType();
  396. /** @var ArgumentDefinitionNode $argument */
  397. $argument = $type->fields[0]->arguments[0];
  398. $this->assertSame('b', $argument->directives[0]->arguments[0]->name->value);
  399. }
  400. /**
  401. * @return void
  402. * @throws ExpectationFailedException
  403. * @throws InvalidArgumentException
  404. * @throws ParserRuntimeExceptionInterface
  405. * @throws \Throwable
  406. */
  407. public function testFieldArgumentDirectiveArgumentValue(): void
  408. {
  409. $type = $this->interfaceType();
  410. /** @var ArgumentDefinitionNode $argument */
  411. $argument = $type->fields[0]->arguments[0];
  412. $this->assertEquals(new IntValue(42), $argument->directives[0]->arguments[0]->value);
  413. }
  414. }