Skip to content

Commit 3455cc5

Browse files
committed
Add strict mode to ResponseBuilder
1 parent ba9ed53 commit 3455cc5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/ResponseBuilder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class ResponseBuilder implements ResponseBuilderInterface
4040
*/
4141
private $responseFactory;
4242

43+
/**
44+
* @var bool
45+
*/
46+
private $strictMode = false;
47+
4348
/**
4449
* @param string $fixturesPath
4550
* @param array $domainAliases
@@ -52,6 +57,26 @@ public function __construct(string $fixturesPath, array $domainAliases = [], Res
5257
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
5358
}
5459

60+
/**
61+
* @return bool
62+
*/
63+
public function useStrictMode(): bool
64+
{
65+
return $this->strictMode;
66+
}
67+
68+
/**
69+
* @param bool $strictMode
70+
*
71+
* @return self
72+
*/
73+
public function setStrictMode(bool $strictMode): self
74+
{
75+
$this->strictMode = $strictMode;
76+
77+
return $this;
78+
}
79+
5580
/**
5681
* @param \Psr\Http\Message\RequestInterface $request
5782
*
@@ -184,6 +209,10 @@ protected function getPossibleMockFilePathsForRequest(RequestInterface $request,
184209
$possibleFiles[] = implode('.', [$basePathToFile, $method, $type]);
185210
$possibleFiles[] = implode('.', [$basePathToFile, $type]);
186211

212+
if ($this->useStrictMode()) {
213+
$possibleFiles = array_slice($possibleFiles, 0, 1);
214+
}
215+
187216
return $possibleFiles;
188217
}
189218

tests/ResponseBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ public function getResponses(): array
6262
];
6363
}
6464

65+
/**
66+
* @test
67+
*/
68+
public function it_can_be_set_to_strict_mode()
69+
{
70+
$builder = $this->getBuilder();
71+
72+
// Strict mode off
73+
$this->assertFalse($builder->useStrictMode());
74+
75+
$messageFactory = MessageFactoryDiscovery::find();
76+
$builder->build($messageFactory->createRequest('POST', 'http://example.com/api/articles?foo=bar'));
77+
78+
// Strict mode on
79+
$builder->setStrictMode(true);
80+
$this->assertTrue($builder->useStrictMode());
81+
82+
$this->expectException(MockNotFoundException::class);
83+
84+
$messageFactory = MessageFactoryDiscovery::find();
85+
$builder->build($messageFactory->createRequest('POST', 'http://example.com/api/articles?foo=bar'));
86+
}
87+
6588
/**
6689
* @test
6790
*/

0 commit comments

Comments
 (0)