Skip to content

Commit

Permalink
Add more assertions to the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Jan 31, 2022
1 parent 5618f16 commit 132add3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 25 deletions.
46 changes: 26 additions & 20 deletions tests/Unit/Configuration/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function provide_validators_with_given_value(): iterable
public function it_accepts_null(Validator $validator, string $method): void
{
$config = new EditableConfig();
// set non-null values to test they will be changed
$config->setCachePath('/cache/path');
$config->setMaxScoreThreshold(1.0);
$config->setMinScoreToShow(1);

$validator->validate($config, [$validator->getKey() => null]);

$this->assertNull($config->$method());
Expand All @@ -99,33 +104,34 @@ public function provide_validators_accepting_null(): iterable
* @test
* @dataProvider provide_validators_with_invalid_value
*/
public function it_throws_with_invalid_value(Validator $validator, $invalidValue): void
public function it_throws_with_invalid_value(Validator $validator, $invalidValue, string $errorMessage): void
{
$config = new EditableConfig();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($errorMessage);
$validator->validate($config, [$validator->getKey() => $invalidValue]);
}

public function provide_validators_with_invalid_value(): iterable
{
yield 'CachePath / int' => [new CachePath(), 123];
yield 'CommitsSince / int' => [new CommitsSince(), 123];
yield 'CommitsSince / null' => [new CommitsSince(), null];
yield 'DirectoriesToScan / string' => [new DirectoriesToScan(), 'foo'];
yield 'DirectoriesToScan / null' => [new DirectoriesToScan(), null];
yield 'FileExtensions / string' => [new FileExtensions(), 'foo'];
yield 'FileExtensions / null' => [new FileExtensions(), null];
yield 'FilesToIgnore / string' => [new FilesToIgnore(), 'foo'];
yield 'FilesToIgnore / null' => [new FilesToIgnore(), null];
yield 'FilesToShow / string' => [new FilesToShow(), 'foo'];
yield 'FilesToShow / null' => [new FilesToShow(), null];
yield 'Hooks / string' => [new Hooks(), 'foo'];
yield 'Hooks / null' => [new Hooks(), null];
yield 'MaxScoreThreshold / string' => [new MaxScoreThreshold(), 'foo'];
yield 'MinScoreToShow / string' => [new MinScoreToShow(), 'foo'];
yield 'ParallelJobs / string' => [new ParallelJobs(), 'foo'];
yield 'ParallelJobs / null' => [new ParallelJobs(), null];
yield 'Vcs / int' => [new Vcs(), 123];
yield 'Vcs / null' => [new Vcs(), null];
yield 'CachePath / int' => [new CachePath(), 123, 'Cache path should be a string'];
yield 'CommitsSince / int' => [new CommitsSince(), 123, 'Commits since should be a string'];
yield 'CommitsSince / null' => [new CommitsSince(), null, 'Commits since should be a string'];
yield 'DirectoriesToScan / string' => [new DirectoriesToScan(), 'foo', 'Directories to scan should be an array of strings'];
yield 'DirectoriesToScan / null' => [new DirectoriesToScan(), null, 'Directories to scan should be an array of strings'];
yield 'FileExtensions / string' => [new FileExtensions(), 'foo', 'File extensions should be an array of strings'];
yield 'FileExtensions / null' => [new FileExtensions(), null, 'File extensions should be an array of strings'];
yield 'FilesToIgnore / string' => [new FilesToIgnore(), 'foo', 'Files to ignore should be an array of strings'];
yield 'FilesToIgnore / null' => [new FilesToIgnore(), null, 'Files to ignore should be an array of strings'];
yield 'FilesToShow / string' => [new FilesToShow(), 'foo', 'Files to show should be an integer'];
yield 'FilesToShow / null' => [new FilesToShow(), null, 'Files to show should be an integer'];
yield 'Hooks / string' => [new Hooks(), 'foo', 'Hooks should be an array of strings'];
yield 'Hooks / null' => [new Hooks(), null, 'Hooks should be an array of strings'];
yield 'MaxScoreThreshold / string' => [new MaxScoreThreshold(), 'foo', 'Maximum score threshold should be a number'];
yield 'MinScoreToShow / string' => [new MinScoreToShow(), 'foo', 'Minimum score to show should be a number'];
yield 'ParallelJobs / string' => [new ParallelJobs(), 'foo', 'Amount of parallel jobs should be an integer'];
yield 'ParallelJobs / null' => [new ParallelJobs(), null, 'Amount of parallel jobs should be an integer'];
yield 'Vcs / int' => [new Vcs(), 123, 'VCS should be a string'];
yield 'Vcs / null' => [new Vcs(), null, 'VCS should be a string'];
}
}
46 changes: 46 additions & 0 deletions tests/Unit/File/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

namespace Churn\Tests\Unit\File;

use InvalidArgumentException;
use Churn\File\FileHelper;
use Churn\Tests\BaseTestCase;
use Symfony\Component\Filesystem\Filesystem;

class FileHelperTest extends BaseTestCase
{
/** @var string[] */
private $filesToDelete = [];

protected function tearDown()
{
(new Filesystem())->remove($this->filesToDelete);
}

/**
* @test
Expand Down Expand Up @@ -50,4 +59,41 @@ public function provide_relative_paths(): iterable
yield ['C:\\foo\\file.php', 'C:\\foo\\', 'file.php'];
}
}

/**
* @test
* @dataProvider provide_writable_paths
*/
public function it_can_ensure_a_file_is_writable(string $filePath): void
{
$dirPath = \dirname($filePath);

FileHelper::ensureFileIsWritable($filePath);

$this->assertTrue(\is_dir($dirPath), "Directory should exist: " . $dirPath);
}

public function provide_writable_paths(): iterable
{
yield [__FILE__];
yield [__DIR__ . '/non-existing-file'];
$this->filesToDelete[] = __DIR__ . '/path';
yield [__DIR__ . '/path/to/non-existing-file'];
}

/**
* @test
* @dataProvider provide_invalid_writable_paths
*/
public function it_throws_with_invalid_writable_files(string $filePath): void
{
$this->expectException(InvalidArgumentException::class);
FileHelper::ensureFileIsWritable($filePath);
}

public function provide_invalid_writable_paths(): iterable
{
yield [''];
yield [__DIR__];
}
}
8 changes: 5 additions & 3 deletions tests/Unit/Process/CacheProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ class CacheProcessFactoryTest extends BaseTestCase
* @test
* @dataProvider provide_invalid_paths
*/
public function it_throws_for_invalid_cache_path(string $cachePath): void
public function it_throws_for_invalid_cache_path(string $cachePath, string $errorMessage): void
{
$factory = m::mock(ProcessFactory::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid cache file path: ' . $errorMessage);
$this->expectExceptionCode(0);
new CacheProcessFactory($cachePath, $factory);
}

public function provide_invalid_paths(): iterable
{
yield [''];
yield [__DIR__];
yield ['', 'Path cannot be empty'];
yield [__DIR__, 'Path cannot be a folder'];
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Process/ConcreteProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function it_throws_exception_if_VCS_is_not_supported()
{
$config = new Config();
$this->expectException(InvalidArgumentException::class);
$this->processFactory = new ConcreteProcessFactory('not a valid VCS', $config->getCommitsSince());
$this->expectExceptionMessage('Unsupported VCS: not a valid VCS');
new ConcreteProcessFactory('not a valid VCS', $config->getCommitsSince());
}

/** @test */
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Result/ResultsRendererFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function it_returns_the_console_renderer_when_provided_text_format()
public function it_throws_exception_if_format_is_invalid()
{
$this->expectException(InvalidArgumentException::class);
$this->assertInstanceOf(ConsoleResultsRenderer::class, $this->factory->getRenderer('foobar'));
$this->expectExceptionMessage('Invalid output format provided');
$this->factory->getRenderer('foobar');
}

public function setUp()
Expand Down

0 comments on commit 132add3

Please sign in to comment.