Skip to content

Commit

Permalink
Adds missing test
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelstolt committed Nov 11, 2024
1 parent ee3fbc3 commit bc98c0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ public static function glob($pattern, $flags = 0, $forceFallback = false)
* @param int $flags
* @return array
*/
public static function globArray(string $pattern, array $array, int $flags = FNM_CASEFOLD)
public static function globArray(string $pattern, array $array, int $flags = FNM_CASEFOLD): array
{
$pattern = \str_replace(['{', '}'], '', $pattern);

$patternParts = \explode(',', $pattern);

foreach ($patternParts as $index => $patternPart) {
$matches[] = \array_filter($array, function ($val) use ($patternPart, $flags) {
return \fnmatch($patternPart, $val, $flags);
foreach ($patternParts as $patternPart) {
$matches[] = \array_filter($array, function ($value) use ($patternPart, $flags) {
return \fnmatch($patternPart, $value, $flags);
});
}

$excludes = [];
foreach ($matches as $index => $value) {
$excludes[] = \array_values($value)[0];
foreach ($matches as $match) {
if (\count(\array_values($match)) > 0) {
$excludes[] = \array_values($match)[0];
}
}

return $excludes;
Expand Down
29 changes: 29 additions & 0 deletions tests/GlobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Stolt\LeanPackage\Tests;

use PHPUnit\Framework\Attributes\Test;
use Stolt\LeanPackage\Glob;

class GlobTest extends TestCase
{
#[Test]
public function globArrayWorksAsExpected(): void
{
$expectedResult = ['README.md', 'LICENSE.md', 'docs/'];

$actualResult = Glob::globArray(
'{READ*.md,LICENSE.md,docs*}',
['spec.dist.yml', 'phpunit.dist.xml', 'README.md', 'LICENSE.md', 'docs/']
);
$this->assertEquals($expectedResult, $actualResult);

$actualResult = Glob::globArray(
'{composer.lock}',
['spec.dist.yml', 'phpunit.dist.xml', 'README.md', 'LICENSE.md', 'docs/']
);
$this->assertEquals([], $actualResult);
}
}

0 comments on commit bc98c0e

Please sign in to comment.