1818use Sylius \Component \Grid \Definition \Field ;
1919use Sylius \Component \Grid \Exception \UnexpectedValueException ;
2020use Sylius \Component \Grid \FieldTypes \FieldTypeInterface ;
21+ use Symfony \Contracts \Service \ServiceLocatorTrait ;
22+ use Symfony \Contracts \Service \ServiceProviderInterface ;
2123
2224final class CallableFieldTypeSpec extends ObjectBehavior
2325{
2426 function let (DataExtractorInterface $ dataExtractor ): void
2527 {
26- $ this ->beConstructedWith ($ dataExtractor );
28+ $ this ->beConstructedWith (
29+ $ dataExtractor ,
30+ new class ([
31+ 'my_service ' => fn () => new class {
32+ public function __invoke (string $ value ): string { return strtoupper ($ value ); }
33+ public function concatenate (array $ value ): string { return implode (', ' , $ value ); }
34+ },
35+ ]) implements ServiceProviderInterface {
36+ use ServiceLocatorTrait;
37+ },
38+ );
2739 }
2840
2941 function it_is_a_grid_field_type (): void
@@ -79,6 +91,31 @@ function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_static_callabl
7991 ])->shouldReturn ('bar ' );
8092 }
8193
94+ function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_service (
95+ DataExtractorInterface $ dataExtractor ,
96+ Field $ field ,
97+ ): void {
98+ $ dataExtractor ->get ($ field , ['foo ' => 'bar ' ])->willReturn ('bar ' );
99+
100+ $ this ->render ($ field , ['foo ' => 'bar ' ], [
101+ 'service ' => 'my_service ' ,
102+ 'htmlspecialchars ' => true ,
103+ ])->shouldReturn ('BAR ' );
104+ }
105+
106+ function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_service_and_method (
107+ DataExtractorInterface $ dataExtractor ,
108+ Field $ field ,
109+ ): void {
110+ $ dataExtractor ->get ($ field , ['foo ' => ['foo ' , 'bar ' , 'foobar ' ]])->willReturn (['foo ' , 'bar ' , 'foobar ' ]);
111+
112+ $ this ->render ($ field , ['foo ' => ['foo ' , 'bar ' , 'foobar ' ]], [
113+ 'service ' => 'my_service ' ,
114+ 'method ' => 'concatenate ' ,
115+ 'htmlspecialchars ' => true ,
116+ ])->shouldReturn ('foo, bar, foobar ' );
117+ }
118+
82119 function it_throws_an_exception_when_a_callable_return_value_cannot_be_casted_to_string (
83120 DataExtractorInterface $ dataExtractor ,
84121 Field $ field ,
@@ -98,6 +135,35 @@ function it_throws_an_exception_when_a_callable_return_value_cannot_be_casted_to
98135 ]);
99136 }
100137
138+ function it_throws_an_exception_when_neither_callable_nor_service_options_are_defined (
139+ DataExtractorInterface $ dataExtractor ,
140+ Field $ field ,
141+ ): void {
142+ $ this
143+ ->shouldThrow (\RuntimeException::class)
144+ ->during ('render ' , [
145+ $ field ,
146+ ['foo ' => 'bar ' ],
147+ [],
148+ ]);
149+ }
150+
151+ function it_throws_an_exception_when_both_callable_and_service_options_are_defined (
152+ DataExtractorInterface $ dataExtractor ,
153+ Field $ field ,
154+ ): void {
155+ $ this
156+ ->shouldThrow (\RuntimeException::class)
157+ ->during ('render ' , [
158+ $ field ,
159+ ['foo ' => 'bar ' ],
160+ [
161+ 'callable ' => fn () => new \stdclass (),
162+ 'service ' => 'my_service '
163+ ],
164+ ]);
165+ }
166+
101167 static function callable (mixed $ value ): string
102168 {
103169 return strtolower ($ value );
0 commit comments