forked from thephpleague/factory-muffin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SaveAndDeleteTest.php
372 lines (317 loc) · 10.8 KB
/
SaveAndDeleteTest.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/*
* This file is part of Factory Muffin.
*
* (c) Graham Campbell <[email protected]>
* (c) Scott Robertson <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use League\FactoryMuffin\Exceptions\DeletingFailedException;
use League\FactoryMuffin\Exceptions\SaveFailedException;
use League\FactoryMuffin\Exceptions\SaveMethodNotFoundException;
use League\FactoryMuffin\FactoryMuffin;
use League\FactoryMuffin\Stores\ModelStore;
/**
* This is save and delete test class.
*
* @author Graham Campbell <[email protected]>
* @author Scott Robertson <[email protected]>
*/
class SaveAndDeleteTest extends AbstractTestCase
{
public function testShouldCreateAndDelete()
{
$obj = static::$fm->create('ModelThatWillSaveStub');
$reflection = new ReflectionClass(static::$fm);
$store = $reflection->getProperty('store');
$store->setAccessible(true);
$value = $store->getValue(static::$fm);
$this->assertTrue($value->isSaved($obj));
$this->assertTrue(is_numeric($obj->id));
$this->assertInternalType('array', $value->saved());
$this->assertCount(1, $value->saved());
$this->assertCount(0, $value->pending());
static::$fm->deleteSaved();
}
public function testShouldNotSave()
{
$obj = static::$fm->instance('ModelThatWillSaveStub');
$reflection = new ReflectionClass(static::$fm);
$store = $reflection->getProperty('store');
$store->setAccessible(true);
$value = $store->getValue(static::$fm);
$this->assertCount(0, $value->saved());
$this->assertCount(0, $value->pending());
$this->assertFalse($value->isSaved($obj));
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\SaveMethodNotFoundException
*/
public function testShouldThrowExceptionAfterSaveMethodRename()
{
static::$fm = new FactoryMuffin(new ModelStore('foo'));
static::$fm->loadFactories(__DIR__.'/factories');
try {
static::$fm->create($model = 'ModelThatWillSaveStub');
} catch (SaveMethodNotFoundException $e) {
$this->assertSame("The save method 'foo' was not found on the model: '$model'.", $e->getMessage());
$this->assertSame($model, $e->getModelClass());
$this->assertSame('foo', $e->getMethodName());
$this->reload();
throw $e;
}
$this->reload();
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException
*/
public function testShouldThrowExceptionAfterDeleteMethodRename()
{
static::$fm = new FactoryMuffin(new ModelStore(null, 'bar'));
static::$fm->loadFactories(__DIR__.'/factories');
static::$fm->create($model = 'ModelThatWillSaveStub');
try {
static::$fm->deleteSaved();
} catch (DeletingFailedException $e) {
$exceptions = $e->getExceptions();
$this->assertSame('We encountered 1 problem while trying to delete the saved models.', $e->getMessage());
$this->assertSame("The delete method 'bar' was not found on the model: '$model'.", $exceptions[0]->getMessage());
$this->assertSame($model, $exceptions[0]->getModelClass());
$this->assertSame('bar', $exceptions[0]->getMethodName());
$this->reload();
throw $e;
}
$this->reload();
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\SaveFailedException
*/
public function testShouldThrowExceptionOnModelSaveFailure()
{
try {
static::$fm->create($model = 'ModelThatFailsToSaveStub');
} catch (SaveFailedException $e) {
$this->assertSame("We could not save the model: '$model'.", $e->getMessage());
$this->assertSame($model, $e->getModelClass());
$this->assertNull($e->getValidationErrors());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException
*/
public function testShouldThrowExceptionOnModelDeleteFailure()
{
try {
static::$fm->create($model = 'ModelThatFailsToDeleteStub');
static::$fm->deleteSaved();
} catch (DeletingFailedException $e) {
$exceptions = $e->getExceptions();
$this->assertSame('We encountered 1 problem while trying to delete the saved models.', $e->getMessage());
$this->assertSame("We could not delete the model: '$model'.", $exceptions[0]->getMessage());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException
*/
public function testShouldAlsoThrowExceptionOnModelDeleteFailure()
{
try {
static::$fm->create($model = 'ModelThatAlsoFailsToDeleteStub');
static::$fm->deleteSaved();
} catch (DeletingFailedException $e) {
$exceptions = $e->getExceptions();
$this->assertSame('We encountered 1 problem while trying to delete the saved models.', $e->getMessage());
$this->assertSame('OH NOES!', $exceptions[0]->getMessage());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\SaveMethodNotFoundException
*/
public function testShouldThrowExceptionWithoutSaveMethod()
{
try {
static::$fm->create($model = 'ModelWithNoSaveMethodStub');
} catch (SaveMethodNotFoundException $e) {
$this->assertSame("The save method 'save' was not found on the model: '$model'.", $e->getMessage());
$this->assertSame($model, $e->getModelClass());
$this->assertSame('save', $e->getMethodName());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException
*/
public function testShouldThrowExceptionWithoutDeleteMethod()
{
try {
static::$fm->create($model = 'ModelWithNoDeleteMethodStub');
static::$fm->deleteSaved();
} catch (DeletingFailedException $e) {
$exceptions = $e->getExceptions();
$this->assertSame('We encountered 1 problem while trying to delete the saved models.', $e->getMessage());
$this->assertSame("The delete method 'delete' was not found on the model: '$model'.", $exceptions[0]->getMessage());
$this->assertSame($model, $exceptions[0]->getModelClass());
$this->assertSame('delete', $exceptions[0]->getMethodName());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\SaveFailedException
*/
public function testShouldThrowExceptionWithValidationErrors()
{
try {
static::$fm->create($model = 'ModelWithValidationErrorsStub');
} catch (SaveFailedException $e) {
$this->assertSame("Failed to save! We could not save the model: '$model'.", $e->getMessage());
$this->assertSame($model, $e->getModelClass());
$this->assertSame('Failed to save!', $e->getValidationErrors());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\SaveFailedException
*/
public function testShouldThrowExceptionWithBadValidationErrors()
{
try {
static::$fm->create($model = 'ModelWithBadValidationErrorsStub');
} catch (SaveFailedException $e) {
$this->assertSame("Oh noes. We could not save the model: '$model'.", $e->getMessage());
$this->assertSame($model, $e->getModelClass());
$this->assertSame('Oh noes.', $e->getValidationErrors());
throw $e;
}
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException
*/
public function testShouldThrowMultipleDeletionExceptions()
{
try {
static::$fm->create($model = 'ModelWithNoDeleteMethodStub');
static::$fm->create('ModelThatAlsoFailsToDeleteStub');
static::$fm->deleteSaved();
} catch (DeletingFailedException $e) {
$exceptions = $e->getExceptions();
$this->assertSame('We encountered 2 problems while trying to delete the saved models.', $e->getMessage());
$this->assertSame('OH NOES!', $exceptions[0]->getMessage());
$this->assertSame("The delete method 'delete' was not found on the model: '$model'.", $exceptions[1]->getMessage());
$this->assertSame($model, $exceptions[1]->getModelClass());
$this->assertSame('delete', $exceptions[1]->getMethodName());
$this->assertInternalType('array', $e->getExceptions());
$this->assertCount(2, $e->getExceptions());
throw $e;
}
}
public function testModelIsNotPersistedASecondTimeIfClosureReturnsFalse()
{
$obj1 = static::$fm->create('no return:ModelWithTrackedSaves');
$obj2 = static::$fm->create('return true:ModelWithTrackedSaves');
$obj3 = static::$fm->create('return false:ModelWithTrackedSaves');
$this->assertEquals(2, $obj1->saveCounter);
$this->assertEquals(2, $obj2->saveCounter);
$this->assertEquals(1, $obj3->saveCounter);
}
}
class ModelThatWillSaveStub
{
public function save()
{
$this->id = date('U');
return true;
}
public function delete()
{
return true;
}
}
class ModelThatFailsToSaveStub
{
public function save()
{
return false;
}
public function delete()
{
return true;
}
}
class ModelThatFailsToDeleteStub
{
public function save()
{
return true;
}
public function delete()
{
return false;
}
}
class ModelThatAlsoFailsToDeleteStub
{
public function save()
{
return true;
}
public function delete()
{
throw new Exception('OH NOES!');
}
}
class ModelWithNoSaveMethodStub
{
public function delete()
{
return true;
}
}
class ModelWithNoDeleteMethodStub
{
public function save()
{
return true;
}
}
class ModelWithValidationErrorsStub
{
public $validationErrors = 'Failed to save!';
public function save()
{
//
}
public function delete()
{
return true;
}
}
class ModelWithBadValidationErrorsStub
{
public $validationErrors = 'Oh noes';
public function save()
{
//
}
public function delete()
{
return true;
}
}
class ModelWithTrackedSaves
{
public $saveCounter = 0;
public function save()
{
$this->saveCounter++;
return true;
}
public function delete()
{
return true;
}
}