Completed
Pull Request — master (#2)
by Tomáš
01:43
created

MutationFieldsProviderTest::getMutationField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 13

Duplication

Lines 53
Ratio 100 %

Importance

Changes 0
Metric Value
dl 53
loc 53
rs 9.5797
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A MutationFieldsProviderTest.php$0 ➔ getName() 4 4 1
A MutationFieldsProviderTest.php$0 ➔ getType() 4 4 1
A MutationFieldsProviderTest.php$0 ➔ getDescription() 4 4 1
A MutationFieldsProviderTest.php$0 ➔ getArgs() 6 6 1
A MutationFieldsProviderTest.php$0 ➔ resolve() 4 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Mutation\MutationFieldInterface;
9
use Portiny\GraphQL\Contract\Provider\MutationFieldsProviderInterface;
10
use Portiny\GraphQL\Provider\MutationFieldsProvider;
11
12
13 View Code Duplication
class MutationFieldsProviderTest 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 MutationFieldsProviderInterface
18
	 */
19
	private $mutationFieldProvider;
20
21
22
	protected function setUp()
23
	{
24
		$this->mutationFieldProvider = new MutationFieldsProvider;
25
	}
26
27
28
	public function testAddField()
29
	{
30
		$mutationField = $this->getMutationField();
31
32
		$this->assertEmpty($this->mutationFieldProvider->getFields());
33
34
		$this->mutationFieldProvider->addField($mutationField);
35
36
		$this->assertCount(1, $this->mutationFieldProvider->getFields());
37
	}
38
39
40
	/**
41
	 * @expectedException \Portiny\GraphQL\Exception\Provider\ExistingMutationFieldException
42
	 * @expectedExceptionMessage Mutation field with name "Some name" is already registered.
43
	 */
44
	public function testAddFieldAlreadyExists()
45
	{
46
		$mutationField = $this->getMutationField();
47
48
		$this->assertEmpty($this->mutationFieldProvider->getFields());
49
50
		$this->mutationFieldProvider->addField($mutationField);
51
		$this->mutationFieldProvider->addField($mutationField);
52
	}
53
54
55
	public function testGetFields()
56
	{
57
		$mutationField = $this->getMutationField();
58
		$this->mutationFieldProvider->addField($mutationField);
59
60
		$fields = $this->mutationFieldProvider->getFields();
61
		$this->assertCount(1, $fields);
62
		$this->assertSame($mutationField, reset($fields));
63
	}
64
65
66
	public function testConvertFieldsToArray()
67
	{
68
		$mutationField = $this->getMutationField();
69
		$this->mutationFieldProvider->addField($mutationField);
70
71
		$output = $this->mutationFieldProvider->convertFieldsToArray();
72
		$this->assertSame('Some name', key($output));
73
74
		$mutationFieldAsArray = reset($output);
75
		$this->assertInstanceOf(StringType::class, $mutationFieldAsArray['type']);
76
		$this->assertSame('Some description', $mutationFieldAsArray['description']);
77
		$this->assertArrayHasKey('someArg', $mutationFieldAsArray['args']);
78
		$this->assertArrayHasKey('type', $mutationFieldAsArray['args']['someArg']);
79
		$this->assertInstanceOf(StringType::class, $mutationFieldAsArray['args']['someArg']['type']);
80
		$this->assertTrue(is_callable($mutationFieldAsArray['resolve']));
81
	}
82
83
84
	private function getMutationField(): MutationFieldInterface
85
	{
86
		return (new class () implements MutationFieldInterface
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