Skip to content

Commit

Permalink
test: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 20, 2023
1 parent eec36e4 commit 5a24c0c
Show file tree
Hide file tree
Showing 18 changed files with 717 additions and 186 deletions.
23 changes: 22 additions & 1 deletion tests/Factory/AbstractPsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ public function __call($name, $arguments)
$attribute = Str::snake(Str::after($name, 'with'));
$value = $arguments[0];

return $this->with([$attribute => $value]);
return $this->addAttribute($attribute, $value, $arguments[1] ?? []);
}

throw new BadMethodCallException(sprintf('Method %s does not exist', $name));
}

/**
* @param string $key
*
* @return mixed
*/
public function getAttribute(string $key)
{
return $this->attributes->get($key);
}

/**
* @param array<string, mixed> $data
*
Expand All @@ -40,4 +50,15 @@ public function with(array $data): PsFactoryInterface

return $this;
}

/**
* @param string $attribute
* @param mixed $value
*
* @return $this
*/
protected function addAttribute(string $attribute, $value): self
{
return $this->with([$attribute => $value]);
}
}
103 changes: 96 additions & 7 deletions tests/Factory/AbstractPsObjectModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,55 @@
use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\PsFactoryInterface;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\PsObjectModelFactoryInterface;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModel;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModels;
use MyParcelNL\Sdk\src\Support\Str;
use ObjectModel;

/**
* @template T of ObjectModel
* @method self withDateAdd(string $dateAdd)
* @method self withDateUpd(string $dateUpd)
* @method self withDeleted(bool $deleted)
* @implements PsObjectModelFactoryInterface<T>
* @extends AbstractPsFactory<T>
*/
abstract class AbstractPsObjectModelFactory extends AbstractPsModelFactory implements PsObjectModelFactoryInterface
{
/**
* @var PsObjectModelFactoryInterface[]
*/
private $additionalModelsToStore = [];

/**
* @var null|int
*/
private $id;

/**
* @param null|int $id
*/
public function __construct(?int $id = null)
{
$this->id = $id;

parent::__construct();
}

/**
* @return T
*/
public function store(): ObjectModel
{
$result = parent::store();

foreach ($this->additionalModelsToStore as $modelToStore) {
$modelToStore->store();
}

return $result;
}

/**
* @param int $id
*
Expand All @@ -27,12 +66,36 @@ public function withId(int $id): self
return $this->with(['id' => $id]);
}

/**
* @param string $attribute
* @param mixed $value
* @param array $attributes
*
* @return \MyParcelNL\PrestaShop\Tests\Factory\AbstractPsFactory
*/
protected function addAttribute(string $attribute, $value, array $attributes = []): AbstractPsFactory
{
if (Str::startsWith($attribute, 'id_')) {
return $this->withModel(Str::after($attribute, 'id_'), $value, $attributes);
}

if ($value instanceof PsObjectModelFactoryInterface || $value instanceof MockPsObjectModel) {
return $this->withModel($attribute, $value, $attributes);
}

return parent::addAttribute($attribute, $value);
}

/**
* @return $this
*/
protected function createDefault(): FactoryInterface
{
return $this->withId($this->getNextId());
return $this
->withId($this->id ?? $this->getNextId())
->withDeleted(false)
->withDateAdd('2023-01-01 00:00:00')
->withDateUpd('2023-01-01 00:00:00');
}

/**
Expand Down Expand Up @@ -86,19 +149,45 @@ protected function save($model): void
}

/**
* @param string $key
* @param ObjectModel|PsObjectModelFactoryInterface $input
* @param string $key
* @param int|ObjectModel|PsObjectModelFactoryInterface $input
* @param array $attributes
*
* @return $this
*/
protected function withModel(string $key, $input): self
protected function withModel(string $key, $input, array $attributes = []): self
{
if (is_int($input)) {
$model = Str::after($key, 'id_');

return $this->withModel($model, MockPsObjectModels::get($model, $input), $attributes);
}

if ($input instanceof PsFactoryInterface) {
return $this->withModel($key, $input->make());
$this->additionalModelsToStore[] = $input;

$model = $input
->with($attributes)
->make();

return $this->withModel($key, $model);
}

$idKey = sprintf('id_%s', $key);
$idKey = sprintf('id_%s', Str::snake($key));

return $this->with([$idKey => $input->id]);
}

return $this->with([$idKey => $input]);
/**
* @param string $key
* @param int|ObjectModel|PsObjectModelFactoryInterface $input
* @param array $attributes
* @param string $foreignKey
*
* @return $this
*/
protected function withRelation(string $key, $input, array $attributes, string $foreignKey): self
{
return $this->withModel($key, $input, array_replace($attributes, [$foreignKey => $this->getId()]));
}
}
7 changes: 7 additions & 0 deletions tests/Factory/Contract/PsFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

interface PsFactoryInterface extends FactoryInterface
{
/**
* @param string $key
*
* @return mixed
*/
public function getAttribute(string $key);

/**
* @param array $data
*
Expand Down
37 changes: 32 additions & 5 deletions tests/Mock/BaseMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MyParcelNL\PrestaShop\Tests\Mock;

use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Sdk\src\Support\Str;

abstract class BaseMock implements Arrayable
Expand Down Expand Up @@ -42,7 +43,7 @@ public function __call($name, $arguments)
if (Str::startsWith($name, 'get')) {
$attribute = Str::snake(substr($name, 3));

return $this->attributes[$attribute] ?? null;
return $this->getAttribute($attribute);
}

// If no matching attribute is found, return $this to silently ignore the call
Expand All @@ -56,7 +57,7 @@ public function __call($name, $arguments)
*/
public function __get(string $name)
{
return $this->attributes[$name] ?? null;
return $this->getAttribute($name);
}

/**
Expand All @@ -66,7 +67,7 @@ public function __get(string $name)
*/
public function __isset($name): bool
{
return isset($this->attributes[$name]);
return null !== $this->getAttribute($name);
}

/**
Expand All @@ -77,7 +78,18 @@ public function __isset($name): bool
*/
public function __set(string $name, $value): void
{
$this->attributes[$name] = $value;
$this->setAttribute($name, $value);
}

/**
* @param string $key
* @param null $default
*
* @return mixed
*/
public function getAttribute(string $key, $default = null)
{
return Arr::get($this->attributes, $key, $default);
}

/**
Expand All @@ -88,6 +100,19 @@ public function getAttributes(): array
return $this->attributes;
}

/**
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function setAttribute(string $key, $value): self
{
$this->attributes[$key] = $value;

return $this;
}

/**
* @return array
*/
Expand All @@ -103,6 +128,8 @@ public function toArray(): array
*/
protected function fill(array $attributes): void
{
$this->attributes = $attributes;
foreach ($attributes as $key => $value) {
$this->setAttribute($key, $value);
}
}
}
41 changes: 25 additions & 16 deletions tests/Mock/MockItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
abstract class MockItems implements StaticMockInterface
{
/**
* @var Collection
* @var Collection<Collection>
*/
private static $items;

Expand All @@ -21,13 +21,14 @@ abstract class MockItems implements StaticMockInterface
*/
public static function add(object $object): bool
{
$all = static::all();
$class = get_class($object);
$byClass = self::getByClass($class);

if ($all->has($object->id)) {
if ($byClass->has($object->id)) {
return false;
}

$object->id = self::getId($object);
$object->id = self::getOrCreateId($object);

static::update($object);

Expand All @@ -45,7 +46,7 @@ public static function addOrUpdate(object $entity): void
}

/**
* @return \MyParcelNL\Pdk\Base\Support\Collection
* @return Collection<Collection>
*/
public static function all(): Collection
{
Expand All @@ -68,14 +69,17 @@ public static function delete(int $id): void
}

/**
* @param int $id
* @param string $class
* @param int $id
*
* @return null|object
*/
public static function get(int $id): ?object
public static function get(string $class, int $id): ?object
{
return static::all()
->get($id);
return static::getByClass($class)
->first(function (object $item) use ($id) {
return $item->id === $id;
});
}

/**
Expand All @@ -86,8 +90,7 @@ public static function get(int $id): ?object
public static function getByClass(string $class): Collection
{
return static::all()
->whereInstanceOf($class)
->values();
->get($class, new Collection());
}

public static function reset(): void
Expand All @@ -102,7 +105,14 @@ public static function reset(): void
*/
public static function update(object $entity): bool
{
static::all()
$class = get_class($entity);
$all = static::all();

if (! $all->has($class)) {
$all->put($class, new Collection());
}

$all->get($class)
->put($entity->id, $entity);

return true;
Expand All @@ -113,15 +123,14 @@ public static function update(object $entity): bool
*
* @return int
*/
protected static function getId(object $object): int
protected static function getOrCreateId(object $object): int
{
if (isset($object->id)) {
return $object->id;
}

$count = self::all()
->count();
$byClass = self::getByClass(get_class($object));

return $count + 1;
return $byClass->count() + 1;
}
}
Loading

0 comments on commit 5a24c0c

Please sign in to comment.