ModelCrossSchemaHabtmTest.php 6.4 KB

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