Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@
"psr-4": {
"WordPress\\Plugin_Check\\": "includes/"
}
},
"autoload-dev": {
"psr-4": {
"WordPress\\Plugin_Check\\Test_Data\\": "tests/testdata"
}
}
}
123 changes: 123 additions & 0 deletions includes/Checker/Checks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* Class WordPress\Plugin_Check\Checker\Checks
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\Checker;

use WordPress\Plugin_Check\Checker\Check_Context;
use Exception;

/**
* Class to run checks on a plugin.
*
* @since n.e.x.t
*/
class Checks {

/**
* Context for the plugin to check.
*
* @since n.e.x.t
* @var Check_Context
*/
protected $check_context;

/**
* Sets the main context and the main file of the plugin to check.
*
* @since n.e.x.t
*
* @param string $plugin_main_file Absolute path to the plugin main file.
*/
public function __construct( $plugin_main_file ) {
$this->check_context = new Check_Context( $plugin_main_file );
}

/**
* Runs checks against the plugin.
*
* @since n.e.x.t
*
* @param array $checks An array of Check objects to run.
* @return Check_Result Object containing all check results.
*
* @throws Exception Thrown when check fails with critical error.
*/
public function run_checks( array $checks ) {
$result = new Check_Result( $this->check_context );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jjgrainger Do we needs to add use WordPress\Plugin_Check\Checker\Check_Result;?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshpanchal27 it's not necessarily a requirement as it shares the same namespace, but I see no harm in explicitly defining this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really needed, I think we can avoid that in the future. But okay to keep for now.

$all_checks = $this->get_checks();

// Create an array of Check objects to run based on the check names passed.
$checks_to_run = array_filter(
$checks,
function( $check ) use ( $all_checks ) {
return in_array( $check, $all_checks, true );
}
);

// Run the checks.
array_walk(
$checks_to_run,
function( Check $check ) use ( $result ) {
$this->run_check_with_result( $check, $result );
}
);

return $result;
}

/**
* Runs a given check with the given result object to amend.
*
* @since n.e.x.t
*
* @param Check $check The check to run.
* @param Check_Result $result The result object to amend.
*
* @throws Exception Thrown when check fails with critical error.
*/
protected function run_check_with_result( Check $check, Check_Result $result ) {
// If $check implements Preparation interface, ensure the preparation and clean up is run.
if ( $check instanceof Preparation ) {
$cleanup = $check->prepare();

try {
$check->run( $result );
} catch ( Exception $e ) {
// Run clean up in case of any exception thrown from check.
$cleanup();
throw $e;
}

$cleanup();
return;
}

// Otherwise, just run the check.
$check->run( $result );
}

/**
* Gets the available plugin check classes.
*
* @since n.e.x.t
*
* @return array List of plugin check class instances implementing the Check interface.
*/
public function get_checks() {
// TODO: Add checks once implemented.
$checks = array();

/**
* Filters the available plugin check classes.
*
* @since n.e.x.t
*
* @param array $checks List of plugin check class instances implementing the Check interface.
*/
return apply_filters( 'wp_plugin_check_checks', $checks );
}
}
87 changes: 87 additions & 0 deletions tests/Checker/Checks_Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Tests for the Checks class.
*
* @package plugin-check
*/

use Exception;
use WordPress\Plugin_Check\Checker\Check_Result;
use WordPress\Plugin_Check\Checker\Checks;

class Checks_Tests extends WP_UnitTestCase {

protected $checks;

public function set_up() {
parent::set_up();

$this->checks = new Checks( 'test-plugin/test-plugin.php' );
}

public function test_get_checks_returns_array_of_expected_checks() {
Comment thread
jjgrainger marked this conversation as resolved.
$expected = array(
new WordPress\Plugin_Check\Test_Data\Empty_Check(),
new WordPress\Plugin_Check\Test_Data\Error_Check(),
);

add_filter(
'wp_plugin_check_checks',
function( $checks ) use ( $expected ) {
return $expected;
}
);

$checks = $this->checks->get_checks();

$this->assertIsArray( $checks );
$this->assertSame( $expected, $checks );
}

public function test_run_checks() {
$all_checks = array(
new WordPress\Plugin_Check\Test_Data\Empty_Check(),
new WordPress\Plugin_Check\Test_Data\Error_Check(),
);

$checks_to_run = array(
$all_checks[0],
);

add_filter(
'wp_plugin_check_checks',
function( $checks ) use ( $all_checks ) {
return $all_checks;
}
);

$results = $this->checks->run_checks( $checks_to_run );

$this->assertInstanceOf( Check_Result::class, $results );
$this->assertEmpty( $results->get_warnings() );
$this->assertEmpty( $results->get_errors() );
}

public function test_run_checks_with_error() {
$all_checks = array(
new WordPress\Plugin_Check\Test_Data\Empty_Check(),
new WordPress\Plugin_Check\Test_Data\Error_Check(),
);

$checks_to_run = array(
$all_checks[1],
);

add_filter(
'wp_plugin_check_checks',
function( $checks ) use ( $all_checks ) {
return $all_checks;
}
);

$results = $this->checks->run_checks( $checks_to_run );

$this->assertEmpty( $results->get_warnings() );
$this->assertNotEmpty( $results->get_errors() );
}
}
12 changes: 12 additions & 0 deletions tests/testdata/Checks/Empty_Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace WordPress\Plugin_Check\Test_Data;

use WordPress\Plugin_Check\Checker\Check;
use WordPress\Plugin_Check\Checker\Check_Result;

class Empty_Check implements Check {
public function run( Check_Result $check_result ) {
return;
}
}
21 changes: 21 additions & 0 deletions tests/testdata/Checks/Error_Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace WordPress\Plugin_Check\Test_Data;

use WordPress\Plugin_Check\Checker\Check;
use WordPress\Plugin_Check\Checker\Check_Result;

class Error_Check implements Check {
public function run( Check_Result $check_result ) {
$check_result->add_message(
true,
'Error message',
array(
'code' => 'check_error',
'file' => 'error-file.php',
'line' => 10,
'column' => 5,
)
);
}
}