phpunit.xml
:<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="wp-load.php"
convertWarningsToExceptions="false"
convertNoticesToExceptions="false"
>
<testsuites>
<testsuite name="All Tests from ./tests/">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./wp-content/themes/FOO/BAR/</directory>
</include>
<report>
<html outputDirectory="./tests/report"/>
</report>
</coverage>
</phpunit>
test.yml
):.test:
stage: test
only: [ branches ]
interruptible: true
variables:
MYSQL_HOST: mysql
MYSQL_ROOT_PASSWORD: &mysql-root-password root_password
MYSQL_DATABASE: &mysql-db db_name
MYSQL_USER: &mysql-user db_user
MYSQL_PASSWORD: &mysql-password db_password
.inside-app-image: # Job template: use application image for commands execution
variables:
APP_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-branch # eg.: `registry.gitlab.com/group/repository:foo-branch
WORDPRESS_DB_HOST: 'mysql:3306'
WORDPRESS_DB_NAME: *mysql-db
WORDPRESS_DB_USER: *mysql-user
WORDPRESS_DB_PASSWORD: *mysql-password
DB_NAME: *mysql-db
DB_USER: *mysql-user
DB_PASSWORD: *mysql-password
image:
name: "$APP_IMAGE"
entrypoint: [ '' ]
before_script:
- cd /usr/src/wordpress # change working directory (path defined in Dockerfile)
# Run PHPUnit tests
phpunit:
extends: [ .test, .inside-app-image ]
services:
- mysql:8.0
script:
- sleep 10
- composer install
- composer phpunit
> @php ./vendor/bin/phpunit --no-coverage --color=always
[03-Mar-2023 13:13:58 UTC] PHP Warning: Undefined array key "HTTP_HOST" in /usr/src/wordpress/wp-includes/functions.php on line 6065
Cleaning up project directory and file based variables
00:00
Job succeeded
> @php ./vendor/bin/phpunit --no-coverage --color=always
PHPUnit 9.5.27 by Sebastian Bergmann and contributors.
................................................................. 65 / 96 ( 67%)
............................... 96 / 96 (100%)
Time: 00:00.017, Memory: 64.50 MB
OK (96 tests, 102 assertions)
$_SERVER
. Но в GitLab CI эти переменные не установлены, и поэтому возникает ошибка "Undefined array key "HTTP_HOST"".bootstrap="wp-load.php"
на bootstrap="tests/bootstrap.php"
.tests/bootstrap.php
со следующим содержимым:<?php
// Подключаем файл wp-load.php, чтобы иметь доступ к функциям Wordpress
require_once dirname( __FILE__ ) . '/../wp-load.php';
// Устанавливаем значения для некоторых суперглобальных переменных, которые используются в Wordpress
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['REQUEST_URI'] = '/';
wp-load.php
, устанавливает значения для необходимых суперглобальных переменных и предотвращает возникновение ошибок "Undefined array key "HTTP_HOST"".phpunit.xml
php
настройки внутри вашего phpunit.xml
файла, которые переопределят значение HTTP_HOST
, напр.:<php>
<server name="HTTP_HOST" value="localhost"/>
</php>
phpunit.xml
две константы WP_HOME
и WP_SITEURL
подобным образом:<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
bootstrap="wp-load.php"
convertWarningsToExceptions="false"
convertNoticesToExceptions="false"
>
<testsuites>
...
</testsuites>
<coverage>
...
</coverage>
<php>
<const name="WP_HOME" value="http://example.com"/>
<const name="WP_SITEURL" value="http://example.com/wp"/>
</php>
</phpunit>