Skip to content
Snippets Groups Projects
Commit 6a4f6b19 authored by dyszczo's avatar dyszczo
Browse files

plugin template with scoped libraries

parent 0975d24d
No related branches found
No related tags found
No related merge requests found
Showing
with 266 additions and 67 deletions
#!/bin/bash
export WPDESK_PLUGIN_SLUG=plugin-template
export WPDESK_PLUGIN_TITLE="WP Desk Plugin Template"
export WOOTESTS_IP=${WOOTESTS_IP:wootests}
sh ./vendor/wpdesk/wp-codeception/scripts/common_bootstrap.sh
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
*
!.gitignore
<?php
/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class AcceptanceTester extends \Codeception\Actor
{
use _generated\AcceptanceTesterActions;
use \WPDesk\Codeception\Tests\Acceptance\Tester\TesterWordpressActions;
use \WPDesk\Codeception\Tests\Acceptance\Tester\TesterWooCommerceActions;
/**
* Define custom actions here
*/
}
<?php
/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class FunctionalTester extends \Codeception\Actor
{
use _generated\FunctionalTesterActions;
/**
* Define custom actions here
*/
}
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Acceptance extends \Codeception\Module
{
}
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Functional extends \Codeception\Module
{
}
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Unit extends \Codeception\Module
{
}
<?php
/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \Codeception\Actor
{
use _generated\UnitTesterActions;
/**
* Define custom actions here
*/
}
*
!.gitignore
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WPWebDriver or WPBrowser.
# Use WPDb to set up your initial database fixture.
# If you need both WPWebDriver and WPBrowser tests - create a separate suite.
actor: AcceptanceTester
modules:
enabled:
- Cli
- WPDb
- WPWebDriver
- WPFilesystem
- WPCLI
- \Helper\Acceptance
config:
WPDb:
dsn: 'mysql:host=%TEST_SITE_DB_HOST%;dbname=%TEST_SITE_DB_NAME%'
user: '%TEST_SITE_DB_USER%'
password: '%TEST_SITE_DB_PASSWORD%'
#dump: 'tests/_data/dump.sql'
#import the dump before the tests; this means the test site database will be repopulated before the tests.
populate: false
# re-import the dump between tests; this means the test site database will be repopulated between the tests.
cleanup: true
waitlock: 10
url: '%TEST_SITE_WP_URL%'
urlReplacement: true #replace the hardcoded dump URL with the one above
tablePrefix: '%TEST_SITE_TABLE_PREFIX%'
WPBrowser:
url: '%TEST_SITE_WP_URL%'
adminUsername: '%TEST_SITE_ADMIN_USERNAME%'
adminPassword: '%TEST_SITE_ADMIN_PASSWORD%'
adminPath: '%TEST_SITE_WP_ADMIN_PATH%'
log_js_errors: true
WPWebDriver:
browser: chrome # 'chrome' or 'firefox'
url: '%TEST_SITE_WP_URL%'
host: chrome
adminUsername: '%TEST_SITE_ADMIN_USERNAME%'
adminPassword: '%TEST_SITE_ADMIN_PASSWORD%'
adminPath: '%TEST_SITE_WP_ADMIN_PATH%'
log_js_errors: true
WPCLI:
path: '%WP_ROOT_FOLDER%'
throw: true
WPFilesystem:
wpRootFolder: '%WP_ROOT_FOLDER%'
<?php
use WPDesk\Codeception\Tests\Acceptance\Cest;
class ActivationCest extends Cest {
/**
* Deactivate plugins before tests.
*
* @param AcceptanceTester $i .
*
* @throws \Codeception\Exception\ModuleException .
*/
public function _before( AcceptanceTester $i ) {
$i->loginAsAdmin();
$i->amOnPluginsPage();
$i->deactivatePlugin( $this->getPluginSlug() );
$i->amOnPluginsPage();
$i->deactivatePlugin( 'woocommerce' );
}
/**
* Plugin activation.
*
* @param AcceptanceTester $i .
*
* @throws \Codeception\Exception\ModuleException .
*/
public function pluginActivation( AcceptanceTester $i ) {
$i->loginAsAdmin();
$i->amOnPluginsPage();
$i->seePluginDeactivated( $this->getPluginSlug() );
$i->activateWPDeskPlugin(
$this->getPluginSlug(),
array( 'woocommerce' ),
array( 'The “WP Desk Plugin Template” plugin cannot run without WooCommerce active. Please install and activate WooCommerce plugin.' )
);
}
}
# Codeception Test Suite Configuration
#
# Suite for functional tests
# Emulate web requests and make application process them
# Include one of framework modules (Symfony2, Yii2, Laravel5) to use it
# Remove this suite if you don't use frameworks
actor: FunctionalTester
modules:
enabled:
# add a framework module here
- \Helper\Functional
step_decorators: ~
\ No newline at end of file
# Codeception Test Suite Configuration
#
# Suite for unit or integration tests.
actor: UnitTester
modules:
enabled:
- Asserts
- \Helper\Unit
step_decorators: ~
\ No newline at end of file
<?php
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
// disable xdebug backtrace
if ( function_exists( 'xdebug_disable' ) ) {
xdebug_disable();
......@@ -13,10 +18,20 @@ if ( getenv( 'PLUGIN_PATH' ) !== false ) {
require_once( getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit/includes/functions.php' );
tests_add_filter( 'muplugins_loaded', function () {
require PLUGIN_PATH . '/plugin-template.php';
$plugins_to_active[] = 'plugin-template/plugin-template.php';
update_option( 'active_plugins', $plugins_to_active );
update_option( 'active_plugins', [] );
}, 100 );
putenv('WP_TESTS_DIR=' . getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit');
require_once( getenv( 'WC_DEVELOP_DIR' ) . '/tests/bootstrap.php' );
$plugins_to_active = get_option( 'active_plugins' );
if (!is_array($plugins_to_active)) {
$plugins_to_active = [];
}
$plugins_to_active[] = 'woocommerce/woocommerce.php';
update_option( 'active_plugins', $plugins_to_active );
echo "\n\n";
echo 'WC_VERSION=' . WC_VERSION . "\n";
echo 'PHP_VERSION=' . phpversion() . "\n";
echo "\n\n";
<?php
use PHPUnit\Framework\TestCase;
require_once( PLUGIN_PATH . '/classes/wpdesk/class-plugin.php' );
require_once( PLUGIN_PATH . '/classes/plugin-template-factory.php' );
class Test_WPDesk_Plugin_Template_Factory extends TestCase {
public function testBuildForNotActivatedPlugin() {
$factory = new WPDesk_Plugin_Template_Factory();
$this->assertInstanceOf( WPDesk_Plugin_Template_Plugin::class, $factory::build_plugin() );
}
public function testGetPluginAlwaysSingleInstance() {
$factory = new WPDesk_Plugin_Template_Factory();
$instanceOne = $factory::get_plugin_instance();
$instanceTwo = $factory::get_plugin_instance();
$this->assertInstanceOf( WPDesk_Plugin_Template_Plugin::class, $instanceOne );
$this->assertSame($instanceOne, $instanceTwo);
}
}
\ No newline at end of file
<?php
use PHPUnit\Framework\TestCase;
require_once( PLUGIN_PATH . '/classes/wpdesk/class-plugin.php' );
require_once( PLUGIN_PATH . '/classes/plugin-template-factory.php' );
class Test_WPDesk_Plugin_Template_Factory extends TestCase {
public function setUp() {
WP_Mock::setUp();
}
public function tearDown() {
WP_Mock::tearDown();
}
public function testBuildForNotActivatedPlugin() {
WP_Mock::userFunction( 'plugin_basename', [
'return' => 'some-plugin-name'
] );
WP_Mock::userFunction( 'admin_url', [
'return' => 'http://whatever.com'
] );
WP_Mock::userFunction( 'get_option', [
'args' => [ 'api_._activated', '0' ],
'return' => 'not Activated'
] );
WP_Mock::userFunction( 'plugin_dir_url', [
'return' => 'whatever'
] );
WP_Mock::userFunction( 'trailingslashit', [
'return_arg' => 0
] );
$plugin_version = '1.0';
define( 'PLUGIN_TEMPLATE_VERSION', $plugin_version );
$factory = new WPDesk_Plugin_Template_Factory();
$this->assertInstanceOf( WPDesk_Plugin_Template_Plugin::class, $factory::build_plugin() );
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment