-
Notifications
You must be signed in to change notification settings - Fork 4
/
DependencyLookupTest.php
72 lines (65 loc) · 1.61 KB
/
DependencyLookupTest.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
66
67
68
69
70
71
72
<?php
class DependencyLookupTest extends PHPUnit_Framework_TestCase
{
/**
* We use the real Locator, since we can change its configuration.
* It's not totally a unit test since it involves the Locator, but
* it's often very simple code that would be perfectly replicated by the
* first Stub in the chain.
*/
public function testViewProducesAParagraphAndAnUrl()
{
$viewHelperBroker = new ViewHelperBroker(array(
'p' => 'NullHelper',
'url' => 'NullHelper'
));
$view = new ViewScript($viewHelperBroker);
$expected = "Hello World!|"
. "\n"
. "user|1";
$this->assertEquals($expected, $view->render());
}
}
/**
* A collaborator (a Stub one).
*/
class NullHelper
{
public function __invoke($arg1 = null, $arg2 = null)
{
return $arg1 . '|' . $arg2;
}
}
/**
* The SUT.
*/
class ViewScript
{
private $helperBroker;
public function __construct(ViewHelperBroker $broker)
{
$this->helperBroker = $broker;
}
public function render()
{
return $this->helperBroker->p()->__invoke('Hello World!')
. "\n"
. $this->helperBroker->url()->__invoke('user', '1');
}
}
/**
* The Service Locator.
*/
class ViewHelperBroker
{
private $configuration;
public function __construct(array $helpersConfiguration)
{
$this->configuration = $helpersConfiguration;
}
public function __call($helperName, $arguments)
{
$class = $this->configuration[$helperName];
return new $class;
}
}