ModelCrossSchemaHabtmTest.php 6.3 KB

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