From 31d475becacaff46beb5f5c69705bdef400fe09e Mon Sep 17 00:00:00 2001 From: marcusquinn <6428977+marcusquinn@users.noreply.github.com> Date: Fri, 18 Apr 2025 16:02:17 +0100 Subject: [PATCH] Fix test workflow issues with WP_Mock, PHP 7.0 compatibility, and code style --- .github/workflows/tests.yml | 12 +++++- admin/lib/admin.php | 74 ++++++++++++++++++++++++++++++++++ composer.json | 6 ++- includes/core.php | 30 ++++++++++++++ includes/plugin.php | 53 ++++++++++++++++++++++++ includes/updater.php | 57 ++++++++++++++++++++++++++ tests/test-admin.php | 28 ++++++------- tests/test-core.php | 12 +++--- wp-plugin-starter-template.php | 12 +++--- 9 files changed, 254 insertions(+), 30 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 070d6c4..171b8e4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -27,7 +27,15 @@ jobs: tools: composer:v2 - name: Install dependencies - run: composer install --prefer-dist --no-progress + run: | + if [[ "${{ matrix.php-versions }}" == "7.0" ]]; then + composer require --dev phpunit/phpunit:"^6.5" --update-with-dependencies + composer require --dev 10up/wp_mock:"0.3.0" --update-with-dependencies + composer require --dev antecedent/patchwork:"^2.1.21" --update-with-dependencies + else + composer install --prefer-dist --no-progress + composer require --dev 10up/wp_mock:"0.3.0" --update-with-dependencies + fi - name: Run tests run: ./vendor/bin/phpunit @@ -51,4 +59,4 @@ jobs: run: composer install --prefer-dist --no-progress - name: Run PHPCS - run: ./vendor/bin/phpcs --standard=WordPress --runtime-set installed_paths vendor/wp-coding-standards/wpcs + run: ./vendor/bin/phpcs --standard=WordPress --runtime-set installed_paths vendor/wp-coding-standards/wpcs ./includes ./admin ./wp-plugin-starter-template.php diff --git a/admin/lib/admin.php b/admin/lib/admin.php index b3d9bbc..0d5fa0a 100644 --- a/admin/lib/admin.php +++ b/admin/lib/admin.php @@ -1 +1,75 @@ core = $core; + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] ); + } + + /** + * Initialize admin + */ + public function init() { + // Initialize admin + } + + /** + * Enqueue admin assets + * + * @param string $hook Current admin page. + */ + public function enqueue_admin_assets( $hook ) { + wp_enqueue_style( + 'wpst-admin-styles', + WPST_PLUGIN_URL . 'assets/css/admin.css', + [], + WPST_VERSION + ); + + wp_enqueue_script( + 'wpst-admin-scripts', + WPST_PLUGIN_URL . 'assets/js/admin.js', + [ 'jquery' ], + WPST_VERSION, + true + ); + + wp_localize_script( + 'wpst-admin-scripts', + 'wpstData', + [ + 'ajaxUrl' => admin_url( 'admin-ajax.php' ), + 'nonce' => wp_create_nonce( 'wpst-admin-nonce' ), + 'strings' => [ + 'success' => esc_html__( 'Success!', 'wp-plugin-starter-template' ), + 'error' => esc_html__( 'Error!', 'wp-plugin-starter-template' ), + ], + ] + ); + } +} diff --git a/composer.json b/composer.json index 14bad69..71b8af6 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,12 @@ "php": ">=7.0" }, "require-dev": { - "phpunit/phpunit": "^7.5 || ^8.0 || ^9.0", + "phpunit/phpunit": "^6.5 || ^7.5 || ^8.0 || ^9.0", "squizlabs/php_codesniffer": "^3.5", "wp-coding-standards/wpcs": "^2.3", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0" + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", + "10up/wp_mock": "0.3.0", + "antecedent/patchwork": "^2.1.21" }, "autoload": { "psr-4": { diff --git a/includes/core.php b/includes/core.php index b3d9bbc..f97d883 100644 --- a/includes/core.php +++ b/includes/core.php @@ -1 +1,31 @@ plugin_file = $plugin_file; + $this->version = $version; + $this->core = new Core(); + } + + /** + * Initialize the plugin + */ + public function init() { + // Initialize plugin. + } +} diff --git a/includes/updater.php b/includes/updater.php index b3d9bbc..cd24352 100644 --- a/includes/updater.php +++ b/includes/updater.php @@ -1 +1,58 @@ plugin_file = $plugin_file; + $this->init(); + } + + /** + * Initialize the updater + */ + public function init() { + add_filter('plugin_action_links_' . plugin_basename($this->plugin_file), array($this, 'add_update_source_link')); + add_action('admin_post_wpst_update_source', array($this, 'handle_update_source')); + } + + /** + * Add update source link to plugin actions + * + * @param array $links Plugin action links. + * @return array Modified plugin action links. + */ + public function add_update_source_link($links) { + $update_source_link = '' . esc_html__('Update Source', 'wp-plugin-starter-template') . ''; + array_unshift($links, $update_source_link); + return $links; + } + + /** + * Handle update source selection + */ + public function handle_update_source() { + // Handle update source selection. + } +} diff --git a/tests/test-admin.php b/tests/test-admin.php index 9c6e099..879263d 100644 --- a/tests/test-admin.php +++ b/tests/test-admin.php @@ -11,7 +11,7 @@ use WPALLSTARS\PluginStarterTemplate\Core; /** * Admin test case. */ -class AdminTest extends WP_Mock\Tools\TestCase { +class AdminTest extends \WP_Mock\Tools\TestCase { /** * Test instance @@ -30,21 +30,21 @@ class AdminTest extends WP_Mock\Tools\TestCase { /** * Set up test environment */ - public function setUp(): void { + public function setUp() { parent::setUp(); - + // Set up mocks WP_Mock::setUp(); - + // Mock Core class $this->core = $this->createMock(Core::class); - + // Set up WordPress function mocks WP_Mock::userFunction('add_action', [ 'times' => 1, 'args' => ['admin_enqueue_scripts', \WP_Mock\Functions::type('array')], ]); - + // Create instance of Admin class $this->admin = new Admin($this->core); } @@ -52,7 +52,7 @@ class AdminTest extends WP_Mock\Tools\TestCase { /** * Tear down test environment */ - public function tearDown(): void { + public function tearDown() { WP_Mock::tearDown(); parent::tearDown(); } @@ -74,38 +74,38 @@ class AdminTest extends WP_Mock\Tools\TestCase { 'times' => 1, 'args' => ['wpst-admin-styles', \WP_Mock\Functions::type('string'), [], \WP_Mock\Functions::type('string')], ]); - + WP_Mock::userFunction('wp_enqueue_script', [ 'times' => 1, 'args' => ['wpst-admin-scripts', \WP_Mock\Functions::type('string'), ['jquery'], \WP_Mock\Functions::type('string'), true], ]); - + WP_Mock::userFunction('wp_localize_script', [ 'times' => 1, 'args' => ['wpst-admin-scripts', 'wpstData', \WP_Mock\Functions::type('array')], ]); - + WP_Mock::userFunction('esc_html__', [ 'times' => 2, 'args' => [\WP_Mock\Functions::type('string'), 'wp-plugin-starter-template'], 'return' => 'Translated string', ]); - + WP_Mock::userFunction('admin_url', [ 'times' => 1, 'args' => ['admin-ajax.php'], 'return' => 'http://example.org/wp-admin/admin-ajax.php', ]); - + WP_Mock::userFunction('wp_create_nonce', [ 'times' => 1, 'args' => ['wpst-admin-nonce'], 'return' => '1234567890', ]); - + // Call the method $this->admin->enqueue_admin_assets('plugins.php'); - + // If we get here, the test passed $this->assertTrue(true); } diff --git a/tests/test-core.php b/tests/test-core.php index c9e680c..2e1c924 100644 --- a/tests/test-core.php +++ b/tests/test-core.php @@ -10,7 +10,7 @@ use WPALLSTARS\PluginStarterTemplate\Core; /** * Core test case. */ -class CoreTest extends WP_Mock\Tools\TestCase { +class CoreTest extends \WP_Mock\Tools\TestCase { /** * Test instance @@ -22,12 +22,12 @@ class CoreTest extends WP_Mock\Tools\TestCase { /** * Set up test environment */ - public function setUp(): void { + public function setUp() { parent::setUp(); - + // Set up mocks WP_Mock::setUp(); - + // Create instance of Core class $this->core = new Core(); } @@ -35,7 +35,7 @@ class CoreTest extends WP_Mock\Tools\TestCase { /** * Tear down test environment */ - public function tearDown(): void { + public function tearDown() { WP_Mock::tearDown(); parent::tearDown(); } @@ -53,7 +53,7 @@ class CoreTest extends WP_Mock\Tools\TestCase { */ public function test_filter_content() { $content = 'Test content'; - + // Test that filter_content returns the content $this->assertEquals($content, $this->core->filter_content($content)); } diff --git a/wp-plugin-starter-template.php b/wp-plugin-starter-template.php index 4b03445..ebb352f 100644 --- a/wp-plugin-starter-template.php +++ b/wp-plugin-starter-template.php @@ -27,12 +27,12 @@ */ // If this file is called directly, abort. -if (!defined('WPINC')) { - die; +if ( ! defined( 'WPINC' ) ) { + die; } -// Load the main plugin class -require_once plugin_dir_path(__FILE__) . 'includes/plugin.php'; +// Load the main plugin class. +require_once plugin_dir_path( __FILE__ ) . 'includes/plugin.php'; -// Initialize the plugin -new WPALLSTARS\PluginStarterTemplate\Plugin(__FILE__, '0.1.6'); +// Initialize the plugin. +new WPALLSTARS\PluginStarterTemplate\Plugin( __FILE__, '0.1.7' );