/
ProviderTest.php
102 lines (78 loc) · 3.85 KB
/
ProviderTest.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace spec\watoki\factory;
use watoki\scrut\Specification;
/**
* Providers can be used to create instances of classes on-demand.
*
* @property \spec\watoki\factory\FactoryFixture $fix <-
*/
class ProviderTest extends Specification {
public function testFindProviderDirectly() {
$this->fix->givenTheClassDefinition('class MyClass {}');
$this->givenTheProvider_Providing('SingleMindedProvider', 'return json_decode(\'{"nothing":"just this"}\');');
$this->givenIHaveSet_ToProvideFor('SingleMindedProvider', 'MyClass');
$this->fix->whenIGet_FromTheFactory('MyClass');
$this->fix->thenTheObjectShouldBeAnInstanceOf('stdClass');
$this->fix->thenTheTheProperty_OfTheObjectShouldBe('nothing', 'just this');
}
public function testFindBaseProvider() {
$this->fix->givenTheClassDefinition('
class BaseClass {}
');
$this->fix->givenTheClassDefinition('
class SubClass extends BaseClass {}
');
$this->givenTheProvider_Providing('BaseProvider', '$o = new $class; $o->provided = "yes"; return $o;');
$this->givenIHaveSet_ToProvideFor('BaseProvider', 'BaseClass');
$this->fix->whenIGet_FromTheFactory('SubClass');
$this->fix->thenTheObjectShouldBeAnInstanceOf('SubClass');
$this->fix->thenTheTheProperty_OfTheObjectShouldBe('provided', 'yes');
}
public function testFindProviderForInterface() {
$this->fix->givenTheClassDefinition('
interface BaseInterface {}
interface SomeInterface {}
interface SomeOtherInterface extends BaseInterface {}
class SomeImplementation implements SomeInterface, SomeOtherInterface {}
');
$this->givenTheProvider_Providing('InterfaceProvider', 'return new \DateTime;');
$this->givenIHaveSet_ToProvideFor('InterfaceProvider', 'BaseInterface');
$this->fix->whenIGet_FromTheFactory('SomeImplementation');
$this->fix->thenTheObjectShouldBeAnInstanceOf('DateTime');
}
public function testFindSpecificProviderFirst() {
$this->fix->givenTheClassDefinition('
class Base2Class {}
');
$this->fix->givenTheClassDefinition('
class Sub2Class extends BaseClass {}
');
$this->givenTheProvider_Providing('LastProvider', '$o = new $class; $o->provided = "last"; return $o;');
$this->givenIHaveSet_ToProvideFor('LastProvider', 'Base2Class');
$this->givenTheProvider_Providing('FirstProvider', '$o = new $class; $o->provided = "first"; return $o;');
$this->givenIHaveSet_ToProvideFor('FirstProvider', 'Sub2Class');
$this->fix->whenIGet_FromTheFactory('Sub2Class');
$this->fix->thenTheObjectShouldBeAnInstanceOf('Sub2Class');
$this->fix->thenTheTheProperty_OfTheObjectShouldBe('provided', 'first');
}
public function testNormalizeClassName() {
$this->fix->givenTheClassDefinition('namespace My; class FooClass {}');
$this->givenTheProvider_Providing('JustAProvider', 'return json_decode(\'{"nothing":"just this"}\');');
$this->givenIHaveSet_ToProvideFor('JustAProvider', '\My\FooClass');
$this->fix->whenIGet_FromTheFactory('my\fooclass');
$this->fix->thenTheObjectShouldBeAnInstanceOf('stdClass');
$this->fix->thenTheTheProperty_OfTheObjectShouldBe('nothing', 'just this');
}
private function givenTheProvider_Providing($providerName, $statement) {
$this->fix->givenTheClassDefinition("
class $providerName implements \\watoki\\factory\\Provider {
public function provide(\$class, array \$args = array()) {
$statement
}
}
");
}
private function givenIHaveSet_ToProvideFor($provider, $class) {
$this->fix->factory->setProvider($class, new $provider);
}
}