Skip to content

Commit 4272f98

Browse files
committed
added client test
1 parent dd3f974 commit 4272f98

File tree

4 files changed

+109
-3
lines changed

4 files changed

+109
-3
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/features export-ignore
33
/spec export-ignore
44
/tests export-ignore
5+
/.editorconfig export-ignore
56
/.gitattributes export-ignore
67
/.gitignore export-ignore
78
/.scrutinizer.yml export-ignore

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"kriswallsmith/buzz": "^1.0",
2121
"localheinz/composer-normalize": "^1.1",
2222
"nyholm/psr7": "^1.1",
23+
"phpspec/phpspec": "^5.1",
2324
"phpstan/phpstan": "^0.10.3",
2425
"phpunit/phpunit": "^8.0",
2526
"symplify/easy-coding-standard": "^5.1",
@@ -41,8 +42,11 @@
4142
"prefer-stable": true,
4243
"scripts": {
4344
"analyse": "./vendor/bin/phpstan analyse -c phpstan.neon -l max src",
44-
"check-style": "./vendor/bin/ecs check --ansi src/ tests/",
45-
"fix-style": "./vendor/bin/ecs check --fix --ansi src/ tests/",
46-
"test": "./vendor/bin/phpunit"
45+
"check-style": "./vendor/bin/ecs check --ansi src/ tests/ spec/",
46+
"fix-style": "./vendor/bin/ecs check --fix --ansi src/ tests/ spec/",
47+
"test": [
48+
"./vendor/bin/phpspec run",
49+
"./vendor/bin/phpunit"
50+
]
4751
}
4852
}

phpspec.yml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
suites:
2+
main:
3+
namespace: Setono\PostNord
4+
psr4_prefix: Setono\PostNord

spec/Client/ClientSpec.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.'&param1=value1&param2=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.'&param1=value1&param2=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

Comments
 (0)