FixturesShell for CakePHP

The FixturesShell for CakePHP is a neat console application allowing you to populate and reset your applications database with all existing fixtures stored in /app/test/fixtures.

Download

You can download the source code of the FixturesShell for CakePHP here: Download 1.0 (Latest)

Installation

Place the file from above in /vendors/shell/fixtures.php. That's it - you are done : ).

Usage

Pick any table in your database and create a fixture for it. An example for a UserFixture placed inside /app/tests/fixtures/user_fixture.php can be seen below.

php
  1. class UserFixture extends CakeTestFixture {
  2.   var $name = 'User';
  3.   var $records = array(
  4.     array(
  5.       'id' => '4810a53f-5c28-4775-8ad9-a5cb4dddecbf',
  6.       'email' => 'felix@debuggable.com',
  7.       'password' => '3984890d90s9dasad9as',
  8.       'level' => 'admin',
  9.     ),
  10.     array(
  11.       'id' => '4810a53f-ba4c-49b9-b2e8-a5cb4dddecbf',
  12.       'email' => 'tim@debuggable.com',
  13.       'password' => '3984890d90s9dasad9ae',
  14.       'level' => 'admin',
  15.     ),
  16.   );
  17. }

To dump all tables that you have fixtures for and re-populate them with the values inside your fixtures run this from within /cake/console:

$ ./cake fixtures

And you should get some output like this:

Welcome to CakePHP v1.2.0.6311 beta Console
---------------------------------------------------------------
App : app
Path: /Users/felix/dev/www/php5/scorpion/app
---------------------------------------------------------------
-> Truncating table "users"
-> Inserting records for "users"
-> Done inserting 3 records for 1 table

The shell will pick up any fixture named *_fixture.php inside /app/tests/fixtures/ automatically.

If you are working with UUIDs instead of default auto-incrementing ones, then this might also be useful to you:

$ ./cake fixtures uuid

Which returns 10 UUIDs to use in your fixtures:

Welcome to CakePHP v1.2.0.6311 beta Console
---------------------------------------------------------------
App : app
Path: /Users/felix/dev/www/php5/scorpion/app
---------------------------------------------------------------
481c596e-e820-4bdb-b735-51714dddecbf
481c596e-0f94-4483-99b4-51714dddecbf
481c596e-2254-4268-947d-51714dddecbf
481c596e-3514-4f47-af37-51714dddecbf
481c596e-4770-4aea-9098-51714dddecbf
481c596e-59cc-488d-a298-51714dddecbf
481c596e-6d54-41e8-aed7-51714dddecbf
481c596e-7f4c-4798-a132-51714dddecbf
481c596e-91a8-4157-9ee6-51714dddecbf
481c596e-a4cc-4968-921f-51714dddecbf

Support & Feedback

If you have any questions or feedback regarding the FixturesShell for CakePHP, please leave a comment on our blog post: FixturesShell - Share your test data

License

The FixturesShell is licensed under the MIT license. This means you can do just about anything with it other than removing the license information. Software projects that are using this code do not need to be open source or even mention the usage of the code published here.

Unless stated otherwise, the entire code was written by Debuggable Limited.