forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelsForeignKeysTest.php
250 lines (192 loc) · 6.5 KB
/
ModelsForeignKeysTest.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
+------------------------------------------------------------------------+
*/
class ModelsForeignKeysTest extends PHPUnit_Framework_TestCase
{
public function __construct()
{
spl_autoload_register(array($this, 'modelsAutoloader'));
}
public function __destruct()
{
spl_autoload_unregister(array($this, 'modelsAutoloader'));
}
public function modelsAutoloader($className)
{
if (file_exists('unit-tests/models/'.$className.'.php')) {
require 'unit-tests/models/'.$className.'.php';
}
}
protected function _getDI()
{
Phalcon\DI::reset();
$di = new Phalcon\DI();
$di->set('modelsManager', function(){
return new Phalcon\Mvc\Model\Manager();
}, true);
$di->set('modelsMetadata', function(){
return new Phalcon\Mvc\Model\Metadata\Memory();
}, true);
return $di;
}
public function testForeignKeysMysql()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped("Skipped");
return;
}
$di = $this->_getDI();
$di->set('db', function(){
require 'unit-tests/config.db.php';
return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
}, true);
$this->_executeTestsNormal($di);
$this->_executeTestsRenamed($di);
}
public function testForeignKeysPostgresql()
{
require 'unit-tests/config.db.php';
if (empty($configPostgresql)) {
$this->markTestSkipped("Skipped");
return;
}
$di = $this->_getDI();
$di->set('db', function(){
require 'unit-tests/config.db.php';
return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql);
}, true);
$this->_executeTestsNormal($di);
$this->_executeTestsRenamed($di);
}
public function testForeignKeysSqlite()
{
require 'unit-tests/config.db.php';
if (empty($configSqlite)) {
$this->markTestSkipped("Skipped");
return;
}
$di = $this->_getDI();
$di->set('db', function(){
require 'unit-tests/config.db.php';
return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite);
}, true);
$this->_executeTestsNormal($di);
$this->_executeTestsRenamed($di);
}
public function _executeTestsNormal($di)
{
//Normal foreign keys
$robotsParts = new RobotsParts();
$robotsParts->robots_id = 1;
$robotsParts->parts_id = 100;
$this->assertFalse($robotsParts->save());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'Value of field "parts_id" does not exist on referenced table',
'_field' => 'parts_id',
))
);
$this->assertEquals($robotsParts->getMessages(), $messages);
$robotsParts->robots_id = 100;
$robotsParts->parts_id = 1;
$this->assertFalse($robotsParts->save());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'The robot code does not exist',
'_field' => 'robots_id',
))
);
$this->assertEquals($robotsParts->getMessages(), $messages);
//Reverse foreign keys
$robot = Robots::findFirst();
$this->assertNotEquals($robot, false);
$this->assertFalse($robot->delete());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'Record is referenced by model RobotsParts',
'_field' => 'id',
))
);
$this->assertEquals($robot->getMessages(), $messages);
$part = Parts::findFirst();
$this->assertNotEquals($part, false);
$this->assertFalse($part->delete());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'Parts cannot be deleted because is referenced by a Robot',
'_field' => 'id',
))
);
$this->assertEquals($part->getMessages(), $messages);
}
public function _executeTestsRenamed($di)
{
//Normal foreign keys with column renaming
$robottersDeles = new RobottersDeles();
$robottersDeles->robottersCode = 1;
$robottersDeles->delesCode = 100;
$this->assertFalse($robottersDeles->save());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'Value of field "delesCode" does not exist on referenced table',
'_field' => 'delesCode',
))
);
$this->assertEquals($robottersDeles->getMessages(), $messages);
$robottersDeles->robottersCode = 100;
$robottersDeles->delesCode = 1;
$this->assertFalse($robottersDeles->save());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'The robotters code does not exist',
'_field' => 'robottersCode',
))
);
$this->assertEquals($robottersDeles->getMessages(), $messages);
//Reverse foreign keys with renaming
$robotter = Robotters::findFirst();
$this->assertNotEquals($robotter, false);
$this->assertFalse($robotter->delete());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'Record is referenced by model RobottersDeles',
'_field' => 'code',
))
);
$this->assertEquals($robotter->getMessages(), $messages);
$dele = Deles::findFirst();
$this->assertNotEquals($dele, false);
$this->assertFalse($dele->delete());
$messages = array(
0 => Phalcon\Mvc\Model\Message::__set_state(array(
'_type' => 'ConstraintViolation',
'_message' => 'Deles cannot be deleted because is referenced by a Robotter',
'_field' => 'code',
))
);
$this->assertEquals($dele->getMessages(), $messages);
}
}