diff --git a/includes/Checker/Check_Result.php b/includes/Checker/Check_Result.php index 70ca9f55..e501e1eb 100644 --- a/includes/Checker/Check_Result.php +++ b/includes/Checker/Check_Result.php @@ -7,11 +7,182 @@ namespace WordPress\Plugin_Check\Checker; +use WordPress\Plugin_Check\Checker\Check_Context; + /** * Result for running checks on a plugin. * * @since n.e.x.t */ class Check_Result { - // @TODO: Complete as part of issue #11. + + /** + * Context for the plugin to check. + * + * @since n.e.x.t + * @var Check_Context + */ + protected $check_context; + + /** + * List of errors. + * + * @since n.e.x.t + * @var array + */ + protected $errors = array(); + + /** + * List of warnings. + * + * @since n.e.x.t + * @var array + */ + protected $warnings = array(); + + /** + * Number of errors. + * + * @since n.e.x.t + * @var int + */ + protected $error_count = 0; + + /** + * Number of warnings. + * + * @since n.e.x.t + * @var int + */ + protected $warning_count = 0; + + /** + * Sets the context for the plugin to check. + * + * @since n.e.x.t + * + * @param Check_Context $check_context Check context instance for the plugin. + */ + public function __construct( Check_Context $check_context ) { + $this->check_context = $check_context; + } + + /** + * Returns the context for the plugin to check. + * + * @since n.e.x.t + * + * @return Check_Context Plugin context instance. + */ + public function plugin() { + return $this->check_context; + } + + /** + * Adds an error or warning to the respective stack. + * + * @since n.e.x.t + * + * @param bool $error Whether it is an error message. + * @param string $message The message. + * @param array $args { + * Additional message arguments. + * + * @type string $code Violation code according to the message. Default empty string. + * @type string $file The file in which the message occurred. Default empty string (unknown file). + * @type int $line The line on which the message occurred. Default 0 (unknown line). + * @type int $column The column on which the message occurred. Default 0 (unknown column). + * } + */ + public function add_message( $error, $message, $args = array() ) { + $defaults = array( + 'code' => '', + 'file' => '', + 'line' => 0, + 'column' => 0, + ); + + $data = array_merge( + array( + 'message' => $message, + ), + $defaults, + array_intersect_key( $args, $defaults ) + ); + + $file = str_replace( $this->plugin()->path( '/' ), '', $data['file'] ); + $line = $data['line']; + $column = $data['column']; + unset( $data['line'], $data['column'], $data['file'] ); + + if ( $error ) { + if ( ! isset( $this->errors[ $file ] ) ) { + $this->errors[ $file ] = array(); + } + if ( ! isset( $this->errors[ $file ][ $line ] ) ) { + $this->errors[ $file ][ $line ] = array(); + } + if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) { + $this->errors[ $file ][ $line ][ $column ] = array(); + } + $this->errors[ $file ][ $line ][ $column ][] = $data; + $this->error_count++; + } else { + if ( ! isset( $this->warnings[ $file ] ) ) { + $this->warnings[ $file ] = array(); + } + if ( ! isset( $this->warnings[ $file ][ $line ] ) ) { + $this->warnings[ $file ][ $line ] = array(); + } + if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) { + $this->warnings[ $file ][ $line ][ $column ] = array(); + } + $this->warnings[ $file ][ $line ][ $column ][] = $data; + $this->warning_count++; + } + } + + /** + * Returns all errors. + * + * @since n.e.x.t + * + * @return array All errors with their data. + */ + public function get_errors() { + return $this->errors; + } + + /** + * Returns all warnings. + * + * @since n.e.x.t + * + * @return array All warnings with their data. + */ + public function get_warnings() { + return $this->warnings; + } + + /** + * Returns the number of errors. + * + * @since n.e.x.t + * + * @return int Number of errors found. + */ + public function get_error_count() { + return $this->error_count; + } + + /** + * Returns the number of warnings. + * + * @since n.e.x.t + * + * @return int Number of warnings found. + */ + public function get_warning_count() { + return $this->warning_count; + } } diff --git a/tests/Checker/Check_Result_Tests.php b/tests/Checker/Check_Result_Tests.php new file mode 100644 index 00000000..3c3bdb98 --- /dev/null +++ b/tests/Checker/Check_Result_Tests.php @@ -0,0 +1,179 @@ +check_result = new Check_Result( $check_context ); + } + + public function test_plugin() { + $this->assertInstanceOf( Check_Context::class, $this->check_result->plugin() ); + + // Check the Check_Context has the correct basename. + $this->assertSame( 'test-plugin/test-plugin.php', $this->check_result->plugin()->basename() ); + } + + public function test_add_message_with_warning() { + $this->check_result->add_message( + false, + 'Warning message', + array( + 'code' => 'test_warning', + 'file' => 'test-plugin/test-plugin.php', + 'line' => 12, + 'column' => 40, + ) + ); + + $warnings = $this->check_result->get_warnings(); + + // Tests that warnings contains an error. + $this->assertNotEmpty( $warnings ); + + // Tests warnings count incremented correctly. + $this->assertEquals( 1, $this->check_result->get_warning_count() ); + + // Tests no errors were added or error count incrememeted. + $this->assertEmpty( $this->check_result->get_errors() ); + $this->assertEquals( 0, $this->check_result->get_error_count() ); + + // Tests the warning exists in the array. + $expected = array( + 'message' => 'Warning message', + 'code' => 'test_warning', + ); + + $this->assertEquals( $expected, $warnings['test-plugin.php'][12][40][0] ); + } + + public function test_add_message_with_error() { + $this->check_result->add_message( + true, + 'Error message', + array( + 'code' => 'test_error', + 'file' => 'test-plugin/test-plugin.php', + 'line' => 22, + 'column' => 30, + ) + ); + + $errors = $this->check_result->get_errors(); + + // Tests that errors contains an error. + $this->assertNotEmpty( $errors ); + + // Tests errors count incremented correctly. + $this->assertEquals( 1, $this->check_result->get_error_count() ); + + // Tests no warnings were added or warnings count incrememeted. + $this->assertEmpty( $this->check_result->get_warnings() ); + $this->assertEquals( 0, $this->check_result->get_warning_count() ); + + // Tests the error exists in the array. + $expected = array( + 'message' => 'Error message', + 'code' => 'test_error', + ); + + $this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] ); + } + + public function test_get_errors() { + $this->assertEmpty( $this->check_result->get_errors() ); + } + + public function test_get_errors_with_errors() { + $this->check_result->add_message( + true, + 'Error message', + array( + 'code' => 'test_error', + 'file' => 'test-plugin/test-plugin.php', + 'line' => 22, + 'column' => 30, + ) + ); + + $errors = $this->check_result->get_errors(); + + // Tests errors are not empty. + $this->assertNotEmpty( $errors ); + + // Tests the error exists in the array. + $expected = array( + 'message' => 'Error message', + 'code' => 'test_error', + ); + + $this->assertEquals( $expected, $errors['test-plugin.php'][22][30][0] ); + } + + public function test_get_warnings() { + $this->assertEmpty( $this->check_result->get_warnings() ); + } + + public function test_get_warnings_with_warnings() { + $this->check_result->add_message( + false, + 'Warning message', + array( + 'code' => 'test_warning', + 'file' => 'test-plugin/test-plugin.php', + 'line' => 22, + 'column' => 30, + ) + ); + + $warnings = $this->check_result->get_warnings(); + + // Tests warnings are not empty. + $this->assertNotEmpty( $warnings ); + + // Tests the warning exists in the array. + $expected = array( + 'message' => 'Warning message', + 'code' => 'test_warning', + ); + + $this->assertEquals( $expected, $warnings['test-plugin.php'][22][30][0] ); + } + + public function test_get_warning_count() { + $this->assertEquals( 0, $this->check_result->get_warning_count() ); + } + + public function test_get_warning_count_with_message() { + $this->check_result->add_message( false, 'Warning message' ); + + $this->assertEquals( 1, $this->check_result->get_warning_count() ); + } + + public function test_get_error_count() { + $this->assertEquals( 0, $this->check_result->get_error_count() ); + } + + public function test_get_error_count_with_message() { + $this->check_result->add_message( true, 'Error message' ); + + $this->assertEquals( 1, $this->check_result->get_error_count() ); + } +}