-
Notifications
You must be signed in to change notification settings - Fork 4
/
DependencyInjectionTest.php
65 lines (57 loc) · 1.58 KB
/
DependencyInjectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
class DependencyInjectionTest extends PHPUnit_Framework_TestCase
{
/**
* The System Under Test is exercised in isolation via a .
*/
public function testInjectsATestDoubleViaConstructor()
{
$collaboratorStub = $this->getMock('Collaborator');
$sut = new GoodSut($collaboratorStub);
$collaboratorStub->expects($this->any())->method('baseValue')->will($this->returnValue(5));
$this->assertEquals(50, $sut->calculateValue());
}
/**
* The System Under Test cannot be exercised in isolation, and since
* the other class is not yet finished we're blocked. We cannot even
* substitute Collaborator just because of speed if it's a really heavy
* and complex to set up implementation using a database or the filesystem.
*/
public function testCannotSubstituteTheCollaborator()
{
$sut = new BadSut();
// now what?
$this->markTestIncomplete('Kernel panic!');
}
}
class GoodSut
{
private $collaborator;
public function __construct(Collaborator $collaborator)
{
$this->collaborator = $collaborator;
}
public function calculateValue()
{
return $this->collaborator->baseValue() * 10;
}
}
class BadSut
{
private $collaborator;
public function __construct()
{
$this->collaborator = new Collaborator();
}
public function calculateValue()
{
return $this->collaborator->baseValue() * 10;
}
}
/**
* The Collaborator class is not even finished yet.
*/
class Collaborator
{
public function baseValue() {}
}