1111use JMS \Serializer \SerializerInterface ;
1212use Pagerfanta \Adapter \FixedAdapter ;
1313use Pagerfanta \Pagerfanta ;
14+ use PHPUnit \Framework \Attributes \DataProvider ;
1415use PHPUnit \Framework \TestCase ;
1516
17+ /**
18+ * @note The {@see PagerfantaHandler::PRESERVE_KEYS_KEY} constant value is inlined to avoid autoloader issues when the JMS packages are not installed
19+ */
1620final class PagerfantaHandlerTest extends TestCase
1721{
22+ public static function setUpBeforeClass (): void
23+ {
24+ if (!class_exists (SerializerBuilder::class)) {
25+ self ::markTestSkipped ('Test requires JMS Serializer ' );
26+ }
27+ }
28+
1829 public function testSerializeToJson (): void
1930 {
2031 $ pager = new Pagerfanta (new FixedAdapter (100 , range (1 , 5 )));
@@ -33,14 +44,15 @@ public static function dataSerializeWithPreserveKeysContext(): \Generator
3344 {
3445 yield 'Context not set ' => [[0 => 'item1 ' , 2 => 'item2 ' , 4 => 'item3 ' ], [], '{"items":{"0":"item1","2":"item2","4":"item3"},"pagination":{"current_page":1,"has_previous_page":false,"has_next_page":false,"per_page":10,"total_items":3,"total_pages":1}} ' ];
3546
36- yield 'Context with preserve keys disabled ' => [[0 => 'item1 ' , 2 => 'item2 ' , 4 => 'item3 ' ], [PagerfantaHandler:: PRESERVE_KEYS_KEY => false ], '{"items":["item1","item2","item3"],"pagination":{"current_page":1,"has_previous_page":false,"has_next_page":false,"per_page":10,"total_items":3,"total_pages":1}} ' ];
47+ yield 'Context with preserve keys disabled ' => [[0 => 'item1 ' , 2 => 'item2 ' , 4 => 'item3 ' ], [' pagerfanta_preserve_keys ' => false ], '{"items":["item1","item2","item3"],"pagination":{"current_page":1,"has_previous_page":false,"has_next_page":false,"per_page":10,"total_items":3,"total_pages":1}} ' ];
3748
38- yield 'Context with preserve keys enabled ' => [[0 => 'item1 ' , 2 => 'item2 ' , 4 => 'item3 ' ], [PagerfantaHandler:: PRESERVE_KEYS_KEY => true ], '{"items":{"0":"item1","2":"item2","4":"item3"},"pagination":{"current_page":1,"has_previous_page":false,"has_next_page":false,"per_page":10,"total_items":3,"total_pages":1}} ' ];
49+ yield 'Context with preserve keys enabled ' => [[0 => 'item1 ' , 2 => 'item2 ' , 4 => 'item3 ' ], [' pagerfanta_preserve_keys ' => true ], '{"items":{"0":"item1","2":"item2","4":"item3"},"pagination":{"current_page":1,"has_previous_page":false,"has_next_page":false,"per_page":10,"total_items":3,"total_pages":1}} ' ];
3950 }
4051
4152 /**
4253 * @dataProvider dataSerializeWithPreserveKeysContext
4354 */
55+ #[DataProvider('dataSerializeWithPreserveKeysContext ' )]
4456 public function testSerializeToJsonWithPreserveKeysContext (array $ data , array $ context , string $ expectedJson ): void
4557 {
4658 $ pager = new Pagerfanta (new FixedAdapter (\count ($ data ), $ data ));
@@ -66,7 +78,7 @@ public function testNormalizeRejectsInvalidPreserveKeysContext(): void
6678 $ pager ->setMaxPerPage (5 );
6779
6880 $ serializationContext = new SerializationContext ();
69- $ serializationContext ->setAttribute (PagerfantaHandler:: PRESERVE_KEYS_KEY , 'invalid ' );
81+ $ serializationContext ->setAttribute (' pagerfanta_preserve_keys ' , 'invalid ' );
7082
7183 $ this ->createSerializer ()->serialize ($ pager , 'json ' , $ serializationContext );
7284 }
0 commit comments