Skip to content

Commit

Permalink
test: add tests for base factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed May 13, 2024
1 parent aa9cdba commit 7e8a27a
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Facades/Testing/RecordFactories/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ public function set(string $key, mixed $value): self
return $this;
}

public function data(array $data): self
{
$this->elements = array_merge($this->elements, $data);

return $this;
}

public static function make(): self
{
return new static();
}

public static function times(int $times): array
public static function times(int $times = 1): array
{
return array_fill(0, $times, static::make());
}
Expand All @@ -56,4 +63,16 @@ public function toArray(): array
'elements' => $this->elements,
];
}

public function __call(string $name, array $arguments): self
{
if (str_starts_with($name, 'set')) {
$key = lcfirst(substr($name, 3));
$value = data_get($arguments, 0);

return $this->set($key, $value);
}

return $this;
}
}
134 changes: 134 additions & 0 deletions tests/Testing/Factories/BaseFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

use Katalam\OnOfficeAdapter\Facades\Testing\RecordFactories\BaseFactory;

class Factory extends BaseFactory
{
}

describe('construct', function () {
it('builds', function () {
$factory = Factory::make();

expect($factory)->toBeInstanceOf(Factory::class);
});
});

describe('id', function () {
it('defaults to 0', function () {
$factory = Factory::make();

expect($factory->id)->toBe(0);
});

it('can set an id', function () {
$factory = Factory::make()
->id(2);

expect($factory->id)->toBe(2);
});
});

describe('type', function () {
it('defaults to empty string', function () {
$factory = Factory::make();

expect($factory->type)->toBe('');
});

it('can set a type', function () {
$factory = Factory::make()
->type('estate');

expect($factory->type)->toBe('estate');
});
});

describe('set', function () {
it('defaults to empty array', function () {
$factory = Factory::make();

expect($factory->elements)->toBe([]);
});

it('can set data', function () {
$factory = Factory::make()
->set('foo', 'bar');

expect($factory->elements)->toBe(['foo' => 'bar']);
});
});

describe('times', function () {
it('defaults to 1', function () {
$factories = Factory::make()
->times();

expect($factories)->toBeArray()
->toHaveCount(1);
});

it('can make multiple times', function () {
$factories = Factory::make()
->times(2);

expect($factories)->toBeArray()
->toHaveCount(2);
});
});

describe('__call', function () {
it('can set data', function () {
$factory = Factory::make()
->setFoo('bar');

expect($factory->elements)->toBe(['foo' => 'bar']);
});

it('can set data with multiple words', function () {
$factory = Factory::make()
->setFooBar('baz');

expect($factory->elements)->toBe(['fooBar' => 'baz']);
});

it('will skip if name does not start with set', function () {
$factory = Factory::make()
->fooBar('baz');

expect($factory->elements)->toBe([]);
});
});

describe('data', function () {
it('can set data', function () {
$factory = Factory::make()
->data([
'foo' => 'bar',
]);

expect($factory->elements)->toBe(['foo' => 'bar']);
});

it('can set data multiple times', function () {
$factory = Factory::make()
->data([
'foo' => 'bar',
])
->data([
'baz' => 'qux',
]);

expect($factory->elements)->toBe([
'foo' => 'bar',
'baz' => 'qux',
]);
});

it('can set empty data', function () {
$factory = Factory::make()
->data([]);

expect($factory->elements)->toBe([]);
});
});

0 comments on commit 7e8a27a

Please sign in to comment.