Simple CakePHP 2.x App Demoing Unit Testing in CakePHP
Ruby Puppet PHP Shell Perl Erlang Other
Latest commit 342db1a Nov 13, 2014 @justinyost Merge pull request #25 from jtyost2/f/phpunit-speeds
F/phpunit speeds
Failed to load latest commit information.
Config Exclude the config directory from tests Nov 13, 2014
Console Fixed auto test Jul 23, 2014
Controller Add Tests for an External Lib Nov 7, 2014
Lib Updated AppModel and added an AppEmail Class Nov 7, 2014
Locale/eng/LC_MESSAGES Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
Model Updated AppModel and added an AppEmail Class Nov 7, 2014
Plugin Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
Test Updated AppModel and added an AppEmail Class Nov 7, 2014
View Using Admin Helper in the App Jul 23, 2014
tmp Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
webroot Corrects Paths to Cake Libraries Jul 23, 2014
.editorconfig Adds an editorconfig file Aug 24, 2014
.gitignore Ignore ssh files Jul 9, 2014
.gitmodules Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
.htaccess Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
.travis.yml Updated Travis.yml file Nov 7, 2014
Gruntfile.js Fixed auto test Jul 23, 2014
LICENSE.txt Adds MIT Software License Jul 30, 2014 Added Code Climate Badge Nov 7, 2014
Vagrantfile Updated Vagrant File to Correct Ports If Needed Jul 30, 2014 Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
composer.json Whitespace issue in the Composer.json Nov 13, 2014
composer.lock Updated Composer json and lock file Nov 13, 2014
index.php Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014
package.json Initial Commit of CakePHP Unit Testing Repo Jul 8, 2014

CakePHP Unit Testing

Build Status

Code Climate

Sample Unit Testing Application


These items should be installed and available before cloning the project repo.

  • CakePHP v2.4.5+
  • PHP v5.4+
    • PDO + MySQL
  • MySQL v5+


Noted that all development (and production) dependencies are already available inside the vagrant VM (as provisioned by puhphet). There are no "optional" installs. Developers must be able to run tests, generate phpDocs and run phpcs locally before committing.

  • vagrant (If you have this, you can ignore the rest of this since it is all in the VM.)
  • xdebug 2+
  • phpunit 3.7+
  • nodejs + npm (for auto-running tests)
  • phpDocumentor
  • PHP Code Sniffer

Included Libaries and Submodules

Libraries should be included with Composer whenever possible. Git submodules should be used as a fallback, and directly bundling the code into the project repo as a last resort.


No installation steps as this should never be deployed as a real application.

Development (vagrant)

Developers are expected to use the vagrant environment for all local work. Using a _AMP stack on your machine directly is no longer advised or supported.

git clone ./
vagrant up
vagrant ssh
cd /var/www

The bootstrap file takes care of installing dependencies. After this process, the project should be available at http://localhost:8080/.

Writeable Directories

Writeable directories are managed by Config/writedirs.txt, and they can be set by running bin/writedirs.


This is a sample app for demonstrating Unit Testing Principles. Anything done should improve the basics of testing a CakePHP application.

After Pulling

Things to do after pulling updates from the remote repo.

On your host:

  • bin/deps-install (Install any changes/updated dependencies from git submodules, composer, pear, npm, etc.)
  • vagrant provision (Make any changes to the VM's config that may be necessary.)

From inside the vagrant VM (via vagrant ssh):

  • bin/clear-cache (Make sure temp files are reset between host/vm use.)
  • bin/migrations (Set up the DB with the latest schema.)

@TODO: These final steps could really be rolled into the vagrant provisioning step.


App configuration is stored in Config/core.php. This configuration is then added to (or overwritten by) anything defined in the environment-specific config file, such as Config/core-vagrant.php or Config/core-staging.php.

Database configurations for all environments is stored in Config/database.php and switched using an environment variable.

The bundled vagrant VM automatically sets APP_ENV=vagrant both on the command line (via vagrant ssh and in the Apache context.) If you want to work with the project on your machine locally, you need to export APP_ENV=dev (or whatever environment you want to match for core-*.php and in database.php) before running bin/cake.

Database Changes

Because the MySQL DB runs inside of the vagrant VM, you must connect to it via SSH. The easiest way to do this is using Sequel Pro.

Create a new "SSH" connection with the following settings:

  • Name: vagrant@vagrant
  • MySQL Host: (This is the MySQL server's address after you've SSHed into the vagrant box.)
  • Username: vagrant
  • Password: vagrant (as defined in Lib/puphpet/config.yaml.)
  • Database: vagrant (again per Lib/puphpet/config.yaml.)
  • Port: 3306
  • SSH Host:
  • SSH User: vagrant
  • SSH Password: vagrant (Or some guys online say you can point to your local ~/.vagrant.d/insecureprivatekey.)
  • SSH Port: 2222 (per Lib/puphpet/config.yaml.)

This setup is handy for backing up your data if you're about to destroy the box, or for making Schema or Seed changes before running the Shell commands in the VM.

Schema Migrations

  • The database schema is maintained using the CakeDC Mgrations plugin.
  • Once you have made changes to your development database using the process above, run bin/cake Migrations.migration generate -f from inside the vagrant box (via vagrant ssh).
  • When prompted to update schema.php, choose yes and then choose overwrite.
  • Then review and commit the changes to Config/schema.php and the new file from Config/Migration/.


Unit tests should be created for all new code written in the following categories:

  • Model methods
  • Behaviors
  • Controller actions
  • AppController methods
  • Components
  • Helper methods
  • Shells and Tasks
  • Libraries in `Lib/'

Testing can be done through the browser like normal (by visiting http://localhost:8080/test.php).

Also there is a script that runs the tests

vagrant ssh
cd /var/www

Command line automated test running is also possible with Grunt, which is already installed in the vagrant box.

vagrant ssh
cd /var/www
grunt watch

This will block the terminal while it waits for file changes. New files should get picked up as well.


Copyright (c) 2014 Justin Yost