Code Duplication    Length = 126-126 lines in 2 locations

tests/Provider/MutationFieldsProviderTest.php 1 location

@@ 13-138 (lines=126) @@
10
use Portiny\GraphQL\Provider\MutationFieldsProvider;
11
12
13
class MutationFieldsProviderTest extends TestCase
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

tests/Provider/QueryFieldsProviderTest.php 1 location

@@ 13-138 (lines=126) @@
10
use Portiny\GraphQL\Provider\QueryFieldsProvider;
11
12
13
class QueryFieldsProviderTest extends TestCase
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