/
ConcreteInheritanceBehaviorTest.php
321 lines (287 loc) · 14.7 KB
/
ConcreteInheritanceBehaviorTest.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Tests\Generator\Behavior;
use Propel\Tests\Helpers\Bookstore\BookstoreTestBase;
use Propel\Tests\Bookstore\Behavior\ConcreteArticle;
use Propel\Tests\Bookstore\Behavior\ConcreteArticleQuery;
use Propel\Tests\Bookstore\Behavior\ConcreteArticlePeer;
use Propel\Tests\Bookstore\Behavior\ConcreteAuthorPeer;
use Propel\Tests\Bookstore\Behavior\ConcreteCategory;
use Propel\Tests\Bookstore\Behavior\ConcreteCategoryQuery;
use Propel\Tests\Bookstore\Behavior\ConcreteContent;
use Propel\Tests\Bookstore\Behavior\ConcreteContentQuery;
use Propel\Tests\Bookstore\Behavior\ConcreteContentPeer;
use Propel\Tests\Bookstore\Behavior\ConcreteQuizz;
use Propel\Tests\Bookstore\Behavior\ConcreteQuizzPeer;
use Propel\Tests\Bookstore\Behavior\ConcreteQuizzQuery;
use Propel\Runtime\Exception\PropelException;
use Propel\Runtime\Map\RelationMap;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Generator\Util\QuickBuilder;
/**
* Tests for ConcreteInheritanceBehavior class
*
* @author François Zaniontto
*/
class ConcreteInheritanceBehaviorTest extends BookstoreTestBase
{
public function setUp()
{
parent::setUp();
if (!class_exists('ConcreteContentSetPkQuery')) {
$schema = <<<EOF
<database name="concrete_content_set_pk">
<table name="concrete_content_set_pk" allowPkInsert="true">
<column name="id" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
<column name="title" type="VARCHAR" size="100" primaryString="true" />
<index>
<index-column name="title" />
</index>
</table>
<table name="concrete_article_set_pk" allowPkInsert="true">
<column name="body" type="longvarchar" />
<column name="author_id" required="false" type="INTEGER" />
<behavior name="concrete_inheritance">
<parameter name="extends" value="concrete_content_set_pk" />
</behavior>
</table>
</database>
EOF;
QuickBuilder::buildSchema($schema);
}
}
public function testParentBehavior()
{
$behaviors = ConcreteContentPeer::getTableMap()->getBehaviors();
$this->assertTrue(array_key_exists('concrete_inheritance_parent', $behaviors), 'modifyTable() gives the parent table the concrete_inheritance_parent behavior');
$this->assertEquals('descendant_class', $behaviors['concrete_inheritance_parent']['descendant_column'], 'modifyTable() passed the descendant_column parameter to the parent behavior');
}
public function testModifyTableAddsParentColumn()
{
$contentColumns = array('id', 'title', 'category_id');
$article = ConcreteArticlePeer::getTableMap();
foreach ($contentColumns as $column) {
$this->assertTrue($article->hasColumn($column), 'modifyTable() adds the columns of the parent table');
}
$quizz = ConcreteQuizzPeer::getTableMap();
$this->assertEquals(3, count($quizz->getColumns()), 'modifyTable() does not add a column of the parent table if a similar column exists');
}
public function testModifyTableCopyDataAddsOneToOneRelationships()
{
$article = ConcreteArticlePeer::getTableMap();
$this->assertTrue($article->hasRelation('ConcreteContent'), 'modifyTable() adds a relationship to the parent');
$relation = $article->getRelation('ConcreteContent');
$this->assertEquals(RelationMap::MANY_TO_ONE, $relation->getType(), 'modifyTable adds a one-to-one relationship');
$content = ConcreteContentPeer::getTableMap();
$relation = $content->getRelation('ConcreteArticle');
$this->assertEquals(RelationMap::ONE_TO_ONE, $relation->getType(), 'modifyTable adds a one-to-one relationship');
}
public function testModifyTableNoCopyDataNoParentRelationship()
{
$quizz = ConcreteQuizzPeer::getTableMap();
$this->assertFalse($quizz->hasRelation('ConcreteContent'), 'modifyTable() does not add a relationship to the parent when copy_data is false');
}
public function testModifyTableCopyDataRemovesAutoIncrement()
{
$content = new ConcreteContent();
$content->save();
$c = new Criteria;
$c->add(ConcreteArticlePeer::ID, $content->getId());
try {
ConcreteArticlePeer::doInsert($c);
$this->assertTrue(true, 'modifyTable() removed autoIncrement from copied Primary keys');
} catch (PropelException $e) {
$this->fail('modifyTable() removed autoIncrement from copied Primary keys');
}
}
public function testModifyTableNoCopyDataKeepsAutoIncrement()
{
$this->setExpectedException('Propel\\Runtime\\Exception\\PropelException');
$content = new ConcreteContent();
$content->save();
$c = new Criteria;
$c->add(ConcreteQuizzPeer::ID, $content->getId());
ConcreteQuizzPeer::doInsert($c);
}
public function testModifyTableAddsForeignKeys()
{
$article = ConcreteArticlePeer::getTableMap();
$this->assertTrue($article->hasRelation('ConcreteCategory'), 'modifyTable() copies relationships from parent table');
}
public function testModifyTableAddsForeignKeysWithoutDuplicates()
{
$article = ConcreteAuthorPeer::getTableMap();
$this->assertTrue($article->hasRelation('ConcreteNews'), 'modifyTable() copies relationships from parent table and removes hardcoded refPhpName');
}
// no way to test copying of indices and uniques, except by reverse engineering the db...
public function testParentObjectClass()
{
$r = new \ReflectionClass('Propel\Tests\Bookstore\Behavior\Base\ConcreteArticle');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteContent', $r->getParentClass()->getName(), 'concrete_inheritance changes the parent class of the Model Object to the parent object class');
$r = new \ReflectionClass('Propel\Tests\Bookstore\Behavior\Base\ConcreteQuizz');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteContent', $r->getParentClass()->getName(), 'concrete_inheritance changes the parent class of the Model Object to the parent object class');
}
public function testParentQueryClass()
{
$r = new \ReflectionClass('Propel\Tests\Bookstore\Behavior\Base\ConcreteArticleQuery');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteContentQuery', $r->getParentClass()->getName(), 'concrete_inheritance changes the parent class of the Query Object to the parent object class');
$r = new \ReflectionClass('Propel\Tests\Bookstore\Behavior\Base\ConcreteQuizzQuery');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteContentQuery', $r->getParentClass()->getName(), 'concrete_inheritance changes the parent class of the Query Object to the parent object class');
}
/**
* @link http://www.propelorm.org/ticket/1262
*/
public function testParentPeerClass()
{
$r = new \ReflectionClass('Propel\Tests\Bookstore\Behavior\Base\ConcreteArticlePeer');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteContentPeer', $r->getParentClass()->getName(), 'concrete_inheritance changes the parent class of the Peer Object to the parent object class');
$r = new \ReflectionClass('Propel\Tests\Bookstore\Behavior\Base\ConcreteQuizzPeer');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteContentPeer', $r->getParentClass()->getName(), 'concrete_inheritance changes the parent class of the Peer Object to the parent object class');
}
public function testPreSaveCopyData()
{
ConcreteArticleQuery::create()->deleteAll();
ConcreteQuizzQuery::create()->deleteAll();
ConcreteContentQuery::create()->deleteAll();
ConcreteCategoryQuery::create()->deleteAll();
$category = new ConcreteCategory();
$category->setName('main');
$article = new ConcreteArticle();
$article->setConcreteCategory($category);
$article->save();
$this->assertNotNull($article->getId());
$this->assertNotNull($category->getId());
$content = ConcreteContentQuery::create()->findPk($article->getId());
$this->assertNotNull($content);
$this->assertEquals($category->getId(), $content->getCategoryId());
}
public function testPreSaveNoCopyData()
{
ConcreteArticleQuery::create()->deleteAll();
ConcreteQuizzQuery::create()->deleteAll();
ConcreteContentQuery::create()->deleteAll();
$quizz = new ConcreteQuizz();
$quizz->save();
$this->assertNotNull($quizz->getId());
$content = ConcreteContentQuery::create()->findPk($quizz->getId());
$this->assertNull($content);
}
public function testGetParentOrCreateNew()
{
$article = new ConcreteArticle();
$content = $article->getParentOrCreate();
$this->assertTrue($content instanceof ConcreteContent, 'getParentOrCreate() returns an instance of the parent class');
$this->assertTrue($content->isNew(), 'getParentOrCreate() returns a new instance of the parent class if the object is new');
$this->assertEquals('Propel\Tests\Bookstore\Behavior\ConcreteArticle', $content->getDescendantClass(), 'getParentOrCreate() correctly sets the descendant_class of the parent object');
}
public function testGetParentOrCreateExisting()
{
$article = new ConcreteArticle();
$article->save();
ConcreteContentPeer::clearInstancePool();
$content = $article->getParentOrCreate();
$this->assertTrue($content instanceof ConcreteContent, 'getParentOrCreate() returns an instance of the parent class');
$this->assertFalse($content->isNew(), 'getParentOrCreate() returns an existing instance of the parent class if the object is persisted');
$this->assertEquals($article->getId(), $content->getId(), 'getParentOrCreate() returns the parent object related to the current object');
}
public function testGetParentOrCreateExistingParent()
{
ConcreteContentQuery::create()->deleteAll();
ConcreteArticleQuery::create()->deleteAll();
$content = new ConcreteContent();
$content->save();
$id = $content->getId();
ConcreteContentPeer::clearInstancePool();
$article = new ConcreteArticle();
$article->setId($id);
$article->save();
$this->assertEquals($id, $article->getId(), 'getParentOrCreate() keeps manually set pk');
$this->assertEquals(1, ConcreteContentQuery::create()->count(), 'getParentOrCreate() creates no new parent entry');
}
public function testGetSyncParent()
{
$category = new ConcreteCategory();
$category->setName('main');
$article = new ConcreteArticle();
$article->setTitle('FooBar');
$article->setConcreteCategory($category);
$content = $article->getSyncParent();
$this->assertEquals('FooBar', $content->getTitle(), 'getSyncParent() returns a synchronized parent object');
$this->assertEquals($category, $content->getConcreteCategory(), 'getSyncParent() returns a synchronized parent object');
}
public function testPostDeleteCopyData()
{
ConcreteArticleQuery::create()->deleteAll();
ConcreteQuizzQuery::create()->deleteAll();
ConcreteContentQuery::create()->deleteAll();
ConcreteCategoryQuery::create()->deleteAll();
$category = new ConcreteCategory();
$category->setName('main');
$article = new ConcreteArticle();
$article->setConcreteCategory($category);
$article->save();
$id = $article->getId();
$article->delete();
$this->assertNull(ConcreteContentQuery::create()->findPk($id), 'delete() removes the parent record as well');
}
public function testGetParentOrCreateNewWithPK()
{
\ConcreteContentSetPkQuery::create()->deleteAll();
\ConcreteArticleSetPkQuery::create()->deleteAll();
$article = new \ConcreteArticleSetPk();
$article->setId(5);
$content = $article->getParentOrCreate();
$this->assertEquals(5, $article->getId(), 'getParentOrCreate() keeps manually set pk');
$this->assertTrue($content instanceof \ConcreteContentSetPk, 'getParentOrCreate() returns an instance of the parent class');
$this->assertTrue($content->isNew(), 'getParentOrCreate() returns a new instance of the parent class if the object is new');
$this->assertEquals(5,$content->getId(), 'getParentOrCreate() returns a instance of the parent class with pk set');
$this->assertEquals('ConcreteArticleSetPk', $content->getDescendantClass(), 'getParentOrCreate() correctly sets the descendant_class of the parent object');
}
public function testSetPKOnNewObject()
{
\ConcreteContentSetPkQuery::create()->deleteAll();
\ConcreteArticleSetPkQuery::create()->deleteAll();
$article = new \ConcreteArticleSetPk();
$article->setId(2);
$article->save();
$this->assertEquals(2, $article->getId(), 'getParentOrCreate() keeps manually set pk after save');
$this->assertEquals(1, \ConcreteContentSetPkQuery::create()->count(), 'getParentOrCreate() creates a parent entry');
$articledb = \ConcreteArticleSetPkQuery::create()->findOneById(2);
$this->assertEquals(2, $articledb->getId(), 'getParentOrCreate() keeps manually set pk after save and reload from db');
}
public function testSetPKOnNewObjectWithPkAlreadyInParentTable()
{
\ConcreteContentSetPkQuery::create()->deleteAll();
\ConcreteArticleSetPkQuery::create()->deleteAll();
try {
$article = new \ConcreteArticleSetPk();
$article->setId(4);
$article->save();
$article = new \ConcreteArticleSetPk();
$article->setId(4);
$article->save();
$this->fail('getParentOrCreate() returns a new parent object on new child objects with pk set');
} catch (PropelException $e) {
$this->assertTrue(true, 'getParentOrCreate() returns a new parent object on new child objects with pk set');
}
}
public function testSetPkAllowPkInsertIsFalse()
{
ConcreteContentQuery::create()->deleteAll();
ConcreteArticleQuery::create()->deleteAll();
try {
$article = new ConcreteArticle();
$article->setId(4);
$article->save();
$this->fail('SetPk fails when allowPkInsert is false');
} catch (PropelException $e) {
$this->assertTrue(true, 'SetPk fails when allowPkInsert is false');
}
}
}