-
Notifications
You must be signed in to change notification settings - Fork 98
Create Checks class #57
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f9a6d8b
create Checks class
jjgrainger b227986
add tests
jjgrainger b377148
update checks test
jjgrainger ced6a12
Merge branch 'trunk' into feature/create-checks-class
jjgrainger fb2792b
remove prepare method and property
jjgrainger 538efef
remove references to main context
jjgrainger 493eb62
add run_checks method
jjgrainger d769688
update composer autoload-dev
jjgrainger 00487f7
update check matching and tests
jjgrainger 56a1891
Merge branch 'trunk' into feature/create-checks-class
felixarntz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); | ||
| $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 ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { | ||
|
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() ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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;?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.