Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Check_Result class #50

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 172 additions & 1 deletion includes/Checker/Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
201 changes: 201 additions & 0 deletions tests/Checker/Check_Result_Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php
/**
* Tests for the Check_Result class.
*
* @package plugin-check
*/

use WordPress\Plugin_Check\Checker\Check_Context;
use WordPress\Plugin_Check\Checker\Check_Result;

class Check_Result_Tests extends WP_UnitTestCase {
public function set_up() {
parent::set_up();

$check_context = new Check_Context( 'test-plugin/test-plugin.php' );

$this->check_result = new Check_Result( $check_context );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_plugin() {
$this->assertInstanceOf( Check_Context::class, $this->check_result->plugin() );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_add_message() {
$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 the filename used as the main key for the message associated with that file.
$this->assertArrayHasKey( 'test-plugin.php', $warnings );

// Tests the line number is used as the first key for the filename array.
$this->assertArrayHasKey( 12, $warnings['test-plugin.php'] );

// Tests the column is used as the first key for the line number array.
$this->assertArrayHasKey( 40, $warnings['test-plugin.php'][12] );

// Tests the column array contains the message details.
$expected = array(
'message' => 'Warning message',
'code' => 'test_warning',
);

$this->assertEquals( $expected, $warnings['test-plugin.php'][12][40][0] );
}
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

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() );
}
}