|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Syndesi\CypherDataStructures\Tests\Helper; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Syndesi\CypherDataStructures\Exception\InvalidArgumentException; |
| 9 | +use Syndesi\CypherDataStructures\Helper\ToStringHelper; |
| 10 | + |
| 11 | +class ToStringHelperTest extends TestCase |
| 12 | +{ |
| 13 | + public function testMustNameBeEscaped(): void |
| 14 | + { |
| 15 | + $this->assertFalse(ToStringHelper::mustNameBeEscaped('abc')); |
| 16 | + $this->assertFalse(ToStringHelper::mustNameBeEscaped('Abc')); |
| 17 | + $this->assertFalse(ToStringHelper::mustNameBeEscaped('ABC')); |
| 18 | + $this->assertFalse(ToStringHelper::mustNameBeEscaped('abc123')); |
| 19 | + $this->assertFalse(ToStringHelper::mustNameBeEscaped('abc_123')); |
| 20 | + $this->assertTrue(ToStringHelper::mustNameBeEscaped('123')); |
| 21 | + $this->assertTrue(ToStringHelper::mustNameBeEscaped('abc.abc')); |
| 22 | + $this->assertTrue(ToStringHelper::mustNameBeEscaped('abc abc')); |
| 23 | + } |
| 24 | + |
| 25 | + public function escapeStringProvider(): array |
| 26 | + { |
| 27 | + return [ |
| 28 | + ['hello world', 'hello world'], |
| 29 | + ["hello ' world", "hello \' world"], // hello ' world |
| 30 | + ["hello \' world", "hello \' world"], // hello \' world |
| 31 | + ["hello \\' world", "hello \' world"], // hello \' world |
| 32 | + ["hello \\\' world", "hello \\\\\' world"], // hello \\' world |
| 33 | + ["hello \\\\' world", "hello \\\\\' world"], // hello \\' world |
| 34 | + ["hello \\\\\' world", "hello \\\\\' world"], // hello \\\' world |
| 35 | + ["hello \\\\\\' world", "hello \\\\\' world"], // hello \\\' world |
| 36 | + ["hello \\\\\\\' world", "hello \\\\\\\\\' world"], // hello \\\\' world |
| 37 | + ["hello \\\\\\\\' world", "hello \\\\\\\\\' world"], // hello \\\\' world |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @dataProvider escapeStringProvider |
| 43 | + */ |
| 44 | + public function testEscapeCharacter(string $string, string $output): void |
| 45 | + { |
| 46 | + $result = ToStringHelper::escapeString($string); |
| 47 | + $this->assertSame($output, $result); |
| 48 | + } |
| 49 | + |
| 50 | + public function testInvalidEscapeCharacter(): void |
| 51 | + { |
| 52 | + if (false !== getenv("LEAK")) { |
| 53 | + $this->markTestSkipped(); |
| 54 | + } |
| 55 | + $this->expectExceptionMessage('Escape character must be of length 1, got \'--\''); |
| 56 | + $this->expectException(InvalidArgumentException::class); |
| 57 | + ToStringHelper::escapeString('some string', '--'); |
| 58 | + } |
| 59 | + |
| 60 | + public function valueToStringProvider(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + [null, 'null'], |
| 64 | + [true, 'true'], |
| 65 | + [false, 'false'], |
| 66 | + [0, '0'], |
| 67 | + [123, '123'], |
| 68 | + [1.23, '1.23'], |
| 69 | + ['some string', "'some string'"], |
| 70 | + ['some \'string', "'some \'string'"], |
| 71 | + [[1, 2, 3], '[1, 2, 3]'], |
| 72 | + [[1, 3, 2], '[1, 2, 3]'], |
| 73 | + [[0, null, 'hi', 'abc'], "[0, null, 'abc', 'hi']"], |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @dataProvider valueToStringProvider |
| 79 | + */ |
| 80 | + public function testValueToString($value, $string): void |
| 81 | + { |
| 82 | + $this->assertSame($string, ToStringHelper::valueToString($value)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testPropertyArrayToString(): void |
| 86 | + { |
| 87 | + $properties = [ |
| 88 | + 'int' => 123, |
| 89 | + 'float' => 123.4, |
| 90 | + 'string' => 'string', |
| 91 | + 'stringWithSpace' => 'hello world', |
| 92 | + 'stringWithDot' => 'hello.world', |
| 93 | + 'stringWithBacktick' => 'hello\'world', |
| 94 | + 'array' => ['a', 'b', 'c'], |
| 95 | + 'problematic .\' name' => 'hi :D', |
| 96 | + ]; |
| 97 | + $this->assertSame("array: ['a', 'b', 'c'], float: 123.4, int: 123, `problematic .\' name`: 'hi :D', string: 'string', stringWithBacktick: 'hello\'world', stringWithDot: 'hello.world', stringWithSpace: 'hello world'", ToStringHelper::propertyArrayToString($properties)); |
| 98 | + } |
| 99 | + |
| 100 | + public function testLabelsToString(): void |
| 101 | + { |
| 102 | + $labels = ['a', 'z', 'b', 'E', '_c', '012', 'problematic label', '#label']; |
| 103 | + $this->assertSame(":`#label`:`012`:E:_c:a:b:`problematic label`:z", ToStringHelper::labelsToString($labels)); |
| 104 | + } |
| 105 | +} |
0 commit comments