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 (['my_service ' => fn () => new class () {
31+ public function __invoke (string $ value ): string
32+ {
33+ return strtoupper ($ value );
34+ }
35+
36+ public function concatenate (array $ value ): string
37+ {
38+ return implode (', ' , $ value );
39+ }
40+ },
41+ ]) implements ServiceProviderInterface {
42+ use ServiceLocatorTrait;
43+ },
44+ );
2745 }
2846
2947 function it_is_a_grid_field_type (): void
@@ -79,6 +97,31 @@ function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_static_callabl
7997 ])->shouldReturn ('bar ' );
8098 }
8199
100+ function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_service (
101+ DataExtractorInterface $ dataExtractor ,
102+ Field $ field ,
103+ ): void {
104+ $ dataExtractor ->get ($ field , ['foo ' => 'bar ' ])->willReturn ('bar ' );
105+
106+ $ this ->render ($ field , ['foo ' => 'bar ' ], [
107+ 'service ' => 'my_service ' ,
108+ 'htmlspecialchars ' => true ,
109+ ])->shouldReturn ('BAR ' );
110+ }
111+
112+ function it_uses_data_extractor_to_obtain_data_and_passes_it_to_a_service_and_method (
113+ DataExtractorInterface $ dataExtractor ,
114+ Field $ field ,
115+ ): void {
116+ $ dataExtractor ->get ($ field , ['foo ' => ['foo ' , 'bar ' , 'foobar ' ]])->willReturn (['foo ' , 'bar ' , 'foobar ' ]);
117+
118+ $ this ->render ($ field , ['foo ' => ['foo ' , 'bar ' , 'foobar ' ]], [
119+ 'service ' => 'my_service ' ,
120+ 'method ' => 'concatenate ' ,
121+ 'htmlspecialchars ' => true ,
122+ ])->shouldReturn ('foo, bar, foobar ' );
123+ }
124+
82125 function it_throws_an_exception_when_a_callable_return_value_cannot_be_casted_to_string (
83126 DataExtractorInterface $ dataExtractor ,
84127 Field $ field ,
@@ -98,6 +141,35 @@ function it_throws_an_exception_when_a_callable_return_value_cannot_be_casted_to
98141 ]);
99142 }
100143
144+ function it_throws_an_exception_when_neither_callable_nor_service_options_are_defined (
145+ DataExtractorInterface $ dataExtractor ,
146+ Field $ field ,
147+ ): void {
148+ $ this
149+ ->shouldThrow (\RuntimeException::class)
150+ ->during ('render ' , [
151+ $ field ,
152+ ['foo ' => 'bar ' ],
153+ [],
154+ ]);
155+ }
156+
157+ function it_throws_an_exception_when_both_callable_and_service_options_are_defined (
158+ DataExtractorInterface $ dataExtractor ,
159+ Field $ field ,
160+ ): void {
161+ $ this
162+ ->shouldThrow (\RuntimeException::class)
163+ ->during ('render ' , [
164+ $ field ,
165+ ['foo ' => 'bar ' ],
166+ [
167+ 'callable ' => fn () => new \stdclass (),
168+ 'service ' => 'my_service ' ,
169+ ],
170+ ]);
171+ }
172+
101173 static function callable (mixed $ value ): string
102174 {
103175 return strtolower ($ value );
0 commit comments