Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FactoryMuffin.php « Unit « tests - github.com/nextcloud/polls.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50e92c7f0d6b7d5f11f035ad1f956b703c7ff65e (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
35
36
<?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;
			}
		}
	}
}