File tree Expand file tree Collapse file tree 3 files changed +22
-10
lines changed Expand file tree Collapse file tree 3 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -33,18 +33,16 @@ trait EscapeTrait
3333{
3434 /**
3535 * Escapes a 'name' if it needs to be escaped.
36+ * @see https://neo4j.com/docs/cypher-manual/4.4/syntax/naming
3637 * A 'name' in cypher is any string that should be included directly in a cypher query,
3738 * such as variable names, labels, property names and relation types
3839 *
39- * Note that empty strings are usually not allowed as names, so these should not be passed to this function.
40- * However, some neo4j versions do not crash on empty string as variable name, so let's just escape them anyways.
41- *
4240 * @param string $name
4341 * @return string
4442 */
4543 public static function escape (string $ name ): string
4644 {
47- if ($ name !== '' && preg_match ('/^\p{L}[\p{L}\d_]*$/u ' , $ name )) {
45+ if (preg_match ('/^\p{L}[\p{L}\d_]*$/u ' , $ name )) {
4846 return $ name ;
4947 }
5048
@@ -57,7 +55,6 @@ public static function escape(string $name): string
5755 */
5856 public static function escapeRaw ($ name )
5957 {
60-
6158 // Escape backticks that are included in $name by doubling them.
6259 $ name = str_replace ('` ' , '`` ' , $ name );
6360
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ public function testEmptyNode(): void
4848 $ this ->assertSame ($ name , $ node ->getVariable ());
4949 }
5050
51+ // Further tests can be found in Traits/EscapeTraitTest
5152 public function testBacktickIsEscaped (): void
5253 {
5354 $ node = new Node ();
Original file line number Diff line number Diff line change @@ -65,11 +65,6 @@ public function testUnsafeValueIsEscaped(string $value)
6565 $ this ->assertSame ($ expected , $ actual );
6666 }
6767
68- public function testValueWithBacktickIsProperlyEscaped ()
69- {
70- $ this ->assertSame ('`foo``bar` ' , $ this ->trait ->escape ("foo`bar " ));
71- }
72-
7368 public function provideSafeValueIsNotEscapedData (): array
7469 {
7570 return [
@@ -104,4 +99,23 @@ public function provideUnsafeValueIsEscapedData(): array
10499 ['2 ' ],
105100 ];
106101 }
102+
103+ /**
104+ * @dataProvider provideValueWithBacktickIsProperlyEscapedData
105+ */
106+ public function testValueWithBacktickIsProperlyEscaped ($ input , $ expected )
107+ {
108+ $ this ->assertSame ('`foo``bar` ' , $ this ->trait ->escape ("foo`bar " ));
109+ }
110+
111+ public function provideValueWithBacktickIsProperlyEscapedData (): array
112+ {
113+ return [
114+ ['foo`bar ' ,'`foo``bar` ' ],
115+ ['`foo ' ,'```foo` ' ],
116+ ['foo` ' ,'`foo``` ' ],
117+ ['foo``bar ' ,'`foo````bar` ' ],
118+ ['`foo` ' ,'```foo``` ' ],
119+ ];
120+ }
107121}
You can’t perform that action at this time.
0 commit comments