Welcome to the Cookbook

loading...

4.7.3.3 Importing table information and records

Your application may have already working models with real data associated to them, and you might decide to test your model with that data. It would be then a duplicate effort to have to define the table definition and/or records on your fixtures. Fortunately, there's a way for you to define that table definition and/or records for a particular fixture come from an existing model or an existing table.

Let's start with an example. Assuming you have a model named Article available in your application (that maps to a table named articles), change the example fixture given in the previous section (app/tests/fixtures/article_fixture.php) to:

 <?php  
   class ArticleFixture extends CakeTestFixture { 
          var $name = 'Article'; 
          var $import = 'Article'; 
   } 
   ?> 
 
  1. <?php
  2. class ArticleFixture extends CakeTestFixture {
  3. var $name = 'Article';
  4. var $import = 'Article';
  5. }
  6. ?>

This statement tells the test suite to import your table definition from the table linked to the model called Article. You can use any model available in your application. The statement above does not import records, you can do so by changing it to:

<?php   
class ArticleFixture extends CakeTestFixture {
    var $name = 'Article';
    var $import = array('model' => 'Article', 'records' => true);  
}
?> 
  1. <?php
  2. class ArticleFixture extends CakeTestFixture {
  3. var $name = 'Article';
  4. var $import = array('model' => 'Article', 'records' => true);
  5. }
  6. ?>

If on the other hand you have a table created but no model available for it, you can specify that your import will take place by reading that table information instead. For example:

 <?php  
   class ArticleFixture extends CakeTestFixture { 
          var $name = 'Article'; 
          var $import = array('table' => 'articles'); 
   } 
 ?> 
  1. <?php
  2. class ArticleFixture extends CakeTestFixture {
  3. var $name = 'Article';
  4. var $import = array('table' => 'articles');
  5. }
  6. ?>

Will import table definition from a table called 'articles' using your CakePHP database connection named 'default'. If you want to change the connection to use just do:

 <?php  
   class ArticleFixture extends CakeTestFixture { 
   var $name = 'Article'; 
   var $import = array('table' => 'articles', 'connection' => 'other'); 
   } 
   ?> 
  1. <?php
  2. class ArticleFixture extends CakeTestFixture {
  3. var $name = 'Article';
  4. var $import = array('table' => 'articles', 'connection' => 'other');
  5. }
  6. ?>

Since it uses your CakePHP database connection, if there's any table prefix declared it will be automatically used when fetching table information. The two snippets above do not import records from the table. To force the fixture to also import its records, change it to:

 <?php  
   class ArticleFixture extends CakeTestFixture { 
          var $name = 'Article'; 
          var $import = array('table' => 'articles', 'records' => true); 
   } 
 ?> 
  1. <?php
  2. class ArticleFixture extends CakeTestFixture {
  3. var $name = 'Article';
  4. var $import = array('table' => 'articles', 'records' => true);
  5. }
  6. ?>

You can naturally import your table definition from an existing model/table, but have your records defined directly on the fixture as it was shown on previous section. For example:

 <?php  
   class ArticleFixture extends CakeTestFixture { 
          var $name = 'Article'; 
          var $import = 'Article'; 
           
          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 $import = 'Article';
  5. var $records = array(
  6. 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'),
  7. 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'),
  8. 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')
  9. );
  10. }
  11. ?>