Welcome to the Cookbook

loading...
Please login to continue

4.7.3.2 Creating fixtures

When creating a fixture you will mainly define two things: how the table is created (which fields are part of the table), and which records will be initially populated to the test table. Let's then create our first fixture, that will be used to test our own Article model. Create a file named article_fixture.php in your app/tests/fixtures directory, with the following content:

If you are testing a plugin, see the section Testing Plugins.

<?php  
 class ArticleFixture extends CakeTestFixture { 
      var $name = 'Article'; 
       
      var $fields = array( 
          'id' => array('type' => 'integer', 'key' => 'primary'), 
          'title' => array('type' => 'string', 'length' => 255, 'null' => false), 
          'body' => 'text', 
          'published' => array('type' => 'integer', 'default' => '0', 'null' => false), 
          'created' => 'datetime', 
          'updated' => 'datetime' 
      ); 
      var $records = array( 
          array ('id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => '1', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'), 
          array ('id' => 2, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => '1', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'), 
          array ('id' => 3, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => '1', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31') 
      ); 
 } 
 ?> 
  1. <?php
  2. class ArticleFixture extends CakeTestFixture {
  3. var $name = 'Article';
  4. var $fields = array(
  5. 'id' => array('type' => 'integer', 'key' => 'primary'),
  6. 'title' => array('type' => 'string', 'length' => 255, 'null' => false),
  7. 'body' => 'text',
  8. 'published' => array('type' => 'integer', 'default' => '0', 'null' => false),
  9. 'created' => 'datetime',
  10. 'updated' => 'datetime'
  11. );
  12. var $records = array(
  13. array ('id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => '1', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  14. array ('id' => 2, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => '1', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  15. array ('id' => 3, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => '1', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31')
  16. );
  17. }
  18. ?>
The $name variable is extremely significant. If you omit it, cake will use the wrong table names when it sets up your test database, and you'll get strange errors that are difficult to debug. If you use PHP 5.2, you might be used to writing model classes without $name, but you must remember to include it in your fixture files. You can also specify the table name to be created by including a $table variable in the fixture.

We use $fields to specify which fields will be part of this table, on how they are defined. The format used to define these fields is the same used in the function generateColumnSchema() defined on Cake's database engine classes (for example, on file dbo_mysql.php.) Let's see the available attributes a field can take and their meaning:

type
CakePHP internal data type. Currently supported: string (maps to VARCHAR), text (maps to TEXT), integer (maps to INT), float (maps to FLOAT), datetime (maps to DATETIME), timestamp (maps to TIMESTAMP), time (maps to TIME), date (maps to DATE), and binary (maps to BLOB)
key
set to primary to make the field AUTO_INCREMENT, and a PRIMARY KEY for the table.
length
set to the specific length the field should take.
null
set to either true (to allow NULLs) or false (to disallow NULLs)
default
default value the field takes.

We lastly can set a set of records that will be populated after the test table is created. The format is fairly straight forward and needs little further explanation. Just keep in mind that each record in the $records array must have a key for every field specified in the $fields array. If a field for a particular record needs to have a NULL value, just specify the value of that key as NULL.