|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The "Fake-Manager" instantiates fake objects, mainly to replace webservices with |
|
4
|
|
|
* faked implementations. It also provides shortcuts for creating complex fake datasets |
|
5
|
|
|
* through {@link FakeDatabase}. |
|
6
|
|
|
* |
|
7
|
|
|
* This class needs to be instantiated early in the application bootstrap process. |
|
8
|
|
|
* By default that's implemented through the {@link SampleTestSessionExtension} |
|
9
|
|
|
* and config.yml. |
|
10
|
|
|
* |
|
11
|
|
|
* The instantiation is conditional on there being a Behat test actually running |
|
12
|
|
|
* (by virtue of there being a TestSession instantiated) by a 'useFakeManager' flag |
|
13
|
|
|
* set through the "TestSession" module (@see {@link App\Test\Behaviour\FeatureContext->getTestSessionState()}). |
|
14
|
|
|
* |
|
15
|
|
|
* Since both the FeatureContext and actual application bootstrap share the same |
|
16
|
|
|
* {@link FakeDatabase} persisted on disk, they can share state. |
|
17
|
|
|
* |
|
18
|
|
|
* In terms of mock-data, at time of writing we're using the same sources as unit-test |
|
19
|
|
|
* mocks do. |
|
20
|
|
|
* |
|
21
|
|
|
* @author SilverStripe Iterators <[email protected]> |
|
22
|
|
|
* @author SilverStripe Science Ninjas <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class SampleFakeManager { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @var type |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $db; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* |
|
34
|
|
|
* @var type |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $addressRightGateway; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* @param FakeDatabase $db |
|
41
|
|
|
*/ |
|
42
|
|
|
function __construct($db = null) { |
|
|
|
|
|
|
43
|
|
|
$testState = Injector::inst()->get('TestSessionEnvironment')->getState(); |
|
44
|
|
|
|
|
45
|
|
|
if(!$db) { |
|
46
|
|
|
$db = new FakeDatabase($testState->fakeDatabasePath); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->addressRightGateway = new AddressRightGatewayFake(); |
|
50
|
|
|
$this->addressRightGateway->setDb($db); |
|
51
|
|
|
|
|
52
|
|
|
/* |
|
53
|
|
|
* Registering a service is where the actual injection happens, depending on |
|
54
|
|
|
* whether a TestSession exists or not. |
|
55
|
|
|
*/ |
|
56
|
|
|
Injector::inst()->registerService( |
|
57
|
|
|
$this->addressRightGateway, |
|
58
|
|
|
'AddressRightGateway' |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$this->iguna2Gateway = new Iguana2GatewayFake(); |
|
|
|
|
|
|
62
|
|
|
$this->iguna2Gateway->setDb($db); |
|
63
|
|
|
|
|
64
|
|
|
/* |
|
65
|
|
|
* Registering a service is where the actual injection happens, depending on |
|
66
|
|
|
* whether a TestSession exists or not. |
|
67
|
|
|
*/ |
|
68
|
|
|
Injector::inst()->registerService( |
|
69
|
|
|
$this->iguna2Gateway, |
|
70
|
|
|
'Iguana2Gateway' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$this->db = $db; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getDb() { |
|
77
|
|
|
return $this->db; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// For use within specific Behat contexts |
|
81
|
|
|
public function getAddressRightGateway() { |
|
82
|
|
|
return $this->addressRightGateway; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// For use within specific Behat contexts |
|
86
|
|
|
public function getIguana2Gateway() { |
|
87
|
|
|
return $this->iguana2Gateway; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
class AddressRightGatewayFake extends AddressRightGateway { |
|
93
|
|
|
|
|
94
|
|
|
protected $db; |
|
95
|
|
|
|
|
96
|
|
|
public function __construct() { |
|
97
|
|
|
// Don't initialize client |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function setDb($db) { |
|
101
|
|
|
$this->db = $db; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getDb() { |
|
105
|
|
|
return $this->db; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* In more complex API's this function would be named for the remote method call |
|
110
|
|
|
* that is under test e.g. getAccountDataForMember() or something. |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function get($endpoint = 'autocomplete') { |
|
115
|
|
|
if($response = GatewayCommon::get_mock_behat_data('Addressright')) { |
|
116
|
|
|
return $response; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
class Iguana2GatewayFake extends Iguana2Gateway { |
|
122
|
|
|
|
|
123
|
|
|
protected $db; |
|
124
|
|
|
|
|
125
|
|
|
public function __construct() { |
|
126
|
|
|
// Don't initialize client |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function setDb($db) { |
|
130
|
|
|
$this->db = $db; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getDb() { |
|
134
|
|
|
return $this->db; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* In more complex API's this function would be named for the remote method call |
|
139
|
|
|
* that is under test e.g. getAccountDataForMember() or something. |
|
140
|
|
|
* |
|
141
|
|
|
* AddressRight is a single URL and a single 'endpoint' so we've simply called it 'get'. |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
|
|
public function get() { |
|
146
|
|
|
if($response = GatewayCommon::get_mock_behat_data('Iguana2')) { |
|
147
|
|
|
return $response; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* |
|
153
|
|
|
* @see {@link $this->get()} |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getNZX() { |
|
157
|
|
|
if($response = GatewayCommon::get_mock_behat_data('Iguana2', 'NZX')) { |
|
158
|
|
|
return $response; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* |
|
164
|
|
|
* @see {@link $this->get()} |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getASX() { |
|
168
|
|
|
if($response = GatewayCommon::get_mock_behat_data('Iguana2', 'ASX')) { |
|
169
|
|
|
return $response; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.