ModelCrossSchemaHabtmTest.php 6.3 KB

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