Initial commit of WordPress Plugin Starter Template for AI Coding
Some checks failed
Tests / PHP 7.0 (push) Has been cancelled
Tests / PHP 7.4 (push) Has been cancelled
Tests / PHP 8.0 (push) Has been cancelled
Tests / Code Style (push) Has been cancelled
Sync Wiki / Sync Wiki to GitHub (push) Has been cancelled
Release / Build and Release (push) Has been cancelled
Some checks failed
Tests / PHP 7.0 (push) Has been cancelled
Tests / PHP 7.4 (push) Has been cancelled
Tests / PHP 8.0 (push) Has been cancelled
Tests / Code Style (push) Has been cancelled
Sync Wiki / Sync Wiki to GitHub (push) Has been cancelled
Release / Build and Release (push) Has been cancelled
This commit is contained in:
84
tests/README.md
Normal file
84
tests/README.md
Normal file
@ -0,0 +1,84 @@
|
||||
# Plugin Tests
|
||||
|
||||
This directory contains test files for the plugin.
|
||||
|
||||
## Test Structure
|
||||
|
||||
- `bootstrap.php`: Sets up the test environment
|
||||
- `test-core.php`: Tests for the Core class
|
||||
- `test-admin.php`: Tests for the Admin class
|
||||
- Add more test files as needed for additional classes
|
||||
|
||||
## Running Tests
|
||||
|
||||
To run the tests, you need to have PHPUnit and WP Mock installed. You can install them using Composer:
|
||||
|
||||
```bash
|
||||
composer install
|
||||
```
|
||||
|
||||
Then, run the tests:
|
||||
|
||||
```bash
|
||||
./vendor/bin/phpunit
|
||||
```
|
||||
|
||||
## Writing Tests
|
||||
|
||||
When writing tests:
|
||||
|
||||
1. Create a new file named `test-{class-name}.php` for each class you want to test
|
||||
2. Extend the `WP_Mock\Tools\TestCase` class
|
||||
3. Use WP Mock to mock WordPress functions
|
||||
4. Write test methods for each method in your class
|
||||
|
||||
Example:
|
||||
|
||||
```php
|
||||
<?php
|
||||
use WPALLSTARS\PluginStarterTemplate\YourClass;
|
||||
|
||||
class YourClassTest extends WP_Mock\Tools\TestCase {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
WP_Mock::setUp();
|
||||
}
|
||||
|
||||
public function tearDown(): void {
|
||||
WP_Mock::tearDown();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_your_method() {
|
||||
// Set up mocks
|
||||
WP_Mock::userFunction('wp_function', [
|
||||
'times' => 1,
|
||||
'args' => ['argument'],
|
||||
'return' => 'result',
|
||||
]);
|
||||
|
||||
// Create instance of your class
|
||||
$instance = new YourClass();
|
||||
|
||||
// Call the method
|
||||
$result = $instance->your_method('argument');
|
||||
|
||||
// Assert the result
|
||||
$this->assertEquals('expected result', $result);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Test Coverage
|
||||
|
||||
To generate a test coverage report:
|
||||
|
||||
```bash
|
||||
./vendor/bin/phpunit --coverage-html coverage
|
||||
```
|
||||
|
||||
This will generate an HTML report in the `coverage` directory.
|
||||
|
||||
## Continuous Integration
|
||||
|
||||
The tests are automatically run on GitHub Actions when you push to the repository. See the `.github/workflows/tests.yml` file for details.
|
27
tests/bootstrap.php
Normal file
27
tests/bootstrap.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
/**
|
||||
* PHPUnit bootstrap file
|
||||
*
|
||||
* @package WPALLSTARS\PluginStarterTemplate
|
||||
*/
|
||||
|
||||
// First, we need to load the composer autoloader so we can use WP Mock.
|
||||
require_once dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
// Now call the bootstrap method of WP Mock.
|
||||
WP_Mock::bootstrap();
|
||||
|
||||
/**
|
||||
* Now we define a few constants to help us with testing.
|
||||
*/
|
||||
define('WPST_PLUGIN_DIR', 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/core.php';
|
||||
require_once WPST_PLUGIN_DIR . 'includes/plugin.php';
|
||||
require_once WPST_PLUGIN_DIR . 'admin/lib/admin.php';
|
112
tests/test-admin.php
Normal file
112
tests/test-admin.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* Class AdminTest
|
||||
*
|
||||
* @package WPALLSTARS\PluginStarterTemplate
|
||||
*/
|
||||
|
||||
use WPALLSTARS\PluginStarterTemplate\Admin\Admin;
|
||||
use WPALLSTARS\PluginStarterTemplate\Core;
|
||||
|
||||
/**
|
||||
* Admin test case.
|
||||
*/
|
||||
class AdminTest extends WP_Mock\Tools\TestCase {
|
||||
|
||||
/**
|
||||
* Test instance
|
||||
*
|
||||
* @var Admin
|
||||
*/
|
||||
private $admin;
|
||||
|
||||
/**
|
||||
* Core mock
|
||||
*
|
||||
* @var Core
|
||||
*/
|
||||
private $core;
|
||||
|
||||
/**
|
||||
* Set up test environment
|
||||
*/
|
||||
public function setUp(): void {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down test environment
|
||||
*/
|
||||
public function tearDown(): void {
|
||||
WP_Mock::tearDown();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor
|
||||
*/
|
||||
public function test_constructor() {
|
||||
// Verify that the constructor initializes hooks
|
||||
$this->assertInstanceOf(Admin::class, $this->admin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test enqueue_admin_assets
|
||||
*/
|
||||
public function test_enqueue_admin_assets() {
|
||||
// Set up WordPress function mocks
|
||||
WP_Mock::userFunction('wp_enqueue_style', [
|
||||
'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);
|
||||
}
|
||||
}
|
60
tests/test-core.php
Normal file
60
tests/test-core.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Class CoreTest
|
||||
*
|
||||
* @package WPALLSTARS\PluginStarterTemplate
|
||||
*/
|
||||
|
||||
use WPALLSTARS\PluginStarterTemplate\Core;
|
||||
|
||||
/**
|
||||
* Core test case.
|
||||
*/
|
||||
class CoreTest extends WP_Mock\Tools\TestCase {
|
||||
|
||||
/**
|
||||
* Test instance
|
||||
*
|
||||
* @var Core
|
||||
*/
|
||||
private $core;
|
||||
|
||||
/**
|
||||
* Set up test environment
|
||||
*/
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
// Set up mocks
|
||||
WP_Mock::setUp();
|
||||
|
||||
// Create instance of Core class
|
||||
$this->core = new Core();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down test environment
|
||||
*/
|
||||
public function tearDown(): void {
|
||||
WP_Mock::tearDown();
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test constructor
|
||||
*/
|
||||
public function test_constructor() {
|
||||
// Verify that the constructor initializes hooks
|
||||
$this->assertInstanceOf(Core::class, $this->core);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test example method
|
||||
*/
|
||||
public function test_filter_content() {
|
||||
$content = 'Test content';
|
||||
|
||||
// Test that filter_content returns the content
|
||||
$this->assertEquals($content, $this->core->filter_content($content));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user