ModelCrossSchemaHabtmTest.php 6.1 KB

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