-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract examples from Test Doubles chapter
- Loading branch information
1 parent
c987b0d
commit 037d147
Showing
36 changed files
with
648 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AbstractClassTest extends TestCase | ||
{ | ||
public function testConcreteMethod(): void | ||
{ | ||
$stub = $this->getMockForAbstractClass(AbstractClass::class); | ||
|
||
$stub->expects($this->any()) | ||
->method('abstractMethod') | ||
->will($this->returnValue(true)); | ||
|
||
$this->assertTrue($stub->concreteMethod()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
./tools/phpunit tests/AbstractClassTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
. 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
OK (1 test, 1 assertion) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AbstractTraitTest extends TestCase | ||
{ | ||
public function testConcreteMethod(): void | ||
{ | ||
$mock = $this->getMockForTrait(AbstractTrait::class); | ||
|
||
$mock->expects($this->any()) | ||
->method('abstractMethod') | ||
->will($this->returnValue(true)); | ||
|
||
$this->assertTrue($mock->concreteMethod()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
./tools/phpunit tests/AbstractTraitTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
. 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
OK (1 test, 1 assertion) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<definitions name = "HelloService" | ||
targetNamespace = "http://www.examples.com/wsdl/HelloService.wsdl" | ||
xmlns = "http://schemas.xmlsoap.org/wsdl/" | ||
xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/" | ||
xmlns:tns = "http://www.examples.com/wsdl/HelloService.wsdl" | ||
xmlns:xsd = "http://www.w3.org/2001/XMLSchema"> | ||
|
||
<message name = "SayHelloRequest"> | ||
<part name = "firstName" type = "xsd:string"/> | ||
</message> | ||
|
||
<message name = "SayHelloResponse"> | ||
<part name = "greeting" type = "xsd:string"/> | ||
</message> | ||
|
||
<portType name = "Hello_PortType"> | ||
<operation name = "sayHello"> | ||
<input message = "tns:SayHelloRequest"/> | ||
<output message = "tns:SayHelloResponse"/> | ||
</operation> | ||
</portType> | ||
|
||
<binding name = "Hello_Binding" type = "tns:Hello_PortType"> | ||
<soap:binding style = "rpc" | ||
transport = "http://schemas.xmlsoap.org/soap/http"/> | ||
<operation name = "sayHello"> | ||
<soap:operation soapAction = "sayHello"/> | ||
<input> | ||
<soap:body | ||
encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" | ||
namespace = "urn:examples:helloservice" | ||
use = "encoded"/> | ||
</input> | ||
|
||
<output> | ||
<soap:body | ||
encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" | ||
namespace = "urn:examples:helloservice" | ||
use = "encoded"/> | ||
</output> | ||
</operation> | ||
</binding> | ||
|
||
<service name = "Hello_Service"> | ||
<documentation>WSDL File for HelloService</documentation> | ||
<port binding = "tns:Hello_Binding" name = "Hello_Port"> | ||
<soap:address | ||
location = "http://www.examples.com/SayHello/" /> | ||
</port> | ||
</service> | ||
</definitions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MockBuilderExampleTest extends TestCase | ||
{ | ||
public function testStub(): void | ||
{ | ||
// Create a stub for the SomeClass class. | ||
$stub = $this->getMockBuilder(SomeClass::class) | ||
->disableOriginalConstructor() | ||
->disableOriginalClone() | ||
->disableArgumentCloning() | ||
->disallowMockingUnknownTypes() | ||
->getMock(); | ||
|
||
// Configure the stub. | ||
$stub->method('doSomething') | ||
->willReturn('foo'); | ||
|
||
// Calling $stub->doSomething() will now return | ||
// 'foo'. | ||
$this->assertSame('foo', $stub->doSomething()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
./tools/phpunit tests/MockBuilderExampleTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
E 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
There was 1 error: | ||
|
||
1) MockBuilderExampleTest::testStub | ||
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "SomeClass" is declared "final" and cannot be doubled | ||
|
||
/path/to/tests/MockBuilderExampleTest.php:14 | ||
|
||
ERRORS! | ||
Tests: 1, Assertions: 0, Errors: 1. |
20 changes: 20 additions & 0 deletions
20
src/examples/test-doubles/OnConsecutiveCallsExampleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class OnConsecutiveCallsExampleTest extends TestCase | ||
{ | ||
public function testOnConsecutiveCallsStub(): void | ||
{ | ||
// Create a stub for the SomeClass class. | ||
$stub = $this->createStub(SomeClass::class); | ||
|
||
// Configure the stub. | ||
$stub->method('doSomething') | ||
->will($this->onConsecutiveCalls(2, 3, 5, 7)); | ||
|
||
// $stub->doSomething() returns a different value each time | ||
$this->assertSame(2, $stub->doSomething()); | ||
$this->assertSame(3, $stub->doSomething()); | ||
$this->assertSame(5, $stub->doSomething()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/examples/test-doubles/OnConsecutiveCallsExampleTest.php.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
./tools/phpunit tests/OnConsecutiveCallsExampleTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
E 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
There was 1 error: | ||
|
||
1) OnConsecutiveCallsExampleTest::testOnConsecutiveCallsStub | ||
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "SomeClass" is declared "final" and cannot be doubled | ||
|
||
/path/to/tests/OnConsecutiveCallsExampleTest.php:9 | ||
|
||
ERRORS! | ||
Tests: 1, Assertions: 0, Errors: 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ReturnArgumentExampleTest extends TestCase | ||
{ | ||
public function testReturnArgumentStub(): void | ||
{ | ||
// Create a stub for the SomeClass class. | ||
$stub = $this->createStub(SomeClass::class); | ||
|
||
// Configure the stub. | ||
$stub->method('doSomething') | ||
->will($this->returnArgument(0)); | ||
|
||
// $stub->doSomething('foo') returns 'foo' | ||
$this->assertSame('foo', $stub->doSomething('foo')); | ||
|
||
// $stub->doSomething('bar') returns 'bar' | ||
$this->assertSame('bar', $stub->doSomething('bar')); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/examples/test-doubles/ReturnArgumentExampleTest.php.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
./tools/phpunit tests/ReturnArgumentExampleTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
E 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
There was 1 error: | ||
|
||
1) ReturnArgumentExampleTest::testReturnArgumentStub | ||
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "SomeClass" is declared "final" and cannot be doubled | ||
|
||
/path/to/tests/ReturnArgumentExampleTest.php:9 | ||
|
||
ERRORS! | ||
Tests: 1, Assertions: 0, Errors: 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ReturnCallbackExampleTest extends TestCase | ||
{ | ||
public function testReturnCallbackStub(): void | ||
{ | ||
// Create a stub for the SomeClass class. | ||
$stub = $this->createStub(SomeClass::class); | ||
|
||
// Configure the stub. | ||
$stub->method('doSomething') | ||
->will($this->returnCallback('str_rot13')); | ||
|
||
// $stub->doSomething($argument) returns str_rot13($argument) | ||
$this->assertSame('fbzrguvat', $stub->doSomething('something')); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/examples/test-doubles/ReturnCallbackExampleTest.php.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
./tools/phpunit tests/ReturnCallbackExampleTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
E 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
There was 1 error: | ||
|
||
1) ReturnCallbackExampleTest::testReturnCallbackStub | ||
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "SomeClass" is declared "final" and cannot be doubled | ||
|
||
/path/to/tests/ReturnCallbackExampleTest.php:9 | ||
|
||
ERRORS! | ||
Tests: 1, Assertions: 0, Errors: 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ReturnSelfExampleTest extends TestCase | ||
{ | ||
public function testReturnSelf(): void | ||
{ | ||
// Create a stub for the SomeClass class. | ||
$stub = $this->createStub(SomeClass::class); | ||
|
||
// Configure the stub. | ||
$stub->method('doSomething') | ||
->will($this->returnSelf()); | ||
|
||
// $stub->doSomething() returns $stub | ||
$this->assertSame($stub, $stub->doSomething()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
./tools/phpunit tests/ReturnSelfExampleTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
E 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
There was 1 error: | ||
|
||
1) ReturnSelfExampleTest::testReturnSelf | ||
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "SomeClass" is declared "final" and cannot be doubled | ||
|
||
/path/to/tests/ReturnSelfExampleTest.php:9 | ||
|
||
ERRORS! | ||
Tests: 1, Assertions: 0, Errors: 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ReturnValueMapExampleTest extends TestCase | ||
{ | ||
public function testReturnValueMapStub(): void | ||
{ | ||
// Create a stub for the SomeClass class. | ||
$stub = $this->createStub(SomeClass::class); | ||
|
||
// Create a map of arguments to return values. | ||
$map = [ | ||
['a', 'b', 'c', 'd'], | ||
['e', 'f', 'g', 'h'], | ||
]; | ||
|
||
// Configure the stub. | ||
$stub->method('doSomething') | ||
->will($this->returnValueMap($map)); | ||
|
||
// $stub->doSomething() returns different values depending on | ||
// the provided arguments. | ||
$this->assertSame('d', $stub->doSomething('a', 'b', 'c')); | ||
$this->assertSame('h', $stub->doSomething('e', 'f', 'g')); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/examples/test-doubles/ReturnValueMapExampleTest.php.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
./tools/phpunit tests/ReturnValueMapExampleTest.php | ||
PHPUnit 10.0.11 by Sebastian Bergmann and contributors. | ||
|
||
Runtime: PHP 8.2.3 | ||
|
||
E 1 / 1 (100%) | ||
|
||
Time: 00:00.001, Memory: 14.29 MB | ||
|
||
There was 1 error: | ||
|
||
1) ReturnValueMapExampleTest::testReturnValueMapStub | ||
PHPUnit\Framework\MockObject\ClassIsFinalException: Class "SomeClass" is declared "final" and cannot be doubled | ||
|
||
/path/to/tests/ReturnValueMapExampleTest.php:9 | ||
|
||
ERRORS! | ||
Tests: 1, Assertions: 0, Errors: 1. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php declare(strict_types=1); | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeClassTest extends TestCase | ||
{ | ||
public function testDoesSomething(): void | ||
{ | ||
$sut = new SomeClass; | ||
|
||
// Create a test stub for the Dependency interface | ||
$dependency = $this->createStub(Dependency::class); | ||
|
||
// Configure the test stub | ||
$dependency->method('doSomething') | ||
->willReturn('foo'); | ||
|
||
$result = $sut->doSomething($dependency); | ||
|
||
$this->assertStringEndsWith('foo', $result); | ||
} | ||
} |
Oops, something went wrong.