Tue 2 Dec 2008
The CakePHP bake utility can create unit tests and fixtures that can be used for testing your web app. Although these templates work fine, they are a bit difficult to maintain, especially as your application grows. I spent some time tweaking the test case and fixtures to make the process easier. All the sample templates are available at GitHub.
Fixture Changes
The first thing you’ll notice is that the schema is no longer explicitly defined in the fixture. Instead it will be pulled from the original table, which means you won’t have to worry about updating the fixture if you alter the table.
The second change is the addition of the $import line. This tells the fixture which table it should use as the base from the original database. Although it is possible to specify a model, which would then be used to figure out the table name, I found it was best to point directly to the table. If you original table isn’t in the default database, you can add a “connection” option to $import for which datasource should be used.
Test Case Changes
The model test case has one important change. To create an instance of the model being testing the ClassRegistry::init method is used. By doing it this way the model instance and any associated models will automatically be set to use the test database. This means you no longer have to extend the original model to override any particular option.
Notes
The setup described above assumes the test database is completely empty to start - no data and no tables. Also, because the original models are used, the fixture for any related model must also be included. This can be kind of a pain for larger databases. I don’t have a great alternative to this at the moment.
Popularity: 6% [?]










Follow!
Email!
December 18th, 2008 at 12:28 pm
You’ve been able to get all this to actually work? 0.o
December 18th, 2008 at 2:03 pm
Hey Chuck,
Yep. What’s the problem your’re seeing?