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