Skip to content

Commit a1d8452

Browse files
committed
Implement a PSR-18 compliant client stub
1 parent 5307986 commit a1d8452

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

spec/HttpClientDiscoverySpec.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Puli\Repository\Api\ResourceRepository;
1313
use PhpSpec\ObjectBehavior;
1414
use spec\Http\Discovery\Helper\DiscoveryHelper;
15+
use Http\Discovery\HttpClientDiscovery;
16+
use spec\Http\Discovery\Stub\HttpClientStub;
17+
use spec\Http\Discovery\Stub\PSR18ClientStub;
1518

1619
class HttpClientDiscoverySpec extends ObjectBehavior
1720
{
@@ -23,23 +26,34 @@ function let()
2326

2427
function it_is_initializable()
2528
{
26-
$this->shouldHaveType('Http\Discovery\HttpClientDiscovery');
29+
$this->shouldHaveType(HttpClientDiscovery::class);
2730
}
2831

2932
function it_is_a_class_discovery()
3033
{
31-
$this->shouldHaveType('Http\Discovery\ClassDiscovery');
34+
$this->shouldHaveType(ClassDiscovery::class);
3235
}
3336

34-
function it_finds_a_http_client(DiscoveryStrategy $strategy) {
37+
function it_finds_a_http_client(DiscoveryStrategy $strategy)
38+
{
39+
$candidate = ['class' => HttpClientStub::class, 'condition' => true];
40+
if ($this->psr18IsInUse()) {
41+
$candidate['class'] = PSR18ClientStub::class;
42+
}
3543

36-
$candidate = ['class' => 'spec\Http\Discovery\Stub\HttpClientStub', 'condition' => true];
3744
DiscoveryHelper::setClasses(HttpClient::class, [$candidate]);
3845

39-
$this->find()->shouldImplement('Http\Client\HttpClient');
46+
$this->find()->shouldImplement(HttpClient::class);
4047
}
4148

4249
function it_throw_exception(DiscoveryStrategy $strategy) {
4350
$this->shouldThrow(NotFoundException::class)->duringFind();
4451
}
52+
53+
private function psr18IsInUse()
54+
{
55+
$reflection = new \ReflectionMethod(HttpClient::class, 'sendRequest');
56+
57+
return $reflection->hasReturnType();
58+
}
4559
}

spec/Stub/PSR18ClientStub.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Http\Discovery\Stub;
4+
5+
use Http\Client\HttpClient;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
class PSR18ClientStub implements HttpClient
10+
{
11+
public function sendRequest(RequestInterface $request): ResponseInterface
12+
{
13+
}
14+
}

0 commit comments

Comments
 (0)