forked from thephpleague/factory-muffin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomisationTest.php
156 lines (124 loc) · 3.71 KB
/
CustomisationTest.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
<?php
use League\FactoryMuffin\Exceptions\DeletingFailedException;
use League\FactoryMuffin\Exceptions\SaveFailedException;
use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* @group customisation
*/
class CustomisationTest extends AbstractTestCase
{
public function testCustomMaker()
{
FactoryMuffin::setCustomMaker(function ($class) {
return new $class('example');
});
$obj = FactoryMuffin::instance('MakerCustomisationModelStub');
$this->assertSame('example', $obj->test);
$this->reload();
}
public function testCustomSetter()
{
FactoryMuffin::setCustomSetter(function ($object, $name, $value) {
$object->set($name, $value);
});
$obj = FactoryMuffin::instance('SetterCustomisationModelStub');
$this->assertSame('baz', $obj->get('bar'));
$this->assertNull($obj->get('foo'));
$this->reload();
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\SaveFailedException
*/
public function testCustomSaverFail()
{
FactoryMuffin::setCustomSaver(function ($object) {
$object->customSave();
});
try {
FactoryMuffin::create($model = 'SaverAndDeleterCustomisationModelStub');
} catch (SaveFailedException $e) {
$this->assertEquals("We could not save the model of type: '$model'.", $e->getMessage());
$this->assertEquals($model, $e->getModel());
$this->assertNull($e->getErrors());
$this->reload();
throw $e;
}
$this->reload();
}
/**
* @expectedException \League\FactoryMuffin\Exceptions\DeletingFailedException
*/
public function testCustomDeleterFail()
{
FactoryMuffin::setCustomSaver(function ($object) {
return $object->customSave();
});
FactoryMuffin::setCustomDeleter(function ($object) {
$object->customDelete();
});
try {
FactoryMuffin::create($model = 'SaverAndDeleterCustomisationModelStub');
FactoryMuffin::deleteSaved();
} catch (DeletingFailedException $e) {
$exceptions = $e->getExceptions();
$this->assertEquals("We encountered 1 problem(s) while trying to delete the saved models.", $e->getMessage());
$this->assertEquals("We could not delete the model of type: '$model'.", $exceptions[0]->getMessage());
$this->reload();
throw $e;
}
$this->reload();
}
public function testCustomSaverAndDeleter()
{
FactoryMuffin::setCustomSaver(function ($object) {
$object->test = 'foo';
return $object->customSave();
});
FactoryMuffin::setCustomDeleter(function ($object) {
return $object->customDelete();
});
$obj = FactoryMuffin::create('SaverAndDeleterCustomisationModelStub');
$this->assertSame('foo', $obj->test);
$this->reload();
}
}
class MakerCustomisationModelStub
{
public $test;
public function __construct($test)
{
$this->test = $test;
}
}
class SetterCustomisationModelStub
{
public $attributes = array();
public function get($name)
{
return array_get($this->attributes, $name);
}
public function set($name, $value)
{
$this->attributes[$name] = $value;
}
}
class SaverAndDeleterCustomisationModelStub
{
public $test = 'bar';
public function save()
{
return false;
}
public function customSave()
{
return true;
}
public function delete()
{
return false;
}
public function customDelete()
{
return true;
}
}