@@ -23,8 +23,8 @@ public function testBuildPath()
2323 ->index (0 )
2424 ->key ('address ' );
2525
26- $ this ->assertSame ('$. users[0]. address ' , (string ) $ path );
27- $ this ->assertSame ('$. users[0]. address.. city ' , (string ) $ path ->deepScan ()->key ('city ' ));
26+ $ this ->assertSame ('$[" users"] [0][" address"] ' , (string ) $ path );
27+ $ this ->assertSame ('$[" users"] [0][" address"]..[" city"] ' , (string ) $ path ->deepScan ()->key ('city ' ));
2828 }
2929
3030 public function testBuildWithFilter ()
@@ -33,7 +33,7 @@ public function testBuildWithFilter()
3333 $ path = $ path ->key ('users ' )
3434 ->filter ('@.age > 18 ' );
3535
36- $ this ->assertSame ('$. users[?(@.age > 18)] ' , (string ) $ path );
36+ $ this ->assertSame ('$[" users"] [?(@.age > 18)] ' , (string ) $ path );
3737 }
3838
3939 public function testAll ()
@@ -42,7 +42,7 @@ public function testAll()
4242 $ path = $ path ->key ('users ' )
4343 ->all ();
4444
45- $ this ->assertSame ('$. users[*] ' , (string ) $ path );
45+ $ this ->assertSame ('$[" users"] [*] ' , (string ) $ path );
4646 }
4747
4848 public function testFirst ()
@@ -51,7 +51,7 @@ public function testFirst()
5151 $ path = $ path ->key ('users ' )
5252 ->first ();
5353
54- $ this ->assertSame ('$. users[0] ' , (string ) $ path );
54+ $ this ->assertSame ('$[" users"] [0] ' , (string ) $ path );
5555 }
5656
5757 public function testLast ()
@@ -60,6 +60,47 @@ public function testLast()
6060 $ path = $ path ->key ('users ' )
6161 ->last ();
6262
63- $ this ->assertSame ('$.users[-1] ' , (string ) $ path );
63+ $ this ->assertSame ('$["users"][-1] ' , (string ) $ path );
64+ }
65+
66+ /**
67+ * @dataProvider provideKeysToEscape
68+ */
69+ public function testEscapedKey (string $ key , string $ expectedPath )
70+ {
71+ $ path = new JsonPath ();
72+ $ path = $ path ->key ($ key );
73+
74+ $ this ->assertSame ($ expectedPath , (string ) $ path );
75+ }
76+
77+ public static function provideKeysToEscape (): iterable
78+ {
79+ yield ['simple_key ' , '$["simple_key"] ' ];
80+ yield ['key"with"quotes ' , '$["key \\"with \\"quotes"] ' ];
81+ yield ['path \\backslash ' , '$["path \\backslash"] ' ];
82+ yield ['mixed \\"case ' , '$["mixed \\\\\\"case"] ' ];
83+ yield ['unicode_🔑 ' , '$["unicode_🔑"] ' ];
84+ yield ['"quotes_only" ' , '$[" \\"quotes_only \\""] ' ];
85+ yield ['\\\\multiple \\\\backslashes ' , '$[" \\\\\\\\multiple \\\\\\backslashes"] ' ];
86+ yield ["control \x00\x1f\x1echar " , '$["control\u0000\u001f\u001echar"] ' ];
87+
88+ yield ['key"with \\"mixed ' , '$["key \\"with \\\\\\"mixed"] ' ];
89+ yield ['\\"complex \\"case \\" ' , '$[" \\\\\\"complex \\\\\\"case \\\\\\""] ' ];
90+ yield ['json_like":{"value":"test"} ' , '$["json_like \\":{ \\"value \\": \\"test \\"}"] ' ];
91+ yield ['C: \\Program Files \\"App Name" ' , '$["C: \\\\Program Files \\\\\\"App Name \\""] ' ];
92+
93+ yield ['key_with_é_accents ' , '$["key_with_é_accents"] ' ];
94+ yield ['unicode_→_arrows ' , '$["unicode_→_arrows"] ' ];
95+ yield ['chinese_中文_key ' , '$["chinese_中文_key"] ' ];
96+
97+ yield ['' , '$[""] ' ];
98+ yield [' ' , '$[" "] ' ];
99+ yield [' spaces ' , '$[" spaces "] ' ];
100+ yield ["\t\n\r" , '$[" \\t \\n \\r"] ' ];
101+ yield ["control \x00char " , '$["control\u0000char"] ' ];
102+ yield ["newline \nkey " , '$["newline \\nkey"] ' ];
103+ yield ["tab \tkey " , '$["tab \\tkey"] ' ];
104+ yield ["carriage \rreturn " , '$["carriage \\rreturn"] ' ];
64105 }
65106}
0 commit comments