Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvlpp committed Sep 25, 2024
1 parent a8e21b9 commit 9936fa8
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 7 deletions.
134 changes: 134 additions & 0 deletions tests/Http/Context/SharpContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

use Code16\Sharp\EntityList\Filters\EntityListSelectFilter;
use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity;
use Code16\Sharp\Tests\Fixtures\Sharp\PersonList;
use Code16\Sharp\Tests\Unit\Utils\FakesBreadcrumb;
use Code16\Sharp\Utils\Entities\SharpEntity;

uses(FakesBreadcrumb::class);

it('allows to get form update state from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/1/s-form/child/2');

expect(sharp()->context())
->isForm()->toBeTrue()
->isUpdate()->toBeTrue()
->isCreation()->toBeFalse()
->isShow()->toBeFalse()
->isEntityList()->toBeFalse();
});

it('allows to get form creation state from request', function () {
// We have to define "child" as a non-single form
app()->bind('child_entity', fn () => new class extends SharpEntity {});
sharp()->config()->addEntity('child', 'child_entity');

$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/1/s-form/child');

expect(sharp()->context())
->isForm()->toBeTrue()
->isUpdate()->toBeFalse()
->isCreation()->toBeTrue()
->isShow()->toBeFalse()
->isEntityList()->toBeFalse();
});

it('allows to get show state from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/1');

expect(sharp()->context())
->isForm()->toBeFalse()
->isUpdate()->toBeFalse()
->isCreation()->toBeFalse()
->isShow()->toBeTrue()
->isEntityList()->toBeFalse();
});

it('allows to get entity list state from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person');

expect(sharp()->context())
->isForm()->toBeFalse()
->isUpdate()->toBeFalse()
->isCreation()->toBeFalse()
->isShow()->toBeFalse()
->isEntityList()->toBeTrue();
});

it('allows to get current breadcrumb item from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/1/s-form/child/2');

expect(sharp()->context()->breadcrumb()->currentSegment())
->isForm()->toBeTrue()
->isSingleForm()->toBeFalse()
->entityKey()->toBe('child')
->instanceId()->toEqual(2);
});

it('allows to get previous show from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/42/s-form/child/2');

expect(sharp()->context()->breadcrumb()->previousShowSegment())
->entityKey()->toBe('person')
->instanceId()->toEqual(42);
});

it('allows to get previous show of a given key from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/31/s-show/person/42/s-show/child/84/s-form/child/84');

expect(sharp()->context()->breadcrumb())
->previousShowSegment()->entityKey()->toBe('child')
->previousShowSegment()->instanceId()->toEqual(84)
->previousShowSegment('person')->entityKey()->toBe('person')
->previousShowSegment('person')->instanceId()->toEqual(42);
});

it('allows to get previous url from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/42/s-form/child/2');

expect(sharp()->context()->breadcrumb()->getUrlOfPreviousSegment())
->toEqual(url('/sharp/s-list/person/s-show/person/42'));
});

it('allow to retrieve retained filters value in the context', function () {
sharp()->config()->addEntity('person', PersonEntity::class);
login();

fakeListFor('person', new class extends PersonList
{
protected function getFilters(): ?array
{
return [
new class extends EntityListSelectFilter
{
public function buildFilterConfig(): void
{
$this->configureKey('job')
->configureRetainInSession();
}

public function values(): array
{
return [
'physicist' => 'Physicist',
'physician' => 'Physician',
];
}
},
];
}
});

expect(sharp()->context()->retainedFilterValue('job'))->toBeNull();

$this
->withoutExceptionHandling()
->post(route('code16.sharp.list.filters.store', ['entityKey' => 'person']), [
'filterValues' => [
'job' => 'physicist',
],
]);

expect(sharp()->context()->retainedFilterValue('job'))->toEqual('physicist');
});
7 changes: 0 additions & 7 deletions tests/Unit/Utils/CurrentSharpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,3 @@
->and(currentSharpRequest()->getPreviousShowFromBreadcrumbItems('person')->entityKey())->toBe('person')
->and(currentSharpRequest()->getPreviousShowFromBreadcrumbItems('person')->instanceId())->toEqual(42);
});

it('allows to get previous url from request', function () {
$this->fakeBreadcrumbWithUrl('/sharp/s-list/person/s-show/person/42/s-form/child/2');

expect(currentSharpRequest()->getUrlOfPreviousBreadcrumbItem())
->toEqual(url('/sharp/s-list/person/s-show/person/42'));
});

0 comments on commit 9936fa8

Please sign in to comment.