-
Notifications
You must be signed in to change notification settings - Fork 72
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
2 changed files
with
134 additions
and
7 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
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'); | ||
}); |
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