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