Completed
Push — master ( 9263b2...ac54ef )
by Tomáš
16s
created

viderTest.php$0   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 50
loc 50
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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