Skip to content

Commit

Permalink
Decouple Filter tests from fixture directory contents
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Aug 19, 2020
1 parent a857067 commit a95e4c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 89 deletions.
1 change: 1 addition & 0 deletions tests/_files/filter/a.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php declare(strict_types=1);
1 change: 1 addition & 0 deletions tests/_files/filter/b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php declare(strict_types=1);
132 changes: 43 additions & 89 deletions tests/tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
*/
namespace SebastianBergmann\CodeCoverage;

use function sort;
use function unserialize;
use function realpath;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;

/**
* @covers \SebastianBergmann\CodeCoverage\Filter
Expand All @@ -24,116 +22,73 @@ final class FilterTest extends TestCase
*/
private $filter;

/**
* @var array
*/
private $files = [];

protected function setUp(): void
{
$this->filter = new Filter;
}

$this->files = [
TEST_FILES_PATH . 'BankAccount.php',
TEST_FILES_PATH . 'BankAccountTest.php',
TEST_FILES_PATH . 'ClassThatUsesAnonymousClass.php',
TEST_FILES_PATH . 'ClassWithNameThatIsPartOfItsNamespacesName.php',
TEST_FILES_PATH . 'CoverageClassExtendedTest.php',
TEST_FILES_PATH . 'CoverageClassTest.php',
TEST_FILES_PATH . 'CoverageFunctionParenthesesTest.php',
TEST_FILES_PATH . 'CoverageFunctionParenthesesWhitespaceTest.php',
TEST_FILES_PATH . 'CoverageFunctionTest.php',
TEST_FILES_PATH . 'CoverageMethodOneLineAnnotationTest.php',
TEST_FILES_PATH . 'CoverageMethodParenthesesTest.php',
TEST_FILES_PATH . 'CoverageMethodParenthesesWhitespaceTest.php',
TEST_FILES_PATH . 'CoverageMethodTest.php',
TEST_FILES_PATH . 'CoverageNoneTest.php',
TEST_FILES_PATH . 'CoverageNotPrivateTest.php',
TEST_FILES_PATH . 'CoverageNotProtectedTest.php',
TEST_FILES_PATH . 'CoverageNotPublicTest.php',
TEST_FILES_PATH . 'CoverageNothingTest.php',
TEST_FILES_PATH . 'CoveragePrivateTest.php',
TEST_FILES_PATH . 'CoverageProtectedTest.php',
TEST_FILES_PATH . 'CoveragePublicTest.php',
TEST_FILES_PATH . 'CoverageTwoDefaultClassAnnotations.php',
TEST_FILES_PATH . 'CoveredClass.php',
TEST_FILES_PATH . 'CoveredFunction.php',
TEST_FILES_PATH . 'NamespaceCoverageClassExtendedTest.php',
TEST_FILES_PATH . 'NamespaceCoverageClassTest.php',
TEST_FILES_PATH . 'NamespaceCoverageCoversClassPublicTest.php',
TEST_FILES_PATH . 'NamespaceCoverageCoversClassTest.php',
TEST_FILES_PATH . 'NamespaceCoverageMethodTest.php',
TEST_FILES_PATH . 'NamespaceCoverageNotPrivateTest.php',
TEST_FILES_PATH . 'NamespaceCoverageNotProtectedTest.php',
TEST_FILES_PATH . 'NamespaceCoverageNotPublicTest.php',
TEST_FILES_PATH . 'NamespaceCoveragePrivateTest.php',
TEST_FILES_PATH . 'NamespaceCoverageProtectedTest.php',
TEST_FILES_PATH . 'NamespaceCoveragePublicTest.php',
TEST_FILES_PATH . 'NamespaceCoveredClass.php',
TEST_FILES_PATH . 'NamespacedBankAccount.php',
TEST_FILES_PATH . 'NotExistingCoveredElementTest.php',
TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php',
TEST_FILES_PATH . 'source_with_class_and_fqcn_constant.php',
TEST_FILES_PATH . 'source_with_empty_class.php',
TEST_FILES_PATH . 'source_with_ignore.php',
TEST_FILES_PATH . 'source_with_interface.php',
TEST_FILES_PATH . 'source_with_namespace.php',
TEST_FILES_PATH . 'source_with_oneline_annotations.php',
TEST_FILES_PATH . 'source_with_use_statements.php',
TEST_FILES_PATH . 'source_without_ignore.php',
TEST_FILES_PATH . 'source_without_namespace.php',
];
public function testIsInitiallyEmpty(): void
{
$this->assertTrue($this->filter->isEmpty());
}

public function testSingleFileCanBeAdded(): void
{
$this->filter->includeFile($this->files[0]);
$file = realpath(__DIR__ . '/../_files/filter/a.php');

$this->filter->includeFile($file);

$this->assertFalse($this->filter->isEmpty());

$this->assertEquals(
[$this->files[0]],
$this->assertSame(
[
$file,
],
$this->filter->files()
);
}

public function testMultipleFilesCanBeAdded(): void
{
$files = (new FileIteratorFacade)->getFilesAsArray(
TEST_FILES_PATH,
$suffixes = '.php'
);
$files = [
realpath(__DIR__ . '/../_files/filter/a.php'),
realpath(__DIR__ . '/../_files/filter/b.php'),
];

$this->filter->includeFiles($files);

$files = $this->filter->files();
sort($files);

$this->assertEquals($this->files, $files);
$this->assertSame($files, $this->filter->files());
}

public function testDirectoryCanBeAdded(): void
{
$this->filter->includeDirectory(TEST_FILES_PATH);

$files = $this->filter->files();
sort($files);
$this->filter->includeDirectory(__DIR__ . '/../_files/filter');

$this->assertEquals($this->files, $files);
$this->assertSame(
[
realpath(__DIR__ . '/../_files/filter/a.php'),
realpath(__DIR__ . '/../_files/filter/b.php'),
],
$this->filter->files()
);
}

public function testSingleFileCanBeRemoved(): void
{
$this->filter->includeFile($this->files[0]);
$this->filter->excludeFile($this->files[0]);
$this->filter->includeFile(realpath(__DIR__ . '/../_files/filter/a.php'));
$this->filter->excludeFile(realpath(__DIR__ . '/../_files/filter/a.php'));

$this->assertEquals([], $this->filter->files());
$this->assertTrue($this->filter->isEmpty());
$this->assertSame([], $this->filter->files());
}

public function testDirectoryCanBeRemoved(): void
{
$this->filter->includeDirectory(TEST_FILES_PATH);
$this->filter->excludeDirectory(TEST_FILES_PATH);
$this->filter->includeDirectory(__DIR__ . '/../_files/filter');
$this->filter->excludeDirectory(__DIR__ . '/../_files/filter');

$this->assertEquals([], $this->filter->files());
$this->assertTrue($this->filter->isEmpty());
$this->assertSame([], $this->filter->files());
}

public function testDeterminesWhetherStringContainsNameOfRealFileThatExists(): void
Expand All @@ -144,21 +99,21 @@ public function testDeterminesWhetherStringContainsNameOfRealFileThatExists(): v
$this->assertFalse($this->filter->isFile('runtime-created function'));
$this->assertFalse($this->filter->isFile('assert code'));
$this->assertFalse($this->filter->isFile('regexp code'));
$this->assertTrue($this->filter->isFile(__FILE__));
$this->assertTrue($this->filter->isFile(__DIR__ . '/../_files/filter/a.php'));
}

public function testIncludedFileIsNotFiltered(): void
{
$this->filter->includeFile($this->files[0]);
$this->filter->includeFile(realpath(__DIR__ . '/../_files/filter/a.php'));

$this->assertFalse($this->filter->isExcluded($this->files[0]));
$this->assertFalse($this->filter->isExcluded(realpath(__DIR__ . '/../_files/filter/a.php')));
}

public function testNotIncludedFileIsFiltered(): void
{
$this->filter->includeFile($this->files[0]);
$this->filter->includeFile(realpath(__DIR__ . '/../_files/filter/a.php'));

$this->assertTrue($this->filter->isExcluded($this->files[1]));
$this->assertTrue($this->filter->isExcluded(realpath(__DIR__ . '/../_files/filter/b.php')));
}

public function testNonFilesAreFiltered(): void
Expand All @@ -176,10 +131,9 @@ public function testNonFilesAreFiltered(): void
*/
public function testTryingToAddFileThatDoesNotExistDoesNotChangeFilter(): void
{
$filter = new Filter;

$filter->includeFile('does_not_exist');
$this->filter->includeFile('does_not_exist');

$this->assertEmpty($filter->files());
$this->assertTrue($this->filter->isEmpty());
$this->assertSame([], $this->filter->files());
}
}

0 comments on commit a95e4c1

Please sign in to comment.