Browse Source

Switch to WordPress core theme tests.

develop
Chris Wiegman 2 years ago
parent
commit
9e82ae4187
11 changed files with 161 additions and 2108 deletions
  1. +55
    -11
      .travis.yml
  2. +0
    -28
      bootstrap.php
  3. +11
    -4
      composer.json
  4. +0
    -1911
      composer.lock
  5. +17
    -0
      phpcs.xml.dist
  6. +0
    -21
      phpunit.xml
  7. +15
    -0
      phpunit.xml.dist
  8. +43
    -0
      tests/bootstrap.php
  9. +0
    -51
      tests/phpunit/Core_Tests.php
  10. +0
    -82
      tests/phpunit/test-tools/TestCase.php
  11. +20
    -0
      tests/test-sample.php

+ 55
- 11
.travis.yml View File

@ -1,19 +1,63 @@
sudo: false
dist: trusty
language: php
php:
- 7.1
- nightly
notifications:
email:
on_success: never
on_failure: change
branches:
only:
- master
cache:
directories:
- $HOME/.composer/cache
matrix:
fast_finish: true
allow_failures:
- php: nightly
include:
- php: 7.1
env: WP_VERSION=latest
- php: 7.0
env: WP_VERSION=latest
- php: 5.6
env: WP_VERSION=latest
- php: 5.6
env: WP_VERSION=trunk
- php: 5.6
env: WP_TRAVISCI=phpcs
- php: 5.3
env: WP_VERSION=latest
dist: precise
before_script:
- composer install --dev --no-interaction
after_script:
- php vendor/bin/coveralls -v
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then
phpenv config-rm xdebug.ini
else
echo "xdebug.ini does not exist"
fi
- |
if [[ ! -z "$WP_VERSION" ]] ; then
bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
composer global require "phpunit/phpunit=4.8.*|5.7.*"
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
composer global require wp-coding-standards/wpcs
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs
fi
script:
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- |
if [[ ! -z "$WP_VERSION" ]] ; then
phpunit
WP_MULTISITE=1 phpunit
fi
- |
if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then
phpcs
fi

+ 0
- 28
bootstrap.php View File

@ -1,28 +0,0 @@
<?php
if ( ! defined( 'PROJECT' ) ) {
define( 'PROJECT', __DIR__ . '/includes/' );
}
// Place any additional bootstrapping requirements here for PHP Unit.
if ( ! defined( 'WP_LANG_DIR' ) ) {
define( 'WP_LANG_DIR', 'lang_dir' );
}
if ( ! defined( 'CW_THEME_INCLUDES' ) ) {
define( 'CW_THEME_INCLUDES', PROJECT );
}
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
throw new PHPUnit_Framework_Exception(
'ERROR' . PHP_EOL . PHP_EOL .
'You must use Composer to install the test suite\'s dependencies!' . PHP_EOL
);
}
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/tests/phpunit/test-tools/TestCase.php';
WP_Mock::setUsePatchwork( true );
WP_Mock::bootstrap();
WP_Mock::tearDown();

+ 11
- 4
composer.json View File

@ -16,9 +16,16 @@
"require": {
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "5.7.19",
"10up/wp_mock": "dev-master",
"banago/phploy": "4.6.3"
"post-install-cmd": [
"wp-enforcer"
],
"post-update-cmd": [
"wp-enforcer"
],
"require-dev": {
"wp-cli/wp-cli": "^1.5@dev",
"phpunit/phpunit": "6.5.*",
"banago/phploy": "4.6.3",
"stevegrunwell/wp-enforcer": "dev-master"
}
}

+ 0
- 1911
composer.lock
File diff suppressed because it is too large
View File


+ 17
- 0
phpcs.xml.dist View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for Plugins">
<description>Generally-applicable sniffs for WordPress plugins</description>
<rule ref="WordPress-Core" />
<rule ref="WordPress-Docs" />
<!-- Check all PHP files in directory tree by default. -->
<arg name="extensions" value="php"/>
<file>.</file>
<!-- Show progress and sniff codes in all reports -->
<arg value="ps"/>
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
</ruleset>

+ 0
- 21
phpunit.xml View File

@ -1,21 +0,0 @@
<phpunit
bootstrap="bootstrap.php"
backupGlobals="false"
processIsolation="false"
colors="false">
<testsuites>
<testsuite name="Default">
<directory suffix="Tests.php">./tests/phpunit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./includes</directory>
</whitelist>
</filter>
<php>
<ini name="error_reporting" value="32767"/>
<ini name="display_errors" value="1"/>
<ini name="display_startup_errors" value="1"/>
</php>
</phpunit>

+ 15
- 0
phpunit.xml.dist View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

+ 43
- 0
tests/bootstrap.php View File

@ -0,0 +1,43 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package Chriswiegman_Theme
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
throw new Exception( "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" );
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
function _register_theme() {
$theme_dir = dirname( __DIR__ );
$current_theme = basename( $theme_dir );
$theme_root = dirname( $theme_dir );
add_filter( 'theme_root', function() use ( $theme_root ) {
return $theme_root;
} );
register_theme_directory( $theme_root );
add_filter( 'pre_option_template', function() use ( $current_theme ) {
return $current_theme;
});
add_filter( 'pre_option_stylesheet', function() use ( $current_theme ) {
return $current_theme;
});
}
tests_add_filter( 'muplugins_loaded', '_register_theme' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';

+ 0
- 51
tests/phpunit/Core_Tests.php View File

@ -1,51 +0,0 @@
<?php
/**
* Test Core functions
*
* Unit tests for Core functions
*
* @since 0.1.0
*
* @package CW\Theme\Core
*
* @author Chris Wiegman <chris@chriswiegman.com>
*/
namespace CW\Theme\Functions\Core;
use CW\Theme as Base;
/**
* Class Core_Tests
*/
class Functions_Tests extends Base\TestCase {
/**
* Files required for test.
*
* @var array
*/
protected $test_files = array(
'functions/core.php',
);
/**
* Test theme setup
*
* @since 5.0.0
*/
function test_init() {
// Setup.
\WP_Mock::wpPassthruFunction( 'remove_action' );
\WP_Mock::wpPassthruFunction( 'remove_filter' );
\WP_Mock::expectActionAdded( 'after_setup_theme', 'CW\Theme\Functions\Core\action_after_setup_theme' );
// Act.
init();
// Verify.
$this->assertConditionsMet();
}
}

+ 0
- 82
tests/phpunit/test-tools/TestCase.php View File

@ -1,82 +0,0 @@
<?php
namespace CW\Theme;
use PHPUnit_Framework_TestResult;
use Text_Template;
use WP_Mock;
use WP_Mock\Tools\TestCase as BaseTestCase;
class TestCase extends BaseTestCase {
public function run( PHPUnit_Framework_TestResult $result = null ) {
$this->setPreserveGlobalState( false );
return parent::run( $result );
}
protected $test_files = array();
public function setUp() {
if ( ! empty( $this->test_files ) ) {
foreach ( $this->test_files as $file ) {
if ( file_exists( PROJECT . $file ) ) {
require_once( PROJECT . $file );
}
}
}
parent::setUp();
}
public function assertActionsCalled() {
$actions_not_added = $expected_actions = 0;
try {
WP_Mock::assertActionsCalled();
} catch ( \Exception $e ) {
$actions_not_added = 1;
$expected_actions = $e->getMessage();
}
$this->assertEmpty( $actions_not_added, $expected_actions );
}
public function ns( $function ) {
if ( ! is_string( $function ) || false !== strpos( $function, '\\' ) ) {
return $function;
}
$thisClassName = trim( get_class( $this ), '\\' );
if ( ! strpos( $thisClassName, '\\' ) ) {
return $function;
}
// $thisNamespace is constructed by exploding the current class name on
// namespace separators, running array_slice on that array starting at 0
// and ending one element from the end (chops the class name off) and
// imploding that using namespace separators as the glue.
$thisNamespace = implode( '\\', array_slice( explode( '\\', $thisClassName ), 0, - 1 ) );
return "$thisNamespace\\$function";
}
/**
* Define constants after requires/includes
*
* See http://kpayne.me/2012/07/02/phpunit-process-isolation-and-constant-already-defined/
* for more details
*
* @param \Text_Template $template
*/
public function prepareTemplate( \Text_Template $template ) {
$template->setVar( [
'globals' => '$GLOBALS[\'__PHPUNIT_BOOTSTRAP\'] = \'' . $GLOBALS['__PHPUNIT_BOOTSTRAP'] . '\';',
] );
parent::prepareTemplate( $template );
}
}

+ 20
- 0
tests/test-sample.php View File

@ -0,0 +1,20 @@
<?php
/**
* Class SampleTest
*
* @package Chriswiegman_Theme
*/
/**
* Sample test case.
*/
class SampleTest extends WP_UnitTestCase {
/**
* A single example test.
*/
function test_sample() {
// Replace this with some actual testing code.
$this->assertTrue( true );
}
}