blob: 48ba9696a6aca4b521bd07ae872f4d2db9554af0 (
plain)
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
|
<?php
namespace OCA\Polls\Tests\Unit;
use League\FactoryMuffin\FactoryMuffin as OriginalFactoryMuffin;
use OCP\AppFramework\Db\Entity;
class FactoryMuffin extends OriginalFactoryMuffin {
/**
* Generate and set the model attributes.
* NOTE: Patch the original method to support dynamic setter and getter
* of the OCP\AppFramework\Db\Entity class
*
* @param object $model The model instance.
* @param array $attr The model attributes.
*
* @return void
*/
protected function generate($model, array $attr = []) {
foreach ($attr as $key => $kind) {
$value = $this->factory->generate($kind, $model, $this);
$setter = 'set' . ucfirst(static::camelize($key));
// check if there is a setter and use it instead
if ($model instanceof Entity && is_callable([$model, $setter])) {
$model->$setter($value);
} elseif (method_exists($model, $setter) && is_callable([$model, $setter])) {
$model->$setter($value);
} else {
$model->$key = $value;
}
}
}
}
|