diff --git a/tests/Phing/Util/StringHelperTest.php b/tests/Phing/Util/StringHelperTest.php index e1db83e71b..1e50d7db8e 100644 --- a/tests/Phing/Util/StringHelperTest.php +++ b/tests/Phing/Util/StringHelperTest.php @@ -135,4 +135,178 @@ public function isBooleanProvider() [null, false], ]; } + + /** + * @dataProvider startsWithProvider + * @covers \Phing\Util\StringHelper::startsWith + */ + public function testStartsWith($needle, $haystack, $expected) + { + $result = StringHelper::startsWith($needle, $haystack); + $this->assertSame($expected, $result); + } + + public function startsWithProvider() + { + return [ + // True + ['F', 'FooBarBaz', true], + ['Foo', 'FooBarBaz', true], + ['FooBarBaz', 'FooBarBaz', true], + ['', 'FooBarBaz', true], + ['', "\x00", true], + ["\x00", "\x00", true], + ["\x00", "\x00a", true], + ["a\x00b", "a\x00bc", true], + // False + ['Foo', 'BarBaz', false], + ['foo', 'FooBarBaz', false], + ['Foo', 'fooBarBaz', false], + ['Foo', '', false], + ]; + } + + /** + * @dataProvider endsWithProvider + * @covers \Phing\Util\StringHelper::endsWith + */ + public function testEndsWith($needle = 'o', $haystack = 'foo', $expected = true) + { + $result = StringHelper::endsWith($needle, $haystack); + $this->assertSame($expected, $result); + } + + + public function endsWithProvider() + { + return [ + // True + ['z', 'FooBarBaz', true], + ['Baz', 'FooBarBaz', true], + ['FooBarBaz', 'FooBarBaz', true], + ['', 'FooBarBaz', true], + ['', "\x00", true], + ["\x00", "\x00", true], + ["\x00", "a\x00", true], + ["b\x00c", "ab\x00c", true], + // False + ['Baz', 'FooBar', false], + ['baz', 'FooBarBaz', false], + ['Baz', 'foobarbaz', false], + ['Baz', '', false], + ]; + } + + /** + * @covers \Phing\Util\StringHelper::substring + */ + public function testSubstringSimple() + { + $result = StringHelper::substring('FooBarBaz', 3); + $this->assertSame('BarBaz', $result); + } + + /** + * @covers \Phing\Util\StringHelper::substring + * @dataProvider substringProvider + */ + public function testSubstring($string, $start, $end, $expected) + { + $result = StringHelper::substring($string, $start, $end); + $this->assertSame($expected, $result); + } + + public function substringProvider() + { + return [ + ['FooBarBaz', 0, 0, 'F'], + ['FooBarBaz', 0, 1, 'Fo'], + ['FooBarBaz', 2, 4, 'oBa'], + ['FooBarBaz', 0, 0, 'F'], + ['FooBarBaz', 3, 3, 'B'], + ['FooBarBaz', 0, 8, 'FooBarBaz'], + ['FooBarBaz', 0, -1, 'FooBarBaz'], + ['FooBarBaz', 5, 8, 'rBaz'], + ['FooBarBaz', 5, -1, 'rBaz'], + ['FooBarBaz', 8, 8, 'z'], + ]; + } + + /** + * @covers \Phing\Util\StringHelper::substring + * @dataProvider substringErrorProvider + */ + public function testSubstringError($string, $start, $end, $message) + { + $this->expectError(); + $this->expectErrorMessage($message); + StringHelper::substring($string, $start, $end); + } + + public function substringErrorProvider() + { + return [ + ['FooBarBaz', -1, 1, 'substring(), Startindex out of bounds must be 0assertSame($expected, $result); + } + + public function isSlotVarProvider() + { + return [ + // 1 + ['%{x}', 1], + ['%{dummy}', 1], + ['%{my.var}', 1], + ['%{Foo.Bar.Baz}', 1], + ['%{user.first-name}', 1], + ['%{user.first_name}', 1], + [' %{slot.var} ', 1], + // 0 + ['slot.var', 0], + ['%{&é@}', 0], + ['%{slot§var}', 0], + ['%{}', 0], + ['%{slotèvar}', 0], + ['%{slot%var}', 0], + ['%{ slot.var }', 0], + ['}%slot.var{', 0], + ]; + } + + /** + * @covers \Phing\Util\StringHelper::slotVar + * @dataProvider slotVarProvider + */ + public function testSlotVar($var, $expected) + { + $result = StringHelper::slotVar($var); + $this->assertSame($expected, $result); + } + + public function slotVarProvider() + { + return [ + ['%{slot.var}', 'slot.var'], + ['%{&é@}', '&é@'], + ['', ''], + ['%{}', ''], + ['%{ }', ''], + [' %{ slot.var } ', 'slot.var'], + ['FooBarBaz', 'FooBarBaz'], + ]; + } }