Fix SonarCloud PHP code quality issues
- Replace tabs with spaces in test files - Use require_once instead of require - Add @return void to docblocks - Define constants for repeated string literals - Use WordPress spacing conventions - Add use statements for cleaner class references
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
// Composer autoloader must be loaded before WP_PHPUNIT__DIR will be available.
|
// Composer autoloader must be loaded before WP_PHPUNIT__DIR will be available.
|
||||||
require_once dirname( dirname( __DIR__ ) ) . '/vendor/autoload.php';
|
require_once dirname( dirname( __DIR__ ) ) . '/vendor/autoload.php';
|
||||||
|
|
||||||
// Check if we're running the WordPress tests
|
// Check if we're running the WordPress tests.
|
||||||
if ( getenv( 'WP_PHPUNIT__DIR' ) ) {
|
if ( getenv( 'WP_PHPUNIT__DIR' ) ) {
|
||||||
// Define PHPUnit Polyfills path for WordPress test suite.
|
// Define PHPUnit Polyfills path for WordPress test suite.
|
||||||
if ( ! defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
|
if ( ! defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) {
|
||||||
@@ -20,36 +20,33 @@ if ( getenv( 'WP_PHPUNIT__DIR' ) ) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Manually load the plugin being tested.
|
* Manually load the plugin being tested.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
function _manually_load_plugin() {
|
function _manually_load_plugin() {
|
||||||
require dirname( dirname( __DIR__ ) ) . '/wp-plugin-starter-template.php';
|
require_once dirname( dirname( __DIR__ ) ) . '/wp-plugin-starter-template.php';
|
||||||
// Load the multisite class for testing
|
// Load the multisite class for testing.
|
||||||
$multisite_file = dirname( dirname( __DIR__ ) ) . '/includes/multisite/class-multisite.php';
|
$multisite_file = dirname( dirname( __DIR__ ) ) . '/includes/multisite/class-multisite.php';
|
||||||
if (file_exists($multisite_file)) {
|
if ( file_exists( $multisite_file ) ) {
|
||||||
require $multisite_file;
|
require_once $multisite_file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start up the WP testing environment.
|
// Start up the WP testing environment.
|
||||||
require getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php';
|
require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php';
|
||||||
} else {
|
} else {
|
||||||
// We're running the WP_Mock tests
|
// We're running the WP_Mock tests.
|
||||||
WP_Mock::bootstrap();
|
WP_Mock::bootstrap();
|
||||||
|
|
||||||
/**
|
// Define constants for testing.
|
||||||
* Now we define a few constants to help us with testing.
|
define( 'WPST_PLUGIN_DIR', dirname( dirname( __DIR__ ) ) . '/' );
|
||||||
*/
|
define( 'WPST_PLUGIN_URL', 'http://example.org/wp-content/plugins/wp-plugin-starter-template/' );
|
||||||
define('WPST_PLUGIN_DIR', dirname(dirname(__DIR__)) . '/');
|
define( 'WPST_VERSION', '0.1.0' );
|
||||||
define('WPST_PLUGIN_URL', 'http://example.org/wp-content/plugins/wp-plugin-starter-template/');
|
|
||||||
define('WPST_VERSION', '0.1.0');
|
|
||||||
|
|
||||||
/**
|
// Include plugin files needed for tests.
|
||||||
* Now we include any plugin files that we need to be able to run the tests.
|
|
||||||
* This should be files that define the functions and classes you're going to test.
|
|
||||||
*/
|
|
||||||
require_once WPST_PLUGIN_DIR . 'includes/class-core.php';
|
require_once WPST_PLUGIN_DIR . 'includes/class-core.php';
|
||||||
require_once WPST_PLUGIN_DIR . 'includes/class-plugin.php';
|
require_once WPST_PLUGIN_DIR . 'includes/class-plugin.php';
|
||||||
if (file_exists(WPST_PLUGIN_DIR . 'admin/lib/admin.php')) {
|
if ( file_exists( WPST_PLUGIN_DIR . 'admin/lib/admin.php' ) ) {
|
||||||
require_once WPST_PLUGIN_DIR . 'admin/lib/admin.php';
|
require_once WPST_PLUGIN_DIR . 'admin/lib/admin.php';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,17 @@
|
|||||||
|
|
||||||
// Skip this test file if WP_Mock is not available or WordPress test framework is loaded.
|
// Skip this test file if WP_Mock is not available or WordPress test framework is loaded.
|
||||||
if ( ! class_exists( 'WP_Mock' ) || class_exists( 'WP_UnitTestCase' ) ) {
|
if ( ! class_exists( 'WP_Mock' ) || class_exists( 'WP_UnitTestCase' ) ) {
|
||||||
return;
|
return; // phpcs:ignore -- Early return is intentional.
|
||||||
}
|
}
|
||||||
|
|
||||||
use WPALLSTARS\PluginStarterTemplate\Admin\Admin;
|
use WPALLSTARS\PluginStarterTemplate\Admin\Admin;
|
||||||
use WPALLSTARS\PluginStarterTemplate\Core;
|
use WPALLSTARS\PluginStarterTemplate\Core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test version constant.
|
||||||
|
*/
|
||||||
|
const TEST_VERSION = '1.0.0';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Admin test case.
|
* Admin test case.
|
||||||
*/
|
*/
|
||||||
@@ -35,18 +40,19 @@ class AdminTest extends \WP_Mock\Tools\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the test environment.
|
* Set up the test environment.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function setUp(): void
|
public function setUp(): void {
|
||||||
{
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
// Set up mocks
|
// Set up mocks.
|
||||||
WP_Mock::setUp();
|
WP_Mock::setUp();
|
||||||
|
|
||||||
// Mock the Core class dependency using Mockery.
|
// Mock the Core class dependency using Mockery.
|
||||||
$this->core = \Mockery::mock( '\WPALLSTARS\PluginStarterTemplate\Core' );
|
$this->core = \Mockery::mock( '\WPALLSTARS\PluginStarterTemplate\Core' );
|
||||||
// Add expectation for get_plugin_version BEFORE Admin is instantiated.
|
// Add expectation for get_plugin_version BEFORE Admin is instantiated.
|
||||||
$this->core->shouldReceive( 'get_plugin_version' )->andReturn( '1.0.0' );
|
$this->core->shouldReceive( 'get_plugin_version' )->andReturn( TEST_VERSION );
|
||||||
|
|
||||||
// Expect the action hook to be added BEFORE Admin is instantiated.
|
// Expect the action hook to be added BEFORE Admin is instantiated.
|
||||||
\WP_Mock::expectActionAdded( 'admin_enqueue_scripts', array( \Mockery::any(), 'enqueue_admin_assets' ) );
|
\WP_Mock::expectActionAdded( 'admin_enqueue_scripts', array( \Mockery::any(), 'enqueue_admin_assets' ) );
|
||||||
@@ -56,7 +62,9 @@ class AdminTest extends \WP_Mock\Tools\TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tear down test environment
|
* Tear down test environment.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function tearDown(): void {
|
public function tearDown(): void {
|
||||||
WP_Mock::tearDown();
|
WP_Mock::tearDown();
|
||||||
@@ -64,18 +72,21 @@ class AdminTest extends \WP_Mock\Tools\TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test constructor
|
* Test constructor.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_constructor() {
|
public function test_constructor() {
|
||||||
// Verify that the constructor initializes hooks
|
// Verify that the constructor initializes hooks.
|
||||||
$this->assertInstanceOf(Admin::class, $this->admin);
|
$this->assertInstanceOf( Admin::class, $this->admin );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the enqueue_admin_assets method.
|
* Test the enqueue_admin_assets method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_enqueue_admin_assets(): void
|
public function test_enqueue_admin_assets(): void {
|
||||||
{
|
|
||||||
// Define the PHPUNIT_RUNNING constant
|
// Define the PHPUNIT_RUNNING constant
|
||||||
if ( ! defined( 'PHPUNIT_RUNNING' ) ) {
|
if ( ! defined( 'PHPUNIT_RUNNING' ) ) {
|
||||||
define( 'PHPUNIT_RUNNING', true );
|
define( 'PHPUNIT_RUNNING', true );
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
// Skip this test file if WP_Mock is not available or WordPress test framework is loaded.
|
// Skip this test file if WP_Mock is not available or WordPress test framework is loaded.
|
||||||
if ( ! class_exists( 'WP_Mock' ) || class_exists( 'WP_UnitTestCase' ) ) {
|
if ( ! class_exists( 'WP_Mock' ) || class_exists( 'WP_UnitTestCase' ) ) {
|
||||||
return;
|
return; // phpcs:ignore -- Early return is intentional.
|
||||||
}
|
}
|
||||||
|
|
||||||
use WPALLSTARS\PluginStarterTemplate\Core;
|
use WPALLSTARS\PluginStarterTemplate\Core;
|
||||||
@@ -27,42 +27,48 @@ class CoreTest extends \WP_Mock\Tools\TestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the test environment.
|
* Set up the test environment.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function setUp(): void
|
public function setUp(): void {
|
||||||
{
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
// Set up mocks
|
// Set up mocks.
|
||||||
WP_Mock::setUp();
|
WP_Mock::setUp();
|
||||||
|
|
||||||
// Create instance of Core class
|
// Create instance of Core class.
|
||||||
$this->core = new Core();
|
$this->core = new Core();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tear down the test environment.
|
* Tear down the test environment.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function tearDown(): void
|
public function tearDown(): void {
|
||||||
{
|
|
||||||
WP_Mock::tearDown();
|
WP_Mock::tearDown();
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test constructor
|
* Test constructor.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_constructor() {
|
public function test_constructor() {
|
||||||
// Verify that the constructor initializes hooks
|
// Verify that the constructor initializes hooks.
|
||||||
$this->assertInstanceOf(Core::class, $this->core);
|
$this->assertInstanceOf( Core::class, $this->core );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test example method
|
* Test example method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_filter_content() {
|
public function test_filter_content() {
|
||||||
$content = 'Test content';
|
$content = 'Test content';
|
||||||
|
|
||||||
// Test that filter_content returns the content
|
// Test that filter_content returns the content.
|
||||||
$this->assertEquals($content, $this->core->filter_content($content));
|
$this->assertEquals( $content, $this->core->filter_content( $content ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,23 @@
|
|||||||
* @group wordpress
|
* @group wordpress
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite;
|
||||||
|
|
||||||
// Skip this test file if WordPress test framework is not available.
|
// Skip this test file if WordPress test framework is not available.
|
||||||
if ( ! class_exists( 'WP_UnitTestCase' ) ) {
|
if ( ! class_exists( 'WP_UnitTestCase' ) ) {
|
||||||
return;
|
return; // phpcs:ignore -- Early return is intentional.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multisite class name constant for testing.
|
||||||
|
*/
|
||||||
|
const MULTISITE_CLASS = 'WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skip message constant.
|
||||||
|
*/
|
||||||
|
const MULTISITE_SKIP_MSG = 'Multisite class not available';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sample test case for the Multisite class.
|
* Sample test case for the Multisite class.
|
||||||
*/
|
*/
|
||||||
@@ -18,43 +30,43 @@ class MultisiteTest extends WP_UnitTestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test instance creation.
|
* Test instance creation.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_instance() {
|
public function test_instance() {
|
||||||
// Skip this test if the class doesn't exist
|
if ( ! class_exists( MULTISITE_CLASS ) ) {
|
||||||
if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) {
|
$this->markTestSkipped( MULTISITE_SKIP_MSG );
|
||||||
$this->markTestSkipped('Multisite class not available');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite();
|
$multisite = new Multisite();
|
||||||
$this->assertInstanceOf( 'WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite', $multisite );
|
$this->assertInstanceOf( MULTISITE_CLASS, $multisite );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test is_multisite_compatible method.
|
* Test is_multisite_compatible method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_is_multisite_compatible() {
|
public function test_is_multisite_compatible() {
|
||||||
// Skip this test if the class doesn't exist
|
if ( ! class_exists( MULTISITE_CLASS ) ) {
|
||||||
if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) {
|
$this->markTestSkipped( MULTISITE_SKIP_MSG );
|
||||||
$this->markTestSkipped('Multisite class not available');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite();
|
$multisite = new Multisite();
|
||||||
$this->assertTrue( $multisite->is_multisite_compatible() );
|
$this->assertTrue( $multisite->is_multisite_compatible() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test get_network_sites method.
|
* Test get_network_sites method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_get_network_sites() {
|
public function test_get_network_sites() {
|
||||||
// Skip this test if the class doesn't exist
|
if ( ! class_exists( MULTISITE_CLASS ) ) {
|
||||||
if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) {
|
$this->markTestSkipped( MULTISITE_SKIP_MSG );
|
||||||
$this->markTestSkipped('Multisite class not available');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite();
|
$multisite = new Multisite();
|
||||||
|
|
||||||
// Mock the get_sites function if we're not in a multisite environment.
|
// Mock the get_sites function if we're not in a multisite environment.
|
||||||
if ( ! function_exists( 'get_sites' ) ) {
|
if ( ! function_exists( 'get_sites' ) ) {
|
||||||
@@ -67,15 +79,15 @@ class MultisiteTest extends WP_UnitTestCase {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Test initialize_hooks method.
|
* Test initialize_hooks method.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function test_initialize_hooks() {
|
public function test_initialize_hooks() {
|
||||||
// Skip this test if the class doesn't exist
|
if ( ! class_exists( MULTISITE_CLASS ) ) {
|
||||||
if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) {
|
$this->markTestSkipped( MULTISITE_SKIP_MSG );
|
||||||
$this->markTestSkipped('Multisite class not available');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite();
|
$multisite = new Multisite();
|
||||||
|
|
||||||
// Call the method.
|
// Call the method.
|
||||||
$multisite->initialize_hooks();
|
$multisite->initialize_hooks();
|
||||||
|
|||||||
Reference in New Issue
Block a user