|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace spec\Setono\PostNord\Client; |
| 6 | + |
| 7 | +use PhpSpec\ObjectBehavior; |
| 8 | +use Prophecy\Argument; |
| 9 | +use Psr\Http\Client\ClientInterface as HttpClientInterface; |
| 10 | +use Psr\Http\Message\RequestFactoryInterface; |
| 11 | +use Psr\Http\Message\RequestInterface; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use Psr\Http\Message\StreamFactoryInterface; |
| 14 | +use Psr\Http\Message\StreamInterface; |
| 15 | +use Setono\PostNord\Client\Client; |
| 16 | +use Setono\PostNord\Client\ClientInterface; |
| 17 | + |
| 18 | +class ClientSpec extends ObjectBehavior |
| 19 | +{ |
| 20 | + private const API_KEY = 'api_key'; |
| 21 | + private const BASE_URL = 'https://api2.postnord.com'; |
| 22 | + |
| 23 | + public function let( |
| 24 | + HttpClientInterface $httpClient, |
| 25 | + RequestFactoryInterface $requestFactory, |
| 26 | + StreamFactoryInterface $streamFactory |
| 27 | + ): void { |
| 28 | + $this->beConstructedWith($httpClient, $requestFactory, $streamFactory, self::API_KEY); |
| 29 | + } |
| 30 | + |
| 31 | + public function it_is_initializable(): void |
| 32 | + { |
| 33 | + $this->shouldHaveType(Client::class); |
| 34 | + } |
| 35 | + |
| 36 | + public function it_implements_client_interface(): void |
| 37 | + { |
| 38 | + $this->shouldImplement(ClientInterface::class); |
| 39 | + } |
| 40 | + |
| 41 | + public function it_gets( |
| 42 | + HttpClientInterface $httpClient, |
| 43 | + RequestFactoryInterface $requestFactory, |
| 44 | + ResponseInterface $response, |
| 45 | + StreamInterface $stream |
| 46 | + ): void { |
| 47 | + $requestFactory |
| 48 | + ->createRequest('GET', self::BASE_URL.'/endpoint.json?apikey='.self::API_KEY.'¶m1=value1¶m2=value2') |
| 49 | + ->shouldBeCalled(); |
| 50 | + |
| 51 | + $response->getStatusCode()->willReturn(200); |
| 52 | + $response->getBody()->willReturn($stream); |
| 53 | + $stream->__toString()->willReturn('{"items":[1,2,3]}'); |
| 54 | + $httpClient->sendRequest(Argument::any())->willReturn($response); |
| 55 | + |
| 56 | + $this->get('endpoint.json', [ |
| 57 | + 'param1' => 'value1', |
| 58 | + 'param2' => 'value2', |
| 59 | + ])->shouldReturn([ |
| 60 | + 'items' => [1, 2, 3], |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + public function it_posts( |
| 65 | + HttpClientInterface $httpClient, |
| 66 | + RequestFactoryInterface $requestFactory, |
| 67 | + StreamFactoryInterface $streamFactory, |
| 68 | + RequestInterface $request, |
| 69 | + ResponseInterface $response, |
| 70 | + StreamInterface $stream |
| 71 | + ): void { |
| 72 | + $request->withBody(Argument::any())->willReturn($request); |
| 73 | + |
| 74 | + $requestFactory |
| 75 | + ->createRequest('POST', self::BASE_URL.'/endpoint.json?apikey='.self::API_KEY.'¶m1=value1¶m2=value2') |
| 76 | + ->willReturn($request); |
| 77 | + |
| 78 | + $streamFactory |
| 79 | + ->createStream('{"post_param1":"post_value1","post_param2":"post_value2"}') |
| 80 | + ->shouldBeCalled(); |
| 81 | + |
| 82 | + $response->getStatusCode()->willReturn(200); |
| 83 | + $response->getBody()->willReturn($stream); |
| 84 | + $stream->__toString()->willReturn('{"items":[1,2,3]}'); |
| 85 | + $httpClient->sendRequest($request)->willReturn($response); |
| 86 | + |
| 87 | + $this->post('endpoint.json', [ |
| 88 | + 'param1' => 'value1', |
| 89 | + 'param2' => 'value2', |
| 90 | + ], [ |
| 91 | + 'post_param1' => 'post_value1', |
| 92 | + 'post_param2' => 'post_value2', |
| 93 | + ])->shouldReturn([ |
| 94 | + 'items' => [1, 2, 3], |
| 95 | + ]); |
| 96 | + } |
| 97 | +} |
0 commit comments