From 103e40ab5ca05f09d8264659bf0dccc4380fd7c7 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 31 Oct 2023 03:22:16 +0100 Subject: [PATCH] Reports/Threshold: add dedicated tests Note: the value of the `'YOASTCS_ABOVE_THRESHOLD` constant cannot be tested as that runs into trouble with how PHPCS creates the test suite when using `@runInSeparateProcess`. --- Yoast/Reports/Threshold.php | 5 +- Yoast/Tests/Reports/ThresholdReportTest.php | 192 ++++++++++++++++++++ phpunit.xml.dist | 1 + 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 Yoast/Tests/Reports/ThresholdReportTest.php diff --git a/Yoast/Reports/Threshold.php b/Yoast/Reports/Threshold.php index 22e46ab9..e4bb243f 100644 --- a/Yoast/Reports/Threshold.php +++ b/Yoast/Reports/Threshold.php @@ -155,6 +155,9 @@ public function generate( } // Make the threshold comparison outcome available to the calling script. - \define( 'YOASTCS_ABOVE_THRESHOLD', $above_threshold ); + // The conditional define is only so as to make the method testable. + if ( \defined( 'YOASTCS_ABOVE_THRESHOLD' ) === false ) { + \define( 'YOASTCS_ABOVE_THRESHOLD', $above_threshold ); + } } } diff --git a/Yoast/Tests/Reports/ThresholdReportTest.php b/Yoast/Tests/Reports/ThresholdReportTest.php new file mode 100644 index 00000000..138e9588 --- /dev/null +++ b/Yoast/Tests/Reports/ThresholdReportTest.php @@ -0,0 +1,192 @@ +get_mock_file(); + $report = [ + 'errors' => $errors, + 'warnings' => $warnings, + ]; + + $threshold_report = new Threshold(); + + $this->assertSame( $expected, $threshold_report->generateFileReport( $report, $phpcsFile ) ); + } + + /** + * Data provider. + * + * @see test_generate_file_report() For the array format. + * + * @return array> + */ + public static function data_generate_file_report() { + return [ + 'no errors, no warnings' => [ + 'errors' => 0, + 'warnings' => 0, + 'expected' => false, + ], + 'has errors, no warnings' => [ + 'errors' => 10, + 'warnings' => 0, + 'expected' => true, + ], + 'no errors, has warnings' => [ + 'errors' => 0, + 'warnings' => 5, + 'expected' => true, + ], + 'has errors and warnings' => [ + 'errors' => 3, + 'warnings' => 5, + 'expected' => true, + ], + ]; + } + + /** + * Test creating the threshold report. + * + * @dataProvider data_generate + * @covers ::generate + * + * @param int $error_threshold Allowed nr of errors. + * @param int $warning_threshold Allowed nr of warnings. + * @param string $expected_output Expected screen output regex. + * + * @return void + */ + public function test_generate( $error_threshold, $warning_threshold, $expected_output ) { + // phpcs:disable WordPress.PHP.DiscouragedPHPFunctions + \putenv( "YOASTCS_THRESHOLD_ERRORS=$error_threshold" ); + \putenv( "YOASTCS_THRESHOLD_WARNINGS=$warning_threshold" ); + // phpcs:enable + + $this->expectOutputRegex( $expected_output ); + + $report = new Threshold(); + $report->generate( + '', + 10, // Files. + 150, // Errors. + 300, // Warnings. + 10 // Fixable. + ); + } + + /** + * Data provider. + * + * @see test_generate() For the array format. + * + * @return array> + */ + public static function data_generate() { + return [ + 'Threshold: no errors, no warnings allowed' => [ + 'error_threshold' => 0, + 'warning_threshold' => 0, + 'expected_output' => '`\s+' + . '\\033\[1mPHP CODE SNIFFER THRESHOLD COMPARISON\\033\[0m\s+' + . '-{80}\s+' + . '\\033\[31mCoding standards ERRORS: 150/0\.\\033\[0m\s+' + . '\\033\[33mCoding standards WARNINGS: 300/0\.\\033\[0m\s+' + . '\\033\[31mPlease fix any errors introduced in your code and run PHPCS again to verify\.\\033\[0m\s+' + . '\\033\[33mPlease fix any warnings introduced in your code and run PHPCS again to verify\.\\033\[0m\s+' + . '`', + ], + 'Threshold: both errors and warnings below threshold' => [ + 'error_threshold' => 160, + 'warning_threshold' => 320, + 'expected_output' => '`\s+' + . '\\033\[1mPHP CODE SNIFFER THRESHOLD COMPARISON\\033\[0m\s+' + . '-{80}\s+' + . '\\033\[32mCoding standards ERRORS: 150/160\.\\033\[0m\s+' + . '\\033\[32mCoding standards WARNINGS: 300/320\.\\033\[0m\s+' + . '\\033\[32mFound less errors than the threshold, great job!\\033\[0m\s+' + . 'Please update the ERRORS threshold in the composer\.json file to \\033\[32m150\.\\033\[0m\s+' + . '\\033\[32mFound less warnings than the threshold, great job!\\033\[0m\s+' + . 'Please update the WARNINGS threshold in the composer.json file to \\033\[32m300\.\\033\[0m\s+' + . 'Coding standards checks have passed!\s+' + . '`', + ], + 'Threshold: both errors and warnings exactly at threshold' => [ + 'error_threshold' => 150, + 'warning_threshold' => 300, + 'expected_output' => '`\s+' + . '\\033\[1mPHP CODE SNIFFER THRESHOLD COMPARISON\\033\[0m\s+' + . '-{80}\s+' + . '\\033\[32mCoding standards ERRORS: 150/150\.\\033\[0m\s+' + . '\\033\[32mCoding standards WARNINGS: 300/300\.\\033\[0m\s+' + . 'Coding standards checks have passed!\s+' + . '`', + ], + 'Threshold: errors below threshold, warnings above' => [ + 'error_threshold' => 155, + 'warning_threshold' => 250, + 'expected_output' => '`\s+' + . '\\033\[1mPHP CODE SNIFFER THRESHOLD COMPARISON\\033\[0m\s+' + . '-{80}\s+' + . '\\033\[32mCoding standards ERRORS: 150/155\.\\033\[0m\s+' + . '\\033\[33mCoding standards WARNINGS: 300/250\.\\033\[0m\s+' + . '\\033\[32mFound less errors than the threshold, great job!\\033\[0m\s+' + . 'Please update the ERRORS threshold in the composer\.json file to \\033\[32m150\.\\033\[0m\s+' + . '\\033\[33mPlease fix any warnings introduced in your code and run PHPCS again to verify\.\\033\[0m\s+' + . '`', + ], + 'Threshold: errors above threshold, warnings below' => [ + 'error_threshold' => 100, + 'warning_threshold' => 500, + 'expected_output' => '`\s+' + . '\\033\[1mPHP CODE SNIFFER THRESHOLD COMPARISON\\033\[0m\s+' + . '-{80}\s+' + . '\\033\[31mCoding standards ERRORS: 150/100\.\\033\[0m\s+' + . '\\033\[32mCoding standards WARNINGS: 300/500\.\\033\[0m\s+' + . '\\033\[31mPlease fix any errors introduced in your code and run PHPCS again to verify\.\\033\[0m\s+' + . '\\033\[32mFound less warnings than the threshold, great job!\\033\[0m\s+' + . 'Please update the WARNINGS threshold in the composer.json file to \\033\[32m300\.\\033\[0m\s+' + . '`', + ], + 'Threshold: both errors and warnings above threshold' => [ + 'error_threshold' => 100, + 'warning_threshold' => 200, + 'expected_output' => '`\s+' + . '\\033\[1mPHP CODE SNIFFER THRESHOLD COMPARISON\\033\[0m\s+' + . '-{80}\s+' + . '\\033\[31mCoding standards ERRORS: 150/100\.\\033\[0m\s+' + . '\\033\[33mCoding standards WARNINGS: 300/200\.\\033\[0m\s+' + . '\\033\[31mPlease fix any errors introduced in your code and run PHPCS again to verify\.\\033\[0m\s+' + . '\\033\[33mPlease fix any warnings introduced in your code and run PHPCS again to verify\.\\033\[0m\s+' + . '`', + ], + ]; + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 65ec8d33..98dcffd4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,6 +21,7 @@ + Yoast/Reports/ Yoast/Sniffs/ Yoast/Utils/