Fix test workflow issues with WP_Mock, PHP 7.0 compatibility, and code style
Release / Build and Release (push) Canceled after 0s

This commit is contained in:
marcus committed 2025-04-18 16:02:17 +01:00
1 parent af48e84614
commit 31d475beca
9 files changed
+254 -30

No files matched your search

+10 -2
View File
@@ -27,7 +27,15 @@ jobs:
tools: composer:v2 tools: composer:v2
- name: Install dependencies - 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 - name: Run tests
run: ./vendor/bin/phpunit run: ./vendor/bin/phpunit
@@ -51,4 +59,4 @@ jobs:
run: composer install --prefer-dist --no-progress run: composer install --prefer-dist --no-progress
- name: Run PHPCS - 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
+74
View File
@@ -1 +1,75 @@
<?php <?php
/**
* Admin functionality
*
* @package WPALLSTARS\PluginStarterTemplate
*/
namespace WPALLSTARS\PluginStarterTemplate\Admin;
use WPALLSTARS\PluginStarterTemplate\Core;
/**
* Admin class
*/
class Admin {
/**
* Core instance
*
* @var Core
*/
private $core;
/**
* Constructor
*
* @param Core $core Core instance.
*/
public function __construct( $core ) {
$this->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' ),
],
]
);
}
}
+4 -2
View File
@@ -14,10 +14,12 @@
"php": ">=7.0" "php": ">=7.0"
}, },
"require-dev": { "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", "squizlabs/php_codesniffer": "^3.5",
"wp-coding-standards/wpcs": "^2.3", "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": { "autoload": {
"psr-4": { "psr-4": {
+30
View File
@@ -1 +1,31 @@
<?php <?php
/**
* Core functionality for the plugin
*
* @package WPALLSTARS\PluginStarterTemplate
*/
namespace WPALLSTARS\PluginStarterTemplate;
/**
* Core class
*/
class Core {
/**
* Constructor
*/
public function __construct() {
// Initialize hooks
}
/**
* Example method to filter content
*
* @param string $content The content to filter.
* @return string The filtered content.
*/
public function filter_content( $content ) {
return $content;
}
}
+53
View File
@@ -1 +1,54 @@
<?php <?php
/**
* Main plugin class
*
* @package WPALLSTARS\PluginStarterTemplate
*/
namespace WPALLSTARS\PluginStarterTemplate;
/**
* Plugin class
*/
class Plugin {
/**
* Core instance
*
* @var Core
*/
private $core;
/**
* Plugin file
*
* @var string
*/
private $plugin_file;
/**
* Plugin version
*
* @var string
*/
private $version;
/**
* Constructor
*
* @param string $plugin_file Main plugin file path.
* @param string $version Plugin version.
*/
public function __construct($plugin_file, $version) {
$this->plugin_file = $plugin_file;
$this->version = $version;
$this->core = new Core();
}
/**
* Initialize the plugin
*/
public function init() {
// Initialize plugin.
}
}
+57
View File
@@ -1 +1,58 @@
<?php <?php
/**
* Updater functionality for the plugin
*
* @package WPALLSTARS\PluginStarterTemplate
*/
namespace WPALLSTARS\PluginStarterTemplate;
/**
* Updater class
*/
class Updater {
/**
* Plugin file
*
* @var string
*/
private $plugin_file;
/**
* Constructor
*
* @param string $plugin_file Main plugin file path.
*/
public function __construct($plugin_file) {
$this->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 = '<a href="' . admin_url('admin-post.php?action=wpst_update_source&plugin=' . plugin_basename($this->plugin_file)) . '">' . esc_html__('Update Source', 'wp-plugin-starter-template') . '</a>';
array_unshift($links, $update_source_link);
return $links;
}
/**
* Handle update source selection
*/
public function handle_update_source() {
// Handle update source selection.
}
}
+14 -14
View File
@@ -11,7 +11,7 @@ use WPALLSTARS\PluginStarterTemplate\Core;
/** /**
* Admin test case. * Admin test case.
*/ */
class AdminTest extends WP_Mock\Tools\TestCase { class AdminTest extends \WP_Mock\Tools\TestCase {
/** /**
* Test instance * Test instance
@@ -30,21 +30,21 @@ class AdminTest extends WP_Mock\Tools\TestCase {
/** /**
* Set up test environment * Set up test environment
*/ */
public function setUp(): void { public function setUp() {
parent::setUp(); parent::setUp();
// Set up mocks // Set up mocks
WP_Mock::setUp(); WP_Mock::setUp();
// Mock Core class // Mock Core class
$this->core = $this->createMock(Core::class); $this->core = $this->createMock(Core::class);
// Set up WordPress function mocks // Set up WordPress function mocks
WP_Mock::userFunction('add_action', [ WP_Mock::userFunction('add_action', [
'times' => 1, 'times' => 1,
'args' => ['admin_enqueue_scripts', \WP_Mock\Functions::type('array')], 'args' => ['admin_enqueue_scripts', \WP_Mock\Functions::type('array')],
]); ]);
// Create instance of Admin class // Create instance of Admin class
$this->admin = new Admin($this->core); $this->admin = new Admin($this->core);
} }
@@ -52,7 +52,7 @@ class AdminTest extends WP_Mock\Tools\TestCase {
/** /**
* Tear down test environment * Tear down test environment
*/ */
public function tearDown(): void { public function tearDown() {
WP_Mock::tearDown(); WP_Mock::tearDown();
parent::tearDown(); parent::tearDown();
} }
@@ -74,38 +74,38 @@ class AdminTest extends WP_Mock\Tools\TestCase {
'times' => 1, 'times' => 1,
'args' => ['wpst-admin-styles', \WP_Mock\Functions::type('string'), [], \WP_Mock\Functions::type('string')], 'args' => ['wpst-admin-styles', \WP_Mock\Functions::type('string'), [], \WP_Mock\Functions::type('string')],
]); ]);
WP_Mock::userFunction('wp_enqueue_script', [ WP_Mock::userFunction('wp_enqueue_script', [
'times' => 1, 'times' => 1,
'args' => ['wpst-admin-scripts', \WP_Mock\Functions::type('string'), ['jquery'], \WP_Mock\Functions::type('string'), true], 'args' => ['wpst-admin-scripts', \WP_Mock\Functions::type('string'), ['jquery'], \WP_Mock\Functions::type('string'), true],
]); ]);
WP_Mock::userFunction('wp_localize_script', [ WP_Mock::userFunction('wp_localize_script', [
'times' => 1, 'times' => 1,
'args' => ['wpst-admin-scripts', 'wpstData', \WP_Mock\Functions::type('array')], 'args' => ['wpst-admin-scripts', 'wpstData', \WP_Mock\Functions::type('array')],
]); ]);
WP_Mock::userFunction('esc_html__', [ WP_Mock::userFunction('esc_html__', [
'times' => 2, 'times' => 2,
'args' => [\WP_Mock\Functions::type('string'), 'wp-plugin-starter-template'], 'args' => [\WP_Mock\Functions::type('string'), 'wp-plugin-starter-template'],
'return' => 'Translated string', 'return' => 'Translated string',
]); ]);
WP_Mock::userFunction('admin_url', [ WP_Mock::userFunction('admin_url', [
'times' => 1, 'times' => 1,
'args' => ['admin-ajax.php'], 'args' => ['admin-ajax.php'],
'return' => 'http://example.org/wp-admin/admin-ajax.php', 'return' => 'http://example.org/wp-admin/admin-ajax.php',
]); ]);
WP_Mock::userFunction('wp_create_nonce', [ WP_Mock::userFunction('wp_create_nonce', [
'times' => 1, 'times' => 1,
'args' => ['wpst-admin-nonce'], 'args' => ['wpst-admin-nonce'],
'return' => '1234567890', 'return' => '1234567890',
]); ]);
// Call the method // Call the method
$this->admin->enqueue_admin_assets('plugins.php'); $this->admin->enqueue_admin_assets('plugins.php');
// If we get here, the test passed // If we get here, the test passed
$this->assertTrue(true); $this->assertTrue(true);
} }
+6 -6
View File
@@ -10,7 +10,7 @@ use WPALLSTARS\PluginStarterTemplate\Core;
/** /**
* Core test case. * Core test case.
*/ */
class CoreTest extends WP_Mock\Tools\TestCase { class CoreTest extends \WP_Mock\Tools\TestCase {
/** /**
* Test instance * Test instance
@@ -22,12 +22,12 @@ class CoreTest extends WP_Mock\Tools\TestCase {
/** /**
* Set up test environment * Set up test environment
*/ */
public function setUp(): void { public function setUp() {
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();
} }
@@ -35,7 +35,7 @@ class CoreTest extends WP_Mock\Tools\TestCase {
/** /**
* Tear down test environment * Tear down test environment
*/ */
public function tearDown(): void { public function tearDown() {
WP_Mock::tearDown(); WP_Mock::tearDown();
parent::tearDown(); parent::tearDown();
} }
@@ -53,7 +53,7 @@ class CoreTest extends WP_Mock\Tools\TestCase {
*/ */
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 -6
View File
@@ -27,12 +27,12 @@
*/ */
// If this file is called directly, abort. // If this file is called directly, abort.
if (!defined('WPINC')) { if ( ! defined( 'WPINC' ) ) {
die; die;
} }
// Load the main plugin class // Load the main plugin class.
require_once plugin_dir_path(__FILE__) . 'includes/plugin.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/plugin.php';
// Initialize the plugin // Initialize the plugin.
new WPALLSTARS\PluginStarterTemplate\Plugin(__FILE__, '0.1.6'); new WPALLSTARS\PluginStarterTemplate\Plugin( __FILE__, '0.1.7' );