Upgrading Yii2 to use Codeception 4.1.6+
Codeception is a PHP testing framework that hooks nicely into Yii2. I've been using Codeception in eVitabu for a while but hadn't updated to the latest version. This blog post describes how you can do that with Composer.
Codeception version 4 was announced in December 2019. I was still using version 2 so was two major releases behind.
Why test anyway?
Automated testing is incredibly useful for identifying if the code changes you've just made have broken something. Tests can also confirm if functionality you think you've built in to your application is actually present. I became convinced that automated testing was valuable when I ran a set of tests against eVitabu, only to find anonymous access was possible to some parts of the system. Clearly that wasn't desired!
I've only worked with Codeception, so can't compare it to other testing frameworks, but I find it nice to work with and the output is very clear. The screenshot above shows the results of my tests in an easy to understand fashion. If a test fails then I can look at the HTML output from the point of failure to help me identify the problem.
Update other components first
Before going any further I'd recommend running composer update
to update other packages used by your project. That way our steps later on will not fold in updates you're not expecting.
Warnings from Composer
During package updates with Composer I'd seen warnings about the Codeception package I was using for a while:
Package codeception/base is abandoned, you should avoid using it. No replacement was suggested.
This was a useful nudge to get things sorted.
Removing the old version
The first thing to do was to remove the old version of Codeception, in my case version 2.something. I'm using Composer to manage my packages so removed codeception/base
via the package manager:
Add Codeception version 4
Fortunately there's a Yii2 module for Codeception and installing it pulls in any dependencies required. Again we use Composer to perform the changes, although I started by editing the require
section in my composer.json
to require the new Codeception module codeception/module-yii2
:
You'll note that I've added codeception/module-yii2
as the last package in my require
section. Next we use Composer to install the changes:
$ cd /path/to/project
$ composer update
(Note this will pull in additional package updates, so you should run an update before starting your Codeception upgrade.)
Post installation upgrade steps
There's a handy upgrade script to migrate you to version 4 and you need to run this after Composer has installed the required modules. The upgrade script will add required changes to composer.json
for you, just run vendor/bin/codecept init upgrade4
and follow the prompts.
$ vendor/bin/codecept init upgrade4
Welcome to Codeception v4 Upgrade wizard!
Codeception is maintained since 2011, is free & open-source.
To make it better we need your feedback on it!
Please take a minute and fill in a brief survey:
http://bit.ly/codecept-survey
? Did you fill in the survey? (y/n) n
Anyway...
Adding codeception/module-filesystem for Filesystem to composer.json
Adding codeception/module-asserts for Asserts to composer.json
2 new packages added to require
? composer.json updated. Do you want to run "composer update"? (y/n) y
Troubleshooting composer update
post upgrade
When I ran composer update
it failed due to composer.json
not meeting the correct schema (rules). In my case it appears init upgrade4
changed the value of support
to be an array:
[Composer\Json\JsonValidationException]
"./composer.json" does not match the expected JSON schema:
- support : Array value found, but an object is required
To fix, check the component being referenced (support
in this case) and make the relevant change. For me this meant changing
"support": [],
back to
"support":{
},
After that I could run composer update
without issues.
Changes to your tests
Dependant on what you're using in your tests you may need to make changes. I leave that as an exercise for the reader.
Banner image: the Codeception logo