1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Guiziweb \SyliusGridAssistantPlugin \Tests \Unit \Schema ;
6+
7+ use Guiziweb \SyliusGridAssistantPlugin \Schema \Builder \FilterSchemaBuilderInterface ;
8+ use Guiziweb \SyliusGridAssistantPlugin \Schema \Builder \FilterSchemaBuilderRegistry ;
9+ use Guiziweb \SyliusGridAssistantPlugin \Schema \GridSchemaBuilder ;
10+ use PHPUnit \Framework \TestCase ;
11+ use Sylius \Component \Grid \Definition \Field ;
12+ use Sylius \Component \Grid \Definition \Filter ;
13+ use Sylius \Component \Grid \Definition \Grid ;
14+ use Sylius \Component \Grid \Provider \GridProviderInterface ;
15+ use Symfony \Contracts \Translation \TranslatorInterface ;
16+
17+ final class GridSchemaBuilderTest extends TestCase
18+ {
19+ public function testFiltersWithAiSearchableFalseAreExcluded (): void
20+ {
21+ $ grid = $ this ->makeGrid ();
22+ $ grid ->addFilter ($ this ->makeFilter ('customer ' , 'string ' , ['ai_searchable ' => true ]));
23+ $ grid ->addFilter ($ this ->makeFilter ('internal_notes ' , 'string ' , ['ai_searchable ' => false ]));
24+ $ grid ->addFilter ($ this ->makeFilter ('date ' , 'date ' ));
25+
26+ $ schema = $ this ->makeBuilder ($ grid )->buildSchema ('any ' );
27+
28+ self ::assertArrayHasKey ('customer ' , $ schema ['filters ' ]);
29+ self ::assertArrayHasKey ('date ' , $ schema ['filters ' ]);
30+ self ::assertArrayNotHasKey ('internal_notes ' , $ schema ['filters ' ]);
31+ }
32+
33+ public function testSortableFieldsWithAiSearchableFalseAreExcluded (): void
34+ {
35+ $ grid = $ this ->makeGrid ();
36+ $ grid ->addField ($ this ->makeField ('number ' , sortable: 'number ' ));
37+ $ grid ->addField ($ this ->makeField ('internal_id ' , sortable: 'internalId ' , options: ['ai_searchable ' => false ]));
38+ $ grid ->addField ($ this ->makeField ('total ' , sortable: 'total ' , options: ['ai_searchable ' => true ]));
39+ $ grid ->addField ($ this ->makeField ('actions ' , sortable: null ));
40+
41+ $ schema = $ this ->makeBuilder ($ grid )->buildSchema ('any ' );
42+
43+ self ::assertArrayHasKey ('number ' , $ schema ['sortable_fields ' ]);
44+ self ::assertArrayHasKey ('total ' , $ schema ['sortable_fields ' ]);
45+ self ::assertArrayNotHasKey ('internal_id ' , $ schema ['sortable_fields ' ]);
46+ self ::assertArrayNotHasKey ('actions ' , $ schema ['sortable_fields ' ]);
47+ }
48+
49+ private function makeGrid (): Grid
50+ {
51+ return Grid::fromCodeAndDriverConfiguration ('test_grid ' , 'doctrine/orm ' , ['class ' => 'Foo ' ]);
52+ }
53+
54+ /**
55+ * @param array<string, mixed> $options
56+ */
57+ private function makeFilter (string $ name , string $ type , array $ options = []): Filter
58+ {
59+ $ filter = Filter::fromNameAndType ($ name , $ type );
60+ $ filter ->setOptions ($ options );
61+
62+ return $ filter ;
63+ }
64+
65+ /**
66+ * @param array<string, mixed> $options
67+ */
68+ private function makeField (string $ name , ?string $ sortable , array $ options = []): Field
69+ {
70+ $ field = Field::fromNameAndType ($ name , 'string ' );
71+ $ field ->setSortable ($ sortable );
72+ $ field ->setLabel ($ name );
73+ $ field ->setOptions ($ options );
74+
75+ return $ field ;
76+ }
77+
78+ private function makeBuilder (Grid $ grid ): GridSchemaBuilder
79+ {
80+ $ gridProvider = $ this ->createMock (GridProviderInterface::class);
81+ $ gridProvider ->method ('get ' )->willReturn ($ grid );
82+
83+ $ registry = new FilterSchemaBuilderRegistry ();
84+ $ registry ->register (new class implements FilterSchemaBuilderInterface {
85+ public static function getType (): string |array
86+ {
87+ return ['string ' , 'date ' ];
88+ }
89+
90+ public function build (Filter $ filter ): array
91+ {
92+ return ['type ' => 'string ' ];
93+ }
94+ });
95+
96+ $ translator = $ this ->createMock (TranslatorInterface::class);
97+ $ translator ->method ('trans ' )->willReturnArgument (0 );
98+
99+ return new GridSchemaBuilder ($ gridProvider , $ registry , $ translator );
100+ }
101+ }
0 commit comments