Skip to content

Commit

Permalink
Extract examples from Test Doubles chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 23, 2023
1 parent c987b0d commit 037d147
Show file tree
Hide file tree
Showing 36 changed files with 648 additions and 458 deletions.
12 changes: 12 additions & 0 deletions scripts/processed.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@
"assertions\/XmlFileEqualsXmlFileTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fdd871bbdf213a7577d76f7fe09a64d129be693ba78a591d373e989201939e3a8",
"assertions\/XmlStringEqualsXmlFileTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fa1304c8ff8bfbc552431b903d7f6f2e2cb1e5fbd8ad9ca56dd2eb692a1e30b82",
"assertions\/XmlStringEqualsXmlStringTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f1dd2147bce37b257a5c0ffbac15428c0452ff5186594f8e3a7050dac62c3ed0d",
"test-doubles\/AbstractClassTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f4fd5eb08fc99ff72211555a2a600c06e24d2b9d84b7fd8a5f4dafd8226157de4",
"test-doubles\/AbstractTraitTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f14043ded65431612d59342f82730fff8727112c1a961e9e8a41c9c2632ac9c9d",
"test-doubles\/MockBuilderExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fad2918a85c13ff5d2709e63e7bcadc4effced8350fcf67a7d317908d168a2976",
"test-doubles\/OnConsecutiveCallsExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f1de253aa28ad75d1788f9c9f8708237941290d410c597847440e231f311c766f",
"test-doubles\/ReturnArgumentExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fe32f3bf754ab7633419ff0e92974c996ffcb3177bf3af95dc30e6875759c9e6f",
"test-doubles\/ReturnCallbackExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fbee0288e05e4a0286d8f679ce8beb931505e163dc66e4e436152599a0999ddac",
"test-doubles\/ReturnSelfExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f879f246e98feb21bd344c74621323a67760a0e0ed3f33aec1573de5b811dea0b",
"test-doubles\/ReturnValueMapExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3ffeb7a733c93ca8c847fc0a95b85a94163a2a41ecd23dbd17d2f8bdbf146f0de3",
"test-doubles\/SomeClassTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fdf8031c9c1ad4106bfd497553351de434625fe6c7558b8873fb60006690001d9",
"test-doubles\/SubjectTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3ffc116a65ec6d3f66254ba4e004150b146881265dbdaa612103c74f1e0894d7d2",
"test-doubles\/ThrowExceptionExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fae60279c0e073dba3db8eb7750efbf2c22fe8d07c88db695edc4335381f4a1ea",
"test-doubles\/WsdlStubExampleTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f1c6067971ffc807d088a0adeb94d513f2a93bce2bbb44312356c7cada6c3957a",
"writing-tests-for-phpunit\/ArrayDiffTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3f63fa127c307a2782d6f5965b26c9c06a3d33e31ed47225e7a9a72aeea68a674f",
"writing-tests-for-phpunit\/ArrayWeakComparisonTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3fdc95c700178a0d5324872f6ea646a7b53962f7e8b8027e5a550081ea1b9a6783",
"writing-tests-for-phpunit\/DatabaseTest.php": "72d73f66a3eecf66f7121c1873a8307f3953c7717800d1885c306e9ec0a01b3ff0f34e406da5a40b7a46a2d369c440066b67af27665f9390fe831092942b1785",
Expand Down
16 changes: 16 additions & 0 deletions src/examples/test-doubles/AbstractClassTest.php
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());
}
}
10 changes: 10 additions & 0 deletions src/examples/test-doubles/AbstractClassTest.php.out
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)
16 changes: 16 additions & 0 deletions src/examples/test-doubles/AbstractTraitTest.php
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());
}
}
10 changes: 10 additions & 0 deletions src/examples/test-doubles/AbstractTraitTest.php.out
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)
52 changes: 52 additions & 0 deletions src/examples/test-doubles/HelloService.wsdl
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>
24 changes: 24 additions & 0 deletions src/examples/test-doubles/MockBuilderExampleTest.php
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());
}
}
18 changes: 18 additions & 0 deletions src/examples/test-doubles/MockBuilderExampleTest.php.out
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 src/examples/test-doubles/OnConsecutiveCallsExampleTest.php
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 src/examples/test-doubles/OnConsecutiveCallsExampleTest.php.out
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.
21 changes: 21 additions & 0 deletions src/examples/test-doubles/ReturnArgumentExampleTest.php
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 src/examples/test-doubles/ReturnArgumentExampleTest.php.out
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.
18 changes: 18 additions & 0 deletions src/examples/test-doubles/ReturnCallbackExampleTest.php
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 src/examples/test-doubles/ReturnCallbackExampleTest.php.out
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.
18 changes: 18 additions & 0 deletions src/examples/test-doubles/ReturnSelfExampleTest.php
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());
}
}
18 changes: 18 additions & 0 deletions src/examples/test-doubles/ReturnSelfExampleTest.php.out
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.
26 changes: 26 additions & 0 deletions src/examples/test-doubles/ReturnValueMapExampleTest.php
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 src/examples/test-doubles/ReturnValueMapExampleTest.php.out
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.
21 changes: 21 additions & 0 deletions src/examples/test-doubles/SomeClassTest.php
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);
}
}
Loading

0 comments on commit 037d147

Please sign in to comment.