|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace spec\Omikron\FactFinder\Shopware6\Storefront\Controller; |
| 6 | + |
| 7 | +use GuzzleHttp\Exception\ConnectException; |
| 8 | +use GuzzleHttp\Psr7\Stream; |
| 9 | +use Omikron\FactFinder\Communication\Client\ClientBuilder; |
| 10 | +use Omikron\FactFinder\Communication\Client\ClientInterface; |
| 11 | +use Omikron\FactFinder\Shopware6\Config\Communication; |
| 12 | +use Omikron\FactFinder\Shopware6\Events\BeforeProxyErrorResponseEvent; |
| 13 | +use Omikron\FactFinder\Shopware6\Events\EnrichProxyDataEvent; |
| 14 | +use PhpSpec\ObjectBehavior; |
| 15 | +use PhpSpec\Wrapper\Collaborator; |
| 16 | +use PHPUnit\Framework\Assert; |
| 17 | +use Prophecy\Argument; |
| 18 | +use Psr\Http\Client\ClientExceptionInterface; |
| 19 | +use Psr\Http\Message\RequestInterface; |
| 20 | +use Psr\Http\Message\ResponseInterface; |
| 21 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 22 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 23 | +use Symfony\Component\HttpFoundation\Request; |
| 24 | +use Symfony\Component\HttpFoundation\Response; |
| 25 | + |
| 26 | +class ProxyControllerSpec extends ObjectBehavior |
| 27 | +{ |
| 28 | + private Collaborator $client; |
| 29 | + private Collaborator $clientBuilder; |
| 30 | + |
| 31 | + public function let( |
| 32 | + Communication $config, |
| 33 | + ClientInterface $client, |
| 34 | + ClientBuilder $clientBuilder |
| 35 | + ) { |
| 36 | + $serverUrl = 'https://example.fact-finder.de/fact-finder'; |
| 37 | + $config->getServerUrl()->willReturn($serverUrl); |
| 38 | + $config->getVersion()->willReturn('ng'); |
| 39 | + $config->getCredentials()->willReturn([ |
| 40 | + 'username', |
| 41 | + 'pass', |
| 42 | + ]); |
| 43 | + $this->beConstructedWith($config); |
| 44 | + $clientBuilder->build()->willReturn($client); |
| 45 | + $clientBuilder->withServerUrl(Argument::any())->willReturn($clientBuilder); |
| 46 | + $clientBuilder->withCredentials(Argument::any())->willReturn($clientBuilder); |
| 47 | + $clientBuilder->withVersion(Argument::any())->willReturn($clientBuilder); |
| 48 | + $this->client = $client; |
| 49 | + $this->clientBuilder = $clientBuilder; |
| 50 | + } |
| 51 | + |
| 52 | + public function it_should_return_success_response( |
| 53 | + Request $request, |
| 54 | + ResponseInterface $response, |
| 55 | + EventDispatcherInterface $eventDispatcher, |
| 56 | + EnrichProxyDataEvent $event, |
| 57 | + Stream $stream |
| 58 | + ) { |
| 59 | + // Expect & Given |
| 60 | + $request->getMethod()->willReturn(Request::METHOD_GET); |
| 61 | + $uri = 'rest/v5/search/example_channel?query=bag&sid=123&format=json'; |
| 62 | + $_SERVER['REQUEST_URI'] = sprintf('/fact-finder/proxy/%s', $uri); |
| 63 | + $this->client->request(Request::METHOD_GET, $uri)->willReturn($response); |
| 64 | + $jsonResponse = file_get_contents(dirname(__DIR__, 2) . '/data/proxy/search-bag.json'); |
| 65 | + $responseData = json_decode($jsonResponse, true); |
| 66 | + $stream->__toString()->willReturn($jsonResponse); |
| 67 | + $response->getBody()->willReturn($stream); |
| 68 | + $event->getData()->willReturn($responseData); |
| 69 | + $eventDispatcher->dispatch(Argument::type(EnrichProxyDataEvent::class))->willReturn($event); |
| 70 | + |
| 71 | + // When |
| 72 | + $response = $this->execute('rest/v5/search/example_channel', $request, $this->clientBuilder, $eventDispatcher); |
| 73 | + |
| 74 | + // Then |
| 75 | + $response->shouldBeAnInstanceOf(JsonResponse::class); |
| 76 | + Assert::assertEquals($responseData, json_decode($response->getWrappedObject()->getContent(), true)); |
| 77 | + } |
| 78 | + |
| 79 | + public function it_should_return_error_response( |
| 80 | + Request $request, |
| 81 | + EventDispatcherInterface $eventDispatcher, |
| 82 | + BeforeProxyErrorResponseEvent $event, |
| 83 | + RequestInterface $requestInterface |
| 84 | + ) { |
| 85 | + // Expect & Given |
| 86 | + $request->getMethod()->willReturn(Request::METHOD_GET); |
| 87 | + $uri = 'rest/v5/search/example_channel?query=bag&sid=123&format=json'; |
| 88 | + $_SERVER['REQUEST_URI'] = sprintf('/fact-finder/proxy/%s', $uri); |
| 89 | + $this->client->request(Request::METHOD_GET, $uri)->willThrow(new ConnectException('Unable to connect with server.', $requestInterface->getWrappedObject())); |
| 90 | + $eventDispatcher->dispatch(Argument::type(BeforeProxyErrorResponseEvent::class))->willReturn($event); |
| 91 | + |
| 92 | + // When |
| 93 | + $response = $this->execute('rest/v5/search/example_channel', $request, $this->clientBuilder, $eventDispatcher); |
| 94 | + |
| 95 | + // Then |
| 96 | + $response->shouldBeAnInstanceOf(JsonResponse::class); |
| 97 | + Assert::assertEquals( |
| 98 | + ['message' => 'Unable to connect with server.'], |
| 99 | + json_decode($response->getWrappedObject()->getContent(), true) |
| 100 | + ); |
| 101 | + Assert::assertEquals( |
| 102 | + Response::HTTP_BAD_REQUEST, |
| 103 | + $response->getWrappedObject()->getStatusCode() |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments