generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |