diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index cf765e4..2a10924 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -8,48 +8,45 @@ // Composer autoloader must be loaded before WP_PHPUNIT__DIR will be available. 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' ) ) { - // Define PHPUnit Polyfills path for WordPress test suite. - if ( ! defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) { - define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( dirname( __DIR__ ) ) . '/vendor/yoast/phpunit-polyfills/' ); - } + // Define PHPUnit Polyfills path for WordPress test suite. + if ( ! defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) ) { + define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( dirname( __DIR__ ) ) . '/vendor/yoast/phpunit-polyfills/' ); + } - // Give access to tests_add_filter() function. - require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/functions.php'; + // Give access to tests_add_filter() function. + require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/functions.php'; - /** - * Manually load the plugin being tested. - */ - function _manually_load_plugin() { - require dirname( dirname( __DIR__ ) ) . '/wp-plugin-starter-template.php'; - // Load the multisite class for testing - $multisite_file = dirname( dirname( __DIR__ ) ) . '/includes/multisite/class-multisite.php'; - if (file_exists($multisite_file)) { - require $multisite_file; - } - } + /** + * Manually load the plugin being tested. + * + * @return void + */ + function _manually_load_plugin() { + require_once dirname( dirname( __DIR__ ) ) . '/wp-plugin-starter-template.php'; + // Load the multisite class for testing. + $multisite_file = dirname( dirname( __DIR__ ) ) . '/includes/multisite/class-multisite.php'; + if ( file_exists( $multisite_file ) ) { + require_once $multisite_file; + } + } - // Start up the WP testing environment. - require getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php'; + // Start up the WP testing environment. + require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php'; } else { - // We're running the WP_Mock tests - WP_Mock::bootstrap(); + // We're running the WP_Mock tests. + WP_Mock::bootstrap(); - /** - * 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_VERSION', '0.1.0'); + // Define constants for testing. + define( 'WPST_PLUGIN_DIR', dirname( dirname( __DIR__ ) ) . '/' ); + define( 'WPST_PLUGIN_URL', 'http://example.org/wp-content/plugins/wp-plugin-starter-template/' ); + define( 'WPST_VERSION', '0.1.0' ); - /** - * 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-plugin.php'; - if (file_exists(WPST_PLUGIN_DIR . 'admin/lib/admin.php')) { - require_once WPST_PLUGIN_DIR . 'admin/lib/admin.php'; - } + // Include plugin files needed for tests. + require_once WPST_PLUGIN_DIR . 'includes/class-core.php'; + require_once WPST_PLUGIN_DIR . 'includes/class-plugin.php'; + if ( file_exists( WPST_PLUGIN_DIR . 'admin/lib/admin.php' ) ) { + require_once WPST_PLUGIN_DIR . 'admin/lib/admin.php'; + } } diff --git a/tests/phpunit/test-admin.php b/tests/phpunit/test-admin.php index 52437ab..1b3a342 100644 --- a/tests/phpunit/test-admin.php +++ b/tests/phpunit/test-admin.php @@ -8,12 +8,17 @@ // 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' ) ) { - return; + return; // phpcs:ignore -- Early return is intentional. } use WPALLSTARS\PluginStarterTemplate\Admin\Admin; use WPALLSTARS\PluginStarterTemplate\Core; +/** + * Test version constant. + */ +const TEST_VERSION = '1.0.0'; + /** * Admin test case. */ @@ -35,18 +40,19 @@ class AdminTest extends \WP_Mock\Tools\TestCase { /** * Set up the test environment. + * + * @return void */ - public function setUp(): void - { + public function setUp(): void { parent::setUp(); - // Set up mocks + // Set up mocks. WP_Mock::setUp(); // Mock the Core class dependency using Mockery. $this->core = \Mockery::mock( '\WPALLSTARS\PluginStarterTemplate\Core' ); // 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. \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 { WP_Mock::tearDown(); @@ -64,18 +72,21 @@ class AdminTest extends \WP_Mock\Tools\TestCase { } /** - * Test constructor + * Test constructor. + * + * @return void */ public function test_constructor() { - // Verify that the constructor initializes hooks - $this->assertInstanceOf(Admin::class, $this->admin); + // Verify that the constructor initializes hooks. + $this->assertInstanceOf( Admin::class, $this->admin ); } /** * 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 if ( ! defined( 'PHPUNIT_RUNNING' ) ) { define( 'PHPUNIT_RUNNING', true ); diff --git a/tests/phpunit/test-core.php b/tests/phpunit/test-core.php index 1cc9b36..6394eab 100644 --- a/tests/phpunit/test-core.php +++ b/tests/phpunit/test-core.php @@ -8,7 +8,7 @@ // 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' ) ) { - return; + return; // phpcs:ignore -- Early return is intentional. } use WPALLSTARS\PluginStarterTemplate\Core; @@ -27,42 +27,48 @@ class CoreTest extends \WP_Mock\Tools\TestCase { /** * Set up the test environment. + * + * @return void */ - public function setUp(): void - { + public function setUp(): void { parent::setUp(); - // Set up mocks + // Set up mocks. WP_Mock::setUp(); - // Create instance of Core class + // Create instance of Core class. $this->core = new Core(); } /** * Tear down the test environment. + * + * @return void */ - public function tearDown(): void - { + public function tearDown(): void { WP_Mock::tearDown(); parent::tearDown(); } /** - * Test constructor + * Test constructor. + * + * @return void */ public function test_constructor() { - // Verify that the constructor initializes hooks - $this->assertInstanceOf(Core::class, $this->core); + // Verify that the constructor initializes hooks. + $this->assertInstanceOf( Core::class, $this->core ); } /** - * Test example method + * Test example method. + * + * @return void */ public function test_filter_content() { $content = 'Test content'; - // Test that filter_content returns the content - $this->assertEquals($content, $this->core->filter_content($content)); + // Test that filter_content returns the content. + $this->assertEquals( $content, $this->core->filter_content( $content ) ); } } diff --git a/tests/phpunit/test-multisite.php b/tests/phpunit/test-multisite.php index 826609f..117d13e 100644 --- a/tests/phpunit/test-multisite.php +++ b/tests/phpunit/test-multisite.php @@ -6,81 +6,93 @@ * @group wordpress */ +use WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite; + // Skip this test file if WordPress test framework is not available. 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. */ class MultisiteTest extends WP_UnitTestCase { - /** - * Test instance creation. - */ - public function test_instance() { - // Skip this test if the class doesn't exist - if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) { - $this->markTestSkipped('Multisite class not available'); - return; - } + /** + * Test instance creation. + * + * @return void + */ + public function test_instance() { + if ( ! class_exists( MULTISITE_CLASS ) ) { + $this->markTestSkipped( MULTISITE_SKIP_MSG ); + } - $multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite(); - $this->assertInstanceOf( 'WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite', $multisite ); - } + $multisite = new Multisite(); + $this->assertInstanceOf( MULTISITE_CLASS, $multisite ); + } - /** - * Test is_multisite_compatible method. - */ - public function test_is_multisite_compatible() { - // Skip this test if the class doesn't exist - if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) { - $this->markTestSkipped('Multisite class not available'); - return; - } + /** + * Test is_multisite_compatible method. + * + * @return void + */ + public function test_is_multisite_compatible() { + if ( ! class_exists( MULTISITE_CLASS ) ) { + $this->markTestSkipped( MULTISITE_SKIP_MSG ); + } - $multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite(); - $this->assertTrue( $multisite->is_multisite_compatible() ); - } + $multisite = new Multisite(); + $this->assertTrue( $multisite->is_multisite_compatible() ); + } - /** - * Test get_network_sites method. - */ - public function test_get_network_sites() { - // Skip this test if the class doesn't exist - if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) { - $this->markTestSkipped('Multisite class not available'); - return; - } + /** + * Test get_network_sites method. + * + * @return void + */ + public function test_get_network_sites() { + if ( ! class_exists( MULTISITE_CLASS ) ) { + $this->markTestSkipped( MULTISITE_SKIP_MSG ); + } - $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. - if ( ! function_exists( 'get_sites' ) ) { - $this->assertEquals( array(), $multisite->get_network_sites() ); - } else { - $sites = $multisite->get_network_sites(); - $this->assertIsArray( $sites ); - } - } + // Mock the get_sites function if we're not in a multisite environment. + if ( ! function_exists( 'get_sites' ) ) { + $this->assertEquals( array(), $multisite->get_network_sites() ); + } else { + $sites = $multisite->get_network_sites(); + $this->assertIsArray( $sites ); + } + } - /** - * Test initialize_hooks method. - */ - public function test_initialize_hooks() { - // Skip this test if the class doesn't exist - if (!class_exists('WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite')) { - $this->markTestSkipped('Multisite class not available'); - return; - } + /** + * Test initialize_hooks method. + * + * @return void + */ + public function test_initialize_hooks() { + if ( ! class_exists( MULTISITE_CLASS ) ) { + $this->markTestSkipped( MULTISITE_SKIP_MSG ); + } - $multisite = new WP_Plugin_Starter_Template_For_AI_Coding\Multisite\Multisite(); + $multisite = new Multisite(); - // Call the method. - $multisite->initialize_hooks(); + // Call the method. + $multisite->initialize_hooks(); - // Check if the action was added. - $this->assertEquals( 10, has_action( 'network_admin_menu', array( $multisite, 'add_network_menu' ) ) ); - } + // Check if the action was added. + $this->assertEquals( 10, has_action( 'network_admin_menu', array( $multisite, 'add_network_menu' ) ) ); + } }