Skip to content

Commit ec25af4

Browse files
committed
Update tests and introduce client factory for better unit testing
1 parent 4253473 commit ec25af4

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace FeedIo\Adapter;
4+
5+
use FeedIo\Adapter\ClientInterface as AdapterClientInterface;
6+
use Psr\Http\Client\ClientInterface;
7+
8+
class ClientFactory
9+
{
10+
public function create(ClientInterface $client): AdapterClientInterface
11+
{
12+
return new Client($client);
13+
}
14+
}

src/FeedIo/FeedIo.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace FeedIo;
66

77
use DateTime;
8-
use FeedIo\Adapter\Client;
8+
use FeedIo\Adapter\ClientInterface as AdapterClientInterface;
9+
use FeedIo\Adapter\ClientFactory;
910
use FeedIo\Http\ResponseBuilder;
1011
use FeedIo\Reader\Result;
1112
use FeedIo\Rule\DateTimeBuilderInterface;
@@ -18,7 +19,7 @@
1819
* This class acts as a facade. It provides methods to access feed-io main features
1920
*
2021
* <code>
21-
* // $client is a \FeedIo\Adapter\ClientInterface instance, $logger a \Psr\Log\LoggerInterface
22+
* // $client is a \Psr\Http\Client\ClientInterface, $logger a \Psr\Log\LoggerInterface
2223
* $feedIo = new FeedIo($client, $logger);
2324
*
2425
* // read a feed. Output is a Result instance
@@ -64,17 +65,19 @@
6465
*/
6566
class FeedIo
6667
{
68+
protected AdapterClientInterface $client;
6769
protected Reader $reader;
6870

6971
public function __construct(
7072
?ClientInterface $client = null,
7173
protected LoggerInterface $logger = new NullLogger(),
7274
protected ?SpecificationInterface $specification = null,
75+
ClientFactory $factory = new ClientFactory(),
7376
) {
7477
if (is_null($client)) {
7578
throw new \Exception('You must provide a PSR18-compliant HTTP client');
7679
}
77-
$this->client = new Client($client);
80+
$this->client = $factory->create($client);
7881
if (is_null($this->specification)) {
7982
$this->specification = new Specification($this->logger);
8083
}

tests/FeedIo/FeedIoTest.php

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace FeedIo;
44

5-
use FeedIo\Rule\DateTimeBuilder;
6-
use FeedIo\Standard\Atom;
5+
use FeedIo\Adapter\ClientFactory;
6+
use Nyholm\Psr7\Response;
7+
use Nyholm\Psr7\Stream;
8+
use PHPUnit\Framework\TestCase;
9+
use Psr\Http\Client\ClientInterface;
10+
use Psr\Http\Message\StreamInterface;
711

812
/**
913
* Generated by PHPUnit_SkeletonGenerator on 2015-02-23 at 20:31:12.
1014
*/
11-
use PHPUnit\Framework\TestCase;
12-
1315
class FeedIoTest extends TestCase
1416
{
1517
/**
@@ -23,18 +25,31 @@ class FeedIoTest extends TestCase
2325
*/
2426
protected function setUp(): void
2527
{
26-
$client = $this->getMockForAbstractClass('\FeedIo\Adapter\ClientInterface');
27-
$response = $this->createMock('FeedIo\Adapter\ResponseInterface');
28-
$response->expects($this->any())->method('isModified')->will($this->returnValue(true));
29-
$response->expects($this->any())->method('getBody')->will($this->returnValue(
30-
file_get_contents(dirname(__FILE__)."/../samples/expected-atom.xml")
31-
));
32-
$response->expects($this->any())->method('getLastModified')->will($this->returnValue(new \DateTime()));
33-
$client->expects($this->any())->method('getResponse')->will($this->returnValue($response));
28+
$html = file_get_contents(__DIR__ ."/../samples/expected-atom.xml");
29+
30+
$adapterClient = $this->getMockForAbstractClass('\FeedIo\Adapter\ClientInterface');
31+
$clientFactory = $this->createMock(ClientFactory::class);
32+
$clientFactory->expects($this->once())->method('create')->willReturn($adapterClient);
33+
34+
$stream = $this->createMock(Stream::class);
35+
$stream->expects($this->any())->method('getContents')->willReturn($html);
36+
37+
$psrResponse = $this->createMock(Response::class);
38+
$psrResponse->expects($this->any())->method('getBody')->willReturn($stream);
39+
$psrResponse->expects($this->any())->method('getStatusCode')->willReturn(200);
40+
41+
$client = $this->createMock(ClientInterface::class);
42+
$client->method('sendRequest')->willReturn($psrResponse);
43+
44+
$adapterResponse = $this->createMock('FeedIo\Adapter\ResponseInterface');
45+
$adapterResponse->expects($this->any())->method('isModified')->willReturn(true);
46+
$adapterResponse->expects($this->any())->method('getBody')->willReturn($html);
47+
$adapterResponse->expects($this->any())->method('getLastModified')->willReturn(new \DateTime());
48+
$adapterClient->expects($this->any())->method('getResponse')->willReturn($adapterResponse);
3449

3550
$logger = new \Psr\Log\NullLogger();
3651

37-
$this->object = new FeedIo($client, $logger, new Specification($logger));
52+
$this->object = new FeedIo($client, $logger, new Specification($logger), $clientFactory);
3853
}
3954

4055
/**
@@ -43,20 +58,34 @@ protected function setUp(): void
4358
*/
4459
public function testConstruct()
4560
{
46-
$client = $this->getMockForAbstractClass('\FeedIo\Adapter\ClientInterface');
61+
$client = $this->getMockForAbstractClass(ClientInterface::class);
4762
$feedIo = new FeedIo($client, new \Psr\Log\NullLogger());
4863
$this->assertInstanceOf('\FeedIo\Reader', $feedIo->getReader());
4964
}
5065

5166
public function testDiscovery()
5267
{
53-
$html = file_get_contents(dirname(__FILE__)."/../samples/discovery.html");
54-
$client = $this->createMock('FeedIo\Adapter\ClientInterface');
55-
$response = $this->createMock('FeedIo\Adapter\ResponseInterface');
56-
$response->expects($this->any())->method('getBody')->will($this->returnValue($html));
57-
$client->expects($this->any())->method('getResponse')->will($this->returnValue($response));
68+
$html = file_get_contents(__DIR__ ."/../samples/discovery.html");
5869

59-
$feedIo = new FeedIo($client, new \Psr\Log\NullLogger());
70+
$adapterClient = $this->getMockForAbstractClass('\FeedIo\Adapter\ClientInterface');
71+
$clientFactory = $this->createMock(ClientFactory::class);
72+
$clientFactory->expects($this->once())->method('create')->willReturn($adapterClient);
73+
74+
$stream = $this->createMock(Stream::class);
75+
$stream->expects($this->any())->method('getContents')->willReturn($html);
76+
77+
$psrResponse = $this->createMock(Response::class);
78+
$psrResponse->method('getBody')->willReturn($stream);
79+
$psrResponse->method('getStatusCode')->willReturn(200);
80+
81+
$client = $this->createMock(ClientInterface::class);
82+
$client->method('sendRequest')->willReturn($psrResponse);
83+
84+
$adapterResponse = $this->createMock('FeedIo\Adapter\ResponseInterface');
85+
$adapterResponse->expects($this->any())->method('getBody')->willReturn($html);
86+
$adapterClient->expects($this->any())->method('getResponse')->willReturn($adapterResponse);
87+
88+
$feedIo = new FeedIo($client, new \Psr\Log\NullLogger(), null, $clientFactory);
6089
$urls = $feedIo->discover('https://example.org/feed');
6190

6291
$this->assertCount(2, $urls);

0 commit comments

Comments
 (0)