pictcode / lib / Cake / Test / Case / Model / ModelCrossSchemaHabtmTest.php @ 1368d528
履歴 | 表示 | アノテート | ダウンロード (6.325 KB)
| 1 |
<?php
|
|---|---|
| 2 |
/**
|
| 3 |
* Tests cross database HABTM. Requires $test and $test2 to both be set in DATABASE_CONFIG
|
| 4 |
* NOTE: When testing on MySQL, you must set 'persistent' => false on *both* database connections,
|
| 5 |
* or one connection will step on the other.
|
| 6 |
*
|
| 7 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
| 8 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 9 |
*
|
| 10 |
* Licensed under The MIT License
|
| 11 |
* For full copyright and license information, please see the LICENSE.txt
|
| 12 |
* Redistributions of files must retain the above copyright notice
|
| 13 |
*
|
| 14 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 15 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
| 16 |
* @package Cake.Test.Case.Model
|
| 17 |
* @since CakePHP(tm) v 2.1
|
| 18 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
| 19 |
*/
|
| 20 |
|
| 21 |
require_once dirname(__FILE__) . DS . 'ModelTestBase.php'; |
| 22 |
|
| 23 |
/**
|
| 24 |
* Class ModelCrossSchemaHabtmTest
|
| 25 |
*
|
| 26 |
* @package Cake.Test.Case.Model
|
| 27 |
*/
|
| 28 |
class ModelCrossSchemaHabtmTest extends BaseModelTest { |
| 29 |
|
| 30 |
/**
|
| 31 |
* Fixtures to be used
|
| 32 |
*
|
| 33 |
* @var array
|
| 34 |
*/
|
| 35 |
public $fixtures = array( |
| 36 |
'core.player', 'core.guild', 'core.guilds_player', |
| 37 |
'core.armor', 'core.armors_player', |
| 38 |
); |
| 39 |
|
| 40 |
/**
|
| 41 |
* Don't drop tables if they exist
|
| 42 |
*
|
| 43 |
* @var bool
|
| 44 |
*/
|
| 45 |
public $dropTables = false; |
| 46 |
|
| 47 |
/**
|
| 48 |
* Don't auto load fixtures
|
| 49 |
*
|
| 50 |
* @var bool
|
| 51 |
*/
|
| 52 |
public $autoFixtures = false; |
| 53 |
|
| 54 |
/**
|
| 55 |
* setUp method
|
| 56 |
*
|
| 57 |
* @return void
|
| 58 |
*/
|
| 59 |
public function setUp() { |
| 60 |
parent::setUp();
|
| 61 |
$this->_checkConfigs();
|
| 62 |
} |
| 63 |
|
| 64 |
/**
|
| 65 |
* Check if primary and secondary test databases are configured.
|
| 66 |
*
|
| 67 |
* @return void
|
| 68 |
*/
|
| 69 |
protected function _checkConfigs() { |
| 70 |
$config = ConnectionManager::enumConnectionObjects(); |
| 71 |
$this->skipIf($this->db instanceof Sqlite, 'This test is not compatible with Sqlite.'); |
| 72 |
$this->skipIf(
|
| 73 |
!isset($config['test']) || !isset($config['test2']), |
| 74 |
'Primary and secondary test databases not configured, ' .
|
| 75 |
'skipping cross-database join tests.' .
|
| 76 |
' To run these tests, you must define $test and $test2 in your database configuration.'
|
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/**
|
| 81 |
* testModelDatasources method
|
| 82 |
*
|
| 83 |
* @return void
|
| 84 |
*/
|
| 85 |
public function testModelDatasources() { |
| 86 |
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); |
| 87 |
|
| 88 |
$Player = ClassRegistry::init('Player'); |
| 89 |
$this->assertEquals('test', $Player->useDbConfig); |
| 90 |
$this->assertEquals('test', $Player->Guild->useDbConfig); |
| 91 |
$this->assertEquals('test2', $Player->GuildsPlayer->useDbConfig); |
| 92 |
|
| 93 |
$this->assertEquals('test', $Player->getDataSource()->configKeyName); |
| 94 |
$this->assertEquals('test', $Player->Guild->getDataSource()->configKeyName); |
| 95 |
$this->assertEquals('test2', $Player->GuildsPlayer->getDataSource()->configKeyName); |
| 96 |
} |
| 97 |
|
| 98 |
/**
|
| 99 |
* testHabtmFind method
|
| 100 |
*
|
| 101 |
* @return void
|
| 102 |
*/
|
| 103 |
public function testHabtmFind() { |
| 104 |
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); |
| 105 |
$Player = ClassRegistry::init('Player'); |
| 106 |
|
| 107 |
$players = $Player->find('all', array( |
| 108 |
'fields' => array('id', 'name'), |
| 109 |
'contain' => array( |
| 110 |
'Guild' => array( |
| 111 |
'conditions' => array( |
| 112 |
'Guild.name' => 'Wizards', |
| 113 |
), |
| 114 |
), |
| 115 |
), |
| 116 |
)); |
| 117 |
$this->assertEquals(4, count($players)); |
| 118 |
$wizards = Hash::extract($players, '{n}.Guild.{n}[name=Wizards]'); |
| 119 |
$this->assertEquals(1, count($wizards)); |
| 120 |
|
| 121 |
$players = $Player->find('all', array( |
| 122 |
'fields' => array('id', 'name'), |
| 123 |
'conditions' => array( |
| 124 |
'Player.id' => 1, |
| 125 |
), |
| 126 |
)); |
| 127 |
$this->assertEquals(1, count($players)); |
| 128 |
$wizards = Hash::extract($players, '{n}.Guild.{n}'); |
| 129 |
$this->assertEquals(2, count($wizards)); |
| 130 |
} |
| 131 |
|
| 132 |
/**
|
| 133 |
* testHabtmSave method
|
| 134 |
*
|
| 135 |
* @return void
|
| 136 |
*/
|
| 137 |
public function testHabtmSave() { |
| 138 |
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer'); |
| 139 |
$Player = ClassRegistry::init('Player'); |
| 140 |
$players = $Player->find('count'); |
| 141 |
$this->assertEquals(4, $players); |
| 142 |
|
| 143 |
$player = $Player->create(array( |
| 144 |
'name' => 'rchavik', |
| 145 |
)); |
| 146 |
|
| 147 |
$results = $Player->saveAll($player, array('validate' => 'first')); |
| 148 |
$this->assertNotSame(false, $results); |
| 149 |
$count = $Player->find('count'); |
| 150 |
$this->assertEquals(5, $count); |
| 151 |
|
| 152 |
$count = $Player->GuildsPlayer->find('count'); |
| 153 |
$this->assertEquals(3, $count); |
| 154 |
|
| 155 |
$player = $Player->findByName('rchavik'); |
| 156 |
$this->assertEmpty($player['Guild']); |
| 157 |
|
| 158 |
$player['Guild']['Guild'] = array(1, 2, 3); |
| 159 |
$Player->save($player); |
| 160 |
|
| 161 |
$player = $Player->findByName('rchavik'); |
| 162 |
$this->assertEquals(3, count($player['Guild'])); |
| 163 |
|
| 164 |
$players = $Player->find('all', array( |
| 165 |
'contain' => array( |
| 166 |
'conditions' => array( |
| 167 |
'Guild.name' => 'Rangers', |
| 168 |
), |
| 169 |
), |
| 170 |
)); |
| 171 |
$rangers = Hash::extract($players, '{n}.Guild.{n}[name=Rangers]'); |
| 172 |
$this->assertEquals(2, count($rangers)); |
| 173 |
} |
| 174 |
|
| 175 |
/**
|
| 176 |
* testHabtmWithThreeDatabases method
|
| 177 |
*
|
| 178 |
* @return void
|
| 179 |
*/
|
| 180 |
public function testHabtmWithThreeDatabases() { |
| 181 |
$config = ConnectionManager::enumConnectionObjects(); |
| 182 |
$this->skipIf(
|
| 183 |
!isset($config['test']) || !isset($config['test2']) || !isset($config['test_database_three']), |
| 184 |
'Primary, secondary, and tertiary test databases not configured,' .
|
| 185 |
' skipping test. To run these tests, you must define ' .
|
| 186 |
'$test, $test2, and $test_database_three in your database configuration.'
|
| 187 |
); |
| 188 |
|
| 189 |
$this->loadFixtures('Player', 'Guild', 'GuildsPlayer', 'Armor', 'ArmorsPlayer'); |
| 190 |
|
| 191 |
$Player = ClassRegistry::init('Player'); |
| 192 |
$Player->bindModel(array( |
| 193 |
'hasAndBelongsToMany' => array( |
| 194 |
'Armor' => array( |
| 195 |
'with' => 'ArmorsPlayer', |
| 196 |
'unique' => true, |
| 197 |
), |
| 198 |
), |
| 199 |
), false);
|
| 200 |
$this->assertEquals('test', $Player->useDbConfig); |
| 201 |
$this->assertEquals('test2', $Player->Armor->useDbConfig); |
| 202 |
$this->assertEquals('test_database_three', $Player->ArmorsPlayer->useDbConfig); |
| 203 |
$players = $Player->find('count'); |
| 204 |
$this->assertEquals(4, $players); |
| 205 |
|
| 206 |
$spongebob = $Player->create(array( |
| 207 |
'id' => 10, |
| 208 |
'name' => 'spongebob', |
| 209 |
)); |
| 210 |
$spongebob['Armor'] = array('Armor' => array(1, 2, 3, 4)); |
| 211 |
$result = $Player->save($spongebob); |
| 212 |
|
| 213 |
$expected = array( |
| 214 |
'Player' => array( |
| 215 |
'id' => 10, |
| 216 |
'name' => 'spongebob', |
| 217 |
), |
| 218 |
'Armor' => array( |
| 219 |
'Armor' => array( |
| 220 |
1, 2, 3, 4, |
| 221 |
), |
| 222 |
), |
| 223 |
); |
| 224 |
unset($result['Player']['created']); |
| 225 |
unset($result['Player']['updated']); |
| 226 |
$this->assertEquals($expected, $result); |
| 227 |
|
| 228 |
$spongebob = $Player->find('all', array( |
| 229 |
'conditions' => array( |
| 230 |
'Player.id' => 10, |
| 231 |
) |
| 232 |
)); |
| 233 |
$spongeBobsArmors = Hash::extract($spongebob, '{n}.Armor.{n}'); |
| 234 |
$this->assertEquals(4, count($spongeBobsArmors)); |
| 235 |
} |
| 236 |
} |