CakePHP PHPUnit Lamps Drop Database Table Every Time

I am trying to run a simple unit test in my model. The problem is that every time I run the test, my database table is discarded. I have public $ dropTables = false; Can anyone understand why the merchant_rejects table is still missing? As you will see, I have tried many different methods in my tool.

I think I will have to go through the code and find out when the table will be deleted.

Here is the code for my MovieStarFixture.php fixture:

class MovieStarFixture extends CakeTestFixture {
//  NEW TRY from http://stackoverflow.com/a/2548908/55124
var $name = 'MovieStar';
var $fields = array(
    'id'       => array(
                      'type'=>'string',
                      'null' => false,
                      'default' => NULL,
                      'length' => 36,
                      'key' => 'primary'),

    'movie_id' => array(
                      'type'=>'string',
                      'null' => false,
                      'default' => NULL,
                      'length' => 36),

    'trace' => array('type'=>'string', 'null' => false, 'default' => NULL),
    'star_date' => array(
                       'type'=>'datetime',
                       'null' => false,
                       'default' => NULL),
    'movie_star_type_id' => array(
                       'type'=>'string',
                       'null' => false,
                       'default' => NULL,
                       'length' => 36),
    'code' => array('type'=>'text', 'null' => false, 'default' => NULL),
    'amount' => array('type'=>'float', 'null' => false, 'default' => 0),
    'movie_star_recurrance_id' => array(
                       'type'=>'string',
                       'null' => false,
                       'default' => NULL, 
                       'length' => 36),
    'open' => array('type'=>'boolean', 'null' => false, 'default' => '1'),
    'loss_axia' => array('type'=>'float', 'null' => true, 'default' => 0),
    'loss_mgr1' => array('type'=>'float', 'null' => true, 'default' => 0),
    'loss_mgr2' => array('type'=>'float', 'null' => true, 'default' => 0),
    'loss_rep' => array('type'=>'float', 'null' => true, 'default' => 0)
    );
var $records = array(
                array(
                'id' => '52ab9259-0070-4583-8d6f-4ac6c0a81cd7',
                'movie_id' => '440b7d13-5618-4560-be1d-93c5a2900a5e',
                'trace' => '3331313133423',
                'star_date' => '2013-12-13',
                'movie_star_type_id' => '64f7c386-6725-4c62-83ac-ae309bec8b10',
                 'code' => 'C01',
                'amount' => '222.0000',
                'movie_star_recurrance_id' => '',
                'open' => true,
                'loss_axia' => '23.0000',
                'loss_mgr1' => '0',
                'loss_mgr2' => '0',
                'loss_rep' => '0'
));

     //  THESE ARE ALL OF THE OTHER METHODS I HAVE TRIED
// Loading Fixture Methods / / / / / / / / / / / / / / / / / / / / / / / /  

// #1 - Import model and records / / / / / / / / / / / / / / / / / / / / /  
//public $import = array('model' => 'MovieStar', 'records' => true);


// #2 - Use onlt table info - no model / / / / / / / / / / / / / / / / / /  
// public $import = array('table' => 'movie_stars', 'records' => true);


// #3 - Specify Model and Create Records - Binds Data to Database/ / / / / 
/*
    public $records = array(
                 array(
        'MovieStar' => array(
        'id' => '52ab917d-549c-493b-9ef5-54a1c0a81cd7',
        'movie_id' => '440b7d13-5618-4560-be1d-93c5a2900a5e',
        'trace' => '3331313133',
        'star_date' => '2013-12-13',
        'movie_star_type_id' => '64f7c386-6725-4c62-83ac-ae309bec8b10',
        'code' => 'C01',
        'amount' => '122.0000',
        'movie_star_recurrance_id' => '',
        'open' => true,
        'loss_axia' => null,
        'loss_mgr1' => null,
        'loss_mgr2' => null,
        'loss_rep' => null
        )
    )
    );
public $import = array('model' => 'MovieStar', 'records' => false);
    */  

// #4 - Specify Model and Create Records in Init / / / / / / / / / / / / / 
// public $import = 'MovieStar';

/* public function init() {
        $records = array(
        array(
            'MovieStar' => array(
            'id' => '52ab917d-549c-493b-9ef5-54a1c0a81cd7',
            'movie_id' => '440b7d13-5618-4560-be1d-93c5a2900a5e',
            'trace' => '3331313133',
            'star_date' => '2013-12-13',
            'movie_star_type_id' => '64f7c386-6725-4c62-83ac-ae309bec8b10',
            'code' => 'C01',
            'amount' => '122.0000',
            'movie_star_recurrance_id' => '523525',
            'open' => true,
            'loss_axia' => null,
            'loss_mgr1' => null,
            'loss_mgr2' => null,
            'loss_rep' => null
              )
        )
        );
        parent::init();
}*/

// #5 - Try Model Setup / / / / / / / / / / / / / / / / / / / / / / / / 
// This drops all records after the first test
/*public $records = array(
     array(
        'MovieStar' => array(
        'id' => '52ab917d-549c-493b-9ef5-54a1c0a81cd7',
        'movie_id' => '440b7d13-5618-4560-be1d-93c5a2900a5e',
        'trace' => '3331313133',
        'star_date' => '2013-12-13',
        'movie_star_type_id' => '64f7c386-6725-4c62-83ac-ae309bec8b10',
        'code' => 'C01',
        'amount' => '122.0000',
        'movie_star_recurrance_id' => '',
        'open' => true,
        'loss_axia' => null,
        'loss_mgr1' => null,
        'loss_mgr2' => null,
        'loss_rep' => null
    )
       )
    );

   }*/

This is my MovieStarTest.php:

<?php
App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('MovieStar', 'Model');

/**
 * MovieStar Test Case
 *
 */
class MovieStarTest extends CakeTestCase {

/**
 * Fixtures
 *
 * @var array
 */
public $fixtures = array(
    'app.movie_star'//,
    //'app.movie_star_recurrance',
    //'app.movie_star_type',
    //'app.movie',
    //'app.user',
    //'app.movie_star_line',
    //'app.movie_star_status'
);

public $autoFixtures = false;
public $dropTables = false; 

/**
 * setUp method
 *
 * @return void
 */
public function setUp() {
    parent::setUp();

    $this->MovieStar =& ClassRegistry::init('MovieStar');
    $this->MovieStar->useDbConfig = 'test';

    //$this->MovieStar->query("SELECT truncate_tables('axia')");

    // load data
    $this->loadFixtures('MovieStar');
}

/**
 * tearDown method
 *
 * @return void
 */
public function testFixtures() {
    $numberOfResults = $this->MovieStar->find('count');
    debug($numberOfResults);
    $resultGreaterThanMinimumValue = $numberOfResults > 2;
    $this->assertTrue($resultGreaterThanMinimumValue);
}

public function testFixtures2() {
    $numberOfResults = $this->MovieStar->find('count');
    debug('$numberOfResults');
    debug($numberOfResults);
    $resultIsZero = $numberOfResults == 0;
    $this->assertTrue($resultIsZero);
}

public function testFindStarsByMovieId() {
    $movieId = '440b7d13-5618-4560-be1d-93c5a2900a5e';
    $result = $this->MovieStar->findStarsByMovieId($movieId);
    $expected = array(
        array(
'MovieStar' => array(
    'id' => '52ab9259-0070-4583-8d6f-4ac6c0a81cd7',
    'movie_id' => '440b7d13-5618-4560-be1d-93c5a2900a5e',
    'trace' => '3331313133423',
    'star_date' => '2013-12-13',
    'movie_star_type_id' => '64f7c386-6725-4c62-83ac-ae309bec8b10',
    'code' => 'C01',
    'amount' => '222.0000',
    'movie_star_recurrance_id' => '',
    'open' => true,
    'loss_axia' => '23.0000',
    'loss_mgr1' => null,
    'loss_mgr2' => null,
    'loss_rep' => null
        )
    )
    );

    debug("Expected");
    debug($expected);
    debug("Result");
    debug($result);

    $this->assertEquals($expected, $result);
}

public function tearDown() {
    //$this->MovieStar->deleteAll(true, true);
    //unset($this->MovieStar);

    parent::tearDown();
}

}

I should be able to not drop tables by adding

 public $dropTables = false; 

But I don't see dropTables being checked before this method in lib / Cake / TestSuite / Fixture / CakeFixtureManager.php enter image description here

In fact, this would clip the entire db. Where did my table fall?

+4
4

CakePHP .

, :

  • Fixture.
  • Fixture.
  • .

no. 2, CakePHP ( ) .

public $import = array('model' => 'YourModel', 'records' => false, 'connection' => 'default');

" ", CakePHP , . , , . , .

+3

:

public $dropTables = false; - , merchant_rejects ?

cakephp:

$dropTables

/ .

false, , . . - .

:

-

, public $dropTables = false; , , - . .

, ...

:

class MovieStarFixture extends CakeTestFixture {

    public $import = 'MovieStar';

    //data will be loaded into this fixture by the test cases

    // do not truncate movie_stars table between tests
    public function truncate($db){

        return null;
    }

    // do not drop movie_stars table between tests
    public function drop($db){

        return null;
    }
}

. , "", . , , , ( ), ...

:

MovieStarTest public $dropTables = false;. , . , . , .

CakeTestCase:: test *().

+8

, lib/Cake/TestSuite/Fixture/CakeFixtureManager.php shutdown(), . , $dropTables . enter image description here

+1

Xavier , , , dropTables = false :

class MovieStarFixture extends CakeTestFixture
{

  public $import = ['model' => 'MovieStar', 'connection' => 'default', 'records' => true];

  private $created_flag = false;

  public function create($db)
  {
    $success = null;
    if (!$this->created_flag) {
      $success = parent::create($db);
    }
    return $success;
  }

  public function truncate($db)
  {
    $success = null;
    if (!$this->created_flag) {
      $success = parent::truncate($db);
    }
    return $success;
  }

  public function insert($db)
  {
    $success = null;
    if (!$this->created_flag) {
      $success = parent::insert($db);
      $this->created_flag = true;
    }
    return $success;
  }

  public function drop($db)
  {
    $success = null;
    if (!$this->created_flag) {
      $success = parent::drop($db);
    }
  }
}

, , , , .

+1

All Articles