Skip to content

Commit 73f8da0

Browse files
committed
Move domainAliases from constructor to setter
1 parent c20a6c6 commit 73f8da0

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1111

1212
### Changed
1313
- The `ResponseBuilder` now requires PSR-17 factories instead of a PHP-HTTP factory, to align it with the `Client`. This is only a breaking change in the rare case where you provide your own factory to the `ResponseBuilder`.
14+
- Removed `domainAliases` from constructor arguments. Please use `setDomainAliases()` on the instance instead.
1415

1516
## [2.3.2] - 2021-08-30
1617

src/ResponseBuilder.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ class ResponseBuilder implements ResponseBuilderInterface
3232
*/
3333
private $fixturesPath;
3434

35-
/**
36-
* @var array
37-
*/
38-
private $domainAliases;
39-
4035
/**
4136
* @var \Psr\Http\Message\ResponseFactoryInterface
4237
*/
@@ -48,49 +43,51 @@ class ResponseBuilder implements ResponseBuilderInterface
4843
private $streamFactory;
4944

5045
/**
51-
* @var bool
46+
* @var array
5247
*/
53-
private $strictMode = false;
48+
private $domainAliases = [];
5449

5550
/**
5651
* @var array
5752
*/
5853
private $ignoredQueryParameters = [];
5954

55+
/**
56+
* @var bool
57+
*/
58+
private $strictMode = false;
59+
6060
/**
6161
* @param string $fixturesPath
62-
* @param array $domainAliases
6362
* @param \Psr\Http\Message\ResponseFactoryInterface|null $responseFactory
6463
* @param \Psr\Http\Message\StreamFactoryInterface|null $streamFactory
6564
*/
6665
public function __construct(
6766
string $fixturesPath,
68-
array $domainAliases = [],
6967
ResponseFactoryInterface $responseFactory = null,
7068
StreamFactoryInterface $streamFactory = null
7169
) {
7270
$this->fixturesPath = $fixturesPath;
73-
$this->domainAliases = $domainAliases;
7471
$this->responseFactory = $responseFactory ?: Psr17FactoryDiscovery::findResponseFactory();
7572
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
7673
}
7774

7875
/**
79-
* @return bool
76+
* @return array
8077
*/
81-
public function useStrictMode(): bool
78+
public function getDomainAliases(): array
8279
{
83-
return $this->strictMode;
80+
return $this->domainAliases;
8481
}
8582

8683
/**
87-
* @param bool $strictMode
84+
* @param array $domainAliases
8885
*
89-
* @return self
86+
* @return $this
9087
*/
91-
public function setStrictMode(bool $strictMode): self
88+
public function setDomainAliases(array $domainAliases): self
9289
{
93-
$this->strictMode = $strictMode;
90+
$this->domainAliases = $domainAliases;
9491

9592
return $this;
9693
}
@@ -106,7 +103,7 @@ public function getIgnoredQueryParameters(): array
106103
/**
107104
* @param array $ignoredQueryParameters
108105
*
109-
* @return self
106+
* @return $this
110107
*/
111108
public function setIgnoredQueryParameters(array $ignoredQueryParameters): self
112109
{
@@ -115,6 +112,26 @@ public function setIgnoredQueryParameters(array $ignoredQueryParameters): self
115112
return $this;
116113
}
117114

115+
/**
116+
* @return bool
117+
*/
118+
public function useStrictMode(): bool
119+
{
120+
return $this->strictMode;
121+
}
122+
123+
/**
124+
* @param bool $strictMode
125+
*
126+
* @return $this
127+
*/
128+
public function setStrictMode(bool $strictMode): self
129+
{
130+
$this->strictMode = $strictMode;
131+
132+
return $this;
133+
}
134+
118135
/**
119136
* @param \Psr\Http\Message\RequestInterface $request
120137
*
@@ -261,8 +278,9 @@ protected function getHostFromRequest(RequestInterface $request): string
261278
{
262279
$host = trim($request->getUri()->getHost(), '/');
263280

264-
if (array_key_exists($host, $this->domainAliases)) {
265-
return $this->domainAliases[$host];
281+
$domainAliases = $this->getDomainAliases();
282+
if (array_key_exists($host, $domainAliases)) {
283+
return $domainAliases[$host];
266284
}
267285

268286
return $host;

tests/ResponseBuilderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,13 @@ public function itCanBuildAResponseUsingDomainAliases(): void
117117
// arrange
118118
$requestFactory = Psr17FactoryDiscovery::findRequestFactory();
119119
$responseFactory = Psr17FactoryDiscovery::findResponseFactory();
120+
$builder = $this->getBuilder()->setDomainAliases(['foo.bar' => 'example.com']);
120121

121122
$expectedResponse = $responseFactory->createResponse()
122123
->withBody(Utils::streamFor(file_get_contents($this->getFixturesPath().'/example.com/api/articles.mock')));
123124

124125
// act
125-
$actualResponse = $this->getBuilder()->build($requestFactory->createRequest('GET', 'https://foo.bar/api/articles'));
126+
$actualResponse = $builder->build($requestFactory->createRequest('GET', 'https://foo.bar/api/articles'));
126127

127128
// assert
128129
$this->assertEquals($expectedResponse->getBody()->__toString(), $actualResponse->getBody()->__toString());
@@ -190,7 +191,7 @@ public function itCanBuildAResponseWithCustomStatus(): void
190191
*/
191192
protected function getBuilder(): ResponseBuilderInterface
192193
{
193-
return new ResponseBuilder($this->getFixturesPath(), ['foo.bar' => 'example.com']);
194+
return new ResponseBuilder($this->getFixturesPath());
194195
}
195196

196197
/**

0 commit comments

Comments
 (0)