This repository was archived by the owner on Nov 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFixture.php
More file actions
318 lines (276 loc) · 6.99 KB
/
Fixture.php
File metadata and controls
318 lines (276 loc) · 6.99 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php namespace Codesleeve\Fixture;
use Codesleeve\Fixture\Drivers\DriverInterface;
use Faker\Generator;
/**
* A framework agnostic, simple (yet elegant) fixture library for php.
*
* @package Codesleeve/Fixture
* @version v1.0.0
* @author Travis Bennett <tandrewbennett@hotmail.com>
* @link http://travisbennett.net
*/
class Fixture
{
/**
* An array of eloquent collections (one for each loaded fixture).
*
* @var array
*/
protected $fixtures;
/**
* An array of configuration options.
*
* @var Array
*/
protected $config;
/**
* The ORM specific database driver that's being used.
*
* @var Driver
*/
protected $driver;
/**
* An instance of the Faker library
* @var Generator
*/
protected static $faker;
/**
* Returns the *Singleton* instance of this class.
*
* @staticvar Singleton $instance The *Singleton* instances of this class.
*
* @param array $config
* @param DriverInterface $driver
* @return Singleton The *Singleton* instance.
*/
public static function getInstance(array $config = array(), DriverInterface $driver = null)
{
static $instance = null;
if (null === $instance) {
$instance = new static();
}
if ($config) {
$instance->config = $config;
}
if ($driver) {
$instance->driver = $driver;
}
return $instance;
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*
* @return void
*/
protected function __construct()
{
}
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone()
{
}
/**
* Private unserialize method to prevent unserializing of the *Singleton*
* instance.
*
* @return void
*/
private function __wakeup()
{
}
/**
* Setter method for the configuration array used by
* the fixture.
*
* @param Array $configArray
*/
public function setConfig($configArray)
{
$this->config = $configArray;
}
/**
* Getter method for the configuration array used by
* the fixture.
*
* @return array
*/
public function getConfig()
{
return $this->config;
}
/**
* Setter method for the driver instance used by
* the fixture.
*
* @param Drivers\DriverInterface $driver
*/
public function setDriver(Drivers\DriverInterface $driver)
{
$this->driver = $driver;
}
/**
* Getter method for the driver instance used by
* the fixture.
*
* @return Drivers\DriverInterface
*/
public function getDriver()
{
return $this->driver;
}
/**
* Return all fixtures.
*
* @return array
*/
public function getFixtures()
{
return $this->fixtures;
}
/**
* Set all fixtures.
*
* @param array $fixtures
*/
public function setFixtures(array $fixtures)
{
$this->fixtures = $fixtures;
}
/**
* Handle dynamic method calls to this class.
* This allows us to return fixture objects via method invocation.
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
if (!array_key_exists($name, $this->fixtures)) {
throw new Exceptions\InvalidFixtureNameException("Fixture: $name does not exist", 1);
}
$fixture = $this->fixtures[$name];
if ($arguments && array_key_exists($arguments[0], $fixture)) {
return $fixture[$arguments[0]];
}
return $fixture;
}
/**
* Build fixtures.
*
* @param array $fixtures
* @throws Exceptions\InvalidFixtureLocationException
* @return void
*/
public function up($fixtures = array())
{
$location = $this->config['location'];
if (!is_dir($location)) {
throw new Exceptions\InvalidFixtureLocationException(
"Could not find fixtures folder, please make sure $location exists",
1
);
}
$this->loadFixtures($fixtures);
}
/**
* Destroy fixtures.
*
* @return void
*/
public function down()
{
$this->driver->truncate();
$this->fixtures = array();
}
/**
* Create fake data using Faker.
*
* @return mixed
*/
public static function fake()
{
static::bootFaker();
$params = func_get_args();
$method = array_shift($params);
return call_user_func_array(array(static::$faker, $method), $params);
}
/**
* Create an instance of the faker method (if one doesn't already exist)
* and then hang it on this class as a static property.
*
* @return void
*/
protected static function bootFaker()
{
static::$faker = static::$faker ?: \Faker\Factory::create();
}
/**
* Load fixtures.
*
* @param array $fixtures
* @return void
*/
protected function loadFixtures($fixtures)
{
if ($fixtures) {
$this->loadSomeFixtures($fixtures);
return;
}
$this->loadAllFixtures();
}
/**
* Load all fixtures from the fixture location.
*
* @return void
*/
protected function loadAllFixtures()
{
$fixtures = glob("{$this->config['location']}/*.php");
foreach ($fixtures as $fixture) {
$this->loadFixture($fixture);
}
}
/**
* Load a only a subset of fixtures from the fixtures folder.
*
* @param array $selectedFixtures
* @return void
*/
protected function loadSomeFixtures($selectedFixtures)
{
$fixtures = glob("{$this->config['location']}/*.php");
foreach ($fixtures as $fixture) {
$tableName = basename($fixture, '.php');
if (in_array($tableName, $selectedFixtures)) {
$this->loadFixture($fixture);
}
}
}
/**
* Load a fixture's data into the database.
* We'll also store it inside the fixtures property for easy
* access as an array element or class property from our tests.
*
* @param string $fixture
* @return void
*/
protected function loadFixture($fixture)
{
$tableName = basename($fixture, '.php');
$records = include $fixture;
if (!is_array($records)) {
throw new Exceptions\InvalidFixtureDataException(
"Invalid fixture: $fixture, please ensure this file returns an array of data.",
1
);
}
$this->fixtures[$tableName] = $this->driver->buildRecords($tableName, $records, $this->config);
}
}