@@ -23,8 +23,8 @@ public function testBuildPath()
23
23
->index (0 )
24
24
->key ('address ' );
25
25
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 ' ));
28
28
}
29
29
30
30
public function testBuildWithFilter ()
@@ -33,7 +33,7 @@ public function testBuildWithFilter()
33
33
$ path = $ path ->key ('users ' )
34
34
->filter ('@.age > 18 ' );
35
35
36
- $ this ->assertSame ('$. users[?(@.age > 18)] ' , (string ) $ path );
36
+ $ this ->assertSame ('$[" users"] [?(@.age > 18)] ' , (string ) $ path );
37
37
}
38
38
39
39
public function testAll ()
@@ -42,7 +42,7 @@ public function testAll()
42
42
$ path = $ path ->key ('users ' )
43
43
->all ();
44
44
45
- $ this ->assertSame ('$. users[*] ' , (string ) $ path );
45
+ $ this ->assertSame ('$[" users"] [*] ' , (string ) $ path );
46
46
}
47
47
48
48
public function testFirst ()
@@ -51,7 +51,7 @@ public function testFirst()
51
51
$ path = $ path ->key ('users ' )
52
52
->first ();
53
53
54
- $ this ->assertSame ('$. users[0] ' , (string ) $ path );
54
+ $ this ->assertSame ('$[" users"] [0] ' , (string ) $ path );
55
55
}
56
56
57
57
public function testLast ()
@@ -60,6 +60,47 @@ public function testLast()
60
60
$ path = $ path ->key ('users ' )
61
61
->last ();
62
62
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\000\037\036char"] ' ];
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\000char"] ' ];
102
+ yield ["newline \nkey " , '$["newline \\nkey"] ' ];
103
+ yield ["tab \tkey " , '$["tab \\tkey"] ' ];
104
+ yield ["carriage \rreturn " , '$["carriage \\rreturn"] ' ];
64
105
}
65
106
}
0 commit comments