Passed
Push — master ( 3d645a...097498 )
by Tomáš
05:27
created

terTest.php$0   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 5
1
<?php declare(strict_types=1);
2
3
namespace Portiny\GraphQL\Tests\Converter;
4
5
use GraphQL\Type\Definition\StringType;
6
use GraphQL\Type\Definition\Type;
7
use Portiny\GraphQL\Contract\Mutation\MutationFieldInterface;
8
use Portiny\GraphQL\Converter\MutationFieldConverter;
9
use Portiny\GraphQL\Tests\AbstractContainerTestCase;
10
11
final class MutationFieldConverterTest extends AbstractContainerTestCase
12
{
13
	public function testToArray(): void
14
	{
15
		$mutationField = $this->getMutationField();
16
		$output = MutationFieldConverter::toArray($mutationField);
17
18
		self::assertSame('Some name', key($output));
19
20
		$mutationFieldAsArray = reset($output);
21
		self::assertInstanceOf(StringType::class, $mutationFieldAsArray['type']);
22
		self::assertSame('Some description', $mutationFieldAsArray['description']);
23
		self::assertArrayHasKey('someArg', $mutationFieldAsArray['args']);
24
		self::assertArrayHasKey('type', $mutationFieldAsArray['args']['someArg']);
25
		self::assertInstanceOf(StringType::class, $mutationFieldAsArray['args']['someArg']['type']);
26
		self::assertTrue(is_callable($mutationFieldAsArray['resolve']));
27
	}
28
29
	public function testToObject(): void
30
	{
31
		$mutationField = $this->getMutationField();
32
		$mutationFieldAsArray = MutationFieldConverter::toArray($mutationField);
33
		$output = MutationFieldConverter::toObject($mutationFieldAsArray);
34
35
		self::assertSame('Some name', $output->getName());
36
		self::assertInstanceOf(StringType::class, $output->getType());
37
		self::assertSame('Some description', $output->getDescription());
38
		self::assertArrayHasKey('someArg', $output->getArgs());
39
		self::assertArrayHasKey('type', $output->getArgs()['someArg']);
40
		self::assertInstanceOf(StringType::class, $output->getArgs()['someArg']['type']);
41
		self::assertSame('resolved', $output->resolve([], ['someArg' => '']));
42
	}
43
44
	private function getMutationField(): MutationFieldInterface
45
	{
46
		return new class() implements MutationFieldInterface {
47
			/**
48
			 * {@inheritdoc}
49
			 */
50
			public function getName(): string
51
			{
52
				return 'Some name';
53
			}
54
55
			/**
56
			 * {@inheritdoc}
57
			 */
58
			public function getDescription(): string
59
			{
60
				return 'Some description';
61
			}
62
63
			/**
64
			 * {@inheritdoc}
65
			 */
66
			public function getArgs(): array
67
			{
68
				return [
69
					'someArg' => ['type' => Type::string()],
70
				];
71
			}
72
73
			/**
74
			 * {@inheritdoc}
75
			 */
76
			public function getType(): Type
77
			{
78
				return Type::string();
79
			}
80
81
			/**
82
			 * {@inheritdoc}
83
			 */
84
			public function resolve(array $root, array $args, $context = null)
85
			{
86
				return 'resolved';
87
			}
88
		};
89
	}
90
}
91