-
Notifications
You must be signed in to change notification settings - Fork 21
/
EventCollectorTest.php
338 lines (285 loc) · 10.9 KB
/
EventCollectorTest.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
<?php
declare(strict_types=1);
/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson
* @link https://webrider.de/
* @since 1.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace CakephpFixtureFactories\Test\TestCase\Factory;
use Cake\Core\Configure;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use CakephpFixtureFactories\Factory\BaseFactory;
use CakephpFixtureFactories\Factory\EventCollector;
use CakephpFixtureFactories\ORM\FactoryTableRegistry;
use CakephpFixtureFactories\Test\Factory\AddressFactory;
use CakephpFixtureFactories\Test\Factory\ArticleFactory;
use CakephpFixtureFactories\Test\Factory\AuthorFactory;
use CakephpFixtureFactories\Test\Factory\BillFactory;
use CakephpFixtureFactories\Test\Factory\CityFactory;
use CakephpFixtureFactories\Test\Factory\CountryFactory;
use CakephpFixtureFactories\Test\Factory\CustomerFactory;
use CakephpTestSuiteLight\Fixture\TruncateDirtyTables;
use TestApp\Model\Entity\Address;
use TestApp\Model\Entity\Article;
use TestApp\Model\Entity\City;
use TestApp\Model\Table\CountriesTable;
use TestPlugin\Model\Behavior\SomePluginBehavior;
class EventCollectorTest extends TestCase
{
use TruncateDirtyTables;
/**
* @var CountriesTable
*/
private $Countries;
public static function setUpBeforeClass(): void
{
Configure::write('FixtureFactories.testFixtureNamespace', 'CakephpFixtureFactories\Test\Factory');
Configure::write('FixtureFactories.testFixtureGlobalBehaviors', 'SomeBehaviorUsedInMultipleTables');
}
public static function tearDownAfterClass(): void
{
Configure::delete('FixtureFactories.testFixtureNamespace');
Configure::delete('FixtureFactories.testFixtureGlobalBehaviors');
}
public function setUp(): void
{
/** @var CountriesTable $Countries */
$Countries = TableRegistry::getTableLocator()->get('Countries');
$this->Countries = $Countries;
parent::setUp();
}
public function tearDown(): void
{
Configure::delete('FixtureFactories.testFixtureGlobalBehaviors');
unset($this->Countries);
parent::tearDown();
}
/**
* @see EventCollector::setDefaultListeningBehaviors()
*/
public function testSetDefaultListeningBehaviors()
{
Configure::write('FixtureFactories.testFixtureGlobalBehaviors', ['Sluggable']);
$EventManager = new EventCollector('Foo');
$this->assertSame(
['Sluggable', 'Timestamp'],
$EventManager->getListeningBehaviors()
);
}
public function testSetBehaviorEmpty()
{
$EventManager = new EventCollector('Foo');
$expected = [
'SomeBehaviorUsedInMultipleTables',
'Timestamp',
];
$this->assertSame(
$expected,
$EventManager->getListeningBehaviors()
);
}
public static function provideFactories()
{
return [
[ArticleFactory::make()],
[AuthorFactory::make()],
[AddressFactory::make()],
[CityFactory::make()],
[CountryFactory::make()],
[BillFactory::make()],
[CustomerFactory::make()],
];
}
/**
* @dataProvider provideFactories
* @param BaseFactory $factory
*/
public function testTimestamp(BaseFactory $factory)
{
$entity = $factory->persist();
$this->assertNotNull($entity->get('created'));
}
public static function runSeveralTimesWithOrWithoutEvents()
{
return [
[true], [false], [true], [false],
];
}
/**
* @dataProvider runSeveralTimesWithOrWithoutEvents
* @param bool $applyEvent Bind the event once to the model
*/
public function testApplyOrIgnoreBeforeMarshalSetOnTheFly(bool $applyEvent)
{
$name = 'Foo';
$this->Countries->getEventManager()->on('Model.beforeMarshal', function (Event $event, \ArrayObject $entity) use ($applyEvent) {
$entity['eventApplied'] = $applyEvent;
});
// Event should apply
$country = $this->Countries->newEntity(compact('name'));
$this->assertSame($applyEvent, $country->get('eventApplied'));
$factory = CountryFactory::make();
$factory->getTable()->getEventManager()->on('Model.beforeMarshal', function (Event $event, \ArrayObject $entity) use ($applyEvent) {
$entity['eventApplied'] = $applyEvent;
});
$country = $factory->getEntity();
$this->assertSame($applyEvent, $country->get('eventApplied'));
FactoryTableRegistry::getTableLocator()->clear();
// Event should be skipped
$country = CountryFactory::make()->getEntity();
$this->assertSame(null, $country->get('eventApplied'));
$country = CountryFactory::make()->listeningToModelEvents('Model.beforeMarshal')->getEntity();
$this->assertSame(null, $country->get('eventApplied'));
}
/**
* @dataProvider runSeveralTimesWithOrWithoutEvents
*/
public function testApplyOrIgnoreBeforeMarshalSetInTable()
{
$name = 'Foo';
// Event should be skipped
$country = CountryFactory::make()->getEntity();
$this->assertNull($country->get('beforeMarshalTriggered'));
// Event should apply
$country = $this->Countries->newEntity(compact('name'));
$this->assertTrue($country->get('beforeMarshalTriggered'));
$country = CountryFactory::make()->listeningToModelEvents('Model.beforeMarshal')->getEntity();
$this->assertTrue($country->get('beforeMarshalTriggered'));
}
/**
* @dataProvider runSeveralTimesWithOrWithoutEvents
*/
public function testApplyOrIgnoreEventInBehaviors()
{
$title = 'This Article';
$slug = 'This-Article';
$article = ArticleFactory::make(compact('title'))->persist();
$this->assertEquals(null, $article->get('slug'));
$article = ArticleFactory::make(compact('title'))->listeningToBehaviors('Sluggable')->persist();
$this->assertEquals($slug, $article->get('slug'));
}
public function testSetBehaviorOnTheFly()
{
$behavior = 'Foo';
$EventManager = new EventCollector('Bar');
$EventManager->listeningToBehaviors(['Foo']);
$expected = [
'SomeBehaviorUsedInMultipleTables',
'Timestamp',
$behavior,
];
$this->assertSame(
$expected,
$EventManager->getListeningBehaviors()
);
}
public function testGetEntityOnNonExistentBehavior()
{
$behavior = 'Foo';
$article = ArticleFactory::make()->listeningToBehaviors($behavior)->getEntity();
$this->assertInstanceOf(Article::class, $article);
}
/**
* @dataProvider runSeveralTimesWithOrWithoutEvents
*/
public function testApplyOrIgnoreEventInBehaviorsOnTheFlyWithCountries()
{
$name = 'Some Country';
$slug = 'Some-Country';
$country = CountryFactory::make(compact('name'))->persist();
$this->assertNull($country->get('slug'));
$country = CountryFactory::make(compact('name'))
->listeningToBehaviors('Sluggable')
->persist();
$this->assertEquals($slug, $country->get('slug'));
}
/**
* @dataProvider runSeveralTimesWithOrWithoutEvents
*/
public function testApplyOrIgnoreEventInPluginBehaviorsOnTheFlyWithCountries()
{
$field = SomePluginBehavior::BEFORE_SAVE_FIELD;
$this->Countries->addBehavior('TestPlugin.SomePlugin');
// The behavior should not apply
$country = CountryFactory::make()->persist();
$this->assertNull($country->get($field));
// The behavior should apply
$country = CountryFactory::make()->listeningToBehaviors('SomePlugin')->persist();
$this->assertTrue($country->get($field));
// The behavior should not apply
$country = CountryFactory::make()->persist();
$this->assertNull($country->get($field));
// The behavior should apply
Configure::write('FixtureFactories.testFixtureGlobalBehaviors', ['SomePlugin']);
$country = CountryFactory::make()->persist();
$this->assertTrue($country->get($field));
}
public function testSkipValidation()
{
$city = CityFactory::make()->without('Country')->getEntity();
$this->assertInstanceOf(City::class, $city);
$this->assertEmpty($city->getErrors());
}
public function testSkipValidationInAssociation()
{
$address = AddressFactory::make()
->with('City', CityFactory::make()->without('Country'))
->getEntity();
$this->assertInstanceOf(Address::class, $address);
$this->assertInstanceOf(City::class, $address->city);
$this->assertNull($address->city->country);
$this->assertEmpty($address->getErrors());
}
public function testApplyValidationInAssociation()
{
$address = AddressFactory::make()
->with(
'City',
CityFactory::make()
->listeningToModelEvents('Model.beforeMarshal')
->without('Country')
)
->getEntity();
$this->assertInstanceOf(Address::class, $address);
$this->assertInstanceOf(City::class, $address->city);
$this->assertNull($address->city->country);
$this->assertTrue($address->city->get('beforeMarshalTriggered'));
}
/**
* Cities have a rule that always return false
*/
public function testSkipRules()
{
$city = CityFactory::make()->persist();
$this->assertInstanceOf(City::class, $city);
$this->assertEmpty($city->getErrors());
}
public function testSkipRuleInAssociation()
{
$address = AddressFactory::make()->getEntity();
$this->assertInstanceOf(Address::class, $address);
$this->assertInstanceOf(City::class, $address->city);
$this->assertEmpty($address->getErrors());
}
public function testBeforeMarshalIsTriggeredInAssociationWhenDefinedInDefaultTemplate()
{
$bill = BillFactory::make()->getEntity();
$this->assertTrue($bill->get('beforeMarshalTriggeredPerDefault'));
$bill = CustomerFactory::make()->withBills()->getEntity()->bills[0];
$this->assertTrue($bill->get('beforeMarshalTriggeredPerDefault'));
}
public function testAfterSaveIsTriggeredInAssociationWhenDefinedInDefaultTemplate()
{
$bill = BillFactory::make()->persist();
$this->assertTrue($bill->get('afterSaveTriggeredPerDefault'));
$bill = CustomerFactory::make()->withBills()->persist()->bills[0];
$this->assertTrue($bill->get('afterSaveTriggeredPerDefault'));
}
}