From 7920589c5f842b85c1ed6d1348e458cab056d31c Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Sun, 13 Nov 2022 14:06:40 +0100 Subject: [PATCH] Scan tests/ with PHPStan --- .github/workflows/continuous-integration.yml | 3 + phpstan.neon | 7 +- tests/BaseTestCase.php | 2 + tests/EndToEnd/FossilTest.php | 14 +- tests/EndToEnd/MercurialTest.php | 14 +- tests/EndToEnd/SubversionTest.php | 14 +- .../Command/AssessComplexityCommandTest.php | 24 +- tests/Integration/Command/Assets/TestHook.php | 3 + tests/Integration/Command/Assets/hooks | 3 + tests/Integration/Command/RunCommandTest.php | 115 +++-- tests/Integration/File/FileFinderTest.php | 27 +- tests/Integration/ManifestTest.php | 9 +- .../CyclomaticComplexityAssessorTest.php | 7 +- .../Command/Helper/MaxScoreCheckerTest.php | 10 +- tests/Unit/Configuration/LoaderTest.php | 31 +- .../Unit/Configuration/ReadOnlyConfigTest.php | 14 +- tests/Unit/Configuration/ValidatorTest.php | 446 +++++++++++------- .../Event/Event/AfterAnalysisEventTest.php | 8 +- .../Event/AfterFileAnalysisEventTest.php | 8 +- tests/Unit/File/FileFinderTest.php | 59 ++- tests/Unit/File/FileHelperTest.php | 20 +- tests/Unit/File/FileTest.php | 13 +- .../Unit/Process/CacheProcessFactoryTest.php | 3 + .../NoVcsChangesCountProcessTest.php | 19 +- .../Process/ConcreteProcessFactoryTest.php | 44 +- .../CyclomaticComplexityProcessTest.php | 29 +- .../Handler/ParallelProcessHandlerTest.php | 16 +- .../Handler/SequentialProcessHandlerTest.php | 9 +- .../Process/ProcessHandlerFactoryTest.php | 13 +- tests/Unit/Result/HighestScoresTest.php | 20 +- .../Result/Render/CsvResultsRendererTest.php | 8 +- .../Result/Render/JsonResultsRendererTest.php | 8 +- .../Render/MarkdownResultsRendererTest.php | 8 +- tests/Unit/Result/ResultAccumulatorTest.php | 18 +- tests/Unit/Result/ResultTest.php | 47 +- .../Result/ResultsRendererFactoryTest.php | 31 +- 36 files changed, 627 insertions(+), 497 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index e1d073d5..622ee852 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -196,6 +196,9 @@ jobs: - name: "Run PHP Copy/Paste Detector" run: "vendor/bin/phpcpd src" + - name: "Install PHPUnit" + run: "vendor/bin/simple-phpunit --version" + - name: "Run Psalm" run: "vendor/bin/psalm --show-info=true" diff --git a/phpstan.neon b/phpstan.neon index ae6c629a..f80b591f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,11 +2,16 @@ parameters: level: 8 paths: - %currentWorkingDirectory%/src/ + - %currentWorkingDirectory%/tests/ + + bootstrapFiles: + - vendor/bin/.phpunit/phpunit/vendor/autoload.php + - tests/Integration/Command/Assets/hooks checkGenericClassInNonGenericObjectType: false reportUnmatchedIgnoredErrors: true ignoreErrors: - - '/Casting to .+ something that''s already/' + - '/^Casting to .+ something that''s already/' checkTooWideReturnTypesInProtectedAndPublicMethods: true checkUninitializedProperties: true diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 86092f70..cab61fac 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -11,6 +11,8 @@ abstract class BaseTestCase extends TestCase { /** * @see https://github.com/phpspec/prophecy/issues/366#issuecomment-359587114 + * + * @return void */ protected function tearDown() { diff --git a/tests/EndToEnd/FossilTest.php b/tests/EndToEnd/FossilTest.php index c6117cf8..8c7a5efc 100644 --- a/tests/EndToEnd/FossilTest.php +++ b/tests/EndToEnd/FossilTest.php @@ -15,21 +15,27 @@ class FossilTest extends BaseTestCase /** @var CommandTester */ private $commandTester; + /** @return void */ protected function setUp() { + parent::setUp(); + $application = new Application('churn-php', 'test'); $application->add(RunCommand::newInstance()); $command = $application->find('run'); $this->commandTester = new CommandTester($command); } + /** @return void */ protected function tearDown() { - $this->commandTester = null; + parent::tearDown(); + + unset($this->commandTester); } /** @test */ - public function it_works_with_fossil() + public function it_works_with_fossil(): void { $exitCode = $this->commandTester->execute([ 'paths' => [], @@ -51,7 +57,7 @@ public function it_works_with_fossil() +---------+---------------+------------+-------+ "; - $this->assertSame(0, $exitCode); - $this->assertSame($expected, $display); + self::assertSame(0, $exitCode); + self::assertSame($expected, $display); } } diff --git a/tests/EndToEnd/MercurialTest.php b/tests/EndToEnd/MercurialTest.php index af7f2f1d..83e8e40d 100644 --- a/tests/EndToEnd/MercurialTest.php +++ b/tests/EndToEnd/MercurialTest.php @@ -15,21 +15,27 @@ class MercurialTest extends BaseTestCase /** @var CommandTester */ private $commandTester; + /** @return void */ protected function setUp() { + parent::setUp(); + $application = new Application('churn-php', 'test'); $application->add(RunCommand::newInstance()); $command = $application->find('run'); $this->commandTester = new CommandTester($command); } + /** @return void */ protected function tearDown() { - $this->commandTester = null; + parent::tearDown(); + + unset($this->commandTester); } /** @test */ - public function it_works_with_mercurial() + public function it_works_with_mercurial(): void { $exitCode = $this->commandTester->execute([ 'paths' => [], @@ -51,7 +57,7 @@ public function it_works_with_mercurial() +---------+---------------+------------+-------+ "; - $this->assertSame(0, $exitCode); - $this->assertSame($expected, $display); + self::assertSame(0, $exitCode); + self::assertSame($expected, $display); } } diff --git a/tests/EndToEnd/SubversionTest.php b/tests/EndToEnd/SubversionTest.php index b8554746..75d3c36b 100644 --- a/tests/EndToEnd/SubversionTest.php +++ b/tests/EndToEnd/SubversionTest.php @@ -15,21 +15,27 @@ class SubversionTest extends BaseTestCase /** @var CommandTester */ private $commandTester; + /** @return void */ protected function setUp() { + parent::setUp(); + $application = new Application('churn-php', 'test'); $application->add(RunCommand::newInstance()); $command = $application->find('run'); $this->commandTester = new CommandTester($command); } + /** @return void */ protected function tearDown() { - $this->commandTester = null; + parent::tearDown(); + + unset($this->commandTester); } /** @test */ - public function it_works_with_subversion() + public function it_works_with_subversion(): void { $exitCode = $this->commandTester->execute([ 'paths' => [], @@ -51,7 +57,7 @@ public function it_works_with_subversion() +---------+---------------+------------+-------+ "; - $this->assertSame(0, $exitCode); - $this->assertSame($expected, $display); + self::assertSame(0, $exitCode); + self::assertSame($expected, $display); } } diff --git a/tests/Integration/Command/AssessComplexityCommandTest.php b/tests/Integration/Command/AssessComplexityCommandTest.php index 2789a1a3..db90979b 100644 --- a/tests/Integration/Command/AssessComplexityCommandTest.php +++ b/tests/Integration/Command/AssessComplexityCommandTest.php @@ -14,38 +14,44 @@ class AssessComplexityCommandTest extends BaseTestCase /** @var CommandTester */ private $commandTester; + /** @return void */ protected function setUp() { + parent::setUp(); + $application = new Application('churn-php', 'test'); $application->add(AssessComplexityCommand::newInstance()); $command = $application->find('assess-complexity'); $this->commandTester = new CommandTester($command); } + /** @return void */ protected function tearDown() { - $this->commandTester = null; + parent::tearDown(); + + unset($this->commandTester); } /** @test */ - public function it_returns_the_cyclomatic_complexity_greater_than_zero() + public function it_returns_the_cyclomatic_complexity_greater_than_zero(): void { $exitCode = $this->commandTester->execute(['file' => __FILE__]); $result = \rtrim($this->commandTester->getDisplay()); - $this->assertSame(0, $exitCode); - $this->assertTrue(\ctype_digit($result), 'The result of the command must be an integer'); - $this->assertGreaterThan(0, (int) $result); + self::assertSame(0, $exitCode); + self::assertTrue(\ctype_digit($result), 'The result of the command must be an integer'); + self::assertGreaterThan(0, (int) $result); } /** @test */ - public function it_returns_zero_for_non_existing_file() + public function it_returns_zero_for_non_existing_file(): void { $exitCode = $this->commandTester->execute(['file' => 'nonexisting-file.php']); $result = \rtrim($this->commandTester->getDisplay()); - $this->assertSame(0, $exitCode); - $this->assertTrue(\ctype_digit($result), 'The result of the command must be an integer'); - $this->assertSame(0, (int) $result); + self::assertSame(0, $exitCode); + self::assertTrue(\ctype_digit($result), 'The result of the command must be an integer'); + self::assertSame(0, (int) $result); } } diff --git a/tests/Integration/Command/Assets/TestHook.php b/tests/Integration/Command/Assets/TestHook.php index 8f43700b..ba4efaa4 100644 --- a/tests/Integration/Command/Assets/TestHook.php +++ b/tests/Integration/Command/Assets/TestHook.php @@ -14,8 +14,11 @@ class TestHook implements AfterAnalysisHook, AfterFileAnalysisHook, BeforeAnalysisHook { + /** @var int */ public static $nbAfterAnalysisEvent = 0; + /** @var int */ public static $nbAfterFileAnalysisEvent = 0; + /** @var int */ public static $nbBeforeAnalysisEvent = 0; public static function reset(): void diff --git a/tests/Integration/Command/Assets/hooks b/tests/Integration/Command/Assets/hooks index 6f37b173..de6588b1 100644 --- a/tests/Integration/Command/Assets/hooks +++ b/tests/Integration/Command/Assets/hooks @@ -13,6 +13,7 @@ use Churn\Event\Hook\BeforeAnalysisHook; class TestAfterAnalysisHook implements AfterAnalysisHook { + /** @var int */ public static $nbAfterAnalysisEvent = 0; /** @@ -26,6 +27,7 @@ class TestAfterAnalysisHook implements AfterAnalysisHook class TestAfterFileAnalysisHook implements AfterFileAnalysisHook { + /** @var int */ public static $nbAfterFileAnalysisEvent = 0; /** @@ -39,6 +41,7 @@ class TestAfterFileAnalysisHook implements AfterFileAnalysisHook class TestBeforeAnalysisHook implements BeforeAnalysisHook { + /** @var int */ public static $nbBeforeAnalysisEvent = 0; /** diff --git a/tests/Integration/Command/RunCommandTest.php b/tests/Integration/Command/RunCommandTest.php index 2c356166..6cffe538 100644 --- a/tests/Integration/Command/RunCommandTest.php +++ b/tests/Integration/Command/RunCommandTest.php @@ -26,17 +26,23 @@ class RunCommandTest extends BaseTestCase /** @var string|null */ private $tmpFile; + /** @return void */ protected function setUp() { + parent::setUp(); + $application = new Application('churn-php', 'test'); $application->add(RunCommand::newInstance()); $command = $application->find('run'); $this->commandTester = new CommandTester($command); } + /** @return void */ protected function tearDown() { - $this->commandTester = null; + parent::tearDown(); + + unset($this->commandTester); if ($this->tmpFile !== null && \is_file($this->tmpFile)) { \unlink($this->tmpFile); @@ -45,7 +51,7 @@ protected function tearDown() } /** @test */ - public function it_displays_the_logo_at_the_beginning_by_default() + public function it_displays_the_logo_at_the_beginning_by_default(): void { $exitCode = $this->commandTester->execute([ 'paths' => [__DIR__], @@ -53,14 +59,14 @@ public function it_displays_the_logo_at_the_beginning_by_default() ]); $display = $this->commandTester->getDisplay(); - $this->assertSame(0, $exitCode); - $this->assertSame(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO))); + self::assertSame(0, $exitCode); + self::assertSame(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO))); // there is no progress bar by default - $this->assertFalse(strpos($display, self::BAR), 'The progress bar shouldn\'t be displayed'); + self::assertFalse(strpos($display, self::BAR), 'The progress bar shouldn\'t be displayed'); } /** @test */ - public function it_can_show_a_progress_bar() + public function it_can_show_a_progress_bar(): void { $exitCode = $this->commandTester->execute([ 'paths' => [__DIR__], @@ -68,27 +74,28 @@ public function it_can_show_a_progress_bar() ]); $display = $this->commandTester->getDisplay(); - $this->assertSame(0, $exitCode); - $this->assertSame(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO))); + self::assertSame(0, $exitCode); + self::assertSame(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO))); // the progress bar must be right after the logo $display = ltrim(substr($display, strlen(RunCommand::LOGO))); - $this->assertSame(self::BAR, substr($display, 0, strlen(self::BAR))); + self::assertSame(self::BAR, substr($display, 0, strlen(self::BAR))); } /** @test */ - public function it_can_return_a_json_report() + public function it_can_return_a_json_report(): void { $exitCode = $this->commandTester->execute(['paths' => [__DIR__], '--format' => 'json']); $data = \json_decode($this->commandTester->getDisplay(), true); - $this->assertSame(0, $exitCode); - $this->assertReport($data); + self::assertSame(0, $exitCode); + self::assertReport($data); } /** @test */ - public function it_can_write_a_json_report() + public function it_can_write_a_json_report(): void { - $this->tmpFile = \tempnam(\sys_get_temp_dir(), 'churn-test-'); + self::assertNotFalse($tmpFile = \tempnam(\sys_get_temp_dir(), 'churn-test-')); + $this->tmpFile = $tmpFile; $exitCode = $this->commandTester->execute([ 'paths' => [\realpath(__DIR__ . '/../../')], '--format' => 'json', @@ -96,24 +103,28 @@ public function it_can_write_a_json_report() ]); $display = $this->commandTester->getDisplay(); - $this->assertSame(0, $exitCode); - $this->assertSame(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO))); + self::assertSame(0, $exitCode); + self::assertSame(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO))); - $this->assertFileExists($this->tmpFile); - $data = \json_decode(\file_get_contents($this->tmpFile), true); - $this->assertReport($data); + self::assertFileExists($tmpFile); + self::assertNotFalse($contents = \file_get_contents($tmpFile)); + $data = \json_decode($contents, true); + self::assertReport($data); } - private function assertReport($data): void + /** + * @param mixed $data + */ + private static function assertReport($data): void { - $this->assertTrue(is_array($data), 'Expected array, got ' . gettype($data) . ' (' . var_export($data, true) . ')'); + self::assertTrue(is_array($data), 'Expected array, got ' . gettype($data) . ' (' . var_export($data, true) . ')'); $i = 0; foreach ($data as $key => $value) { - $this->assertSame($i++, $key); - $this->assertArrayHasKey('file', $value); - $this->assertArrayHasKey('commits', $value); - $this->assertArrayHasKey('complexity', $value); - $this->assertArrayHasKey('score', $value); + self::assertSame($i++, $key); + self::assertArrayHasKey('file', $value); + self::assertArrayHasKey('commits', $value); + self::assertArrayHasKey('complexity', $value); + self::assertArrayHasKey('score', $value); } } @@ -155,7 +166,7 @@ public function it_can_use_cache(): void if (is_file($cachePath)) { unlink($cachePath); } - $this->assertFalse(file_exists($cachePath), "File $cachePath shouldn't exist"); + self::assertFalse(file_exists($cachePath), "File $cachePath shouldn't exist"); // generate cache $exitCode = $this->commandTester->execute([ @@ -164,9 +175,9 @@ public function it_can_use_cache(): void ]); $displayBeforeCache = $this->commandTester->getDisplay(); - $this->assertSame(0, $exitCode); - $this->assertTrue(file_exists($cachePath), "File $cachePath should exist"); - $this->assertGreaterThan(0, filesize($cachePath), 'Cache file is empty'); + self::assertSame(0, $exitCode); + self::assertTrue(file_exists($cachePath), "File $cachePath should exist"); + self::assertGreaterThan(0, filesize($cachePath), 'Cache file is empty'); // use cache $exitCode = $this->commandTester->execute([ @@ -175,10 +186,10 @@ public function it_can_use_cache(): void ]); $displayAfterCache = $this->commandTester->getDisplay(); - $this->assertSame(0, $exitCode); - $this->assertSame($displayBeforeCache, $displayAfterCache); - $this->assertTrue(file_exists($cachePath), "File $cachePath should exist"); - $this->assertGreaterThan(0, filesize($cachePath), 'Cache file is empty'); + self::assertSame(0, $exitCode); + self::assertSame($displayBeforeCache, $displayAfterCache); + self::assertTrue(file_exists($cachePath), "File $cachePath should exist"); + self::assertGreaterThan(0, filesize($cachePath), 'Cache file is empty'); } /** @test */ @@ -191,10 +202,10 @@ public function it_can_use_a_hook_by_classname(): void '-c' => __DIR__ . '/config/hook-by-classname.yml', ]); - $this->assertSame(0, $exitCode); - $this->assertSame(1, TestHook::$nbAfterAnalysisEvent); - $this->assertSame(2, TestHook::$nbAfterFileAnalysisEvent); - $this->assertSame(1, TestHook::$nbBeforeAnalysisEvent); + self::assertSame(0, $exitCode); + self::assertSame(1, TestHook::$nbAfterAnalysisEvent); + self::assertSame(2, TestHook::$nbAfterFileAnalysisEvent); + self::assertSame(1, TestHook::$nbBeforeAnalysisEvent); } /** @test */ @@ -205,10 +216,10 @@ public function it_can_use_a_hook_by_path(): void '-c' => __DIR__ . '/config/hook-by-path.yml', ]); - $this->assertSame(0, $exitCode); - $this->assertSame(1, TestAfterAnalysisHook::$nbAfterAnalysisEvent); - $this->assertSame(2, TestAfterFileAnalysisHook::$nbAfterFileAnalysisEvent); - $this->assertSame(1, TestBeforeAnalysisHook::$nbBeforeAnalysisEvent); + self::assertSame(0, $exitCode); + self::assertSame(1, TestAfterAnalysisHook::$nbAfterAnalysisEvent); + self::assertSame(2, TestAfterFileAnalysisHook::$nbAfterFileAnalysisEvent); + self::assertSame(1, TestBeforeAnalysisHook::$nbBeforeAnalysisEvent); } /** @test */ @@ -236,8 +247,8 @@ public function it_can_suppress_normal_output(): void $display = ob_get_contents(); ob_end_clean(); - $this->assertSame(0, $exitCode); - $this->assertSame('Churn: DONE', $display); + self::assertSame(0, $exitCode); + self::assertSame('Churn: DONE', $display); } /** @test */ @@ -249,8 +260,8 @@ public function it_can_return_one_as_exit_code(): void ], ['capture_stderr_separately' => true]); $display = $this->commandTester->getErrorOutput(); - $this->assertSame(1, $exitCode); - $this->assertStringContainsString('Max score is over the threshold', $display); + self::assertSame(1, $exitCode); + self::assertStringContainsString('Max score is over the threshold', $display); } /** @test */ @@ -262,12 +273,12 @@ public function it_can_warn_about_unrecognized_keys(): void ], ['capture_stderr_separately' => true]); $display = $this->commandTester->getErrorOutput(); - $this->assertSame(0, $exitCode); - $this->assertStringContainsString('Unrecognized configuration keys: foo, bar', $display); + self::assertSame(0, $exitCode); + self::assertStringContainsString('Unrecognized configuration keys: foo, bar', $display); } /** @test */ - public function it_can_return_a_json_report_and_also_warn() + public function it_can_return_a_json_report_and_also_warn(): void { $exitCode = $this->commandTester->execute([ 'paths' => [__DIR__], @@ -277,8 +288,8 @@ public function it_can_return_a_json_report_and_also_warn() $display = $this->commandTester->getErrorOutput(); $data = \json_decode($this->commandTester->getDisplay(), true); - $this->assertSame(0, $exitCode); - $this->assertReport($data); - $this->assertStringContainsString('Unrecognized configuration keys: foo, bar', $display); + self::assertSame(0, $exitCode); + self::assertReport($data); + self::assertStringContainsString('Unrecognized configuration keys: foo, bar', $display); } } diff --git a/tests/Integration/File/FileFinderTest.php b/tests/Integration/File/FileFinderTest.php index 765b9ad5..ffed26da 100644 --- a/tests/Integration/File/FileFinderTest.php +++ b/tests/Integration/File/FileFinderTest.php @@ -10,31 +10,24 @@ class FileFinderTest extends BaseTestCase { - /** - * The class being tested. - * @var FileFinder - */ + /** @var FileFinder */ private $fileFinder; - /** @test */ - public function it_can_be_instantiated() + /** @return void */ + public function setUp() { - $this->assertInstanceOf(FileFinder::class, $this->fileFinder); + parent::setUp(); + + $this->fileFinder = new FileFinder(['php'], [], __DIR__); } /** @test */ - public function it_can_recursively_get_the_php_files_in_a_path() + public function it_can_recursively_get_the_php_files_in_a_path(): void { $paths = [__DIR__]; + /** @var array $results */ $results = iterator_to_array($this->fileFinder->getPhpFiles($paths), false); - $this->assertCount(1, $results); - $this->assertInstanceOf(File::class, $results[0]); - } - - public function setUp() - { - parent::setup(); - - $this->fileFinder = new FileFinder(['php'], [], __DIR__); + self::assertCount(1, $results); + self::assertInstanceOf(File::class, $results[0]); } } diff --git a/tests/Integration/ManifestTest.php b/tests/Integration/ManifestTest.php index 8aa81c3a..f93cdec2 100644 --- a/tests/Integration/ManifestTest.php +++ b/tests/Integration/ManifestTest.php @@ -10,15 +10,16 @@ class ManifestTest extends BaseTestCase { /** @test */ - public function manifest_is_valid() + public function manifest_is_valid(): void { $path = __DIR__ . '/../../manifest.xml'; - $this->assertTrue(is_file($path), 'manifest.xml not found'); + self::assertTrue(is_file($path), 'manifest.xml not found'); $manifest = ManifestLoader::fromFile($path); + // @phpstan-ignore-next-line $name = method_exists($manifest->getName(), 'asString') ? $manifest->getName()->asString() : (string) $manifest->getName(); - $this->assertSame('bmitch/churn-php', $name); - $this->assertGreaterThan(0, $manifest->getRequirements()->count()); + self::assertSame('bmitch/churn-php', $name); + self::assertGreaterThan(0, $manifest->getRequirements()->count()); } } diff --git a/tests/Unit/Assessor/CyclomaticComplexityAssessorTest.php b/tests/Unit/Assessor/CyclomaticComplexityAssessorTest.php index 484e8a41..27baf8f5 100644 --- a/tests/Unit/Assessor/CyclomaticComplexityAssessorTest.php +++ b/tests/Unit/Assessor/CyclomaticComplexityAssessorTest.php @@ -12,13 +12,16 @@ class CyclomaticComplexityAssessorTest extends BaseTestCase /** * @dataProvider provide_assess */ - public function test_assess(int $expectedScore, string $code) + public function test_assess(int $expectedScore, string $code): void { $assessor = new CyclomaticComplexityAssessor(); - $this->assertSame($expectedScore, $assessor->assess($code)); + self::assertSame($expectedScore, $assessor->assess($code)); } + /** + * @return iterable + */ public function provide_assess(): iterable { yield 'an empty file' => [ diff --git a/tests/Unit/Command/Helper/MaxScoreCheckerTest.php b/tests/Unit/Command/Helper/MaxScoreCheckerTest.php index 8d4c4471..f8abe452 100644 --- a/tests/Unit/Command/Helper/MaxScoreCheckerTest.php +++ b/tests/Unit/Command/Helper/MaxScoreCheckerTest.php @@ -37,12 +37,15 @@ public function it_can_check_the_max_score( $checker = new MaxScoreChecker($threshold); - $this->assertSame( + self::assertSame( $expectedResult, $checker->isOverThreshold($input, $output, $report) ); } + /** + * @return iterable + */ public function provide_arguments(): iterable { yield 'threshold and score are null' => [false, null, null]; @@ -76,9 +79,12 @@ public function it_prints_an_error_message( $checker = new MaxScoreChecker(0); - $this->assertTrue($checker->isOverThreshold($input, $output, $report)); + self::assertTrue($checker->isOverThreshold($input, $output, $report)); } + /** + * @return iterable + */ public function provide_format_and_output(): iterable { yield 'format=text, output is null' => ['text', null]; diff --git a/tests/Unit/Configuration/LoaderTest.php b/tests/Unit/Configuration/LoaderTest.php index f6e694e0..590aef2a 100644 --- a/tests/Unit/Configuration/LoaderTest.php +++ b/tests/Unit/Configuration/LoaderTest.php @@ -13,15 +13,15 @@ class LoaderTest extends BaseTestCase { /** @test */ - public function it_returns_the_default_values_if_there_is_no_default_file() + public function it_returns_the_default_values_if_there_is_no_default_file(): void { - $cwd = \getcwd(); + self::assertNotFalse($cwd = \getcwd()); try { chdir(__DIR__); $config = Loader::fromPath('churn.yml', true); - $this->assertEqualsCanonicalizing(new ReadOnlyConfig(), $config); - $this->assertSame(\getcwd(), $config->getDirPath()); + self::assertEqualsCanonicalizing(new ReadOnlyConfig(), $config); + self::assertSame(\getcwd(), $config->getDirPath()); } finally { // restore cwd chdir($cwd); @@ -29,40 +29,41 @@ public function it_returns_the_default_values_if_there_is_no_default_file() } /** @test */ - public function it_throws_if_the_chosen_file_is_missing() + public function it_throws_if_the_chosen_file_is_missing(): void { $this->expectException(InvalidArgumentException::class); Loader::fromPath('non-existing-config-file.yml', false); } /** @test */ - public function it_throws_if_the_content_is_invalid() + public function it_throws_if_the_content_is_invalid(): void { $this->expectException(InvalidArgumentException::class); Loader::fromPath(__FILE__, false); } /** @test */ - public function it_fallbacks_on_the_distributed_file() + public function it_fallbacks_on_the_distributed_file(): void { - $dirPath = \realpath(__DIR__ . '/config/dist'); + self::assertNotFalse($dirPath = \realpath(__DIR__ . '/config/dist')); $config = Loader::fromPath($dirPath, false); - $this->assertEqualsCanonicalizing(new EditableConfig($dirPath . DIRECTORY_SEPARATOR . 'churn.yml.dist'), $config); - $this->assertSame($dirPath, $config->getDirPath()); + self::assertEqualsCanonicalizing(new EditableConfig($dirPath . DIRECTORY_SEPARATOR . 'churn.yml.dist'), $config); + self::assertSame($dirPath, $config->getDirPath()); } /** @test */ - public function it_fallbacks_on_the_default_distributed_file() + public function it_fallbacks_on_the_default_distributed_file(): void { - $cwd = \getcwd(); - $dirPath = \realpath(__DIR__ . '/config/dist'); + self::assertNotFalse($cwd = \getcwd()); + self::assertNotFalse($dirPath = \realpath(__DIR__ . '/config/dist')); + try { chdir($dirPath); $config = Loader::fromPath('churn.yml', true); - $this->assertEqualsCanonicalizing(new EditableConfig($dirPath . DIRECTORY_SEPARATOR . 'churn.yml.dist'), $config); - $this->assertSame($dirPath, $config->getDirPath()); + self::assertEqualsCanonicalizing(new EditableConfig($dirPath . DIRECTORY_SEPARATOR . 'churn.yml.dist'), $config); + self::assertSame($dirPath, $config->getDirPath()); } finally { // restore cwd chdir($cwd); diff --git a/tests/Unit/Configuration/ReadOnlyConfigTest.php b/tests/Unit/Configuration/ReadOnlyConfigTest.php index e06056cb..73b9753b 100644 --- a/tests/Unit/Configuration/ReadOnlyConfigTest.php +++ b/tests/Unit/Configuration/ReadOnlyConfigTest.php @@ -12,24 +12,18 @@ class ReadOnlyConfigTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated_without_any_parameters() - { - $this->assertInstanceOf(Config::class, new ReadOnlyConfig()); - } - - /** @test */ - public function it_returns_the_current_working_directory_by_default() + public function it_returns_the_current_working_directory_by_default(): void { $config = new ReadOnlyConfig(); - $this->assertSame(\getcwd(), $config->getDirPath()); + self::assertSame(\getcwd(), $config->getDirPath()); } /** @test */ - public function it_returns_the_right_dir_path() + public function it_returns_the_right_dir_path(): void { $config = new ReadOnlyConfig('/path/to/config/file.yml'); - $this->assertSame('/path/to/config', $config->getDirPath()); + self::assertSame('/path/to/config', $config->getDirPath()); } } diff --git a/tests/Unit/Configuration/ValidatorTest.php b/tests/Unit/Configuration/ValidatorTest.php index ac395b33..1e03c23b 100644 --- a/tests/Unit/Configuration/ValidatorTest.php +++ b/tests/Unit/Configuration/ValidatorTest.php @@ -1,167 +1,279 @@ -validate($config, []); - - $this->assertSame($defaultValue, $config->$method()); - } - - public function provide_validators_with_default_value(): iterable - { - yield 'CachePath' => [new CachePath(), 'getCachePath', null]; - yield 'CommitsSince' => [new CommitsSince(), 'getCommitsSince', '10 years ago']; - yield 'DirectoriesToScan' => [new DirectoriesToScan(), 'getDirectoriesToScan', []]; - yield 'FileExtensions' => [new FileExtensions(), 'getFileExtensions', ['php']]; - yield 'FilesToIgnore' => [new FilesToIgnore(), 'getFilesToIgnore', []]; - yield 'FilesToShow' => [new FilesToShow(), 'getFilesToShow', 10]; - yield 'Hooks' => [new Hooks(), 'getHooks', []]; - yield 'MaxScoreThreshold' => [new MaxScoreThreshold(), 'getMaxScoreThreshold', null]; - yield 'MinScoreToShow' => [new MinScoreToShow(), 'getMinScoreToShow', 0.1]; - yield 'ParallelJobs' => [new ParallelJobs(), 'getParallelJobs', 10]; - yield 'Vcs' => [new Vcs(), 'getVCS', 'git']; - } - - /** - * @test - * @dataProvider provide_validators_with_given_value - */ - public function it_returns_the_given_value(Validator $validator, string $method, $value): void - { - $config = new EditableConfig(); - $validator->validate($config, [$validator->getKey() => $value]); - - $this->assertSame($value, $config->$method()); - } - - public function provide_validators_with_given_value(): iterable - { - yield 'CachePath' => [new CachePath(), 'getCachePath', '/tmp/.churn.cache']; - yield 'CommitsSince' => [new CommitsSince(), 'getCommitsSince', '4 years ago']; - yield 'DirectoriesToScan' => [new DirectoriesToScan(), 'getDirectoriesToScan', ['src', 'tests']]; - yield 'FileExtensions' => [new FileExtensions(), 'getFileExtensions', ['php', 'inc']]; - yield 'FilesToIgnore' => [new FilesToIgnore(), 'getFilesToIgnore', ['foo.php', 'bar.php', 'baz.php']]; - yield 'FilesToShow' => [new FilesToShow(), 'getFilesToShow', 13]; - yield 'Hooks' => [new Hooks(), 'getHooks', ['Hook1', 'Hook2']]; - yield 'MaxScoreThreshold' => [new MaxScoreThreshold(), 'getMaxScoreThreshold', 9.5]; - yield 'MinScoreToShow' => [new MinScoreToShow(), 'getMinScoreToShow', 5.0]; - yield 'ParallelJobs' => [new ParallelJobs(), 'getParallelJobs', 7]; - yield 'Vcs' => [new Vcs(), 'getVCS', 'none']; - } - - /** - * @test - * @dataProvider provide_validators_accepting_null - */ - 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()); - } - - public function provide_validators_accepting_null(): iterable - { - yield 'CachePath' => [new CachePath(), 'getCachePath']; - yield 'MaxScoreThreshold' => [new MaxScoreThreshold(), 'getMaxScoreThreshold']; - yield 'MinScoreToShow' => [new MinScoreToShow(), 'getMinScoreToShow']; - } - - /** - * @test - * @dataProvider provide_validators_with_invalid_value - */ - 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, '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']; - } - - /** - * @test - */ - public function it_emits_a_deprecation_warning_for_commit_since(): void - { - $deprecationMessage = null; - set_error_handler(function ($_, $errstr) use (&$deprecationMessage) { - $deprecationMessage = $errstr; - - return true; - }, \E_USER_DEPRECATED); - - try { - $config = new EditableConfig(); - $validator = new CommitsSince(); - $validator->validate($config, ['commitSince' => 'one day ago']); - - $this->assertSame('one day ago', $config->getCommitsSince()); - $this->assertSame('commitSince', $validator->getKey()); - } finally { - restore_error_handler(); - } - - $this->assertSame( - 'The "commitSince" configuration key is deprecated and won\'t be supported' - . ' in the next major version anymore. Use "commitsSince" instead.', - $deprecationMessage - ); - } -} +validate($config, []); + + self::assertSame($defaultValue, $getter($config)); + } + + /** + * @return iterable + */ + public function provide_validators_with_default_value(): iterable + { + yield 'CachePath' => [ + new CachePath(), + static function (EditableConfig $config): ?string { return $config->getCachePath(); }, + null, + ]; + yield 'CommitsSince' => [ + new CommitsSince(), + static function (EditableConfig $config): string { return $config->getCommitsSince(); }, + '10 years ago', + ]; + yield 'DirectoriesToScan' => [ + new DirectoriesToScan(), + static function (EditableConfig $config): array { return $config->getDirectoriesToScan(); }, + [], + ]; + yield 'FileExtensions' => [ + new FileExtensions(), + static function (EditableConfig $config): array { return $config->getFileExtensions(); }, + ['php'], + ]; + yield 'FilesToIgnore' => [ + new FilesToIgnore(), + static function (EditableConfig $config): array { return $config->getFilesToIgnore(); }, + [], + ]; + yield 'FilesToShow' => [ + new FilesToShow(), + static function (EditableConfig $config): int { return $config->getFilesToShow(); }, + 10, + ]; + yield 'Hooks' => [ + new Hooks(), + static function (EditableConfig $config): array { return $config->getHooks(); }, + [], + ]; + yield 'MaxScoreThreshold' => [ + new MaxScoreThreshold(), + static function (EditableConfig $config): ?float { return $config->getMaxScoreThreshold(); }, + null, + ]; + yield 'MinScoreToShow' => [ + new MinScoreToShow(), + static function (EditableConfig $config): ?float { return $config->getMinScoreToShow(); }, + 0.1, + ]; + yield 'ParallelJobs' => [ + new ParallelJobs(), + static function (EditableConfig $config): int { return $config->getParallelJobs(); }, + 10, + ]; + yield 'Vcs' => [ + new Vcs(), + static function (EditableConfig $config): string { return $config->getVCS(); }, + 'git', + ]; + } + + /** + * @test + * @dataProvider provide_validators_with_given_value + * @param mixed $value + */ + public function it_returns_the_given_value(Validator $validator, callable $getter, $value): void + { + $config = new EditableConfig(); + $validator->validate($config, [$validator->getKey() => $value]); + + self::assertSame($value, $getter($config)); + } + + /** + * @return iterable + */ + public function provide_validators_with_given_value(): iterable + { + yield 'CachePath' => [ + new CachePath(), + static function (EditableConfig $config): ?string { return $config->getCachePath(); }, + '/tmp/.churn.cache', + ]; + yield 'CommitsSince' => [ + new CommitsSince(), + static function (EditableConfig $config): string { return $config->getCommitsSince(); }, + '4 years ago', + ]; + yield 'DirectoriesToScan' => [ + new DirectoriesToScan(), + static function (EditableConfig $config): array { return $config->getDirectoriesToScan(); }, + ['src', 'tests'], + ]; + yield 'FileExtensions' => [ + new FileExtensions(), + static function (EditableConfig $config): array { return $config->getFileExtensions(); }, + ['php', 'inc'], + ]; + yield 'FilesToIgnore' => [ + new FilesToIgnore(), + static function (EditableConfig $config): array { return $config->getFilesToIgnore(); }, + ['foo.php', 'bar.php', 'baz.php'], + ]; + yield 'FilesToShow' => [ + new FilesToShow(), + static function (EditableConfig $config): int { return $config->getFilesToShow(); }, + 13, + ]; + yield 'Hooks' => [ + new Hooks(), + static function (EditableConfig $config): array { return $config->getHooks(); }, + ['Hook1', 'Hook2'], + ]; + yield 'MaxScoreThreshold' => [ + new MaxScoreThreshold(), + static function (EditableConfig $config): ?float { return $config->getMaxScoreThreshold(); }, + 9.5, + ]; + yield 'MinScoreToShow' => [ + new MinScoreToShow(), + static function (EditableConfig $config): ?float { return $config->getMinScoreToShow(); }, + 5.0, + ]; + yield 'ParallelJobs' => [ + new ParallelJobs(), + static function (EditableConfig $config): int { return $config->getParallelJobs(); }, + 7, + ]; + yield 'Vcs' => [ + new Vcs(), + static function (EditableConfig $config): string { return $config->getVCS(); }, + 'none', + ]; + } + + /** + * @test + * @dataProvider provide_validators_accepting_null + */ + public function it_accepts_null(Validator $validator, callable $getter): 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]); + + self::assertNull($getter($config)); + } + + /** + * @return iterable + */ + public function provide_validators_accepting_null(): iterable + { + yield 'CachePath' => [ + new CachePath(), + static function (EditableConfig $config): ?string { return $config->getCachePath(); }, + ]; + yield 'MaxScoreThreshold' => [ + new MaxScoreThreshold(), + static function (EditableConfig $config): ?float { return $config->getMaxScoreThreshold(); }, + ]; + yield 'MinScoreToShow' => [ + new MinScoreToShow(), + static function (EditableConfig $config): ?float { return $config->getMinScoreToShow(); }, + ]; + } + + /** + * @test + * @dataProvider provide_validators_with_invalid_value + * @param mixed $invalidValue + */ + 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]); + } + + /** + * @return iterable + */ + public function provide_validators_with_invalid_value(): iterable + { + 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']; + } + + /** + * @test + */ + public function it_emits_a_deprecation_warning_for_commit_since(): void + { + $deprecationMessage = null; + set_error_handler(function ($_, $errstr) use (&$deprecationMessage) { + $deprecationMessage = $errstr; + + return true; + }, \E_USER_DEPRECATED); + + try { + $config = new EditableConfig(); + $validator = new CommitsSince(); + $validator->validate($config, ['commitSince' => 'one day ago']); + + self::assertSame('one day ago', $config->getCommitsSince()); + self::assertSame('commitSince', $validator->getKey()); + } finally { + restore_error_handler(); + } + + self::assertSame( + 'The "commitSince" configuration key is deprecated and won\'t be supported' + . ' in the next major version anymore. Use "commitsSince" instead.', + $deprecationMessage + ); + } +} diff --git a/tests/Unit/Event/Event/AfterAnalysisEventTest.php b/tests/Unit/Event/Event/AfterAnalysisEventTest.php index 3bcf85f1..78e54d06 100644 --- a/tests/Unit/Event/Event/AfterAnalysisEventTest.php +++ b/tests/Unit/Event/Event/AfterAnalysisEventTest.php @@ -26,9 +26,9 @@ public function it_can_return_metrics(): void $report->shouldReceive('getMaxScore')->andReturn($maxScore); $event = new AfterAnalysisEvent($report); - $this->assertSame($numberOfFiles, $event->getNumberOfFiles()); - $this->assertSame($maxNumberOfChanges, $event->getMaxNumberOfChanges()); - $this->assertSame($maxCyclomaticComplexity, $event->getMaxCyclomaticComplexity()); - $this->assertSame($maxScore, $event->getMaxScore()); + self::assertSame($numberOfFiles, $event->getNumberOfFiles()); + self::assertSame($maxNumberOfChanges, $event->getMaxNumberOfChanges()); + self::assertSame($maxCyclomaticComplexity, $event->getMaxCyclomaticComplexity()); + self::assertSame($maxScore, $event->getMaxScore()); } } diff --git a/tests/Unit/Event/Event/AfterFileAnalysisEventTest.php b/tests/Unit/Event/Event/AfterFileAnalysisEventTest.php index 55a5bdda..17b32b10 100644 --- a/tests/Unit/Event/Event/AfterFileAnalysisEventTest.php +++ b/tests/Unit/Event/Event/AfterFileAnalysisEventTest.php @@ -26,9 +26,9 @@ public function it_can_return_metrics(): void $result->shouldReceive('getComplexity')->andReturn($cyclomaticComplexity); $event = new AfterFileAnalysisEvent($result); - $this->assertSame($result, $event->getResult()); - $this->assertSame($fullPath, $event->getFilePath()); - $this->assertSame($numberOfChanges, $event->getNumberOfChanges()); - $this->assertSame($cyclomaticComplexity, $event->getCyclomaticComplexity()); + self::assertSame($result, $event->getResult()); + self::assertSame($fullPath, $event->getFilePath()); + self::assertSame($numberOfChanges, $event->getNumberOfChanges()); + self::assertSame($cyclomaticComplexity, $event->getCyclomaticComplexity()); } } diff --git a/tests/Unit/File/FileFinderTest.php b/tests/Unit/File/FileFinderTest.php index 41d2ddf3..9b585657 100644 --- a/tests/Unit/File/FileFinderTest.php +++ b/tests/Unit/File/FileFinderTest.php @@ -15,91 +15,88 @@ class FileFinderTest extends BaseTestCase */ protected $fileFinder; - public function setup() + /** @return void */ + public function setUp() { - $this->fileFinder = new FileFinder(['php'], [], __DIR__); - } + parent::setUp(); - /** @test */ - public function it_can_be_created() - { - $this->assertInstanceOf(FileFinder::class, $this->fileFinder); + $this->fileFinder = new FileFinder(['php'], [], __DIR__); } /** @test */ - public function it_can_get_the_php_files_in_a_filter() + public function it_can_get_the_php_files_in_a_filter(): void { - $this->assertCount(3, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false)); + self::assertCount(3, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false)); } /** @test */ - public function it_can_get_the_php_files_in_multiple_directories() + public function it_can_get_the_php_files_in_multiple_directories(): void { - $this->assertCount(7, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false)); + self::assertCount(7, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false)); } /** @test */ - public function it_can_get_the_php_files_by_name() + public function it_can_get_the_php_files_by_name(): void { - $this->assertCount(2, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/../Assets/Bar.php', __DIR__ . '/../Assets/Foo.php']), false)); + self::assertCount(2, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/../Assets/Bar.php', __DIR__ . '/../Assets/Foo.php']), false)); } /** @test */ - public function it_does_not_throw_with_non_existing_path() + public function it_does_not_throw_with_non_existing_path(): void { - $this->assertCount(0, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/NotExisting.php']), false)); + self::assertCount(0, iterator_to_array($this->fileFinder->getPhpFiles([__DIR__ . '/NotExisting.php']), false)); } /** @test */ - public function it_ignores_files_specified_to_ignore_in_the_config() + public function it_ignores_files_specified_to_ignore_in_the_config(): void { $fileFinder = new FileFinder(['php'], ['Assets/Baz.php'], __DIR__); - $this->assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false)); + self::assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false)); } /** @test */ - public function it_ignores_everything_within_a_folder() + public function it_ignores_everything_within_a_folder(): void { $fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/*'], __DIR__); - $this->assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); $fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/*'], __DIR__); - $this->assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); } /** @test */ - public function it_ignores_everything_starts_with_a_string() + public function it_ignores_everything_starts_with_a_string(): void { $fileFinder = new FileFinder(['php'], ['Assets2/F*'], __DIR__); - $this->assertCount(3, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(3, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); $fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/Deep*'], __DIR__); - $this->assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); $fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/Dif*'], __DIR__); - $this->assertCount(3, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(3, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); } /** @test */ - public function it_ignores_multiple_matching_patterns_in_multiple_folders() + public function it_ignores_multiple_matching_patterns_in_multiple_folders(): void { $fileFinder = new FileFinder(['php'], ['Assets2/F*', 'Assets/B*'], __DIR__); - $this->assertCount(4, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false)); + self::assertCount(4, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false)); $fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/De*', 'Assets/B*'], __DIR__); - $this->assertCount(5, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false)); + self::assertCount(5, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false)); $fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/Di*', 'Assets2/DeepAssets/De*', 'Assets2/F*'], __DIR__); - $this->assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); $fileFinder = new FileFinder(['php', 'inc'], ['Assets2/*.php'], __DIR__); - $this->assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); + self::assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false)); } /** @test */ - public function it_uses_extensions_specified_in_the_config() + public function it_uses_extensions_specified_in_the_config(): void { $fileFinder = new FileFinder(['php', 'inc'], [], __DIR__); - $this->assertCount(4, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false)); + self::assertCount(4, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false)); } } diff --git a/tests/Unit/File/FileHelperTest.php b/tests/Unit/File/FileHelperTest.php index 1cf44a7e..3794a9bf 100644 --- a/tests/Unit/File/FileHelperTest.php +++ b/tests/Unit/File/FileHelperTest.php @@ -16,6 +16,8 @@ class FileHelperTest extends BaseTestCase protected function tearDown() { + parent::tearDown(); + (new Filesystem())->remove($this->filesToDelete); } @@ -25,9 +27,12 @@ protected function tearDown() */ public function it_can_return_absolute_path(string $path, string $confPath, string $expectedPath): void { - $this->assertSame($expectedPath, FileHelper::toAbsolutePath($path, $confPath)); + self::assertSame($expectedPath, FileHelper::toAbsolutePath($path, $confPath)); } + /** + * @return iterable + */ public function provide_absolute_paths(): iterable { yield ['/tmp', '/path', '/tmp']; @@ -45,9 +50,12 @@ public function provide_absolute_paths(): iterable */ public function it_can_return_relative_path(string $path, string $confPath, string $expectedPath): void { - $this->assertSame($expectedPath, FileHelper::toRelativePath($path, $confPath)); + self::assertSame($expectedPath, FileHelper::toRelativePath($path, $confPath)); } + /** + * @return iterable + */ public function provide_relative_paths(): iterable { yield ['/tmp/file.php', '/tmp', 'file.php']; @@ -70,9 +78,12 @@ public function it_can_ensure_a_file_is_writable(string $filePath): void FileHelper::ensureFileIsWritable($filePath); - $this->assertTrue(\is_dir($dirPath), "Directory should exist: " . $dirPath); + self::assertTrue(\is_dir($dirPath), "Directory should exist: " . $dirPath); } + /** + * @return iterable + */ public function provide_writable_paths(): iterable { yield [__FILE__]; @@ -91,6 +102,9 @@ public function it_throws_with_invalid_writable_files(string $filePath): void FileHelper::ensureFileIsWritable($filePath); } + /** + * @return iterable + */ public function provide_invalid_writable_paths(): iterable { yield ['']; diff --git a/tests/Unit/File/FileTest.php b/tests/Unit/File/FileTest.php index ae2f7714..536fe356 100644 --- a/tests/Unit/File/FileTest.php +++ b/tests/Unit/File/FileTest.php @@ -14,6 +14,7 @@ class FileTest extends BaseTestCase **/ private $file; + /** @return void */ public function setUp() { parent::setUp(); @@ -22,15 +23,9 @@ public function setUp() } /** @test */ - public function it_can_be_instantiated() + public function it_can_return_its_values(): void { - $this->assertInstanceOf(File::class, $this->file); - } - - /** @test */ - public function it_can_return_its_values() - { - $this->assertSame('foo/bar/baz.php', $this->file->getFullPath()); - $this->assertSame('bar/baz.php', $this->file->getDisplayPath()); + self::assertSame('foo/bar/baz.php', $this->file->getFullPath()); + self::assertSame('bar/baz.php', $this->file->getDisplayPath()); } } diff --git a/tests/Unit/Process/CacheProcessFactoryTest.php b/tests/Unit/Process/CacheProcessFactoryTest.php index 1f37747e..d3aa308d 100644 --- a/tests/Unit/Process/CacheProcessFactoryTest.php +++ b/tests/Unit/Process/CacheProcessFactoryTest.php @@ -25,6 +25,9 @@ public function it_throws_for_invalid_cache_path(string $cachePath, string $erro new CacheProcessFactory($cachePath, $factory); } + /** + * @return iterable + */ public function provide_invalid_paths(): iterable { yield ['', 'Path cannot be empty']; diff --git a/tests/Unit/Process/ChangesCount/NoVcsChangesCountProcessTest.php b/tests/Unit/Process/ChangesCount/NoVcsChangesCountProcessTest.php index be30299b..1a58f027 100644 --- a/tests/Unit/Process/ChangesCount/NoVcsChangesCountProcessTest.php +++ b/tests/Unit/Process/ChangesCount/NoVcsChangesCountProcessTest.php @@ -16,35 +16,38 @@ class NoVcsChangesCountProcessTest extends BaseTestCase */ private $process; + /** @return void */ protected function setUp() { + parent::setUp(); + $file = new File('/foo', '/foo'); $this->process = new NoVcsChangesCountProcess($file); } /** @test */ - public function it_always_counts_one() + public function it_always_counts_one(): void { - $this->assertSame(1, $this->process->countChanges()); + self::assertSame(1, $this->process->countChanges()); } /** @test */ - public function it_does_not_change_after_starting() + public function it_does_not_change_after_starting(): void { $process = clone $this->process; $process->start(); - $this->assertEqualsCanonicalizing($this->process, $process); + self::assertEqualsCanonicalizing($this->process, $process); } /** @test */ - public function it_is_always_successful() + public function it_is_always_successful(): void { - $this->assertTrue($this->process->isSuccessful()); + self::assertTrue($this->process->isSuccessful()); } /** @test */ - public function it_can_return_the_file() + public function it_can_return_the_file(): void { - $this->assertSame('/foo', $this->process->getFile()->getDisplayPath()); + self::assertSame('/foo', $this->process->getFile()->getDisplayPath()); } } diff --git a/tests/Unit/Process/ConcreteProcessFactoryTest.php b/tests/Unit/Process/ConcreteProcessFactoryTest.php index 1a78495b..9c91afc3 100644 --- a/tests/Unit/Process/ConcreteProcessFactoryTest.php +++ b/tests/Unit/Process/ConcreteProcessFactoryTest.php @@ -11,6 +11,7 @@ use Churn\Process\ConcreteProcessFactory; use Churn\Tests\BaseTestCase; use InvalidArgumentException; +use RuntimeException; class ConcreteProcessFactoryTest extends BaseTestCase { @@ -19,19 +20,19 @@ class ConcreteProcessFactoryTest extends BaseTestCase */ private $processFactory; - public function setup() + /** @return void */ + public function setUp() { + parent::setUp(); + $config = new ReadOnlyConfig(); $this->processFactory = new ConcreteProcessFactory($config->getVCS(), $config->getCommitsSince()); } - /** @test */ - public function it_can_be_created() - { - $this->assertInstanceOf(ConcreteProcessFactory::class, $this->processFactory); - } - - private function extractChangesCountProcess(iterable $processes): ?ChangesCountInterface + /** + * @param iterable $processes + */ + private function extractChangesCountProcess(iterable $processes): ChangesCountInterface { foreach ($processes as $process) { if ($process instanceof ChangesCountInterface) { @@ -39,10 +40,13 @@ private function extractChangesCountProcess(iterable $processes): ?ChangesCountI } } - return null; + throw new RuntimeException('Changes Count process not found'); } - private function extractCyclomaticComplexityProcess(iterable $processes): ?CyclomaticComplexityInterface + /** + * @param iterable $processes + */ + private function extractCyclomaticComplexityProcess(iterable $processes): CyclomaticComplexityInterface { foreach ($processes as $process) { if ($process instanceof CyclomaticComplexityInterface) { @@ -50,29 +54,27 @@ private function extractCyclomaticComplexityProcess(iterable $processes): ?Cyclo } } - return null; + throw new RuntimeException('Cyclomatic Complexity process not found'); } /** @test */ - public function it_can_create_a_git_commit_count_process() + public function it_can_create_a_git_commit_count_process(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = $this->extractChangesCountProcess($this->processFactory->createProcesses($file)); - $this->assertInstanceOf(ChangesCountInterface::class, $process); - $this->assertSame($file, $process->getFile()); + self::assertSame($file, $process->getFile()); } /** @test */ - public function it_can_create_a_cyclomatic_complexity_process() + public function it_can_create_a_cyclomatic_complexity_process(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = $this->extractCyclomaticComplexityProcess($this->processFactory->createProcesses($file)); - $this->assertInstanceOf(CyclomaticComplexityInterface::class, $process); - $this->assertSame($file, $process->getFile()); + self::assertSame($file, $process->getFile()); } /** @test */ - public function it_throws_exception_if_VCS_is_not_supported() + public function it_throws_exception_if_VCS_is_not_supported(): void { $config = new ReadOnlyConfig(); $this->expectException(InvalidArgumentException::class); @@ -81,12 +83,12 @@ public function it_throws_exception_if_VCS_is_not_supported() } /** @test */ - public function it_always_counts_one_when_there_is_no_VCS() + public function it_always_counts_one_when_there_is_no_VCS(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $this->processFactory = new ConcreteProcessFactory('none', ''); $process = $this->extractChangesCountProcess($this->processFactory->createProcesses($file)); - $this->assertSame($file, $process->getFile()); - $this->assertSame(1, $process->countChanges()); + self::assertSame($file, $process->getFile()); + self::assertSame(1, $process->countChanges()); } } diff --git a/tests/Unit/Process/CyclomaticComplexityProcessTest.php b/tests/Unit/Process/CyclomaticComplexityProcessTest.php index 1e28f60a..46ca8a35 100644 --- a/tests/Unit/Process/CyclomaticComplexityProcessTest.php +++ b/tests/Unit/Process/CyclomaticComplexityProcessTest.php @@ -14,16 +14,7 @@ class CyclomaticComplexityProcessTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated() - { - $file = new File('foo/bar/baz.php', 'bar/baz.php'); - $process = new Process(['foo']); - $churnProcess = new CyclomaticComplexityProcess($file, $process); - $this->assertInstanceOf(CyclomaticComplexityProcess::class, $churnProcess); - } - - /** @test */ - public function it_can_be_started() + public function it_can_be_started(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = m::mock(Process::class); @@ -33,27 +24,27 @@ public function it_can_be_started() } /** @test */ - public function it_can_determine_if_it_was_successful() + public function it_can_determine_if_it_was_successful(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = m::mock(Process::class); $process->shouldReceive('getExitCode')->andReturn(0); $churnProcess = new CyclomaticComplexityProcess($file, $process); - $this->assertTrue($churnProcess->isSuccessful()); + self::assertTrue($churnProcess->isSuccessful()); } /** @test */ - public function it_can_determine_if_it_was_unsuccessful() + public function it_can_determine_if_it_was_unsuccessful(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = m::mock(Process::class); $process->shouldReceive('getExitCode')->andReturn(null); $churnProcess = new CyclomaticComplexityProcess($file, $process); - $this->assertFalse($churnProcess->isSuccessful()); + self::assertFalse($churnProcess->isSuccessful()); } /** @test */ - public function it_throws_with_positive_exit_code() + public function it_throws_with_positive_exit_code(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = m::mock(Process::class); @@ -67,21 +58,21 @@ public function it_throws_with_positive_exit_code() } /** @test */ - public function it_can_get_the_file_it_is_processing() + public function it_can_get_the_file_it_is_processing(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = m::mock(Process::class); $churnProcess = new CyclomaticComplexityProcess($file, $process); - $this->assertSame($file, $churnProcess->getFile()); + self::assertSame($file, $churnProcess->getFile()); } /** @test */ - public function it_can_get_the_cyclomatic_complexity() + public function it_can_get_the_cyclomatic_complexity(): void { $file = new File('foo/bar/baz.php', 'bar/baz.php'); $process = m::mock(Process::class); $process->shouldReceive('getOutput')->andReturn('123'); $churnProcess = new CyclomaticComplexityProcess($file, $process); - $this->assertSame(123, $churnProcess->getCyclomaticComplexity()); + self::assertSame(123, $churnProcess->getCyclomaticComplexity()); } } diff --git a/tests/Unit/Process/Handler/ParallelProcessHandlerTest.php b/tests/Unit/Process/Handler/ParallelProcessHandlerTest.php index 1c8a67bb..73069e6a 100644 --- a/tests/Unit/Process/Handler/ParallelProcessHandlerTest.php +++ b/tests/Unit/Process/Handler/ParallelProcessHandlerTest.php @@ -18,27 +18,19 @@ class ParallelProcessHandlerTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated() + public function it_doesnt_call_the_observer_when_no_file(): void { $broker = m::mock(Broker::class); - $this->assertInstanceOf(ParallelProcessHandler::class, new ParallelProcessHandler(2, $broker)); - } + $broker->shouldReceive('notify')->never(); - /** @test */ - public function it_doesnt_call_the_observer_when_no_file() - { - $broker = m::mock(Broker::class); $processHandler = new ParallelProcessHandler(3, $broker); $processFactory = new ConcreteProcessFactory('none', ''); - $observer = m::mock(OnSuccess::class); - $observer->shouldReceive('__invoke')->never(); - - $processHandler->process($this->getFileGenerator(), $processFactory, $observer); + $processHandler->process($this->getFileGenerator(), $processFactory); } /** @test */ - public function it_calls_the_broker_for_one_file() + public function it_calls_the_broker_for_one_file(): void { $file = new File(__FILE__, __FILE__); diff --git a/tests/Unit/Process/Handler/SequentialProcessHandlerTest.php b/tests/Unit/Process/Handler/SequentialProcessHandlerTest.php index 6bc11017..bce72d2a 100644 --- a/tests/Unit/Process/Handler/SequentialProcessHandlerTest.php +++ b/tests/Unit/Process/Handler/SequentialProcessHandlerTest.php @@ -17,14 +17,7 @@ class SequentialProcessHandlerTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated() - { - $broker = m::mock(Broker::class); - $this->assertInstanceOf(SequentialProcessHandler::class, new SequentialProcessHandler($broker)); - } - - /** @test */ - public function it_calls_the_broker_for_one_file() + public function it_calls_the_broker_for_one_file(): void { $process1 = m::mock(ChangesCountInterface::class); $process1->shouldReceive('start'); diff --git a/tests/Unit/Process/ProcessHandlerFactoryTest.php b/tests/Unit/Process/ProcessHandlerFactoryTest.php index bc033d96..d18822b0 100644 --- a/tests/Unit/Process/ProcessHandlerFactoryTest.php +++ b/tests/Unit/Process/ProcessHandlerFactoryTest.php @@ -14,24 +14,21 @@ class ProcessHandlerFactoryTest extends BaseTestCase { - /** @test */ - public function it_can_be_instantiated() - { - $this->assertInstanceOf(ProcessHandlerFactory::class, new ProcessHandlerFactory()); - } - /** * @test * @dataProvider provide_config_with_process_handler */ - public function it_returns_the_right_process_handler(Config $config, string $expectedClassName) + public function it_returns_the_right_process_handler(Config $config, string $expectedClassName): void { $broker = m::mock(Broker::class); $factory = new ProcessHandlerFactory(); $processHandler = $factory->getProcessHandler($config, $broker); - $this->assertSame($expectedClassName, get_class($processHandler)); + self::assertSame($expectedClassName, get_class($processHandler)); } + /** + * @return iterable + */ public function provide_config_with_process_handler(): iterable { $config = m::mock(Config::class); diff --git a/tests/Unit/Result/HighestScoresTest.php b/tests/Unit/Result/HighestScoresTest.php index bc02e2b6..91ab69b1 100644 --- a/tests/Unit/Result/HighestScoresTest.php +++ b/tests/Unit/Result/HighestScoresTest.php @@ -12,7 +12,7 @@ class HighestScoresTest extends BaseTestCase { /** @test */ - public function it_keeps_results_sorted_by_priority() + public function it_keeps_results_sorted_by_priority(): void { $scores = new HighestScores(10); $scores->add(m::mock(ResultInterface::class, ['getPriority' => 2])); @@ -21,14 +21,14 @@ public function it_keeps_results_sorted_by_priority() $results = $scores->toArray(); - $this->assertCount(3, $results); - $this->assertSame(3, $results[0]->getPriority()); - $this->assertSame(2, $results[1]->getPriority()); - $this->assertSame(1, $results[2]->getPriority()); + self::assertCount(3, $results); + self::assertSame(3, $results[0]->getPriority()); + self::assertSame(2, $results[1]->getPriority()); + self::assertSame(1, $results[2]->getPriority()); } /** @test */ - public function it_keeps_ony_the_highest_priorities() + public function it_keeps_ony_the_highest_priorities(): void { $scores = new HighestScores(3); $scores->add(m::mock(ResultInterface::class, ['getPriority' => 4])); @@ -39,9 +39,9 @@ public function it_keeps_ony_the_highest_priorities() $results = $scores->toArray(); - $this->assertCount(3, $results); - $this->assertSame(5, $results[0]->getPriority()); - $this->assertSame(4, $results[1]->getPriority()); - $this->assertSame(3, $results[2]->getPriority()); + self::assertCount(3, $results); + self::assertSame(5, $results[0]->getPriority()); + self::assertSame(4, $results[1]->getPriority()); + self::assertSame(3, $results[2]->getPriority()); } } diff --git a/tests/Unit/Result/Render/CsvResultsRendererTest.php b/tests/Unit/Result/Render/CsvResultsRendererTest.php index 5f6771ba..18c579ad 100644 --- a/tests/Unit/Result/Render/CsvResultsRendererTest.php +++ b/tests/Unit/Result/Render/CsvResultsRendererTest.php @@ -12,13 +12,7 @@ class CsvResultsRendererTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated() - { - $this->assertInstanceOf(CsvResultsRenderer::class, new CsvResultsRenderer()); - } - - /** @test */ - public function it_can_render_the_results_as_csv() + public function it_can_render_the_results_as_csv(): void { $results = [ ['filename1.php', 5, 7, 0.625], diff --git a/tests/Unit/Result/Render/JsonResultsRendererTest.php b/tests/Unit/Result/Render/JsonResultsRendererTest.php index 9e91299e..145999ef 100644 --- a/tests/Unit/Result/Render/JsonResultsRendererTest.php +++ b/tests/Unit/Result/Render/JsonResultsRendererTest.php @@ -12,13 +12,7 @@ class JsonResultsRendererTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated() - { - $this->assertInstanceOf(JsonResultsRenderer::class, new JsonResultsRenderer()); - } - - /** @test */ - public function it_can_render_the_results_as_json() + public function it_can_render_the_results_as_json(): void { $results = [ ['filename1.php', 5, 7, 0.625], diff --git a/tests/Unit/Result/Render/MarkdownResultsRendererTest.php b/tests/Unit/Result/Render/MarkdownResultsRendererTest.php index 6c0c27e4..b6dca973 100644 --- a/tests/Unit/Result/Render/MarkdownResultsRendererTest.php +++ b/tests/Unit/Result/Render/MarkdownResultsRendererTest.php @@ -12,13 +12,7 @@ class MarkdownResultsRendererTest extends BaseTestCase { /** @test */ - public function it_can_be_instantiated() - { - $this->assertInstanceOf(MarkdownResultsRenderer::class, new MarkdownResultsRenderer()); - } - - /** @test */ - public function it_can_render_the_results_as_markdown() + public function it_can_render_the_results_as_markdown(): void { $results = [ ['filename1.php', 5, 7, 0.625], diff --git a/tests/Unit/Result/ResultAccumulatorTest.php b/tests/Unit/Result/ResultAccumulatorTest.php index db7e06d6..6d0fbb86 100644 --- a/tests/Unit/Result/ResultAccumulatorTest.php +++ b/tests/Unit/Result/ResultAccumulatorTest.php @@ -25,14 +25,14 @@ private function mockResult(int $commits, int $complexity, string $file, float $ } /** @test */ - public function it_returns_max_commits() + public function it_returns_max_commits(): void { $accumulator = new ResultAccumulator(10, 0.1); $accumulator->add($this->mockResult(2, 1, 'file')); $accumulator->add($this->mockResult(1, 1, 'file')); $accumulator->add($this->mockResult(4, 1, 'file')); $accumulator->add($this->mockResult(3, 1, 'file')); - $this->assertSame(4, $accumulator->getMaxCommits()); + self::assertSame(4, $accumulator->getMaxCommits()); } /** @test */ @@ -43,7 +43,7 @@ public function it_returns_max_complexity(): void $accumulator->add($this->mockResult(1, 1, 'file')); $accumulator->add($this->mockResult(1, 4, 'file')); $accumulator->add($this->mockResult(1, 3, 'file')); - $this->assertSame(4, $accumulator->getMaxComplexity()); + self::assertSame(4, $accumulator->getMaxComplexity()); } /** @test */ @@ -53,7 +53,7 @@ public function it_returns_the_number_of_files(): void $accumulator->add($this->mockResult(1, 2, 'file')); $accumulator->add($this->mockResult(1, 1, 'file')); $accumulator->add($this->mockResult(1, 4, 'file')); - $this->assertSame(3, $accumulator->getNumberOfFiles()); + self::assertSame(3, $accumulator->getNumberOfFiles()); } /** @test */ @@ -70,7 +70,7 @@ public function it_returns_metrics_as_an_array(): void ['file3', 1, 4, 0.1], ]; - $this->assertSame($expectedResult, $accumulator->toArray()); + self::assertSame($expectedResult, $accumulator->toArray()); } /** @test */ @@ -89,7 +89,7 @@ public function it_takes_min_score_into_account(): void ['file3', 1, 4, 0.1], ]; - $this->assertSame($expectedResult, $accumulator->toArray()); + self::assertSame($expectedResult, $accumulator->toArray()); } /** @test */ @@ -100,7 +100,7 @@ public function it_ignores_files_with_a_score_of_zero(): void $accumulator->add($this->mockResult(1, 0, 'file2', 0.2)); $accumulator->add($this->mockResult(0, 0, 'file3', 0.1)); - $this->assertSame(0, $accumulator->getNumberOfFiles()); + self::assertSame(0, $accumulator->getNumberOfFiles()); } /** @test */ @@ -108,12 +108,12 @@ public function it_returns_the_max_score(): void { $accumulator = new ResultAccumulator(10, 0.1); - $this->assertNull($accumulator->getMaxScore()); + self::assertNull($accumulator->getMaxScore()); $accumulator->add($this->mockResult(5, 1, 'file2', 0.2)); $accumulator->add($this->mockResult(10, 2, 'file1', 0.3)); $accumulator->add($this->mockResult(1, 4, 'file3', 0.1)); - $this->assertSame(0.3, $accumulator->getMaxScore()); + self::assertSame(0.3, $accumulator->getMaxScore()); } } diff --git a/tests/Unit/Result/ResultTest.php b/tests/Unit/Result/ResultTest.php index 442fd21e..e12a20a9 100644 --- a/tests/Unit/Result/ResultTest.php +++ b/tests/Unit/Result/ResultTest.php @@ -17,67 +17,67 @@ class ResultTest extends BaseTestCase */ protected $result; - public function setup() + /** @return void */ + public function setUp() { + parent::setUp(); + $this->result = new Result(new File('/filename.php', 'filename.php')); $this->result->setCommits(5); $this->result->setComplexity(7); } /** @test */ - public function it_can_be_created() - { - $this->assertInstanceOf(Result::class, $this->result); - } - - /** @test */ - public function it_can_return_the_file() + public function it_can_return_the_file(): void { - $this->assertSame('filename.php', $this->result->getFile()->getDisplayPath()); + self::assertSame('filename.php', $this->result->getFile()->getDisplayPath()); } /** @test */ - public function it_is_complete() + public function it_is_complete(): void { - $this->assertTrue($this->result->isComplete()); + self::assertTrue($this->result->isComplete()); } /** @test */ - public function it_can_return_the_commits() + public function it_can_return_the_commits(): void { - $this->assertSame(5, $this->result->getCommits()); + self::assertSame(5, $this->result->getCommits()); } /** @test */ - public function it_can_return_the_complexity() + public function it_can_return_the_complexity(): void { - $this->assertSame(7, $this->result->getComplexity()); + self::assertSame(7, $this->result->getComplexity()); } /** @test */ - public function it_can_return_the_priority() + public function it_can_return_the_priority(): void { - $this->assertSame(5 * 7, $this->result->getPriority()); + self::assertSame(5 * 7, $this->result->getPriority()); } /** @test */ - public function it_can_calculate_the_score() + public function it_can_calculate_the_score(): void { $maxCommits = 10; $maxComplexity = 10; - $this->assertSame(0.417, $this->result->getScore($maxCommits, $maxComplexity)); + self::assertSame(0.417, $this->result->getScore($maxCommits, $maxComplexity)); } /** * @test * @dataProvider provide_uncomplete_result */ - public function it_returns_false_when_uncomplete(Result $result) + public function it_returns_false_when_uncomplete(Result $result): void { - $this->assertFalse($result->isComplete()); + self::assertFalse($result->isComplete()); } + /** + * @return iterable + */ public function provide_uncomplete_result(): iterable { $file = new File('/filename.php', 'filename.php'); @@ -97,13 +97,16 @@ public function provide_uncomplete_result(): iterable * @test * @dataProvider provide_invalid_score */ - public function it_throws_when_score_is_invalid(int $maxCommits, int $maxComplexity) + public function it_throws_when_score_is_invalid(int $maxCommits, int $maxComplexity): void { $this->expectException(InvalidArgumentException::class); $this->result->getScore($maxCommits, $maxComplexity); } + /** + * @return iterable + */ public function provide_invalid_score(): iterable { yield [0, 0]; diff --git a/tests/Unit/Result/ResultsRendererFactoryTest.php b/tests/Unit/Result/ResultsRendererFactoryTest.php index a0d73e00..573946c4 100644 --- a/tests/Unit/Result/ResultsRendererFactoryTest.php +++ b/tests/Unit/Result/ResultsRendererFactoryTest.php @@ -19,47 +19,44 @@ class ResultsRendererFactoryTest extends BaseTestCase */ private $factory; - /** @test */ - public function it_can_be_created() + /** @return void */ + public function setUp() { - $this->assertInstanceOf(ResultsRendererFactory::class, $this->factory); + parent::setUp(); + + $this->factory = new ResultsRendererFactory(); } /** @test */ - public function it_returns_the_json_renderer_when_provided_json_format() + public function it_returns_the_json_renderer_when_provided_json_format(): void { - $this->assertInstanceOf(JsonResultsRenderer::class, $this->factory->getRenderer('json')); + self::assertInstanceOf(JsonResultsRenderer::class, $this->factory->getRenderer('json')); } /** @test */ - public function it_returns_the_csv_renderer_when_provided_csv_format() + public function it_returns_the_csv_renderer_when_provided_csv_format(): void { - $this->assertInstanceOf(CsvResultsRenderer::class, $this->factory->getRenderer('csv')); + self::assertInstanceOf(CsvResultsRenderer::class, $this->factory->getRenderer('csv')); } /** @test */ - public function it_returns_the_markdown_renderer_when_provided_markdown_format() + public function it_returns_the_markdown_renderer_when_provided_markdown_format(): void { - $this->assertInstanceOf(MarkdownResultsRenderer::class, $this->factory->getRenderer('markdown')); + self::assertInstanceOf(MarkdownResultsRenderer::class, $this->factory->getRenderer('markdown')); } /** @test */ - public function it_returns_the_console_renderer_when_provided_text_format() + public function it_returns_the_console_renderer_when_provided_text_format(): void { $factory = new ResultsRendererFactory(); - $this->assertInstanceOf(ConsoleResultsRenderer::class, $this->factory->getRenderer('text')); + self::assertInstanceOf(ConsoleResultsRenderer::class, $this->factory->getRenderer('text')); } /** @test */ - public function it_throws_exception_if_format_is_invalid() + public function it_throws_exception_if_format_is_invalid(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid output format provided'); $this->factory->getRenderer('foobar'); } - - public function setUp() - { - $this->factory = new ResultsRendererFactory(); - } }