From 93353e51475132c71a14a83fc8ee4ef078045389 Mon Sep 17 00:00:00 2001 From: Gary Gitton Date: Wed, 15 Dec 2021 00:12:43 +0100 Subject: [PATCH] add test on glob and GLOB_BRACE flag Signed-off-by: Gary Gitton --- src/Glob.php | 16 ++++++++-- test/GlobTest.php | 71 ++++++++++++++++++++++++++++++++++++++--- test/_files/{alph,bet}a | 0 3 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 test/_files/{alph,bet}a diff --git a/src/Glob.php b/src/Glob.php index dd187758..60b8c166 100644 --- a/src/Glob.php +++ b/src/Glob.php @@ -109,7 +109,7 @@ protected static function systemGlob($pattern, $flags) */ protected static function fallbackGlob($pattern, $flags) { - if (! $flags & self::GLOB_BRACE) { + if (self::flagsIsEqualTo($flags, self::GLOB_BRACE)) { return static::systemGlob($pattern, $flags); } @@ -195,14 +195,19 @@ protected static function nextBraceSub($pattern, $begin, $flags) $current = $begin; while ($current < $length) { - if (! $flags & self::GLOB_NOESCAPE && $pattern[$current] === '\\') { + $flagsEqualsNoEscape = self::flagsIsEqualTo($flags, self::GLOB_NOESCAPE); + + if ($flagsEqualsNoEscape && $pattern[$current] === '\\') { if (++$current === $length) { break; } $current++; } else { - if (($pattern[$current] === '}' && $depth-- === 0) || ($pattern[$current] === ',' && $depth === 0)) { + if ( + ($pattern[$current] === '}' && $depth-- === 0) + || ($pattern[$current] === ',' && $depth === 0) + ) { break; } elseif ($pattern[$current++] === '{') { $depth++; @@ -212,4 +217,9 @@ protected static function nextBraceSub($pattern, $begin, $flags) return $current < $length ? $current : null; } + + public static function flagsIsEqualTo(int $flags, int $otherFlags): bool + { + return (bool) ($flags & $otherFlags); + } } diff --git a/test/GlobTest.php b/test/GlobTest.php index 7e74754d..a1d08909 100644 --- a/test/GlobTest.php +++ b/test/GlobTest.php @@ -11,6 +11,7 @@ use function count; use function defined; use function glob; +use function realpath; use function str_repeat; use const GLOB_BRACE; @@ -23,15 +24,29 @@ public function testFallback(): void $this->markTestSkipped('GLOB_BRACE not available'); } - self::assertEquals( - glob(__DIR__ . '/_files/{alph,bet}a', GLOB_BRACE), - Glob::glob(__DIR__ . '/_files/{alph,bet}a', Glob::GLOB_BRACE, true) + $expected = glob(__DIR__ . '/_files/{alph,bet}a', GLOB_BRACE); + $actual = Glob::glob( + __DIR__ . '/_files/{alph,bet}a', + Glob::GLOB_BRACE, + true + ); + + self::assertEquals($actual, $expected); + + $notExpectedPath = realpath(__DIR__ . '/_files/{alph,bet}a'); + + self::assertNotContains( + $notExpectedPath, + $actual ); } public function testNonMatchingGlobReturnsArray(): void { - $result = Glob::glob('/some/path/{,*.}{this,orthis}.php', Glob::GLOB_BRACE); + $result = Glob::glob( + '/some/path/{,*.}{this,orthis}.php', + Glob::GLOB_BRACE + ); self::assertIsArray($result); } @@ -80,4 +95,52 @@ public function patternsProvider(): array ], ]; } + + public function testGlobWithoutGlobBraceFlag(): void + { + $expected = [ + realpath(__DIR__ . '/_files/{alph,bet}a'), + ]; + + self::assertEquals( + glob(__DIR__ . '/_files/{alph,bet}a', 0), + $expected + ); + } + + /** + * @psalm-return array + */ + public function flagsIsEqualsToMethodDataProvider(): array + { + return [ + [ + Glob::GLOB_BRACE, + Glob::GLOB_BRACE, + true, + ], + [ + Glob::GLOB_BRACE, + Glob::GLOB_NOSORT, + false, + ], + ]; + } + + /** + * @dataProvider flagsIsEqualsToMethodDataProvider + */ + public function testFlagsIsEqualsToMethod( + int $flags, + int $otherFlags, + bool $expected + ): void { + $actual = Glob::flagsIsEqualTo($flags, $otherFlags); + + $this->assertEquals($expected, $actual); + } } diff --git a/test/_files/{alph,bet}a b/test/_files/{alph,bet}a new file mode 100644 index 00000000..e69de29b