Completed
Push — master ( 70ce3c...d384f9 )
by Martin
07:59
created

AlphaVantageServiceTest::testGetWrongStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cHeeSaW\AlphaVantageBundle\Tests\Service;
6
7
use cHeeSaW\AlphaVantageBundle\Endpoints\Cryptocurrency;
8
use cHeeSaW\AlphaVantageBundle\Service\AlphaVantageService;
9
use Exception;
10
use GuzzleHttp\ClientInterface;
11
use GuzzleHttp\Exception\TransferException;
12
use GuzzleHttp\Psr7\Uri;
13
use PHPUnit\Framework\TestCase;
14
use Prophecy\Argument;
15
use Prophecy\Prophecy\ObjectProphecy;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\StreamInterface;
18
19
class AlphaVantageServiceTest extends TestCase
20
{
21
    /** @var ClientInterface|ObjectProphecy */
22
    private $client;
23
24
    /** @var string */
25
    private $alphaVantageApiUrl;
26
27
    /** @var string */
28
    private $alphaVantageApiKey;
29
30
    protected $subject;
31
32
    public function setUp(): void
33
    {
34
        parent::setUp();
35
        $this->client = $this->prophesize(ClientInterface::class);
36
        $this->alphaVantageApiUrl = '';
37
        $this->alphaVantageApiKey = '';
38
39
        $this->subject = new AlphaVantageService(
40
            $this->client->reveal(),
41
            $this->alphaVantageApiUrl,
42
            $this->alphaVantageApiKey
43
        );
44
    }
45
46
    public function testGetSuccess(): void
47
    {
48
        $contents = 'contents';
49
        /** @var ResponseInterface $responseInterface */
50
        $responseInterface = $this->prophesize(ResponseInterface::class);
51
        $responseInterface->getStatusCode()->willReturn(200);
52
53
        /** @var StreamInterface $streamInterface */
54
        $streamInterface = $this->prophesize(StreamInterface::class);
55
        $responseInterface->getBody()->willReturn($streamInterface);
56
57
        $streamInterface->getContents()->willReturn($contents);
58
        $this->client->request('GET', Argument::type(Uri::class))->willReturn($responseInterface);
59
60
        $endpoint = new Cryptocurrency(Cryptocurrency::CRYPTO_CURRENCY_DAILY);
61
        $response = $this->subject->get($endpoint);
62
63
        self::assertSame($contents, $response);
64
    }
65
66
    public function testGetWrongStatusCode(): void
67
    {
68
        /** @var ResponseInterface $responseInterface */
69
        $responseInterface = $this->prophesize(ResponseInterface::class);
70
        $statusCode = 404;
71
        $responseInterface->getStatusCode()->willReturn($statusCode);
72
73
        $this->client->request('GET', Argument::type(Uri::class))->willReturn($responseInterface);
74
75
        $this->expectException(Exception::class);
76
        $this->expectExceptionMessage('Invalid statuscode returned: ' . $statusCode);
77
78
        $endpoint = new Cryptocurrency(Cryptocurrency::CRYPTO_CURRENCY_DAILY);
79
        $this->subject->get($endpoint);
80
    }
81
82
    public function testGetGuzzleException(): void
83
    {
84
        $this->client->request('GET', Argument::type(Uri::class))->willThrow(TransferException::class);
85
86
        $this->expectException(Exception::class);
87
88
        $endpoint = new Cryptocurrency(Cryptocurrency::CRYPTO_CURRENCY_DAILY);
89
        $this->subject->get($endpoint);
90
    }
91
}
92