Skip to content

Commit

Permalink
Remove the usage of mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Jun 9, 2024
1 parent 0aa9644 commit 5c306f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
13 changes: 7 additions & 6 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());
}

Expand Down
24 changes: 12 additions & 12 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5c306f3

Please sign in to comment.