2008-09-01 14:21:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* CakeTestFixture file
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4667
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-09-01 14:21:57 +00:00
|
|
|
*/
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('DboSource', 'Model/Datasource');
|
2010-12-11 05:47:55 +00:00
|
|
|
App::uses('Model', 'Model');
|
2011-01-31 04:11:59 +00:00
|
|
|
App::uses('CakeTestFixture', 'TestSuite/Fixture');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* CakeTestFixtureTestFixture class
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2008-09-01 14:21:57 +00:00
|
|
|
*/
|
|
|
|
class CakeTestFixtureTestFixture extends CakeTestFixture {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
2011-10-10 21:18:48 +00:00
|
|
|
* Name property
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'FixtureTest';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
2011-10-10 21:18:48 +00:00
|
|
|
* Table property
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $table = 'fixture_tests';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Fields array
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fields = array(
|
2012-11-21 14:39:03 +00:00
|
|
|
'id' => array('type' => 'integer', 'key' => 'primary'),
|
2009-04-28 22:20:04 +00:00
|
|
|
'name' => array('type' => 'string', 'length' => '255'),
|
|
|
|
'created' => array('type' => 'datetime')
|
2008-09-01 14:21:57 +00:00
|
|
|
);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Records property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $records = array(
|
2009-08-02 06:47:28 +00:00
|
|
|
array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'),
|
|
|
|
array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'),
|
|
|
|
array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00')
|
2008-09-01 14:21:57 +00:00
|
|
|
);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-04-08 17:50:36 +00:00
|
|
|
/**
|
2014-01-06 20:07:00 +00:00
|
|
|
* StringTestFixture class
|
2011-04-08 17:50:36 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2011-04-08 17:50:36 +00:00
|
|
|
*/
|
|
|
|
class StringsTestFixture extends CakeTestFixture {
|
|
|
|
|
|
|
|
/**
|
2011-10-10 21:18:48 +00:00
|
|
|
* Name property
|
2011-04-08 17:50:36 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-10-10 21:18:48 +00:00
|
|
|
public $name = 'Strings';
|
2011-04-08 17:50:36 +00:00
|
|
|
|
|
|
|
/**
|
2011-10-10 21:18:48 +00:00
|
|
|
* Table property
|
2011-04-08 17:50:36 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-10-10 21:18:48 +00:00
|
|
|
public $table = 'strings';
|
2011-04-08 17:50:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fields array
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-10-10 21:18:48 +00:00
|
|
|
public $fields = array(
|
2012-11-21 14:39:03 +00:00
|
|
|
'id' => array('type' => 'integer', 'key' => 'primary'),
|
2011-04-08 17:50:36 +00:00
|
|
|
'name' => array('type' => 'string', 'length' => '255'),
|
|
|
|
'email' => array('type' => 'string', 'length' => '255'),
|
2011-10-10 21:18:48 +00:00
|
|
|
'age' => array('type' => 'integer', 'default' => 10)
|
2011-04-08 17:50:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Records property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-10-10 21:18:48 +00:00
|
|
|
public $records = array(
|
2011-05-04 05:40:23 +00:00
|
|
|
array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
|
2011-10-10 21:18:48 +00:00
|
|
|
array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
|
|
|
|
array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
|
2011-04-08 17:50:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:07:00 +00:00
|
|
|
/**
|
|
|
|
* InvalidTestFixture class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.TestSuite
|
|
|
|
*/
|
|
|
|
class InvalidTestFixture extends CakeTestFixture {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name = 'Invalid';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Table property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $table = 'invalid';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fields array - missing "email" row
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $fields = array(
|
|
|
|
'id' => array('type' => 'integer', 'key' => 'primary'),
|
|
|
|
'name' => array('type' => 'string', 'length' => '255'),
|
|
|
|
'age' => array('type' => 'integer', 'default' => 10)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Records property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $records = array(
|
|
|
|
array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
|
|
|
|
array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
|
|
|
|
array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* CakeTestFixtureImportFixture class
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2008-09-01 14:21:57 +00:00
|
|
|
*/
|
|
|
|
class CakeTestFixtureImportFixture extends CakeTestFixture {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Name property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'ImportFixture';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Import property
|
|
|
|
*
|
2008-11-12 17:00:00 +00:00
|
|
|
* @var mixed
|
2008-11-08 02:58:37 +00:00
|
|
|
*/
|
2010-09-26 02:23:48 +00:00
|
|
|
public $import = array('table' => 'fixture_tests', 'connection' => 'fixture_test_suite');
|
2008-09-01 14:21:57 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-12 17:00:00 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* CakeTestFixtureDefaultImportFixture class
|
2008-11-12 17:00:00 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2008-11-12 17:00:00 +00:00
|
|
|
*/
|
|
|
|
class CakeTestFixtureDefaultImportFixture extends CakeTestFixture {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-12 17:00:00 +00:00
|
|
|
/**
|
|
|
|
* Name property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'ImportFixture';
|
2008-11-12 17:00:00 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* FixtureImportTestModel class
|
2008-09-01 14:21:57 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2008-09-01 14:21:57 +00:00
|
|
|
class FixtureImportTestModel extends Model {
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'FixtureImport';
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = 'fixture_tests';
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-09-20 02:58:30 +00:00
|
|
|
public $useDbConfig = 'test';
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
}
|
2010-06-09 03:30:57 +00:00
|
|
|
|
|
|
|
class FixturePrefixTest extends Model {
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-06-26 16:29:20 +00:00
|
|
|
public $name = 'FixturePrefix';
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-06-26 16:29:20 +00:00
|
|
|
public $useTable = '_tests';
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-06-26 16:29:20 +00:00
|
|
|
public $tablePrefix = 'fixture';
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-09-20 02:58:30 +00:00
|
|
|
public $useDbConfig = 'test';
|
2010-06-09 03:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Test case for CakeTestFixture
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2008-09-01 14:21:57 +00:00
|
|
|
*/
|
|
|
|
class CakeTestFixtureTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* setUp method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2013-07-28 23:22:52 +00:00
|
|
|
parent::setUp();
|
2011-09-03 21:07:45 +00:00
|
|
|
$methods = array_diff(get_class_methods('DboSource'), array('enabled'));
|
|
|
|
|
|
|
|
$this->criticDb = $this->getMock('DboSource', $methods);
|
2008-09-01 14:21:57 +00:00
|
|
|
$this->criticDb->fullDebug = true;
|
2010-09-26 02:23:48 +00:00
|
|
|
$this->db = ConnectionManager::getDataSource('test');
|
|
|
|
$this->_backupConfig = $this->db->config;
|
2008-09-01 14:21:57 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* tearDown
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2013-07-28 23:22:52 +00:00
|
|
|
parent::tearDown();
|
2009-03-18 17:55:58 +00:00
|
|
|
unset($this->criticDb);
|
2010-09-26 02:23:48 +00:00
|
|
|
$this->db->config = $this->_backupConfig;
|
2009-03-18 17:55:58 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* testInit
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInit() {
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
2008-09-01 14:21:57 +00:00
|
|
|
unset($Fixture->table);
|
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('fixture_tests', $Fixture->table);
|
|
|
|
$this->assertEquals('id', $Fixture->primaryKey);
|
2008-09-04 01:49:29 +00:00
|
|
|
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
2008-09-01 14:21:57 +00:00
|
|
|
$Fixture->primaryKey = 'my_random_key';
|
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('my_random_key', $Fixture->primaryKey);
|
2010-06-09 03:30:57 +00:00
|
|
|
}
|
2008-09-04 01:49:29 +00:00
|
|
|
|
2010-06-09 03:30:57 +00:00
|
|
|
/**
|
2012-07-18 01:55:29 +00:00
|
|
|
* test that init() correctly sets the fixture table when the connection
|
2012-03-18 21:31:53 +00:00
|
|
|
* or model have prefixes defined.
|
2010-06-09 03:30:57 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInitDbPrefix() {
|
2011-09-04 01:37:40 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
|
2010-09-20 02:58:30 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
2010-06-24 04:38:40 +00:00
|
|
|
$Source = new CakeTestFixtureTestFixture();
|
|
|
|
$Source->drop($db);
|
|
|
|
$Source->create($db);
|
|
|
|
$Source->insert($db);
|
2008-09-04 01:49:29 +00:00
|
|
|
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
2008-09-01 14:21:57 +00:00
|
|
|
$expected = array('id', 'name', 'created');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, array_keys($Fixture->fields));
|
2008-09-04 01:49:29 +00:00
|
|
|
|
|
|
|
$config = $db->config;
|
2010-09-26 02:23:48 +00:00
|
|
|
$config['prefix'] = 'fixture_test_suite_';
|
|
|
|
ConnectionManager::create('fixture_test_suite', $config);
|
2008-09-04 01:49:29 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
$Fixture->fields = $Fixture->records = null;
|
2010-09-20 02:58:30 +00:00
|
|
|
$Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test', 'records' => true);
|
2008-09-01 14:21:57 +00:00
|
|
|
$Fixture->init();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(count($Fixture->records), count($Source->records));
|
2010-09-26 02:23:48 +00:00
|
|
|
$Fixture->create(ConnectionManager::getDataSource('fixture_test_suite'));
|
2008-09-04 01:49:29 +00:00
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Fixture = new CakeTestFixtureImportFixture();
|
2010-07-30 02:01:12 +00:00
|
|
|
$Fixture->fields = $Fixture->records = $Fixture->table = null;
|
2010-09-20 02:58:30 +00:00
|
|
|
$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
|
2008-09-01 14:21:57 +00:00
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
|
|
|
|
$this->assertEquals('fixture_tests', $Fixture->table);
|
2011-04-08 17:50:36 +00:00
|
|
|
|
2008-09-04 01:49:29 +00:00
|
|
|
$keys = array_flip(ClassRegistry::keys());
|
|
|
|
$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
|
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Fixture->drop(ConnectionManager::getDataSource('fixture_test_suite'));
|
2010-06-24 04:38:40 +00:00
|
|
|
$Source->drop($db);
|
2008-09-01 14:21:57 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-30 02:01:12 +00:00
|
|
|
/**
|
|
|
|
* test that fixtures don't duplicate the test db prefix.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInitDbPrefixDuplication() {
|
2011-09-04 01:37:40 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
|
2010-09-20 04:02:50 +00:00
|
|
|
$db = ConnectionManager::getDataSource('test');
|
|
|
|
$backPrefix = $db->config['prefix'];
|
|
|
|
$db->config['prefix'] = 'cake_fixture_test_';
|
2010-09-26 02:23:48 +00:00
|
|
|
ConnectionManager::create('fixture_test_suite', $db->config);
|
|
|
|
$newDb = ConnectionManager::getDataSource('fixture_test_suite');
|
|
|
|
$newDb->config['prefix'] = 'cake_fixture_test_';
|
2010-07-30 02:01:12 +00:00
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Source = new CakeTestFixtureTestFixture();
|
2010-09-20 04:02:50 +00:00
|
|
|
$Source->create($db);
|
|
|
|
$Source->insert($db);
|
2010-07-30 02:01:12 +00:00
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Fixture = new CakeTestFixtureImportFixture();
|
2010-07-30 02:01:12 +00:00
|
|
|
$Fixture->fields = $Fixture->records = $Fixture->table = null;
|
2010-09-20 02:58:30 +00:00
|
|
|
$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
|
2010-07-30 02:01:12 +00:00
|
|
|
|
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
|
|
|
|
$this->assertEquals('fixture_tests', $Fixture->table);
|
2010-07-30 02:01:12 +00:00
|
|
|
|
2010-09-20 04:02:50 +00:00
|
|
|
$Source->drop($db);
|
|
|
|
$db->config['prefix'] = $backPrefix;
|
2010-07-30 02:01:12 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 03:30:57 +00:00
|
|
|
/**
|
|
|
|
* test init with a model that has a tablePrefix declared.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInitModelTablePrefix() {
|
2011-09-04 01:37:40 +00:00
|
|
|
$this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(!empty($this->db->config['prefix']), 'Cannot run this test, you have a database connection prefix.');
|
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Source = new CakeTestFixtureTestFixture();
|
|
|
|
$Source->create($this->db);
|
|
|
|
$Source->insert($this->db);
|
2010-06-09 03:30:57 +00:00
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
2010-06-09 03:30:57 +00:00
|
|
|
unset($Fixture->table);
|
|
|
|
$Fixture->fields = $Fixture->records = null;
|
2010-09-20 02:58:30 +00:00
|
|
|
$Fixture->import = array('model' => 'FixturePrefixTest', 'connection' => 'test', 'records' => false);
|
2010-06-09 03:30:57 +00:00
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('fixture_tests', $Fixture->table);
|
2010-06-09 03:30:57 +00:00
|
|
|
|
|
|
|
$keys = array_flip(ClassRegistry::keys());
|
|
|
|
$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
|
|
|
|
|
2010-09-26 02:23:48 +00:00
|
|
|
$Source->drop($this->db);
|
2010-06-09 03:30:57 +00:00
|
|
|
}
|
|
|
|
|
2008-11-12 17:00:00 +00:00
|
|
|
/**
|
|
|
|
* testImport
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testImport() {
|
2010-09-20 02:58:30 +00:00
|
|
|
$testSuiteDb = ConnectionManager::getDataSource('test');
|
2008-11-12 17:00:00 +00:00
|
|
|
$testSuiteConfig = $testSuiteDb->config;
|
2010-09-26 02:23:48 +00:00
|
|
|
ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
|
|
|
|
$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
|
2008-11-12 17:00:00 +00:00
|
|
|
|
2010-06-24 04:38:40 +00:00
|
|
|
$Source = new CakeTestFixtureTestFixture();
|
2008-11-12 17:00:00 +00:00
|
|
|
$Source->create($newTestSuiteDb);
|
|
|
|
$Source->insert($newTestSuiteDb);
|
|
|
|
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureDefaultImportFixture();
|
2008-11-12 17:00:00 +00:00
|
|
|
$Fixture->fields = $Fixture->records = null;
|
2010-09-26 02:23:48 +00:00
|
|
|
$Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
|
2008-11-12 17:00:00 +00:00
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
|
2008-11-12 17:00:00 +00:00
|
|
|
|
|
|
|
$keys = array_flip(ClassRegistry::keys());
|
|
|
|
$this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
|
|
|
|
|
|
|
|
$Source->drop($newTestSuiteDb);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-03-23 03:28:58 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* test that importing with records works. Make sure to try with postgres as its
|
2010-03-23 03:28:58 +00:00
|
|
|
* handling of aliases is a workaround at best.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testImportWithRecords() {
|
2010-09-20 02:58:30 +00:00
|
|
|
$testSuiteDb = ConnectionManager::getDataSource('test');
|
2010-03-23 03:28:58 +00:00
|
|
|
$testSuiteConfig = $testSuiteDb->config;
|
2010-09-26 02:23:48 +00:00
|
|
|
ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
|
|
|
|
$newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
|
2010-03-23 03:28:58 +00:00
|
|
|
|
2010-06-24 04:38:40 +00:00
|
|
|
$Source = new CakeTestFixtureTestFixture();
|
2010-03-23 03:28:58 +00:00
|
|
|
$Source->create($newTestSuiteDb);
|
|
|
|
$Source->insert($newTestSuiteDb);
|
|
|
|
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureDefaultImportFixture();
|
2010-03-23 03:28:58 +00:00
|
|
|
$Fixture->fields = $Fixture->records = null;
|
|
|
|
$Fixture->import = array(
|
2010-09-26 02:23:48 +00:00
|
|
|
'model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite', 'records' => true
|
2010-03-23 03:28:58 +00:00
|
|
|
);
|
|
|
|
$Fixture->init();
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
|
2010-03-23 03:28:58 +00:00
|
|
|
$this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.');
|
|
|
|
$this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record');
|
|
|
|
|
2011-04-08 17:50:36 +00:00
|
|
|
$Source->drop($newTestSuiteDb);
|
2010-03-23 03:28:58 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* test create method
|
|
|
|
*
|
|
|
|
* @return void
|
2009-03-18 17:55:58 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreate() {
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
|
|
|
$this->criticDb->expects($this->atLeastOnce())->method('execute');
|
|
|
|
$this->criticDb->expects($this->atLeastOnce())->method('createSchema');
|
2008-09-01 14:21:57 +00:00
|
|
|
$return = $Fixture->create($this->criticDb);
|
|
|
|
$this->assertTrue($this->criticDb->fullDebug);
|
|
|
|
$this->assertTrue($return);
|
2008-11-08 02:58:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
unset($Fixture->fields);
|
|
|
|
$return = $Fixture->create($this->criticDb);
|
|
|
|
$this->assertFalse($return);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* test the insert method
|
|
|
|
*
|
|
|
|
* @return void
|
2009-03-18 17:55:58 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInsert() {
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
2011-05-04 05:40:23 +00:00
|
|
|
$this->criticDb->expects($this->atLeastOnce())
|
|
|
|
->method('insertMulti')
|
2012-02-23 08:03:31 +00:00
|
|
|
->will($this->returnCallback(array($this, 'insertCallback')));
|
2008-09-01 14:21:57 +00:00
|
|
|
|
|
|
|
$return = $Fixture->insert($this->criticDb);
|
2011-05-04 05:40:23 +00:00
|
|
|
$this->assertTrue(!empty($this->insertMulti));
|
2008-09-01 14:21:57 +00:00
|
|
|
$this->assertTrue($this->criticDb->fullDebug);
|
|
|
|
$this->assertTrue($return);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('fixture_tests', $this->insertMulti['table']);
|
|
|
|
$this->assertEquals(array('name', 'created'), $this->insertMulti['fields']);
|
2011-04-08 17:50:36 +00:00
|
|
|
$expected = array(
|
2011-05-04 05:40:23 +00:00
|
|
|
array('Gandalf', '2009-04-28 19:20:00'),
|
|
|
|
array('Captain Picard', '2009-04-28 19:20:00'),
|
|
|
|
array('Chewbacca', '2009-04-28 19:20:00')
|
2011-04-08 17:50:36 +00:00
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $this->insertMulti['values']);
|
2011-05-04 05:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to be used as callback and store the parameters of an insertMulti call
|
|
|
|
*
|
2011-10-28 05:01:17 +00:00
|
|
|
* @param string $table
|
|
|
|
* @param string $fields
|
|
|
|
* @param string $values
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool true
|
2011-05-04 05:40:23 +00:00
|
|
|
*/
|
2012-02-23 08:03:31 +00:00
|
|
|
public function insertCallback($table, $fields, $values) {
|
2011-05-04 05:40:23 +00:00
|
|
|
$this->insertMulti['table'] = $table;
|
|
|
|
$this->insertMulti['fields'] = $fields;
|
|
|
|
$this->insertMulti['values'] = $values;
|
2013-03-14 21:51:54 +00:00
|
|
|
$this->insertMulti['fields_values'] = array();
|
2013-03-18 10:12:28 +00:00
|
|
|
foreach ($values as $record) {
|
2013-03-14 21:51:54 +00:00
|
|
|
$this->insertMulti['fields_values'][] = array_combine($fields, $record);
|
|
|
|
}
|
2011-05-04 05:40:23 +00:00
|
|
|
return true;
|
2011-04-08 17:50:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test the insert method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testInsertStrings() {
|
2011-05-04 05:40:23 +00:00
|
|
|
$Fixture = new StringsTestFixture();
|
|
|
|
$this->criticDb->expects($this->atLeastOnce())
|
|
|
|
->method('insertMulti')
|
2012-02-23 08:03:31 +00:00
|
|
|
->will($this->returnCallback(array($this, 'insertCallback')));
|
2008-09-01 14:21:57 +00:00
|
|
|
|
|
|
|
$return = $Fixture->insert($this->criticDb);
|
|
|
|
$this->assertTrue($this->criticDb->fullDebug);
|
|
|
|
$this->assertTrue($return);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('strings', $this->insertMulti['table']);
|
2013-03-14 21:51:54 +00:00
|
|
|
$this->assertEquals(array('name', 'email', 'age'), array_values($this->insertMulti['fields']));
|
2011-04-08 17:50:36 +00:00
|
|
|
$expected = array(
|
2011-05-04 05:40:23 +00:00
|
|
|
array('Mark Doe', 'mark.doe@email.com', null),
|
|
|
|
array('John Doe', 'john.doe@email.com', 20),
|
|
|
|
array('Jane Doe', 'jane.doe@email.com', 30),
|
2011-04-08 17:50:36 +00:00
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $this->insertMulti['values']);
|
2013-03-14 21:51:54 +00:00
|
|
|
$expected = array(
|
|
|
|
array(
|
2013-03-18 10:12:28 +00:00
|
|
|
'name' => 'Mark Doe',
|
|
|
|
'email' => 'mark.doe@email.com',
|
2013-03-14 21:51:54 +00:00
|
|
|
'age' => null
|
|
|
|
),
|
|
|
|
array(
|
2013-03-18 10:12:28 +00:00
|
|
|
'name' => 'John Doe',
|
|
|
|
'email' => 'john.doe@email.com',
|
2013-03-14 21:51:54 +00:00
|
|
|
'age' => 20
|
|
|
|
),
|
|
|
|
array(
|
2013-03-18 10:12:28 +00:00
|
|
|
'name' => 'Jane Doe',
|
|
|
|
'email' => 'jane.doe@email.com',
|
2013-03-14 21:51:54 +00:00
|
|
|
'age' => 30
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $this->insertMulti['fields_values']);
|
2008-09-01 14:21:57 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2014-01-06 20:07:00 +00:00
|
|
|
/**
|
|
|
|
* test the insert method with invalid fixture
|
|
|
|
*
|
|
|
|
* @expectedException CakeException
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInsertInvalid() {
|
|
|
|
$Fixture = new InvalidTestFixture();
|
2015-09-25 15:22:00 +00:00
|
|
|
$Fixture->insert($this->criticDb);
|
2014-01-06 20:07:00 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Test the drop method
|
|
|
|
*
|
|
|
|
* @return void
|
2009-03-18 17:55:58 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDrop() {
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
|
|
|
$this->criticDb->expects($this->at(1))->method('execute')->will($this->returnValue(true));
|
|
|
|
$this->criticDb->expects($this->at(3))->method('execute')->will($this->returnValue(false));
|
|
|
|
$this->criticDb->expects($this->exactly(2))->method('dropSchema');
|
2008-11-08 02:58:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
$return = $Fixture->drop($this->criticDb);
|
|
|
|
$this->assertTrue($this->criticDb->fullDebug);
|
|
|
|
$this->assertTrue($return);
|
2008-11-08 02:58:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
$return = $Fixture->drop($this->criticDb);
|
2011-09-03 23:28:53 +00:00
|
|
|
$this->assertTrue($return);
|
2011-02-09 02:53:12 +00:00
|
|
|
|
|
|
|
unset($Fixture->fields);
|
|
|
|
$return = $Fixture->drop($this->criticDb);
|
|
|
|
$this->assertFalse($return);
|
2008-09-01 14:21:57 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-09-01 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Test the truncate method.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-03-18 17:55:58 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTruncate() {
|
2010-06-24 04:38:40 +00:00
|
|
|
$Fixture = new CakeTestFixtureTestFixture();
|
|
|
|
$this->criticDb->expects($this->atLeastOnce())->method('truncate');
|
2008-09-01 14:21:57 +00:00
|
|
|
$Fixture->truncate($this->criticDb);
|
|
|
|
$this->assertTrue($this->criticDb->fullDebug);
|
|
|
|
}
|
|
|
|
}
|