From 5c306f364613df2f5b68b49007902721f5b1493f Mon Sep 17 00:00:00 2001 From: William Desportes Date: Sun, 9 Jun 2024 13:14:31 +0200 Subject: [PATCH] Remove the usage of mocks --- tests/ResponseTest.php | 13 +++++++------ tests/UriTest.php | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 7bd3206..0141888 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -22,6 +22,12 @@ use function fopen; use function property_exists; +final class TestException implements \Stringable { + public function __toString(): string { + return 'Slim OK'; + } +} + class ResponseTest extends TestCase { public function testConstructorWithDefaultArgs() @@ -141,13 +147,8 @@ public function testReasonPhraseContainsLineFeed() public function testWithStatusValidReasonPhraseObject() { - $mock = $this->getMockBuilder(stdClass::class)->addMethods(['__toString'])->getMock(); - $mock->expects($this->once()) - ->method('__toString') - ->will($this->returnValue('Slim OK')); - $response = new Response(); - $response = $response->withStatus(200, $mock); + $response = $response->withStatus(200, new TestException()); $this->assertEquals('Slim OK', $response->getReasonPhrase()); } diff --git a/tests/UriTest.php b/tests/UriTest.php index 92077ec..97c1347 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -15,6 +15,15 @@ use Slim\Psr7\Uri; use stdClass; +final class TestObject implements \Stringable { + + public function __construct(private readonly string $value) {} + + public function __toString(): string { + return $this->value; + } +} + class UriTest extends TestCase { public function uriFactory(): Uri @@ -133,10 +142,7 @@ public function testWithHost() public function testWithHostValidObject() { - $mock = $this->getMockBuilder(stdClass::class)->addMethods(['__toString'])->getMock(); - $mock->expects($this->once()) - ->method('__toString') - ->will($this->returnValue('host.test')); + $mock = new TestObject('host.test'); $uri = $this->uriFactory()->withHost($mock); $this->assertEquals('host.test', $uri->getHost()); @@ -298,10 +304,7 @@ public function testWithQueryEmpty() public function testWithQueryValidObject() { - $mock = $this->getMockBuilder(stdClass::class)->addMethods(['__toString'])->getMock(); - $mock->expects($this->once()) - ->method('__toString') - ->will($this->returnValue('xyz=123')); + $mock = new TestObject('xyz=123'); $uri = $this->uriFactory()->withQuery($mock); $this->assertEquals('xyz=123', $uri->getQuery()); @@ -350,10 +353,7 @@ public function testWithFragmentEmpty() public function testWithFragmentValidObject() { - $mock = $this->getMockBuilder(stdClass::class)->addMethods(['__toString'])->getMock(); - $mock->expects($this->once()) - ->method('__toString') - ->will($this->returnValue('other-fragment')); + $mock = new TestObject('other-fragment'); $uri = $this->uriFactory()->withFragment($mock); $this->assertEquals('other-fragment', $uri->getFragment());