Skip to content

Commit

Permalink
OfType now allows multiple types
Browse files Browse the repository at this point in the history
You can pass:
 - "PHP"
 - "PHP,XML,JS"
 - ["PHP", "XML", "JS"]

 Related to issue #228
  • Loading branch information
sebastianfeldmann committed Oct 4, 2023
1 parent 4db913a commit cac493c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/Hook/Condition/FileStaged/OfType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
* ]}
* ]
*
* Multiple types can be configured using a comma separated string or an array
* "php,html,xml"
* ["php", "html", "xml"]
*
* @package CaptainHook
* @author Sebastian Feldmann <sf@sebastian-feldmann.info>
* @link https://github.com/captainhookphp/captainhook
Expand All @@ -46,9 +50,9 @@ class OfType implements Condition, Constrained
/**
* File type to check e.g. 'php' or 'html'
*
* @var string
* @var array<int, string>
*/
private string $suffix;
private array $suffixes;

/**
* --diff-filter option
Expand All @@ -60,12 +64,12 @@ class OfType implements Condition, Constrained
/**
* OfType constructor
*
* @param mixed $type
* @param mixed $types
* @param array<int, string>|string $filter
*/
public function __construct($type, $filter = [])
public function __construct($types, $filter = [])
{
$this->suffix = (string) $type;
$this->suffixes = is_array($types) ? $types : explode(',', (string) $types);
$this->diffFilter = FilterUtil::filterFromConfigValue($filter);
}

Expand All @@ -88,7 +92,7 @@ public static function getRestriction(): Restriction
*/
public function isTrue(IO $io, Repository $repository): bool
{
$files = $repository->getIndexOperator()->getStagedFilesOfType($this->suffix, $this->diffFilter);
$files = $repository->getIndexOperator()->getStagedFilesOfTypes($this->suffixes, $this->diffFilter);
return count($files) > 0;
}
}
19 changes: 17 additions & 2 deletions tests/unit/Hook/Condition/FileStaged/OfTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,28 @@ public function testStagedTrue(): void
$io = $this->createIOMock();
$repo = $this->createRepositoryMock();
$index = $this->createGitIndexOperator();
$index->expects($this->once())->method('getStagedFilesOfType')->willReturn(['foo.php', 'bar.php']);
$index->expects($this->once())->method('getStagedFilesOfTypes')->willReturn(['foo.php', 'bar.php']);
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);

$ofType = new OfType('php');
$this->assertTrue($ofType->isTrue($io, $repo));
}

/**
* Tests OfType::isTrue
*/
public function testStagedMultipleTrue(): void
{
$io = $this->createIOMock();
$repo = $this->createRepositoryMock();
$index = $this->createGitIndexOperator();
$index->expects($this->once())->method('getStagedFilesOfTypes')->willReturn(['foo.php', 'bar.html', 'baz.js']);
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);

$ofType = new OfType(['xml', 'js']);
$this->assertTrue($ofType->isTrue($io, $repo));
}

/**
* Tests OfType:isTrue
*/
Expand All @@ -52,7 +67,7 @@ public function testStagedFalse(): void
$io = $this->createIOMock();
$repo = $this->createRepositoryMock();
$index = $this->createGitIndexOperator();
$index->expects($this->once())->method('getStagedFilesOfType')->willReturn([]);
$index->expects($this->once())->method('getStagedFilesOfTypes')->willReturn([]);
$repo->expects($this->once())->method('getIndexOperator')->willReturn($index);

$ofType = new OfType('js', ['A', 'C']);
Expand Down

0 comments on commit cac493c

Please sign in to comment.