From 3b9cea1c70641dc3e260da53e486dce38891e4b7 Mon Sep 17 00:00:00 2001 From: Jens True Date: Mon, 10 Jul 2023 09:20:36 +0000 Subject: [PATCH] Static analysis of unittest code. --- Makefile | 10 +++---- tests/CommandBackupTest.php | 33 ++++++++++---------- tests/CommandShowTest.php | 25 +++++++++------- tests/Notification/NotificationTest.php | 11 +++++-- tests/Notification/NtfyTest.php | 40 +++++++++++++------------ tests/Rclone/RcloneTest.php | 27 +++++++++-------- 6 files changed, 81 insertions(+), 65 deletions(-) diff --git a/Makefile b/Makefile index b893394..a31f800 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,13 @@ analyze: analyze-yaml analyze-phpmd analyze-phpstan analyze-psalm analyze-phpcs analyze-yaml: vendor/bin/yaml-lint *.yml analyze-phpmd: - vendor/bin/phpmd src text cleancode,codesize,controversial,design,naming,unusedcode + vendor/bin/phpmd src,tests text cleancode,codesize,controversial,design,naming,unusedcode analyze-phpstan: - vendor/bin/phpstan analyze --level=8 --error-format=raw src/ backup + vendor/bin/phpstan analyze --level=8 --error-format=raw src/ backup tests analyze-psalm: vendor/bin/psalm --no-cache analyze-phpcs: - vendor/bin/phpcs src backup --report=emacs --standard=PSR12 + vendor/bin/phpcs src backup tests --report=emacs --standard=PSR12 docs: ./phpDocumentor.phar --setting=graphs.enabled=true @@ -19,7 +19,7 @@ install: install-dev: php composer.phar install test: - vendor/bin/phpunit tests + vendor/bin/phpunit tests --display-warnings test-coverage: - vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage --coverage-text --branch-coverage --testdox-html output/test.html + vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage --coverage-text --path-coverage --testdox-html output/test.html \ No newline at end of file diff --git a/tests/CommandBackupTest.php b/tests/CommandBackupTest.php index 22c5c1b..86a26b8 100644 --- a/tests/CommandBackupTest.php +++ b/tests/CommandBackupTest.php @@ -1,27 +1,31 @@ -&1'); - } protected function tearDown(): void { - exec('rclone purge temp 2>&1', $output); + exec('rclone purge temp 2>&1'); } - - public function testBadConfig() + public function testBadConfig(): void { $applicationd = new Application('backup', "1.1.1"); @@ -30,15 +34,14 @@ class CommandBackupTest extends \PHPUnit\Framework\TestCase $command = $applicationd->find('backup'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"bad_file"]); + $commandTester->execute(['config' => "bad_file"]); $output = $commandTester->getDisplay(); $this->assertStringContainsString('[ERROR] Configuration error:File "bad_file" does not exist.', $output); } - public function testGoodConfig() + public function testGoodConfig(): void { - $applicationd = new Application('backup', "1.1.1"); $applicationd->add(new CommandBackup()); @@ -46,13 +49,13 @@ class CommandBackupTest extends \PHPUnit\Framework\TestCase $command = $applicationd->find('backup'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"config.example.yml"]); + $commandTester->execute(['config' => "config.example.yml"]); $output = $commandTester->getDisplay(); $this->assertStringContainsString('[OK] Complete ', $output); } - public function testNoDestinationFolder() + public function testNoDestinationFolder(): void { exec('rclone purge temp/destination 2>&1', $output); $applicationd = new Application('backup', "1.1.1"); @@ -62,13 +65,13 @@ class CommandBackupTest extends \PHPUnit\Framework\TestCase $command = $applicationd->find('backup'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"config.example.yml"]); + $commandTester->execute(['config' => "config.example.yml"]); $output = $commandTester->getDisplay(); $this->assertStringContainsString('[OK] Complete ', $output); } - public function testNoSourceFolder() + public function testNoSourceFolder(): void { exec('rclone purge temp/source 2>&1', $output); $applicationd = new Application('backup', "1.1.1"); @@ -78,9 +81,9 @@ class CommandBackupTest extends \PHPUnit\Framework\TestCase $command = $applicationd->find('backup'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"config.example.yml"]); + $commandTester->execute(['config' => "config.example.yml"]); $output = $commandTester->getDisplay(); $this->assertStringContainsString('[OK] Complete ', $output); } -} \ No newline at end of file +} diff --git a/tests/CommandShowTest.php b/tests/CommandShowTest.php index 2682fdb..fa53034 100644 --- a/tests/CommandShowTest.php +++ b/tests/CommandShowTest.php @@ -1,12 +1,19 @@ -find('show'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"bad_file"]); + $commandTester->execute(['config' => "bad_file"]); $output = $commandTester->getDisplay(); $this->assertStringContainsString('[ERROR] Configuration error: File "bad_file" does not exist.', $output); } - public function testGoodConfig() + public function testGoodConfig(): void { exec('rclone test makefiles temp/source 2>&1'); - + $applicationd = new Application('backup', "1.1.1"); $applicationd->add(new CommandShow()); @@ -33,13 +40,11 @@ class CommandShowTest extends \PHPUnit\Framework\TestCase $command = $applicationd->find('show'); $commandTester = new CommandTester($command); - $commandTester->execute(['config'=>"config.example.yml"]); + $commandTester->execute(['config' => "config.example.yml"]); $output = $commandTester->getDisplay(); $this->assertStringContainsString('Example', $output); $this->assertStringContainsString('temp/source', $output); $this->assertStringContainsString('temp/destination', $output); } - - -} \ No newline at end of file +} diff --git a/tests/Notification/NotificationTest.php b/tests/Notification/NotificationTest.php index 9c21da9..239218e 100644 --- a/tests/Notification/NotificationTest.php +++ b/tests/Notification/NotificationTest.php @@ -1,4 +1,8 @@ -'ntfy', 'domain'=>'https://test.com', 'topic'=>'testing']; + /** @var array */ + private static array $config = ['type' => 'ntfy', 'domain' => 'https://test.com', 'topic' => 'testing']; public function testloadSingle(): void { @@ -47,4 +52,4 @@ final class NotificationTest extends TestCase $mock->expects($this->once())->method('send'); $dut->send('title', 'topic'); } -} \ No newline at end of file +} diff --git a/tests/Notification/NtfyTest.php b/tests/Notification/NtfyTest.php index 71e9cb2..dbb36bf 100644 --- a/tests/Notification/NtfyTest.php +++ b/tests/Notification/NtfyTest.php @@ -1,16 +1,20 @@ -instance = new Ntfy($this->client); } - protected function tearDown(): void - { - $this->instance = null; - } - + /** + * @SuppressWarnings(PHPMD.StaticAccess) + */ public function testFactory(): void { - $config = ['domain'=>'https://test.com', 'topic'=>'something']; + $config = ['domain' => 'https://test.com', 'topic' => 'something']; $instance = Ntfy::factory($config); $this->assertInstanceOf(Ntfy::class, $instance); } @@ -33,10 +35,10 @@ final class NtfyTest extends TestCase public function testSend(): void { $this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class)); - $this->instance->send('title','text'); + $this->instance->send('title', 'text'); } - + /** @return array> */ public static function sendBadParameterProvider(): array { return [ @@ -49,9 +51,9 @@ final class NtfyTest extends TestCase } #[DataProvider('sendBadParameterProvider')] - public function testSendInvalidParameters($title, $message): void + public function testSendInvalidParameters(string $title, string $message): void { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $this->instance->send($title, $message); } @@ -62,6 +64,7 @@ final class NtfyTest extends TestCase $this->assertEquals($topic, $this->instance->getTopic()); } + /** @return array> */ public static function topicBadParameterProvider(): array { return [ @@ -72,10 +75,9 @@ final class NtfyTest extends TestCase } #[DataProvider('topicBadParameterProvider')] - public function testSetTopicInvalidParameters( $topic ): void + public function testSetTopicInvalidParameters(string $topic): void { - $this->expectException(InvalidArgumentException::class); - $this->instance->setTopic( $topic ); - + $this->expectException(\InvalidArgumentException::class); + $this->instance->setTopic($topic); } -} \ No newline at end of file +} diff --git a/tests/Rclone/RcloneTest.php b/tests/Rclone/RcloneTest.php index 9be950a..4d7b25d 100644 --- a/tests/Rclone/RcloneTest.php +++ b/tests/Rclone/RcloneTest.php @@ -1,4 +1,8 @@ -&1'); } protected function tearDown(): void { - exec('rclone purge temp 2>&1', $output); + exec('rclone purge temp 2>&1'); } @@ -23,20 +24,20 @@ final class RcloneTest extends TestCase { $this->expectException(\Exception::class); $rclone = new Rclone('invalid'); - $this->assertString('', $rclone->getVersion()); + $this->assertEquals('', $rclone->getVersion()); } public function testRcloneInvalidVersion(): void { $this->expectException(\Exception::class); $rclone = new Rclone('uname'); - $this->assertString('', $rclone->getVersion()); + $this->assertEquals('', $rclone->getVersion()); } public function testRcloneValidVersion(): void { $rclone = new Rclone(); - $this->assertStringContainsString('rclone', $rclone->getVersion()); + $this->assertStringStartsWith('rclone', $rclone->getVersion()); } public function testRcloneSize(): void @@ -54,16 +55,16 @@ final class RcloneTest extends TestCase public function testRcloneCopy(): void { $rclone = new Rclone(); - $result = $rclone->copy('temp/source','temp/destination'); + $rclone->copy('temp/source', 'temp/destination'); $this->assertDirectoryExists('temp/destination'); $rclone = new Rclone(); - $result = $rclone->copy('temp/source','temp/destination',['bwlimit'=>'6M']); + $rclone->copy('temp/source', 'temp/destination', ['bwlimit' => '6M']); $this->assertDirectoryExists('temp/destination'); - + $this->expectException(\Exception::class); $this->expectExceptionMessage("ERROR"); - $result = $rclone->copy('temp/bogus-source','temp/bogus-destination'); + $rclone->copy('temp/bogus-source', 'temp/bogus-destination'); $this->assertDirectoryDoesNotExist('temp/bogus-destination'); } -} \ No newline at end of file +}