-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e2a2ed
commit dddb163
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,49 @@ | ||
<?php | ||
/** | ||
* Class WordPress\Plugin_Check\Checker\Checks\Enqueued_Resources_Check | ||
* | ||
* @package plugin-check | ||
*/ | ||
|
||
namespace WordPress\Plugin_Check\Checker\Checks; | ||
|
||
use WordPress\Plugin_Check\Checker\Check_Categories; | ||
use WordPress\Plugin_Check\Traits\Stable_Check; | ||
|
||
/** | ||
* Check for running WordPress enqueued resources sniffs. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
class Enqueued_Resources_Check extends Abstract_PHP_CodeSniffer_Check { | ||
|
||
use Stable_Check; | ||
|
||
/** | ||
* Gets the categories for the check. | ||
* | ||
* Every check must have at least one category. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return array The categories for the check. | ||
*/ | ||
public function get_categories() { | ||
return array( Check_Categories::CATEGORY_PERFORMANCE ); | ||
} | ||
|
||
/** | ||
* Returns an associative array of arguments to pass to PHPCS. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @return array An associative array of PHPCS CLI arguments. | ||
*/ | ||
protected function get_args() { | ||
return array( | ||
'extensions' => 'php', | ||
'standard' => 'WordPress', | ||
'sniffs' => 'WordPress.WP.EnqueuedResources', | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tests/phpunit/testdata/plugins/test-plugin-enqueued-resources-with-errors/load.php
This file contains 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,20 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Test Plugin Enqueued Resources check with errors | ||
* Plugin URI: https://github.com/wordpress/plugin-check | ||
* Description: Test plugin for the Enqueued Resources check. | ||
* Requires at least: 6.0 | ||
* Requires PHP: 5.6 | ||
* Version: 1.0.0 | ||
* Author: WordPress Performance Team | ||
* Author URI: https://make.wordpress.org/performance/ | ||
* License: GPLv2 or later | ||
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
* Text Domain: test-plugin-enqueued-resources-with-errors | ||
* | ||
* @package test-plugin-enqueued-resources-with-errors | ||
*/ | ||
|
||
?> | ||
<link rel="stylesheet" href="http://someurl/somefile.css"> | ||
<script src="http://someurl/somefile.js"></script> |
30 changes: 30 additions & 0 deletions
30
tests/phpunit/testdata/plugins/test-plugin-enqueued-resources-without-errors/load.php
This file contains 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,30 @@ | ||
<?php | ||
/** | ||
* Plugin Name: Test Plugin Enqueued Resources check without errors | ||
* Plugin URI: https://github.com/wordpress/plugin-check | ||
* Description: Test plugin for the Enqueued Resources check. | ||
* Requires at least: 6.0 | ||
* Requires PHP: 5.6 | ||
* Version: 1.0.0 | ||
* Author: WordPress Performance Team | ||
* Author URI: https://make.wordpress.org/performance/ | ||
* License: GPLv2 or later | ||
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
* Text Domain: test-plugin-enqueued-resources-without-errors | ||
* | ||
* @package test-plugin-enqueued-resources-without-errors | ||
*/ | ||
|
||
add_action( | ||
'wp_enqueue_scripts', | ||
function() { | ||
wp_enqueue_script( | ||
'plugin_check_script', | ||
'http://someurl/somefile.js' | ||
); | ||
wp_enqueue_style( | ||
'plugin_check_style', | ||
'http://someurl/somefile.css' | ||
); | ||
} | ||
); |
40 changes: 40 additions & 0 deletions
40
tests/phpunit/tests/Checker/Checks/Enqueued_Resources_Check_Tests.php
This file contains 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,40 @@ | ||
<?php | ||
/** | ||
* Tests for the Enqueued_Resources_Check class. | ||
* | ||
* @package plugin-check | ||
*/ | ||
|
||
use WordPress\Plugin_Check\Checker\Check_Context; | ||
use WordPress\Plugin_Check\Checker\Check_Result; | ||
use WordPress\Plugin_Check\Checker\Checks\Enqueued_Resources_Check; | ||
|
||
class Enqueued_Resources_Check_Tests extends WP_UnitTestCase { | ||
|
||
public function test_run_with_errors() { | ||
$check = new Enqueued_Resources_Check(); | ||
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-enqueued-resources-with-errors/load.php' ); | ||
$check_result = new Check_Result( $check_context ); | ||
|
||
$check->run( $check_result ); | ||
|
||
$errors = $check_result->get_errors(); | ||
|
||
$this->assertNotEmpty( $errors ); | ||
$this->assertArrayHasKey( 'load.php', $errors ); | ||
$this->assertEquals( 2, $check_result->get_error_count() ); | ||
} | ||
|
||
public function test_run_without_errors() { | ||
$check = new Enqueued_Resources_Check(); | ||
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-enqueued-resources-without-errors/load.php' ); | ||
$check_result = new Check_Result( $check_context ); | ||
|
||
$check->run( $check_result ); | ||
|
||
$errors = $check_result->get_errors(); | ||
|
||
$this->assertEmpty( $errors ); | ||
$this->assertEquals( 0, $check_result->get_error_count() ); | ||
} | ||
} |