From cca469e690ce692290854e660409ed112bbb849b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 25 Apr 2024 16:33:38 +0200 Subject: [PATCH 1/4] Test commands descriptions follow naming conventions --- .../EnsureCodeFollowsNamingConventionTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php index bd59ea62ab8..4ec0157f2f0 100644 --- a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php +++ b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php @@ -28,6 +28,23 @@ public function testCommandsClassesFollowNamingConventions() } } + public function testCommandsDescriptionsFollowNamingConventions() + { + $files = glob('vendor/hyde/framework/src/Console/Commands/*.php'); + + $this->assertNotEmpty($files, 'No commands found.'); + + // Commands must have a string $description property + foreach ($files as $filepath) { + $class = 'Hyde\\Console\\Commands\\'.basename($filepath, '.php'); + $reflection = new ReflectionClass($class); + + $this->assertTrue($reflection->hasProperty('description') && $reflection->getProperty('description')->isProtected(), + "Command class $class does not have a protected \$description property.\n ".realpath($filepath) + ); + } + } + public function testActionEntryPointsFollowNamingConventions() { $files = glob('vendor/hyde/framework/src/Framework/Actions/*.php'); From 09385392753ad11ca5d0478e1bdceb002291ab46 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 25 Apr 2024 16:38:13 +0200 Subject: [PATCH 2/4] Run assertions on the descriptions --- .../EnsureCodeFollowsNamingConventionTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php index 4ec0157f2f0..6dd86a930e8 100644 --- a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php +++ b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php @@ -4,7 +4,9 @@ namespace Hyde\Framework\Testing\Unit; +use Illuminate\Filesystem\Filesystem; use Hyde\Framework\Actions\BladeMatterParser; +use Hyde\Console\Commands\VendorPublishCommand; use Hyde\Framework\Actions\CreatesNewMarkdownPostFile; use Hyde\Framework\Actions\CreatesNewPageSourceFile; use Hyde\Framework\Actions\MarkdownFileParser; @@ -30,6 +32,8 @@ public function testCommandsClassesFollowNamingConventions() public function testCommandsDescriptionsFollowNamingConventions() { + self::mockConfig(); + $files = glob('vendor/hyde/framework/src/Console/Commands/*.php'); $this->assertNotEmpty($files, 'No commands found.'); @@ -42,6 +46,18 @@ public function testCommandsDescriptionsFollowNamingConventions() $this->assertTrue($reflection->hasProperty('description') && $reflection->getProperty('description')->isProtected(), "Command class $class does not have a protected \$description property.\n ".realpath($filepath) ); + + if ($class === VendorPublishCommand::class) { + $params = [new Filesystem()]; + } else { + $params = []; + } + + $instance = new $class(...$params); + $description = $reflection->getProperty('description')->getValue($instance); + + $this->assertIsString($description); + $this->assertNotEmpty($description); } } From 092e7ff55c7ee7e253b8dac212b1c3e0b8359fe7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 25 Apr 2024 16:38:36 +0200 Subject: [PATCH 3/4] Get description using accessor instead of reflection --- .../tests/Unit/EnsureCodeFollowsNamingConventionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php index 6dd86a930e8..d1a10aad6b4 100644 --- a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php +++ b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php @@ -54,7 +54,7 @@ public function testCommandsDescriptionsFollowNamingConventions() } $instance = new $class(...$params); - $description = $reflection->getProperty('description')->getValue($instance); + $description = $instance->getDescription(); $this->assertIsString($description); $this->assertNotEmpty($description); From 3378926c26e46f5823d070a8dbf2f537247c0341 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 25 Apr 2024 16:49:28 +0200 Subject: [PATCH 4/4] Assert the descriptions follow naming conventions --- .../EnsureCodeFollowsNamingConventionTest.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php index d1a10aad6b4..8f9a638f2c4 100644 --- a/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php +++ b/packages/framework/tests/Unit/EnsureCodeFollowsNamingConventionTest.php @@ -58,6 +58,26 @@ public function testCommandsDescriptionsFollowNamingConventions() $this->assertIsString($description); $this->assertNotEmpty($description); + + $this->assertTrue($description[0] === strtoupper($description[0]), + "Command class $class description does not start with an uppercase letter.\n ".realpath($filepath) + ); + + $this->assertTrue($description[strlen($description) - 1] !== '.' && $description[strlen($description) - 1] !== '!' && $description[strlen($description) - 1] !== '?', + "Command class $class description ends with a period or another punctuation mark.\n ".realpath($filepath) + ); + + $this->assertSame($description, trim($description), + 'Command class '.$class.' description has leading or trailing whitespace.' + ); + + $this->assertStringNotContainsString(' ', $description, + 'Command class '.$class.' description has multiple consecutive spaces.' + ); + + $this->assertStringNotContainsString("\n", $description, + 'Command class '.$class.' description has a newline character.' + ); } }