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