Test PHPUnit ho an’ny theme sy plugin WordPress

Tsy dia mahazatra loatra ny tests rehefa Dev debutant, nefa tena miasa be izy rehefa manao projet long terme ianao. Miovaova mantsy ny code manaraka ny fivoaran'ny teknolojia, ka eo no tena mipetraka ny lanjan'ny tests.

Fa maninona no manoratra tests

Eritrereto anie eh, manao theme WordPress ianao. Mandeha tsara daholo, tsy misy kihana. Nivoaka anefa ny WordPress version 7, betsaka ny zavatra niova tao ka tsy maitsy manao revision ny code-nao ianao hoe mbola mandeha tsara ve ilay theme sa misy Hook niova fomba fiasa?

Dia ho averinao jerena tsirairay ve ny fonction rehetra? Raha fonctions efatra moa ilay theme anao inona no olana? Tsy izany matetika no misy fa code efa ho 1000 lignes mahery no tsy maitsy ho jerena tsirairay hoe mbola mandeha ve.

Inona moa no ataon'ilay test PHPUnit

Bash
php vendor/bin/phpunit
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 9.6.36 by Sebastian Bergmann and contributors.

.............................................................. 107 / 107 (100%)

Time: 00:04.396, Memory: 50.50 MB

OK (107 tests, 367 assertions)
Bash

Ohatr'izao no zavatra avoakany. Tests 107 isa eto, ao anaty 4 segondra dia fantatra fa mandeha araka ny tokony ho izy daholo ny code.

Mandritry ny developpement ianao dia tokony manoratra test isaky ny fonction iray soratanao. Rehefa izay dia raha misy code nampiana, mise a jour manova fonction antsoinao, ka manakorontana ny code izay nosoratanao dia vetivety dia hita hoe ato misy olana.

PHPUnit amin'ny WordPress

Samy manana ny fomba fiasany moa ny tsirairay, eto dia environement docker mahazatra ny mpanoratra no miasa.

Installation PHPUnit

Bash
composer require --dev phpunit/phpunit 
Bash

Repo WordPress test

Araky ny doc officiel moa dia mila ilay repo test an'i WordPress isika (git clone git://develop.git.wordpress.org/ wordpress-develop). Fa isika kamolahy sy kamovavy dia aleo ataon'ny composer ihany ilay asa

Bash
# Mirroir an'ilay repo wordpress-develop ihany ity, fa gere via composer.
composer require --dev wp-phpunit/wp-phpunit
# dependance tsy maintsy ampiasaina raha hanao test wordpress
composer require --dev yoast/phpunit-polyfills
Bash

Base de donnees de test

Mila database manokana moa ny tests mba tsy hanakorotana ilay base de dev. Noho izany mila mamorona base ho azy izany.

Bash
# Miditra ao amin'ilay conteneur db, dia manokatra mariadb en root
docker compose exec db mariadb -u root -p rootpass
Bash

SQL
CREATE DATABASE wordpress_test;
GRANT ALL PRIVILEGES ON wordpress_test.* TO 'wpuser'@'%';
FLUSH PRIVILEGES;
SQL

wp-tests-config.php

Eto amin'izay ny reglage momba ny environnement de tests. Aleo atao ao anaty repertoire test daholo ny resaka tests rehetra.

Bash
# Ato foana isika no miasa manombok'izao
mkdir tests && cd tests
Bash

Ity ilay fichier wp-tests-config :

PHP
<?php
/**
 * Test env configuration.
 */

define( 'DB_NAME', 'wordpress_test' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'wppassword' );

define( 'DB_HOST', 'db' ); // Noho ity dia mila ao anatin'ilay conteneur wordpress ianao no mandefa phpunit
define( 'ABSPATH', '/var/www/html/' ); // Ity dia chemin an'i ao anatin'ilay conteneur

/* Azonao atao tsara no mamoaka ilay base de donnees disponible amina port iray,
define( 'DB_HOST', '127.0.0.1:3306' );
Amin'izay ianao afaka mandefa ilay test any hivelan'ny conteneur.
Za kamo be hanao config hafa amin'izany fa aleoko manao docker compose exec.
Sady ao anaty conteneur efa misy ireo binaire php ilaina rehetra, tsy mila manao install hafa intsony. */

define( 'WP_TESTS_DOMAIN', 'example.org' );
define( 'WP_TESTS_EMAIL', 'admin@example.org' );
define( 'WP_TESTS_TITLE', 'My Theme/Plugin Tests' );

define( 'WP_PHP_BINARY', PHP_BINARY );

$table_prefix = 'wptests_';
PHP

bootstrap.php

PHP
<?php
/**
 * PHPUnit bootstrap file.
 */

// Ato ilay fichiers wordpress-develop rehefa intalle via composer. Azona atao tsara ny manao git clone dia tondroy eto fotsiny.
define( 'WP_TESTS_DIR', dirname( __DIR__ ) . '/vendor/wp-phpunit/wp-phpunit' );

// Special composer ihany koa. Raha git clone ny anao dia ataovy any ny config fa tsy ilaina ity
putenv(
	'WP_PHPUNIT__TESTS_CONFIG=' . __DIR__ . '/wp-tests-config.php'
);

require_once WP_TESTS_DIR . '/includes/functions.php';
require_once WP_TESTS_DIR . '/includes/bootstrap.php';

// Raha plugin : Alefa ilay plugin
require_once 'path-to-plugin/plugin.php'; // ilay fichier d'entree an'ilay plugin

// Raha theme
switch_theme( 'mon-theme' );
PHP

phpunit.xml.dist

Ity no fichier manondro an'i phpunit ireo options rehetra ampiasaina

XML
<?xml version="1.0"?>
<phpunit
	bootstrap="tests/bootstrap.php"
	backupGlobals="false"
	colors="true"
	convertErrorsToExceptions="true"
	convertWarningsToExceptions="true"
	convertNoticesToExceptions="true"
	convertDeprecationsToExceptions="true"
	>
	<testsuites>
		<testsuite name="testing">
			<directory prefix="test-" suffix=".php">./tests/</directory>
		</testsuite>
	</testsuites>
</phpunit>
XML

Izay, vita. Manoratra ireo tests sisa

Navigation

0 commentaire

Envoyer un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

<a href="https://mety-aby.com/author/nampoina/" target="_self">Nampoina Razafindralaisa</a>

Nampoina Razafindralaisa

Developpeur Web ny mpanoratra. Mba mizarazara ny hevitro fotsiny aho eto. Mamakia finaritra!