In the previous article in this series, we looked at unit, integration, functional, and acceptance testing. In this and the following ones, we'll see how to begin installing and configuring the tools you'll need to perform the tests: Composer, PhpUnit, and Codeception. This article focuses on Composer.
In many cases installing and configuring these tools is actually more difficult than developing and running the tests.
When the tools are not configured correctly, it's sometimes difficult to figure out why things aren't working. It could be because of an error in a configuration file, or because some file or files are not in the expected location. The error messages you see (if any) won't always be helpful.
I've done my best to break down the installation and configuration process for you, but because of the differences in platforms and your personal choices, it's difficult to cover all the possibilities.
If you already have Composer and are familiar with it, you can skip this article and the next one. We're going to use Composer to install the other packages we need for our tests, and since more and more packages can be installed with Composer, it's a good idea to have it installed. In fact, future versions of MODX will almost certainly be installable with Composer.
Composer is a tool for managing dependencies for PHP. Think of it like MODX's Package Manager, but with a wider variety of packages and more of a command-line flavor (though GUIs for Composer are starting to appear). Composer will not only install a package, it will also install other packages that the package depends on.
In the bad old days, if you wanted to install a piece of software like PHP, PhpUnit, PhpDoc, etc., you'd download the appropriate package for your environment and configure it. If that package depended on other packages, you'd have to do the same for them. And if those other packages required other packages you'd have to get them too. Lather, rinse, repeat, until all the required packages were in place.
If an update was released for one of the packages, you'd have to repeat the process for that package and all of the packages it depended on (if you could remember which ones they were). Often, updating one package would break your project because the updates were incompatible with some other package.
Composer solves all these problems in a very elegant way. Composer is a "dependency manager" for PHP. It consults a text file called
composer.json, which contains a list of all the packages required for the project and the other packages they depend on (optionally with version information). You can run
composer update at any time and all necessary packages will be updated. By default, composer puts all packages in the
/vendor directory. It's a good idea *not* to change that, although you can specify the location of the
vendor directory in the
composer.json file (by default, it goes in the same directory as the
As a bonus, Composer generates an
autoload.php file at the root of the
vendor directory. For any code you write that uses the packages in the
vendor directory all you need to "include" is the
autoload.php file. You can then instantiate classes and call methods at will because PHP will load any necessary code automatically.
It's unlikely that you'd ever need to know this, but the
autoload.php file in the vendor directory is not the real autoloader, though including it is all you have to do. The
autoload.php file simply "includes" the real autoloader (appropriately named
autoload_real.php) in the
Composer also works nicely with Git to let you specify tags or branches of a Git repository (e.g., GitHub or BitBucket) to install.
In the next article in this series, we'll see how to install Composer.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)