├── .gitignore ├── .travis.yml ├── README.md ├── composer.json ├── example-plugin.php ├── phpunit.xml.dist ├── src └── Plugin.php └── tests ├── bootstrap.php ├── test-example.php └── wp-config.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /wordpress/ 3 | composer.lock 4 | phpunit.xml 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | branches: 2 | only: 3 | - master 4 | 5 | language: php 6 | 7 | services: 8 | - mysql 9 | 10 | env: 11 | global: 12 | - WP_DB_USER=wp 13 | - WP_DB_PASS=password 14 | - WP_DB_NAME=wp_tests 15 | - WP_VERSION=* 16 | 17 | php: 18 | - 8.0 19 | - 7.4 20 | - 5.6 21 | 22 | matrix: 23 | include: 24 | - php: 5.6 25 | env: WP_VERSION=4.9.* 26 | 27 | before_script: 28 | - mysql -u root -e "GRANT ALL PRIVILEGES ON ${WP_DB_NAME}.* TO ${WP_DB_USER} IDENTIFIED BY '${WP_DB_PASS}';" 29 | - mysql -u root -e "CREATE DATABASE ${WP_DB_NAME};" 30 | 31 | install: 32 | - composer validate --strict 33 | - composer require --no-update --dev roots/wordpress:${WP_VERSION} wp-phpunit/wp-phpunit:${WP_VERSION} 34 | - "[ $TRAVIS_PHP_VERSION == '8.0' ] || composer install" 35 | # Temporary hack to allow installing PHPUnit 7 with PHP 8 until WP 5.9. 36 | # as WP PHPUnit does not allow PHPUnit higher than 7 yet. 37 | # See https://github.com/WordPress/wordpress-develop/commit/8def694fe4c5df95f8e20e40389faf9cb92b6dca 38 | - "[ $TRAVIS_PHP_VERSION != '8.0' ] || composer install --ignore-platform-reqs" 39 | - composer show 40 | 41 | script: 42 | - composer test 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example Plugin 2 | 3 | [![Build Status](https://app.travis-ci.com/wp-phpunit/example-plugin.svg?branch=master)](https://app.travis-ci.com/wp-phpunit/example-plugin) 4 | 5 | A complete example for using WP PHPUnit in the context of plugin development. 6 | 7 | ## Features 8 | 9 | **Travis CI Configuration** 10 | 11 | A ready to use `.travis.yml` configured with a reasonable test matrix 12 | - Includes all currently supported versions of PHP (7.4 and 8.0) and the current WP minimum required PHP 5.6 13 | - Tests against the latest version of WordPress, as well as an older version (4.9) although it could be easily extended to include multiple older versions, multisite, etc. 14 | 15 | **Just Add Tests** 16 | 17 | This project includes a working example `test-example.php` test case. Adding tests is as simple as adding methods to this example class! Getting up and running couldn't be easier. 18 | 19 | ## Local Development 20 | 21 | Install Composer dependencies 22 | 23 | ```sh 24 | $ composer install 25 | ``` 26 | 27 | Create a database for your tests to use and update your `tests/wp-config.php` as necessary. 28 | 29 | ```sh 30 | $ mysqladmin create wp_phpunit_tests -u root 31 | ``` 32 | 33 | The database name defaults to `wp_phpunit_tests`, but you can change this in the `tests/wp-config.php` without affecting the Travis configuration which is environment variable-based. 34 | 35 | Run the tests 36 | 37 | ```sh 38 | $ composer test 39 | ``` 40 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wp-phpunit/example-plugin", 3 | "description": "Example plugin using WP PHPUnit", 4 | "type": "wordpress-plugin", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Evan Mattson", 9 | "email": "me@aaemnnost.tv" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "Example\\": "src" 15 | } 16 | }, 17 | "require": { 18 | "php": "^5.6 || ^7.0 || ^8.0" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "^5 || ^6 || ^7", 22 | "roots/wordpress": "^5.0", 23 | "wp-phpunit/wp-phpunit": "^5.0", 24 | "yoast/phpunit-polyfills": "^1.0" 25 | }, 26 | "scripts": { 27 | "test": "phpunit" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /example-plugin.php: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | ./tests/ 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/Plugin.php: -------------------------------------------------------------------------------- 1 | assertTrue( function_exists( 'do_action' ) ); 7 | $this->assertTrue( function_exists( 'example_plugin_file' ) ); 8 | $this->assertTrue( class_exists( 'Example\\Plugin' ) ); 9 | } 10 | 11 | function test_wp_phpunit_is_loaded_via_composer() { 12 | $this->assertStringStartsWith( 13 | dirname( __DIR__ ) . '/vendor/', 14 | getenv( 'WP_PHPUNIT__DIR' ) 15 | ); 16 | 17 | $this->assertStringStartsWith( 18 | dirname( __DIR__ ) . '/vendor/', 19 | ( new ReflectionClass( 'WP_UnitTestCase' ) )->getFileName() 20 | ); 21 | } 22 | } -------------------------------------------------------------------------------- /tests/wp-config.php: -------------------------------------------------------------------------------- 1 |