Completed
Push — master ( b19cf5...8a98cd )
by Tomáš
03:10
created

QueryFieldsProviderTest.php$0 ➔ resolve()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
<?php declare(strict_types=1);
2
3
namespace Portiny\GraphQL\Tests\Provider;
4
5
use GraphQL\Type\Definition\StringType;
6
use GraphQL\Type\Definition\Type;
7
use PHPUnit\Framework\TestCase;
8
use Portiny\GraphQL\Contract\Field\QueryFieldInterface;
9
use Portiny\GraphQL\Contract\Provider\QueryFieldsProviderInterface;
10
use Portiny\GraphQL\Provider\QueryFieldsProvider;
11
use Portiny\GraphQL\Tests\Source\Provider\SomeQueryField;
12
13 View Code Duplication
class QueryFieldsProviderTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
	/**
16
	 * @var QueryFieldsProviderInterface
17
	 */
18
	private $queryFieldsProvider;
19
20
	protected function setUp(): void
21
	{
22
		$this->queryFieldsProvider = new QueryFieldsProvider();
23
	}
24
25
	public function testAddField(): void
26
	{
27
		$queryField = $this->getQueryField();
28
29
		$this->assertEmpty($this->queryFieldsProvider->getFields());
30
31
		$this->queryFieldsProvider->addField($queryField);
32
33
		$this->assertCount(1, $this->queryFieldsProvider->getFields());
34
	}
35
36
	/**
37
	 * @expectedException \Portiny\GraphQL\Exception\Provider\ExistingQueryFieldException
38
	 * @expectedExceptionMessage Query field with name "Some name" is already registered.
39
	 */
40
	public function testAddFieldAlreadyExists(): void
41
	{
42
		$queryField = $this->getQueryField();
43
44
		$this->assertEmpty($this->queryFieldsProvider->getFields());
45
46
		$this->queryFieldsProvider->addField($queryField);
47
		$this->queryFieldsProvider->addField($queryField);
48
	}
49
50
	public function testGetFields(): void
51
	{
52
		$queryField = $this->getQueryField();
53
		$this->queryFieldsProvider->addField($queryField);
54
55
		$fields = $this->queryFieldsProvider->getFields();
56
		$this->assertCount(1, $fields);
57
		$this->assertSame($queryField, reset($fields));
58
	}
59
60
	public function testConvertFieldsToArray(): void
61
	{
62
		$queryField = $this->getQueryField();
63
		$this->queryFieldsProvider->addField($queryField);
64
65
		$output = $this->queryFieldsProvider->convertFieldsToArray();
66
		$this->assertSame('Some name', key($output));
67
68
		$queryFieldAsArray = reset($output);
69
		$this->assertInstanceOf(StringType::class, $queryFieldAsArray['type']);
70
		$this->assertSame('Some description', $queryFieldAsArray['description']);
71
		$this->assertArrayHasKey('someArg', $queryFieldAsArray['args']);
72
		$this->assertArrayHasKey('type', $queryFieldAsArray['args']['someArg']);
73
		$this->assertInstanceOf(StringType::class, $queryFieldAsArray['args']['someArg']['type']);
74
		$this->assertTrue(is_callable($queryFieldAsArray['resolve']));
75
76
		$output = $this->queryFieldsProvider->convertFieldsToArray([SomeQueryField::class]);
77
		$this->assertEmpty($output);
78
	}
79
80
	private function getQueryField(): QueryFieldInterface
81
	{
82
		return new class() implements QueryFieldInterface {
83
			/**
84
			 * {@inheritdoc}
85
			 */
86
			public function getName(): string
87
			{
88
				return 'Some name';
89
			}
90
91
			/**
92
			 * {@inheritdoc}
93
			 */
94
			public function getType(): Type
95
			{
96
				return Type::string();
97
			}
98
99
			/**
100
			 * {@inheritdoc}
101
			 */
102
			public function getDescription(): string
103
			{
104
				return 'Some description';
105
			}
106
107
			/**
108
			 * {@inheritdoc}
109
			 */
110
			public function getArgs(): array
111
			{
112
				return [
113
					'someArg' => ['type' => Type::string()],
114
				];
115
			}
116
117
			/**
118
			 * {@inheritdoc}
119
			 */
120
			public function resolve(array $root, array $args, $context = NULL)
121
			{
122
				return 'resolved';
123
			}
124
		};
125
	}
126
}
127