-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathServiceTest.php
161 lines (136 loc) · 5.79 KB
/
ServiceTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Upward\Test\Resolver;
use Laminas\Http\Client;
use Laminas\Http\Response;
use Magento\Upward\Definition;
use Magento\Upward\DefinitionIterator;
use Magento\Upward\Resolver\Service;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use function BeBat\Verify\verify;
/**
* @runTestsInSeparateProcesses
*/
class ServiceTest extends TestCase
{
use MockeryPHPUnitIntegration;
/**
* @var DefinitionIterator|Mockery\MockInterface
*/
private $definitionIteratorMock;
/**
* @var Service
*/
private $resolver;
protected function setUp(): void
{
$this->resolver = new Service();
$this->definitionIteratorMock = Mockery::mock(DefinitionIterator::class);
$this->resolver->setIterator($this->definitionIteratorMock);
}
/**
* Data provider to verify backwards compatibility with previous indicator.
*
* @return string[]
*/
public function indicatorDataProvider()
{
return [['url'], ['endpoint']];
}
public function testIndicator(): void
{
verify($this->resolver->getIndicator())->is()->sameAs('endpoint');
}
/**
* @dataProvider indicatorDataProvider
*/
public function testIsValid(string $indicator): void
{
$validDefinition = new Definition([$indicator => '/graphql', 'query' => 'gql']);
$validWithMethodDefinition = new Definition([$indicator => '/graphql', 'query' => 'gql', 'method' => 'GET']);
$invalidNoURL = new Definition(['query' => 'gql']);
$invalidNoQuery = new Definition([$indicator => '/graphql']);
$invalidUnsupportedMethod = new Definition([$indicator => '/graphql', 'query' => 'gql', 'method' => 'PUT']);
$this->definitionIteratorMock->shouldReceive('get')
->twice()
->with('method', Mockery::type(Definition::class))
->andReturnUsing(function (string $key, Definition $definition) {
return $definition->get($key);
});
verify($this->resolver->isValid($validDefinition))->is()->true();
verify($this->resolver->isValid($validWithMethodDefinition))->is()->true();
verify($this->resolver->isValid($invalidNoURL))->is()->false();
verify($this->resolver->isValid($invalidNoQuery))->is()->false();
verify($this->resolver->isValid($invalidUnsupportedMethod))->is()->false();
}
/**
* @dataProvider indicatorDataProvider
*/
public function testResolve(string $indicator): void
{
$definition = new Definition([$indicator => '/graphql', 'query' => 'gql']);
$expectedRequestBody = json_encode(['query' => 'gql', 'variables' => []]);
$expectedResponseArray = ['data' => ['key' => 'value']];
$this->definitionIteratorMock->shouldReceive('get')
->twice()
->with(Mockery::type('string'), Mockery::type(Definition::class))
->andReturnUsing(function (string $key, Definition $definition) {
return $definition->get($key);
});
$responseMock = Mockery::mock(Response::class);
$responseMock->shouldReceive('getBody')->andReturn('{"data":{"key":"value"}}');
$zendClientMock = Mockery::mock('overload:' . Client::class);
$zendClientMock->shouldReceive('setMethod')->with('POST');
$zendClientMock->shouldReceive('setHeaders')->with(['Content-type' => 'application/json']);
$zendClientMock->shouldReceive('setRawBody')->with($expectedRequestBody);
$zendClientMock->shouldReceive('send')->andReturn($responseMock);
verify($this->resolver->resolve($definition))->is()->sameAs($expectedResponseArray);
}
public function testResolveThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('$definition must be an instance of Magento\\Upward\\Definition');
$this->resolver->resolve('Not a Definition');
}
/**
* @dataProvider indicatorDataProvider
*/
public function testResolveWithConfiguration(string $indicator): void
{
$definition = new Definition([
$indicator => '/graphql',
'query' => 'gql',
'variables' => [
'var1' => 'var1Value',
],
'headers' => [
'header' => 'headerValue',
],
'method' => 'GET',
]);
$expectedRequestBody = ['query' => 'gql', 'variables' => ['var1' => 'var1Value']];
$expectedResponseArray = ['data' => ['key' => 'value']];
$this->definitionIteratorMock->shouldReceive('get')
->times(5)
->with(Mockery::type('string'), Mockery::type(Definition::class))
->andReturnUsing(function (string $key, Definition $definition) {
$returnValue = $definition->get($key);
return $returnValue instanceof Definition ? $returnValue->toArray() : $returnValue;
});
$responseMock = Mockery::mock(Response::class);
$responseMock->shouldReceive('getBody')->andReturn('{"data":{"key":"value"}}');
$zendClientMock = Mockery::mock('overload:' . Client::class);
$zendClientMock->shouldReceive('setMethod')->with('GET');
$zendClientMock->shouldReceive('setHeaders')
->with(['header' => 'headerValue']);
$zendClientMock->shouldReceive('setParameterGet')->with($expectedRequestBody);
$zendClientMock->shouldReceive('send')->andReturn($responseMock);
verify($this->resolver->resolve($definition))->is()->sameAs($expectedResponseArray);
}
}