Skip to content

Commit 91804b8

Browse files
odanodan
authored andcommitted
Add mocked handler for testing
1 parent 26ba66d commit 91804b8

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/Factory/NovaHttpClientFactory.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace OrcaServices\NovaApi\Factory;
44

5+
use GuzzleHttp\Handler\MockHandler;
6+
use GuzzleHttp\HandlerStack;
57
use InvalidArgumentException;
68
use OrcaServices\NovaApi\Client\NovaHttpClient;
79
use OrcaServices\NovaApi\Configuration\NovaApiConfiguration;
810
use OrcaServices\NovaApi\Method\NovaLoginMethod;
911
use OrcaServices\NovaApi\Parser\NovaApiErrorParser;
12+
use Psr\Http\Message\ResponseInterface;
1013

1114
/**
1215
* Factory.
@@ -23,6 +26,16 @@ final class NovaHttpClientFactory
2326
*/
2427
private $novaErrorParser;
2528

29+
/**
30+
* @var array
31+
*/
32+
private $mockedResponses = [];
33+
34+
/**
35+
* @var HandlerStack|null
36+
*/
37+
private $handler;
38+
2639
/**
2740
* The constructor.
2841
*
@@ -79,10 +92,25 @@ public function createLoggedInHttpClient(): NovaHttpClient
7992
*/
8093
private function createHttpClient(array $settings): NovaHttpClient
8194
{
82-
if (empty($settings['base_uri'])) {
83-
throw new InvalidArgumentException('The NOVA API base URI is not defined');
95+
if ($this->mockedResponses || empty($settings['base_uri']) || $settings['base_uri'] === 'http://localhost/') {
96+
// Use the same mocked handler stack for login and the endpoint client for testing
97+
$this->handler = $this->handler ?: HandlerStack::create(new MockHandler($this->mockedResponses));
98+
$settings['base_uri'] = 'http://localhost';
99+
$settings['handler'] = $this->handler;
84100
}
85101

86102
return new NovaHttpClient($settings);
87103
}
104+
105+
/**
106+
* Set mocked responses.
107+
*
108+
* @param ResponseInterface[] $responses The responses
109+
*
110+
* @return void
111+
*/
112+
public function setMockedResponses(array $responses)
113+
{
114+
$this->mockedResponses = $responses;
115+
}
88116
}

tests/Traits/NovaClientTestTrait.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace OrcaServices\NovaApi\Test\Traits;
44

55
use Cake\Chronos\Chronos;
6-
use GuzzleHttp\Handler\MockHandler;
7-
use GuzzleHttp\HandlerStack;
86
use GuzzleHttp\Psr7\Response;
97
use OrcaServices\NovaApi\Client\NovaApiClient;
108
use OrcaServices\NovaApi\Configuration\NovaApiConfiguration;
9+
use OrcaServices\NovaApi\Factory\NovaHttpClientFactory;
1110
use OrcaServices\NovaApi\Parameter\NovaIdentifierParameter;
1211

1312
/**
@@ -27,24 +26,22 @@ protected function createNovaApiClient(array $responses): NovaApiClient
2726
Chronos::setTestNow('2019-09-01 00:00:00');
2827

2928
$settings = $this->getSettings();
29+
$this->getContainer()->set(NovaApiConfiguration::class, new NovaApiConfiguration($settings));
3030

3131
// To make real http calls, just comment out this line
32-
$settings = $this->mockNovaGuzzleClient($settings, $responses);
33-
34-
$this->getContainer()->set(NovaApiConfiguration::class, new NovaApiConfiguration($settings));
32+
$this->mockNovaGuzzleClient($responses);
3533

3634
return $this->getContainer()->get(NovaApiClient::class);
3735
}
3836

3937
/**
4038
* Mock NOVA Guzzle client and single sign on (SSO).
4139
*
42-
* @param array $settings The nova api settings
4340
* @param array $responses The mocked responses
4441
*
45-
* @return array
42+
* @return void
4643
*/
47-
protected function mockNovaGuzzleClient(array $settings, array $responses): array
44+
protected function mockNovaGuzzleClient(array $responses)
4845
{
4946
// Append the login as first response
5047
$loginResponse = new Response();
@@ -54,9 +51,7 @@ protected function mockNovaGuzzleClient(array $settings, array $responses): arra
5451

5552
array_unshift($responses, $loginResponse);
5653

57-
$settings['default']['handler'] = HandlerStack::create(new MockHandler($responses));
58-
59-
return $settings;
54+
$this->getContainer()->get(NovaHttpClientFactory::class)->setMockedResponses($responses);
6055
}
6156

6257
/**

0 commit comments

Comments
 (0)