Unit Tests in PHP With PHPUnit

## Metadata
- Author: [[JuanMa Garrido]]
- Full Title: Unit Tests in PHP With PHPUnit
- Category: #articles
- Document Note: ---
tags: [#phpunit, #wordpress, #docker]
Author: Juanma
Source: Unit Tests in PHP with PHPUnit (YouTube)
Reference: https://www.youtube.com/watch?v=MEcumw0sb6Y
date created: 2025-08-09 11:22
---
# Title: Unit Testing in PHP with PHPUnit for WordPress Development (Docker + CI)
## Table of Contents
- [📝 Summary](#-summary)
- [📖 Main Content](#-main-content)
- [🗒️ Notes](#️-notes)
- [📖 References](#-references)
- [🗣️ Paraphrase](#-paraphrase)
- [📰 Newsletter](#-newsletter)
- [✅ Action Items](#-action-items)
- [🧪 Hypotheses](#-hypotheses)
- [📊 Methodology](#-methodology)
- [❓ Questions](#-questions)
- [🔮 Future Research](#-future-research)
- [💡 Implications](#-implications)
- [🧠 Learning Styles Assessment](#-learning-styles-assessment)
- [📊 Accessibility Assessment](#-accessibility-assessment)
- [🌍 DEIB Assessment](#-deib-diversity-equity-inclusion-and-belonging-assessment)
- [✅ Logical Validity](#-logical-validity)
- [🔍 DARVO Analysis](#-darvo-analysis)
- [🔍 SEO Evaluation](#-seo-evaluation)
- [🎯 Conclusion](#-conclusion)
---
## 📝 Summary
This session demonstrates setting up PHPUnit for PHP-based unit testing in WordPress plugin development using Docker (via WPM/Dolb BPM) and integrating with Continuous Integration (CI) workflows. It covers WordPress Playground for in-browser PHP testing, scaffolding isolated plugins for test environments, configuring PHPUnit (`phpunit.xml`), handling Docker-based environments, and troubleshooting issues like “no tests executed.” The tutorial also walks through running tests locally, updating configurations, creating plugin settings pages, loading CSS, and writing PHPUnit test cases.
---
## 📖 Main Content
### 🗒️ Notes
**[01:39](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=99)** — *Intro to PHPUnit in PHP*
- Goal: Set up repo to execute unit tests in PHP.
- Uses Dolb BPM with Docker.
- Covers TDD concepts and CI workflows (trigger tests on PR push).
**[06:24](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=384)** — *WordPress Playground & PHP Testing Tools*
- Playground runs WordPress directly in browser — no PHP server needed.
- New tool runs PHP code (including WP code) directly in browser.
- Supports multiple WP versions and core functions.
**[13:04](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=784)** — *Isolated Plugin Setup*
- Fresh plugin environment for isolated testing.
- Removes existing version before initializing new one.
**[16:54](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=1014)** — *Configuring PHPUnit & Dependencies*
- Defines test suites and bootstrap in `phpunit.xml`.
- WPM manages plugins, relies on Docker for execution.
**[23:38](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=1418)** — *Updating WP Instance*
- Uses `npm run mpx start --update` to add plugins to Docker WP instance.
**[27:09](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=1629)** — *Installing PHPUnit*
- Installed via Composer.
- Config ensures consistent bootstrap & test execution.
**[33:34](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=2014)** — *PHPUnit in Docker WP*
- Supports multiple DBs for local & CI.
- Each DB on different ports.
**[37:45](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=2265)** — *WP-CLI for Plugin Management*
- `mpx.pm destroy` resets WP instance.
- Use CLI to execute plugin operations inside container.
**[45:12](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=2712)** — *Running Tests*
- Run PHPUnit inside container folder.
- “No tests executed” often due to misconfigured XML.
**[51:13](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=3073)** — *Troubleshooting PHPUnit*
- Fix: Remove incorrect `exclude` in XML.
**[59:21](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=3561)** — *NPM Scripts for PHPUnit*
- Scripts manage start/stop/clean/test flows.
- WPM as dependency for easy `npm run test all`.
**[1:11:13](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=4273)** — *Loading CSS & Settings*
- `enqueue_style` for custom dashboard CSS.
- Adds settings page to store custom DB option.
**[1:25:55](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=5155)** — *Creating & Executing Tests*
- Test classes must end with `Test` to be recognized.
- Extends WP test case class for utility functions.
**[1:44:40](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=6280)** — *Setup Functions & AI Code Gen*
- `setUp()` runs before each test.
- AI can help generate code, but manual validation needed.
**[2:01:05](https://www.youtube.com/watch?v=MEcumw0sb6Y&t=7265)** — *Refactoring with Tests*
- Tests ensure safe refactoring.
- Next steps: push tests to repo.
---
## 📖 References
- [Unit Tests in PHP with PHPUnit — YouTube](https://www.youtube.com/watch?v=MEcumw0sb6Y)
- [WordPress Playground](https://playground.wordpress.net)
- [PHPUnit Documentation](https://phpunit.de/documentation.html)
---
## 🗣️ Paraphrase
1. *Original*: “WordPress Playground allows you to run a WordPress installation directly in the browser without needing a PHP server.”
*Paraphrase*: You can launch WordPress in your browser instantly using Playground, without setting up a local PHP server.
2. *Original*: “Tests must extend specific classes to utilize utility functions for creating content.”
*Paraphrase*: To use built-in content creation helpers in tests, your test classes need to inherit from specific base classes.
---
## 📰 Newsletter
**🧪 This Week in WordPress Dev: Testing Like a Pro**
Unit testing in PHP just got friendlier for WP devs! 🎯 Using Docker and the WPM toolkit, you can now spin up a fresh WordPress environment, scaffold a plugin, and run PHPUnit tests — all from your terminal. Highlights:
- WordPress Playground’s new in-browser PHP tool 🖥️
- Composer-powered PHPUnit install ⚙️
- NPM scripts for quick `start`, `stop`, and `test all`
- Real-world example: adding CSS & a settings page to your plugin
Full replay & code: [Watch here](https://www.youtube.com/watch?v=MEcumw0sb6Y)
---
## ✅ Action Items
- Create internal guide on setting up PHPUnit for WP plugins using WPM.
- Add “troubleshooting no tests executed” to onboarding docs.
- Explore CI/CD automation for test runs on PR push.
- Integrate Playground PHP tool in contributor training.
- Consider using AI code gen only as a helper, with manual verification.
---
## 🧪 Hypotheses
- If contributors adopt WPM + PHPUnit early, plugin quality and CI pass rates will improve.
- Using Playground for quick WP code tests reduces local setup friction.
---
## 📊 Methodology
- Test setup in Docker with isolated DBs.
- PHPUnit XML for test suites and bootstrap.
- Controlled plugin environment via WPM JSON.
---
## ❓ Questions
- How can Playground’s PHP tool be extended for automated test case generation?
- Could WPM be integrated directly into WP-CLI core?
---
## 🔮 Future Research
- Compare WPM/Docker test speed vs. traditional VVV/local WP setups.
- Investigate remote CI execution with Playground-like environments.
---
## 💡 Implications
- Faster onboarding for WP plugin contributors.
- Lower barrier to TDD adoption in WP ecosystem.
---
## 🧠 Learning Styles Assessment
**Visual**: Could add architecture diagrams for WPM setup.
**Aural**: Provide narrated walkthroughs.
**Verbal**: Rich code comments already present.
**Physical**: Include “try it yourself” tasks.
**Logical**: Good step-by-step setup, could add flowcharts.
**Social**: Pair programming exercises possible.
**Solitary**: Setup allows independent experimentation.
**Naturalistic**: No direct nature references — none needed.
---
## 📊 Accessibility Assessment
- Screen reader compatibility depends on YouTube captions (enabled).
- CLI output should be documented in text for screen reader users.
---
## 🌍 DEIB (Diversity, Equity, Inclusion, and Belonging) Assessment
- Neutral technical language.
- Could add diverse developer perspectives/examples.
---
## ✅ Logical Validity
- Strong causal reasoning: “Tests passing = safe refactor.”
- No major fallacies detected.
- Suggest explicitly stating why certain configs fix test discovery issues.
---
## 🔍 DARVO Analysis
- No DARVO patterns — purely instructional.
---
## 🔍 SEO Evaluation
- Keywords: “PHPUnit WordPress”, “Docker testing WordPress plugins”.
- Cou
- Summary: Explore with me how to test WordPress PHP code with PHPUnit: we’ll spin up a Docker-powered wp-env test stack, create WordPress-standards-compliant unit tests, use TDD for clean architecture, and ,if we have time time, wire everything into CI pipelines—so your plugins and themes stay stable, refactor-ready, and bug-free.
#stream #english
2:24 Introduction to PHPUnit testing with WordPress development
4:23 Setting up WordPress Playground with new demo content and PHP REPL tool
11:42 Creating a plugin scaffold using WP-CLI and Docker setup
16:12 Configuring WP-ENV JSON and installing local dependencies
20:32 Understanding PHPUnit configuration files and bootstrap setup
26:40 Running first PHPUnit test with Docker containers
32:40 Setting up proper test execution environment and debugging
41:04 Implementing a reading time plugin with content filters and settings
1:03:44 Creating plugin functionality with admin settings and styles
1:15:52 Writing unit tests for plugin functionality using AI assistance
1:20:48 Running and debugging PHPUnit test execution
1:32:16 Troubleshooting test failures and WordPress test environment setup
1:42:32 Testing admin functionality and global variable access issues
- URL: https://www.youtube.com/watch?v=MEcumw0sb6Y
Heat. Heat.
Heat. Heat.
Heat. Heat.
Okay. Hey, hello. Hi. Welcome to another
session, another live streaming session
about WordPress uh development. So,
as you know or some of you know, I'm streaming every Friday at around this
hour about WordPress development. And as you can see, I'm here in a special place because I'm in a co-working uh because I just moved to a a new place and the fiber connection is going to take uh some time to be installed at my at my house. So So yeah, I need to to improvise and yeah, I think that this place is good enough for the streamings.
Um, let me know if uh there's something that uh doesn't work, if the sound is not good or or something. But uh yeah, last stream I already did it here and I think it worked uh well enough. So today um I want to focus the session on on unit testings in PHP with PHP unit. And what I would like to cover
today is how to set up um repository to execute to to trigger to launch the the unit testings in in PHP. And we will focus on the Dolb BPM approach which works with Docker. And then we will also um create some tests and and explain a bit a few things uh
about testing like a TDD or um how to how to implement um a workflow for continuous integration. So every time for example we um we push a pull request we get all the all the testings uh triggered. So yeah these are uh the things that I plan to do today and yeah hope we have uh time uh for everything.
I'm going to share my screen because there are a few things I would like to comment before we start some news that have happened in the WordPress ecosystem more specifically in the WordPress playground ecosystem. So let me share my screen. Okay. So what I would like to comment is um okay where where did I share this? here.
Yeah. So, before we start, let me comment this uh few things. Also, if you are in the in the chat, you're over there, just say hi. Um and also share from where you are connected, which is always interesting. I think the sound the background maybe is a bit high. Okay, I'm going to Yeah, I think this is better. Okay, I want to
comment two things today before we dive in the unit testings in PHP. First thing is that playground.wordpress.net which is the like the online instance of uh WordPress playground. Now when you enter it has like a demo content that is more self-explanatory that uh what there was
before and yes you can see you you can get here um hello from from WordPress playground and it explains a little bit that uh playground is um a technology that allows to um launch or to have a WordPress installation running directly in the browser. So you don't need a PHP server and there is a a very good introduction
in video format and then some interesting links. So yeah, um this is one of the things I would like to to share because I think it's it's an improvement and it this will help to adopt this will this will help with the adoption of of this I think um I don't know if you are all familiar with WordPress playground but um in case you are not this is a good starting point play.wordpress.net net also h if you add
docs you will get redirected to the to the docs and there's also official uh WordPress uh playground I think it has like a proper landing page under WordPress.org So yeah, uh lots of places to start uh with this. I invite you to to check this out if you didn't know about this technology. So yeah, that's one of the
things I I wanted to commend. Second thing, the playground team has uh created um a new tool that uh uses the same technology that is behind WordPress playground to run um like a PHP how is it called EPL. Um well the thing is that you can launch PHP directly in
the browser. So this is really good to to quick quickly uh test things, test uh PHP code and also a really cool thing is that you can test um WordPress code. Now you have access here to different versions of WordPress and you have access to all the functions that are uh in WordPress. Um, so yeah, this is a really good way to
to test something quickly and to try um to check the output of a specific functions in warper. For example, also to compare the output in different versions. Here you have um a little example of using the HTML um processor, the HTML API to check a specific HTML uh code, but you can check uh anything you want. So yeah,
I I think this is really cool. So I also wanted to share this. Another cool thing is that um for example if you add uh more things I don't know if it can be seen here um echo uh hello hello from the stream and we run this
and we can also add uh uh here. Um we can run this. Okay. So the cool thing is that if we copy and paste this, yeah, you can see that the the URL
changes every time you change the code. So you can directly I'm going to share this in the in the chat just so you have it as an example and I'm also going to share uh the previous uh page that contains the new the new demo content that I think is more um it's more helpful uh now okay so these are
the two things I wanted to to command. Now let's uh get into PHP uh tests with PHP unit. I have several links we can uh check here. I have some notes here. Um but I think a good place to start as you know I don't have uh much uh prepared
there is no specific agenda. I only have some ideas that I want to comment. So feel free to to suggest um to suggest things. So if we we have this automated testing PHP unit which is the official uh documentation and um what we can do I think what we can do is we can scaffold a plugin using
the Dolby VP CLI and from there uh we can implement Dolby VPM to run those tests and and see what see what happens. So let's let's start from there. Let's open the terminal. Let's open the terminal. I'm going to make this bigger. I'm going to go to projects. Okay. And here um
there is a there is a WPCLI has this scaffold tool. Um okay I'm going to let me
Oops. Okay. I'm going to change the name of this and try to make it um more easier to read. So, my awesome plugin test. If I do this, I can um Okay.
Um Okay, this seem to be okay. Yeah, because I need to do this in a WordPress installation. So, what we're going to do is Okay, I'm going to open the studio WordPress studio
where I have here like several WordPress installations. And from here, I'm going to go to the terminal and here I can go to the Dobby content plugins. And here is where we can do what we were trying to do earlier. In fact, I think that I have already done this. So, let me check my awesome plugin. Yeah. Uh, so I'm going to remove my awesome plugin
and I'm going to run this again. Yeah, too many positional arguments. Yeah, because um this. Okay. And now what we can do is to go to studio again and we can open this in cursor. And here in plugins
we have my awesome plugin. Nice. What we can do is to what I'm going to do is to actually um I'm going to open this in the terminal. I'm going to open uh W content uh
plugins my awesome plugin. I'm going just to work here because so this is part of um WordPress installation in a studio but I want to work on this as if I were working on an isolated plugin and maybe we can um create a specific repository for this plug-in. So this the code of this plugin
is going to be completely isolated from the rest of the WordPress installation. Okay. So let's check uh what we have here. We have um PHP unit which is the the file the configuration file of PHP unit. When we run PHP unit this is the file that is going to be executed. And from here it's going to look for um
for yeah whatever we define here in the test suite. So um files with PHP extension and with the prefix test hyphen which is the the test example that is here. And then we have a test bootstrap which is this file here. This file is executed every time we run the tests. And some of the things that
it does is check that we have some files available. So the thing is that we don't have this um files. We don't have a PHP unit installed and we don't have a lot of things but um we will have them with um Dolby VPM. Okay. So, we need to first run
uh Dolby VPM to run this plug-in inst inside of a warp installation. So, let's do that. So the first thing I have to do here is to install WPM as a um development dependency here. There is no
package JSON here but I think this will create one nice justice. So this will install WBM locally. And what we can do, I think if I run, let me check. If I run um MPX, if I run MPX
WBM start WBM start. I think it will start a WordPress installation with this plug-in installed. H I'm not sure if I I I think it's automatically detected. If not, there is a WPM uh config that we can add to specify that. But uh let's
try this to see what happens. Okay, could not connect to Docker. So we need to open docker and um okay so now we have docker here running and now if we run again wpn start.
Um, okay. So, it says could not find WP configuration and could not determine if uh this is a warp installation, a plug-in or a theme. So, yeah, it's not detecting that. So, we need to to create um a WPM uh JSON file. So, what we need to do here is to create a WPM JSON file.
That can be something like this. I'm going to simplify this. In fact, to start with, I'm just going to do this. I'm going to stop this. And now I'm
going to run again. MPX WBM start. Let's review in the meantime. WPM and all the things that we can do with WBM.
So here we have in the block edator handbook the documentation about this um this tool. We can install it globally which will allow to launch Dolby VM on every project without the need of installing any local dependency. But I think it's a good practice to
um to install it as a local dependency of the project so every developer can very quickly um leverage this um this package as well. Like for example here npmeres hyphen save de like here. Okay. So now we have here um
our WordPress instance and we can see this admin password. If you check the documentation, you will find this credentials and yeah, we can see here that we have our plug-in installed all of that because we have added this WBM JSON. So here we can specify different plugins.
Um, imagine this Dolvp is not in the plugins folder but is in the WordPress code folder. In the WordPress um installation uh folder, we're going to specify which plugins we want to um we want to load in the in the WordPress
instance that is in docker. So for just one um plugin is fine. Also we can add here some other plugins like for example um Gutenber uh Gutenber I think we need to add uh this probably Gutenber so let's try this and what we can do is WPM
update update let me check uh the documentation is update. Yeah. So if we change something from the configuration, what we can do is um what we can do is like refresh
let me just um So here mpm run mpx start hyphen hyphen update. I think it's this. Yeah, there is a difference. Okay, here is using two hyphens because WPM is a script and to pass arguments to
a script, we need to use the the like the double hyphen. We use hyphen to indicate that we are going to pass a flag. Okay, so this is being updated with this change. In theory, it will update the WordPress instance in Docker with two plugins. the one that is in the current folder and WordPress number. Let's see.
So, we are testing WPM to see um how this works. Uh where were we? Here. Okay. See, as you can see, it's downloading
WordPress uh with plugin. And now it will start WordPress and it will show us the the links to access the the WordPress configuration, the WordPress installation. So let's go here. If we go to WP admin and we go to plugins, yeah, we can see Gutenber and we can see my awesome
plugin. Okay, nice. Now next step. Um here we have the configuration of um PHP unit. Here we have what else? The bootstrap file and we have a test sample and we don't have uh anything else. So how
do we launch this uh test? So we need to install PHP unit. And if we go to let's check uh more info about PHPUnit. PHPUnit the PHP testing framework. Okay. Is there is there any introduction here documentation? Uh here there is information that are
supported. Isn't there any generic introduction contributing to PHP unit? Okay, let's see the Redmi MD. Okay, this is about uh contributing. Okay, so no generic information. Nice. So what we need to do is to install um the framework the testing framework and we can do that with uh composer. So if we come here
in this uh repository we have a composer.json that um doesn't have anything special. So we are going to add this package and as you can see it's going to install PHP unit and a lot of things and all these things are uh will be in vendor and we are also going to install some
poly fields that are uh recommended. So we have these two packages. So in any project we need to make sure that our composer.json JSON includes these uh two packages. Then um PHP unit when we run this um it will first execute the PHP unit XML.
I think this is going to take it as well. Um, but I'm going to first wait, let me copy this and let's add then we will try this. This works for sure. This is the configuration file for
the tests. Um it has some configuration parameters. The most important ones are bootstrap um which is the the code we want to execute on every um execution of the tests. And this points to this file bootstrap which um this code
does uh several things. First we check if there is a environment variable named WP tests div. So we need to have this environmental uh environment variable but uh this variable is already defined in uh with WPM. So as we are going to launch these tests inside um
our WordPress installation that is in docker in WPM, we will get access to to this this um test. It will point to Let me change this for another
code that I have here. So I'm going to and I'm going to create another bootstrap. Um yeah, so this um environment variable internally in the container it will point to this
um to this folder. Um so we will this will work but if not there is a fallback for non uh WPM environments. Then we need to check that in that environment inside the container there are some there is the code that uh um contains all the PHP unit code like for example includes functions and there was
another one includes uh bootstrap for example. Um and this will ensure that when we run the test we have all the functions available in our test. For example here it will make sure that we have this WPU unit test case we need to uh load this code here in order to have the tools and utilities in the unit tests. Okay. So
this is a code for now. we are going to have some faith some faith that this is going to work. Um so we have the PHP unit that points to the test bootstrap file which is this one and here is going to do some checkins before running the test. Okay. So we have the most important things in place. Now, what we can do is to I'm going to do
some diagrams later. That's looking really good. Hey, David, good to see you. How you doing? Where are you? Are you in Barcelona still? I'm doing um streams every week and one
week I do the stream in Spanish and the other week it's in English. So yeah, today we need to speak in English. Okay. So I will continue with um with this um Oh yeah, I see that you you sent the message like some time ago. Um
so we have the PHP unit XML which points to the test bootstrap. PHP which is here. And then what we can do is um first going to uh MPX WPM status.
Um we can check docker And well I also have this here which is the docker dashboard. And here we can see that uh doubleVPN has opened like all these
all these different um servers. It has um WordPress one then MySQL then MySQL server. So it it runs I think this is information that we we got when we um launch.
and let's go. So if I run okay so we have a WordPress develop development site but we have another one for test uh for test this is used for continuous integration I think that we can run the local tests here
um and we have different databases one for local development but another one for um continuous integration and each one of them run in different uh in different ports. Okay. So let's continue with the Now what we need to do is to
for example um I'm going to add here some notes and WPM
plugins and well the PHPUnit code is actually here in vin PHPUnit. So what we're going to do is we are going to run a test CLI which is okay. We go to the docker installation which is here.
We have a test CLI. I think it has one because um there is another instance or maybe because we updated it. Let's see if we can get the CLI. So what we can do here is MPX um WBM destroy.
This will destroy the the current WordPress. This is a good way to start from scratch. If uh we start getting errors in the docker um in the docker installation in the docker um version of WordPress. We can start from scratch with the stray mpx.pm destroy and then we can just start again with uh
mpx.pvm start. If we go to docker, we will see that there is no uh WordPress instance anymore, but it will be created. It will be created when we run uh WPM start. So yeah, it's very easy to to to remove all the installation and start from scratch as long as we have the
proper configs and everything. Um that's that's fine. Okay. So yes, MySQL1 yeah it generates several servers inside the container the MySQL1 and um
Guess going to remove this because I don't really need this the composer JSON. No, in the WVPM JSON this was to show we can install more plugins. So for the next time we
don't need to download it. So what we can do now is to execute commands in the different servers. In order to do that, we need to specify which uh environment and we have the WP CLI installed here in tests CLI.
So here we can specify. Okay. So when we run um well let's write this. So for this of use composer PHP unit and WPCLI are available for in the
environment to run this executables use WPM rune the environment the tool and the command. for example, uh DolbyPM room CLI composer install or Dolby PM room tests CLI PHP unit. So for example, we can do let's do let's try this um WPM room CLI bash
and we have access to the bash and here we can see um do do we have the tree? Okay. Uh yeah, too much. Maybe what I can do is three um okay tree uh Unix command. I want to say that uh
levels. Yeah. Three two 11.
Error opening did. Well, here's our WordPress installation. You can also access various shells like WPM room CLI bash or or we can launch this which opens a shell of the WordPress installation. So, um
how was this? Get block info. Uh blocking for name. Blocking for uh description.
lo info uh version. Yeah, for example, we can check the WordPress version because we are running the shell of WordPress which is uh very handy. And then for the Empire CLA and WordPress um they share a database and math volumes but more tools are available in the CLI environment. You should you should use the test CLI and
test tests WordPress environments for a separate testing database. By default, the CBD so the folder where uh the the tool is executed the CVD of the wrong command is the root of the WordPress install. If you are working on a plug-in, you likely need to pass mwd to make sure composer phpunit commands are executed relative to the plug-in you
are working on. For example, um wp um rune cli blah blah blah composing status. Okay. So what we can do is um bash and then we can check inside the uh docker instance we can check if we have access to all these files. So we can do content
uh plugins and yeah we have here access to everything. So we can run things inside this. Um and what we can do is what we can do is um is this
H in my awesome plug-in we can run this. So let's um exit clear and we are going to run this. So we are going to run the CLI um we are going to run a a command test CLI in this um
so we are going to run this tool in this folder that is inside of the docker container in this uh container is the right word environment and these are the environments that we have here in docker. So we have a test cla. Okay. So let's see.
And we have here an error. Okay. So something Um,
no tests executed. Um, running a single side run multi-side use that not running a test. So this um should have run this test sample.
Um So this should have uh run these tests. Okay. So let's add
Um,
okay. Let me see. Um, they have a request. I don't know. Goku sand. It says that um Okay, let me know in the chat um if you
have any questions. So, let's see what happens here. We are running this.
my awesome plugin and let me see PHP unit. It runs uh this. Okay,
let's add another another function just to see the sample uh to this as a true. Okay, no tests executed. Um why there are no tests executed? If we go to PHP unit, we have the test
sample and it's running test bootstrap. It's loading the plugin.
Mhm. It's loading the plugin. Okay, let's ask for help. Um, let's see if um
I'm going to try GBT5. Uh, okay. So I'm uh trying to run uh this uh uh demo test inside of a uh Docker container using Wbpm. It seems the tests are not being executed.
Um, do you have uh any idea why
Okay. So, um the command used to I'll quickly inspect your PHP unit config which is this one to see why not test are being discovered. Then also use the minimal fix and rerun the tests. So yeah, what um what it's doing is checking all the code here and and seeing if there's something
something wrong. Is this working? Let me see.
Yeah, working well. Um, run ah because there was a
um initialize repository here. Okay, let's initialize repository here. And what I'm going to do is I'm going to um let's do one thing. I'm going to move this code
to the streams folder. So my awesome plugin I'm going to Okay. First. Okay. So, what I want to do is I I I have this streams repository and in this streams
repository I have all the projects that I I work on the streams. So, what I want to do is to move this plugin here. Okay. But first before doing that I want to check I think it was just that the uh there was your PHP unit XML PHP unit XML. Uh okay. So it was specifically
excluding this test sample. So I'm going to undo this. H test uh some just doing this with this. Yeah. So now it works. So it was it it was just that everything was working. Okay. So now we can um continue and maybe we
can do some diagrams to explain this um a bit more. What I want to do is to I'm going to uh first MPX.bm BPM destroy I uh no destroy and I when I'm sure yes I'm very sure of that.
Second thing that I'm going to do is I'm going to move this uh my awesome plugin to my awesome plug-in tests inside of the streams repository. So then I can I can upload this uh directory. Okay. Now I'm going to go here. Clear and
I'm going to move my awesome plugin. I'm going to move it to my home folder. projects 2025 streams projects and um I'm going to rename it to my awesome my awesome plug-in with tests.
Nice. Now, what I'm going to do is to I don't have my awesome blank here anymore, but what I'm going to do is to add a sim link to uh from projects 2025 uh streams projects, my awesome plug-in with tests. And I'm going to
my awesome plugin with tests here. So now I have the repository somewhere else. But I should be able to use this uh here. Okay. Nice. What else? Now what we can do is to go to come here and do but what I'm going to do is I'm going to add a batch of um
scripts. If we go here to Okay, this is not working anymore. And now what I have to do is I need to go to um I need to go to projects 2025
um streams projects my awesome plug-in with tests and then I can open this with cursor and then here. What I can do is the package.json which is here. I'm going to add all the scripts and I'm going to ask um
I'm going to ask uh fill more popular fields. I'm going to ask Corser to fill some more files like maybe the the name of the of the project, maybe the the author, whatever, just to have some like a nicer structure.
But what we have here is we have WPM installed as a dependency. And now we have all these scripts that we can use to launch the different uh things. For example, we can start, we can stop, we can clean, and we can launch the unit. Instead of uh doing all of this,
we can do we can do this. We can just run mpm run test on. I need to change the name of the folder which will be the name of the folder. I guess that it will be. Yeah, I think this is nicer.
Good. I like it. I like it. So, what I need to change here is this my awesome plugin with tests. This should work. Now, what I can do is um npm rune m
start. And now it will open the Docker instance using the WPM JSON which is this plugin. I'm not sure about the name. I think it will take the name of this folder. So yeah, we can we can check that uh when the WordPress instance is um
launched in the Docker instance. We can open the shell and see the the name of the folder that have used to to to place to locate this um this plugin. The code of this plug-in.
Okay, I'm going I'm going to go for water.
What the Okay, this has finished. Yep.
And what we can do now is uh let's open the the bash. What was this? Uh, DolbyPM room cla shell. Uh, CLI bash. You can go to WP content. Here we're inspecting the
my awesome plugin with test. So it's taking the the current folder name. Nice. So this code will work. No, what we are doing here is we are specifying if we go to my awesome time with tests and we do
we are specifying that we want everything to run from this folder and from this folder we are actually executing this um executable and we can specify try different things but the test all will run all the tests. So I think that we can just exit here and from here we can do npm rune test all.
So yeah, now we have everything we need to run the tests um locally and uh what we can do is to now check some some code. Um I have some examples here prepared. So um
So, let me see if I can get some some examples here.
Um, I'm going to ask cursor to create some some examples here. Um, okay. Um, yes. Proceed. Okay.
Okay. You know what? We are going to do one thing. We're going to um WordPress.com blog. And we have here a an article that I recently wrote. And here's the
the code of the of the plug-in. So, I'm going to copy and paste this only from here. Okay. I Okay, here.
And if we go to my awesome plugin. Okay. So, this is the code of our plug-in now. And um and we also have this here style
CSS here. This Okay. So this article this uh I'm going to share this in the in the chat. explains how to so it explains how to set up a local development environment with the studio and then how to create a first plugin. Let me do a um like a quick overview of the plug-in.
Um so yeah it explains that in order to generate a plug-in we need to add a folder in the plugins folder then a PHP file with a specific header and then we can start adding hooks. Um we have uh filters and we have actions which are uh mentioned here. And this specific filter it takes the for
individual posts uh whenever we are um so it um excludes specific situations like whenever we are not in a post whenever we are not in the loop or whenever we are not in the main query. Okay, it just returns. And for the simple case of uh a post,
it takes the post content. It strips uh short codes. It strips all tags. So it just um takes the the raw content, the inner content. And for that it counts the the words that are in this uh content and then it does a estimation of the
speed of reading that text and this is calculated here. Um, and then it adds to the content an initial text that is this one that contains the like the the reading time. So this snippet adds a reading time batch to post content using the content filter. This is the content filter.
So every time there is content served, this filter is applied. It uh does some things and it returns the filtered content which is the original content plus some other stuff or the original content uh modify exactly. This is what we as we explained. Nice. Then it loads some specific assets. In order to do that, it
uses the um encq scripts action and then the enq style function. The style function, it loads the style CSS which is the file that we created here with some uh custom styles. And then as an optional thing, this article explains how to add some settings. In order to do that,
we have um an action that is triggered when the dashboard has um so it's h initialized during the setting. So what we can do is register a specific uh setting to store a specific value in the in the database. And what we can store is uh
yeah this value which is called QRT WPM. Okay, this code registers a plug-in option. This is some a value that is stored in the database. Then what we can do here is to register a a settings page. This is the page that we will see in the
dashboard to to customize this value. So then we add a settings page. This will add um a new option in the in the admin menu to access this uh form. And then whenever we have this QT render settings page which defines all the HTML that we will see in that page
and this will update uh we we have an option that we have registered uh here and the form the action is
then this code is using this um this value here. Okay. So we have the the form and then the options PHP it will get the value which is uh here with the idqrt.vpm.
So whenever we submit the this form this value will be updated and then we can modify our original filter which was uh defined here to use this value. So we need to get the option here. We provide a default value and we um convert it into an integer automatically
and then we get this value and we update the calculations using this new value. This is like the the speed. Okay, let's see if we go to if we go to cursor. Um,
yeah, my awesome plug-in with tests. We come here. We run this again to see the to the URLs. And if we come here, WP admin admin password.
Yeah. And here we have the quick reading time and for every post whenever we go to we have this with this styles. Okay, so this works and now what we can do is to add some tests to to this. So we don't have much time. So I'm going to ask um
I'm going to ask cursor to to add some tests and then we can execute them. So, um take the code and uh and add some tests under
Okay. And then what we can do is to check the PHP unit is something that we can upload. Is this is this ignored here? Yeah. I don't want this to be ignored. H okay. Because this was this. Yeah. This is why
uh okay so this is the one that we want to distribute but we can have uh some locals. So I'm going to um I'm going to remove this one and I'm going to leave this. And in theory, if we go to I'm going to terminate this,
we can run this tests. No test executed because this test sample is uh but if I do this Yeah. So these tests are working well.
So we need to update uh test bootstrap. Okay. Okay. So, in the meantime, let's review the PHP unit XML dist um where can we see
some more options of this?
Okay, so we have some tests. Yeah.
PHP. Okay.
Okay. So, yeah, I think this is a a good
battery of tests. Let's uh review them.
Mhm. Mhm. So, yeah, it's saying that the tests uh require access to the Um test the sample.
Okay. Let's see what it has done. So it has created these tests. Um this one. Okay. All of these we will check them in a minute. uh test bootstrap. There is no change uh to be done here, but because it already it already loads the the good plugin and um in PHP unit XML,
which one? This one. This is going to execute all the tests in the tests folder which follows this uh prefix. So that's fine. And then to run the tests we have already all these scripts. So um well test integration yeah that's fine.
I don't have right now specific tests with uh test suite. So that's fine. In package JSON scripts fix the double slash in to a single slash. Which one? Uh okay, I think we're fine. Good. So,
thank you very much. We're going to run the tests now by running MPN run tests all and let's see what happens. Okay. Now, what we can do is run a specific test. H in order to do that, let me check the
WPM documentation. Uh test single
Um Mhm. Okay. So, in order to launch a specific test,
how can I do this? Um, I think I can just pass the So, what we can do is um,
okay, let's ask for some help. How can I run a single test with PHP unit? Because we have these tests all here.
Now we can filter. Exactly. This is what I wanted. So if we do this uh and this and then test uh reading time for example
okay so let's check Mhm. >> Class A test reading time.
Not found in This is fine. We are creating tests that are extending this. And for example,
public function public function. Okay. So when we extend this class we have access to different utils for example to create a post content then we can
come here class test. Okay. Um Mhm.
I don't know where from where this um to execute this test use group external or fast.
Ah, we need to end it the the class name with test. Now running class test within time. Yeah. Okay. PHP unit
um PHP unit h classes should end with test.
Okay. So,
what we can do? Yeah, I think this is what I'm going to do. I'm going to add this um QT maybe we can just
and this here and within time I'm going to also and here and what I'm going to do is here instead of uh using this prefix I'm going to um suffix test PHP. Okay. So in theory we should just get
this test executed for now. So let's see um BM room test all. Okay. Okay. So now the test is failing. We are just launching one test.
What we can do? Um, how can I quickly disable um tests inside a test file? Yeah, because with justest there is like a
a character we can add to the to the function name. And what I want to do here is to just add a main function. Uh this thing group escape.
Uh so what we can do is this. We can do this and this and we can clear test.
Test three assertions. One skipped two. Okay. But incomplete skip or risky text. Okay. So now we are going to focus on this one. And let's see what happens. Okay. So fail asserting that original content. So this test is failing.
Um we are creating we are creating post content st repeat word. Mhm.
Okay. So, is this generating the same word post ID? So st repeat it generates. So this
creates a post. I understand that STL repeat the string to be repeated. Okay. So it creates a post with word 400 times. Simulate main query on single post and run within the loop. So it goes to this place uh and
query posts post ID and post type post content. We have posts the post content apply filters the content uh original content only need the first post.
Okay, I'm going to disable this one and I'm going to run this test. Okay. So, um
Okay. So, it runs the tests. Um, now I want to be there are some settings here that I'm pretty sure I can use to to make this more um
Yeah, more verbos. I think we can do this here. So whenever we launch the tests, we see more info. Um
okay. Um let me ask how can I see more details of the tests being executed. I want to see uh the ones that are uh failing and the ones that
are passing. There must be there must be a setting here.
and add this under the root.
So if we come here, okay, this passes, but this doesn't.
Okay. So it contains main grid.
Okay. So the content the so content here
apply filters the content uh original content. And so we have uh
this filter Um, okay. I don't understand this. So,
Mhm.
Okay. So exactly the plug-in injects a reading time batch into the content or a single post main query. Exactly. So greet a post uses WordPress factory. this factory which is available thanks to this class we are extending and we are creating a post with uh
400 words. So reading time will be more than zero then we simulate main query. Uh in order to do that we go to the perma link. So the global main query and the loop are set as if on the front end. We go to the perma link. So we get a change on the the loop and the main
query. Then we access the mhm this part I don't understand it.
So while it have posts the post and I only need the first post. Okay. So um let's ask directly why this is not working. I don't understand this part. This we are applying
Shouldn't this be original content? Is that a function that is defined? Well, any suggestions are welcome. We can pass. Uh yeah, I would like to leave this uh test uh clarified. We have created some more
to understand environment. Uh okay.
Um, okay. So, it says that is because of the plural, but I don't think this is the reason because this doesn't pass as well. Okay, let's try this.
Um, so this is not passing. Okay, in the meantime we can check that the we can do this test.
We can change this and okay ensure no assets are incubed yet. So we do action in Q scripts. So if we run this.
Okay. So this is passing Then the admin UI we can also um we can also add this extension. And then what we can do is
If I run this
Okay. So, one of the things we have when we extend this class is this setup which
um this is run every time before any of the test. So if we have several tests, this function will be executed every time.
Mhm. Okay. So, so yeah, some this I need to spend more time checking these tests. Um but um but well yeah the idea is that we can
the idea is that we can uh generate this code now with the help of AI is easier. Um sometimes it requires some more uh testing. Okay, let's see if this has fixed the the thing. Okay,
this is some locks that have been added and we can maybe um update option. Ensure default RBM is
set. Mhm. Okay. So um okay now these tests are passing this uh project check the content
where is this is this here find folder QT the back. Okay. C the back. True. Enable plug in the back logs during tests. Okay. Um because the test
add some um okay this has been added here. So I don't want this here. I don't want this here and I don't want this here. So now we can run the tests again. Okay, this is cleaner. And let's see if we can fix the other
ones so we can do a quick review of them. Um, what's happening here? Global sub menu. Um, okay. My settings page is not registered. Do action admin menu and as we have uh admin menu
uh admin menu it should execute this. Uh so global menu slions uh general receptions general
and QRT settings. This is menu um options menu like is options general. Okay, so it's not found. The settings page is registered. Um,
okay. Clear. If we run the tests, the the error we get is
so the error we get is um here the admin UI test the settings page is registered argument two of must be an array or array access. So uh okay so this must be an array and this is not an array. Um
echo Um,
Okay.
admin menu structure is not um admin sub menu structure is not displayed Yeah.
See
Ah here. Okay. So this is empty. So this is not properly accessing this uh so exactly this is null and that's why it's failing. So yeah this is what's uh happening here.
admin menu or admin in it. Uh, well, let me see.
menu. Admin menu. Yeah, it was the admin menu. Okay.
Okay. So in the tests we may need to ensure that the role of administrator is set.
Okay. Okay. I think it's close this throttle. Well, no. Okay. So, I think that we are
um we can end the session here. Um let's see if we can fix this. But yeah, the issue is that we are not accessing properly the sum menu with this um with this global variable.
Maybe it's because we need to set the current user as the administrator. Mhm. So I'm reading this. I'm noticing that the test had an initial error. We need array and set the admin user in set up
which should work. Okay. So in the setup H I don't think we need to create the role of administrator because it's already created. Um, so,
okay, I think I'm going to finish here the session. H, I'm going to upload this. H I'm going to leave these tests as they are and I will try to fix them later and maybe I will do a another video to to finish this test and to to add more explanations. So yeah. Um so yeah, I'm going to go back
here. Um so yeah this has been the session. I am no PHP pro developer but uh I have more experience doing tests in in JavaScript. So I yeah I wanted to explore this uh together. As you can see we can run uh tests with WPM and Docker and then we can um generate tests for the code. We can use the help of AI but then we
need to to finetune. Doing tests is an art and it requires some deep knowledge uh because we need to mock up uh to mock things and to to to really know how things uh go internally and how to emulate them. So, so it's an art as you can see it's not easy but uh once you have them it's really um it's really helpful to have them because you can refactor the code and
and as long as the test uh pass everything will be okay. So it's really a safety net. Um so yeah I will try to finish this test and to upload them to the streams repository and and yeah see you in the next uh session happy to cover any of the ideas you have and yeah hope you are doing well and that you are having a a great summer. So
so yeah see you next week. Okay, bye., Unit Tests in PHP With PHPUnit, JuanMa Garrido, 01k27n9xw32d0f8e5rag3mwvvr.