|
| 1 | +<?php |
| 2 | + |
| 3 | +use Code16\Sharp\Exceptions\SharpInvalidGlobalFilterKeyException; |
| 4 | +use Code16\Sharp\Filters\GlobalRequiredFilter; |
| 5 | +use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity; |
| 6 | +use Inertia\Testing\AssertableInertia as Assert; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + login(); |
| 10 | + sharp()->config()->declareEntity(PersonEntity::class); |
| 11 | +}); |
| 12 | + |
| 13 | +it('does not sends filters of none configured', function () { |
| 14 | + $this |
| 15 | + ->followingRedirects() |
| 16 | + ->get('/sharp/s-list/person') |
| 17 | + ->assertInertia(fn (Assert $page) => $page |
| 18 | + ->where('globalFilters', null) |
| 19 | + ); |
| 20 | +}); |
| 21 | + |
| 22 | +it('does not sends filters of there have no values', function () { |
| 23 | + sharp()->config()->addGlobalFilter( |
| 24 | + new class() extends GlobalRequiredFilter |
| 25 | + { |
| 26 | + public function buildFilterConfig(): void |
| 27 | + { |
| 28 | + $this->configureKey('test-no-values'); |
| 29 | + } |
| 30 | + |
| 31 | + public function values(): array |
| 32 | + { |
| 33 | + return []; |
| 34 | + } |
| 35 | + |
| 36 | + public function defaultValue(): mixed |
| 37 | + { |
| 38 | + return null; |
| 39 | + } |
| 40 | + } |
| 41 | + ); |
| 42 | + |
| 43 | + $this |
| 44 | + ->followingRedirects() |
| 45 | + ->get('/sharp/s-list/person') |
| 46 | + ->assertInertia(fn (Assert $page) => $page |
| 47 | + ->where('globalFilters', null) |
| 48 | + ); |
| 49 | + |
| 50 | + sharp()->config()->addGlobalFilter( |
| 51 | + new class() extends GlobalRequiredFilter |
| 52 | + { |
| 53 | + public function buildFilterConfig(): void |
| 54 | + { |
| 55 | + $this->configureKey('test'); |
| 56 | + } |
| 57 | + |
| 58 | + public function values(): array |
| 59 | + { |
| 60 | + return [ |
| 61 | + 1 => 'One', |
| 62 | + 2 => 'Two', |
| 63 | + 3 => 'Three', |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + public function defaultValue(): mixed |
| 68 | + { |
| 69 | + return 2; |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + $this |
| 75 | + ->followingRedirects() |
| 76 | + ->get('/sharp/3/s-list/person') |
| 77 | + ->assertInertia(fn (Assert $page) => $page |
| 78 | + ->has('globalFilters.config.filters._root', 1) |
| 79 | + ->has('globalFilters.config.filters._root.0', fn (Assert $filter) => $filter |
| 80 | + ->where('key', 'test') |
| 81 | + ->etc() |
| 82 | + ) |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +it('globalFilterValue() throws if global filter is not declared', function () { |
| 87 | + $this |
| 88 | + ->followingRedirects() |
| 89 | + ->get('/sharp/s-list/person'); |
| 90 | + |
| 91 | + expect(fn () => sharp()->context()->globalFilterValue('test'))->toThrow(SharpInvalidGlobalFilterKeyException::class); |
| 92 | +}); |
| 93 | + |
| 94 | +it('globalFilterValue() returns null if global filter is not authorized', function () { |
| 95 | + sharp()->config()->addGlobalFilter( |
| 96 | + new class() extends GlobalRequiredFilter |
| 97 | + { |
| 98 | + public function buildFilterConfig(): void |
| 99 | + { |
| 100 | + $this->configureKey('test'); |
| 101 | + } |
| 102 | + |
| 103 | + public function values(): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + 1 => 'One', |
| 107 | + 2 => 'Two', |
| 108 | + 3 => 'Three', |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + public function defaultValue(): mixed |
| 113 | + { |
| 114 | + return 2; |
| 115 | + } |
| 116 | + |
| 117 | + public function authorize(): bool |
| 118 | + { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + $this->get(route('code16.sharp.list', ['globalFilter' => 'root', 'entityKey' => 'person'])); |
| 125 | + |
| 126 | + expect(sharp()->context()->globalFilterValue('test'))->toBeNull(); |
| 127 | +}); |
| 128 | + |
| 129 | +it('globalFilterValue() returns null if global filter has no values', function () { |
| 130 | + sharp()->config()->addGlobalFilter( |
| 131 | + new class() extends GlobalRequiredFilter |
| 132 | + { |
| 133 | + public function buildFilterConfig(): void |
| 134 | + { |
| 135 | + $this->configureKey('test'); |
| 136 | + } |
| 137 | + |
| 138 | + public function values(): array |
| 139 | + { |
| 140 | + return []; |
| 141 | + } |
| 142 | + |
| 143 | + public function defaultValue(): mixed |
| 144 | + { |
| 145 | + return 2; |
| 146 | + } |
| 147 | + } |
| 148 | + ); |
| 149 | + |
| 150 | + $this->get(route('code16.sharp.list', ['globalFilter' => 'root', 'entityKey' => 'person'])); |
| 151 | + |
| 152 | + expect(sharp()->context()->globalFilterValue('test'))->toBeNull(); |
| 153 | +}); |
0 commit comments