Skip to content

Commit

Permalink
feat: add estate respository fake
Browse files Browse the repository at this point in the history
  • Loading branch information
Katalam committed May 13, 2024
1 parent 6775e50 commit d785b73
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ $users = UserRepository::query()
->where('Nr', $this->userId)
->get();
```
### Usage in tests
```php
EstateRepository::fake([ // First request
[ // First page of first request
EstateFactory::make() // First record of first page of first request
->id(1)
->set('foo', 'bar'),
],
], [ // Second request
[ // First page of second request
EstateFactory::make() // First record of first page of second request
->id(2)
->set('foo', 'baz'),
EstateFactory::make() // Second record of first page of second request
->id(3),
],
]);

// request as normal
$estates = EstateRepository::query()->get();

expect($estates)->toHaveCount(1)
->and($estates->first()['id'])->toBe(1);

$estates = EstateRepository::query()->get();

expect($estates)->toHaveCount(2)
->and($estates->first()['id'])->toBe(2)
->and($estates->last()['id'])->toBe(3);
```

## Testing

Expand Down
8 changes: 8 additions & 0 deletions src/Facades/EstateRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Katalam\OnOfficeAdapter\Facades;

use Illuminate\Support\Facades\Facade;
use Katalam\OnOfficeAdapter\Facades\Testing\EstateRepositoryFake;
use Katalam\OnOfficeAdapter\Query\EstateBuilder;
use Katalam\OnOfficeAdapter\Query\EstateFileBuilder;

Expand All @@ -14,6 +15,13 @@
*/
class EstateRepository extends Facade
{
public static function fake(array ...$fakeResponses): EstateRepositoryFake
{
static::swap($fake = new EstateRepositoryFake(...$fakeResponses));

return $fake;
}

protected static function getFacadeAccessor(): string
{
return \Katalam\OnOfficeAdapter\Repositories\EstateRepository::class;
Expand Down
24 changes: 24 additions & 0 deletions src/Facades/Testing/EstateRepositoryFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades\Testing;

use Illuminate\Support\Collection;
use Katalam\OnOfficeAdapter\Query\Testing\EstateBuilderFake;

class EstateRepositoryFake
{
public Collection $fakeResponses;

public function __construct(array ...$fakeResponses)
{
$this->fakeResponses = collect($fakeResponses);
}

/**
* Returns a new fake estate builder instance.
*/
public function query(): EstateBuilderFake
{
return new EstateBuilderFake($this->fakeResponses);
}
}
52 changes: 52 additions & 0 deletions src/Facades/Testing/RecordFactories/BaseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades\Testing\RecordFactories;

class BaseFactory
{
public int $id = 0;

public string $type = '';

public array $elements = [];

public function id(int $id): self
{
$this->id = $id;

return $this;
}

public function type(string $type): self
{
$this->type = $type;

return $this;
}

public function set(string $key, mixed $value): self
{
$this->elements[$key] = $value;

return $this;
}

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

Check failure on line 36 in src/Facades/Testing/RecordFactories/BaseFactory.php

View workflow job for this annotation

GitHub Actions / phpstan

Unsafe usage of new static().
}

public static function times(int $times): array
{
return array_fill(0, $times, static::make());
}

public function toArray(): array
{
return [
'id' => $this->id,
'type' => $this->type,
'elements' => $this->elements,
];
}
}
8 changes: 8 additions & 0 deletions src/Facades/Testing/RecordFactories/EstateFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Katalam\OnOfficeAdapter\Facades\Testing\RecordFactories;

class EstateFactory extends BaseFactory
{
public string $type = 'estate';
}
65 changes: 65 additions & 0 deletions src/Query/Testing/EstateBuilderFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Katalam\OnOfficeAdapter\Query\Testing;

use Exception;
use Illuminate\Support\Collection;
use Katalam\OnOfficeAdapter\Facades\Testing\RecordFactories\BaseFactory;
use Katalam\OnOfficeAdapter\Query\Builder;
use Throwable;

class EstateBuilderFake extends Builder
{
public function __construct(
public Collection $fakeResponses
) {
}

/**
* @throws Throwable
*/
public function get(): Collection
{
$nextRequest = $this->fakeResponses->shift();
throw_if($nextRequest === null, new Exception('No more fake responses'));

return collect($nextRequest)
->flatten()
->map(fn (BaseFactory $factory) => $factory->toArray());
}

/**
* @throws Throwable
*/
public function first(): array
{
return $this->get()->first();
}

/**
* @throws Throwable
*/
public function find(int $id): array
{
return $this->get()
->first(fn (array $record) => $record['id'] === $id);
}

/**
* @throws Throwable
*/
public function each(callable $callback): void
{
$nextRequest = $this->fakeResponses->shift();
throw_if($nextRequest === null, new Exception('No more fake responses'));

collect($nextRequest)
->each(function (array $factories) use ($callback) {
$records = collect($factories)
->map(fn (BaseFactory $factory) => $factory->toArray())
->toArray();

$callback($records);
});
}
}
126 changes: 126 additions & 0 deletions tests/Testing/EstateRepositoryFakeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

use Katalam\OnOfficeAdapter\Facades\EstateRepository;
use Katalam\OnOfficeAdapter\Facades\Testing\EstateRepositoryFake;
use Katalam\OnOfficeAdapter\Facades\Testing\RecordFactories\EstateFactory;

describe('get', function () {
it('can be faked', function () {
$fake = EstateRepository::fake();

expect($fake)->toBeInstanceOf(EstateRepositoryFake::class);
});

it('can get a fake response', function () {
EstateRepository::fake([
[
EstateFactory::make()
->id(1)
->set('foo', 'bar'),
],
]);

$estates = EstateRepository::query()->get();

expect($estates)->toHaveCount(1)
->and($estates->first()['id'])->toBe(1);
});

it('can get fake responses in pages', function () {
EstateRepository::fake([
[
EstateFactory::make()->id(1),
],
[
EstateFactory::make()->id(2),
EstateFactory::make()->id(3),
],
]);

$estates = EstateRepository::query()->get();

expect($estates)->toHaveCount(3)
->and($estates->first()['id'])->toBe(1)
->and($estates->last()['id'])->toBe(3);
});

it('can get multiple fake responses', function () {
EstateRepository::fake([
[
EstateFactory::make()->id(1),
],
], [
[
EstateFactory::make()->id(2),
EstateFactory::make()->id(3),
],
]);

$estates = EstateRepository::query()->get();

expect($estates)->toHaveCount(1)
->and($estates->first()['id'])->toBe(1);

$estates = EstateRepository::query()->get();

expect($estates)->toHaveCount(2)
->and($estates->first()['id'])->toBe(2)
->and($estates->last()['id'])->toBe(3);
});

it('throws an exception when no more fake responses are available', function () {
EstateRepository::fake([
[
EstateFactory::make()->id(1),
],
]);

EstateRepository::query()->get();

expect(fn () => EstateRepository::query()->get())
->toThrow('No more fake responses');
});
});

describe('find', function () {
it('can find a fake response', function () {
EstateRepository::fake([
[
EstateFactory::make()->id(1),
],
]);

$estate = EstateRepository::query()->find(1);

expect($estate['id'])->toBe(1);
});

it('can find a fake response in pages', function () {
EstateRepository::fake([
[
EstateFactory::make()->id(1),
],
[
EstateFactory::make()->id(2),
EstateFactory::make()->id(3),
],
]);

$estate = EstateRepository::query()->find(3);

expect($estate['id'])->toBe(3);
});

it('throws an exception when no more fake responses are available', function () {
EstateRepository::fake([
[
EstateFactory::make()->id(1),
],
]);

EstateRepository::query()->find(1);

expect(static fn () => EstateRepository::query()->find(1))
->toThrow('No more fake responses');
});
});

0 comments on commit d785b73

Please sign in to comment.