WordPress のテスト環境を以下のように設定します。
test_db:
image: mysql:5.7
platform: linux/amd64
container_name: test_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress_test
MYSQL_DATABASE: wordpress_test
MYSQL_USER: wordpress_test
MYSQL_PASSWORD: wordpress_test
# Install Composer for PHPUnit
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& chmod u+x composer.phar \
&& mv composer.phar /usr/local/bin/composer
ref. Download Composer
Yoast / PHPUnit-Polyfills は WordPress の単体テストで必要になります。
{
"autoload-dev": {
"psr-4": {
"SHiroshi\\Tests\\": "wp-content/themes/my-theme/tests/"
}
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"yoast/phpunit-polyfills":"^2.0"
}
}
composer.json を コンテナにコピーします。
# Install Composer
# Install PHPUnit & PHPUnit Polyfills
COPY ./composer.json /var/www/html/composer.json
RUN cd wp \
&& env COMPOSER_ALLOW_SUPERUSER=1 composer install \
&& composer dump-autoload
テーマディレクトリ(例 my-theme )に phpunit.xml.dis を作成。
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<php>
<const name="WP_TESTS_PHPUNIT_POLYFILLS_PATH" value="/var/www/html/vendor/yoast/phpunit-polyfills"/>
</php>
<testsuites>
<testsuite name="testing">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
ref.
コンテナ内で wp scaffold theme-tests コマンドを発行します。
container$ cd /var/www/html/wp-content/themes
container$ wp scaffold theme-tests my-thema
`my-theme ディレクトリに bin/install-wp-tests.sh が作成されます。
コンテナ内で実行します。
container$ cd /var/www/html/wp-content/themes/my-theme
container$ bin/install-wp-tests.sh wordpress_test wordpress_test wordpress_test test_db
container$ ../../../vendor/bin/phpunit
<?php
/**
* WP_UnitTestCaseのサンプル
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class Sample_Test extends WP_UnitTestCase {
/**
* 単一ページテスト
*
* @link http://qiita.com/miya0001/items/8f1a777b455a8bdcbd62
*/
public function test_the_title() {
$post = $this->post_ids = $this->factory->post->create_and_get( [
'post_title' => 'Sample Post!',
] );
$this->go_to( '/?p=' . $post->ID );
$this->assertTrue( is_single() );
$this->expectOutputString( "Sample Post!" );
the_title();
}
/**
* アーカイブテスト
*/
public function test_single_cat_title() {
$cat = $this->factory->category->create_and_get( [ 'name' => 'Sample Category' ] );
$this->factory->post->create( [ 'post_category' => [ $cat->term_id ] ] );
$this->go_to( '/?cat=' . $cat->term_id );
$this->assertTrue( is_archive() );
$this->assertFalse( is_single() );
$this->expectOutputString( "Sample Category" );
single_cat_title();
}
}