-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataModSQLContext.php
More file actions
215 lines (183 loc) · 5.96 KB
/
Copy pathDataModSQLContext.php
File metadata and controls
215 lines (183 loc) · 5.96 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace Genesis\SQLExtensionWrapper;
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use Genesis\SQLExtensionWrapper\Exception\DataModNotFoundException;
use Genesis\SQLExtension\Context\Debugger;
/**
* DecoratedSQLContext class. This class gives you context step definitions out of the box that work with your
* data modules mapping. To use set the appropriate mapping i.e dataMod => namespacedClass and give it a spin.
*/
class DataModSQLContext implements Context
{
const DEFAULT_NAMESPACE = '\\DataMod\\';
/**
* @var array
*/
private static $dataModMapping = [];
/**
* @var string
*/
private static $userUniqueRef;
/**
* @param array $dataModMapping
* @param boolean $debug
* @param string $userUniqueRef Will be appended to new data created to separate data based on users.
* Best to limit it to 2 characters.
*/
public function __construct($debug = false, $userUniqueRef = null)
{
if ($debug) {
Debugger::enable($debug);
}
self::$userUniqueRef = $userUniqueRef;
}
/**
* @Given I have a/an :dataModRef fixture
* @Given I have a/an :dataModRef fixture with the following data set:
*
* Note: The first row value in the TableNode is considered the unique key.
*
* @param string $dataModRef
* @param TableNode $where
*/
public function givenIACreateFixture($dataModRef, TableNode $where = null)
{
$dataMod = $this->getDataMod($dataModRef);
// You don't need to necessarily have a where clause to create a fixture.
$uniqueKey = null;
$dataSet = array();
if ($where) {
$dataSet = DataRetriever::transformTableNodeToSingleDataSet($where);
$uniqueKey = key($dataSet);
if (! is_numeric($dataSet[$uniqueKey])) {
$dataSet[$uniqueKey] .= self::$userUniqueRef;
}
}
$dataMod::createFixture(
$dataSet,
$uniqueKey
);
}
/**
* @Given I have multiple :dataModRef fixtures with the following data set(s):
*
* Note: The first column value in the TableNode is considered the unique key.
*
* @param string $dataModRef
* @param TableNode $where
*/
public function givenIMultipleCreateFixtures($dataModRef, TableNode $where)
{
$dataMod = $this->getDataMod($dataModRef);
$dataSets = DataRetriever::transformTableNodeToMultiDataSets($where);
foreach ($dataSets as $dataSet) {
$uniqueKey = key($dataSet);
if (! is_numeric($dataSet[$uniqueKey])) {
$dataSet[$uniqueKey] .= self::$userUniqueRef;
}
$dataMod::createFixture(
$dataSet,
$uniqueKey
);
}
}
/**
* @Given I do not have a/any :dataModRef fixture(s)
* @Given I do not have a/any :dataModRef fixture(s) with the following data set:
*/
public function iDoNotHaveAFixtureWithTheFollowingDataSet($dataModRef, TableNode $where = null)
{
$dataMod = $this->getDataMod($dataModRef);
$dataSet = [];
if ($where) {
$dataSet = DataRetriever::transformTableNodeToSingleDataSet($where);
}
$dataMod::delete($dataSet);
}
/**
* Useful when testing against API's. Not recommended to be used else where.
*
* @Then I should have a :dataModRef
* @Then I should have a :dataModRef with the following data set:
*/
public function iShouldHaveAWithTheFollowingDataSet($dataModRef, TableNode $where = null)
{
$dataMod = $this->getDataMod($dataModRef);
$dataSet = [];
if ($where) {
$dataSet = DataRetriever::transformTableNodeToSingleDataSet($where);
}
$dataMod::assertExists($dataSet);
}
/**
* Useful when testing against API's. Not recommended to be used else where.
*
* @Then I should not have a :dataModRef
* @Then I should not have a :dataModRef with the following data set:
*/
public function iShouldNotHaveAWithTheFollowingDataSet($dataModRef, TableNode $where = null)
{
$dataMod = $this->getDataMod($dataModRef);
$dataSet = [];
if ($where) {
$dataSet = DataRetriever::transformTableNodeToSingleDataSet($where);
}
$dataMod::assertNotExists($dataSet);
}
/**
* @Given I save the id as :key
*/
public function iSaveTheIdAs($key)
{
BaseProvider::getApi()->setKeyword($key, BaseProvider::getApi()->getLastId());
return $this;
}
/**
* @param array $dataModMapping
*/
private static function setDataModMappingFromBehatYamlFile(array $dataModMapping = array())
{
if (! $dataModMapping) {
return false;
}
self::setDataModMapping($dataModMapping);
}
/**
* @param array $mapping
*/
public static function setDataModMapping(array $mapping)
{
self::$dataModMapping = $mapping;
}
/**
* @param string $dataModRef
*
* @return DataModInterface
*/
private function getDataMod($dataModRef)
{
$dataMod = $this->resolveDataMod($dataModRef);
if (! class_exists($dataMod)) {
throw new DataModNotFoundException($dataMod, self::$dataModMapping);
}
return $dataMod;
}
/**
* @param string $dataModRef
*
* @return string
*/
private function resolveDataMod($dataModRef)
{
// If we found a custom datamod mapping use that.
if (isset(self::$dataModMapping[$dataModRef])) {
return self::$dataModMapping[$dataModRef];
}
// If we've got a global namespace where all the datamods reside, just use that.
if (isset(self::$dataModMapping['*'])) {
return self::$dataModMapping['*'] . $dataModRef;
}
return self::DEFAULT_NAMESPACE . $dataModRef;
}
}