Pluf Framework

Pluf Framework Git Source Tree


Root/tests/Pluf_Model/PlufModelTest.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume CMS, a website management application.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume CMS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Plume CMS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
error_reporting(E_ALL | E_STRICT);
$path = dirname(__FILE__).'/../../src/';
set_include_path(get_include_path().PATH_SEPARATOR.$path);
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Framework/IncompleteTestError.php';
 
require_once 'Pluf.php';
 
include_once dirname(__FILE__).'/TestModels.php';
 
class PlufModelTest extends PHPUnit_Framework_TestCase
{
 
    protected function setUp()
    {
        Pluf::start(dirname(__FILE__).'/../conf/pluf.config.php');
        $m = require dirname(__FILE__).'/relations.php';
        $GLOBALS['_PX_models'] = array_merge($m, $GLOBALS['_PX_models']);
        $GLOBALS['_PX_config']['pluf_use_rowpermission'] = false;
        $db = Pluf::db();
        $schema = Pluf::factory('Pluf_DB_Schema', $db);
        $m1 = new TestModel();
        $m2 = new RelatedToTestModel();
        $m3 = new RelatedToTestModel2();
        $m4 = new TestModelRecurse();
        $schema->model = $m1;
        $schema->dropTables();
        $schema->createTables();
        $schema->model = $m2;
        $schema->dropTables();
        $schema->createTables();
        $schema->model = $m3;
        $schema->dropTables();
        $schema->createTables();
        $schema->model = $m4;
        $schema->dropTables();
        $schema->createTables();
 
    }
 
    protected function tearDown()
    {
        $db = Pluf::db();
        $schema = Pluf::factory('Pluf_DB_Schema', $db);
        $m1 = new TestModel();
        $schema->model = $m1;
        $schema->dropTables();
        $m2 = new RelatedToTestModel();
        $schema->model = $m2;
        $schema->dropTables();
        $m3 = new RelatedToTestModel2();
        $schema->model = $m3;
        $schema->dropTables();
        $m4 = new TestModelRecurse();
        $schema->model = $m4;
        $schema->dropTables();
    }
 
    public function testSetModelField()
    {
        $model = new TestModel();
        $model->title =  'myvalue';
        $this->assertEquals('myvalue', $model->title);
    }
 
    public function testTestModelRecurse()
    {
        $model = new TestModelRecurse();
        $model->title =  'myvalue';
        $this->assertEquals('myvalue', $model->title);
        $model->create();
        $model2 = new TestModelRecurse();
        $model2->title =  'child';
        $model2->parentid = $model;
        $this->assertEquals(true, $model2->create());
        $a = $model->get_children_list();
        $this->assertEquals($a[0]->title, 'child');
    }
 
    public function testCreateTestModel()
    {
        $model = new TestModel();
        $model->title = 'my title';
        $model->description = 'A small desc.';
        $this->assertEquals(true, $model->create());
        $this->assertEquals(1, (int) $model->id);
    }
 
    public function testGetTestModel()
    {
        $model = new TestModel();
        $model->title = 'my title';
        $model->description = 'A small desc.';
        $model->create();
        $m = new TestModel(1);
        $this->assertEquals('my title', $m->title);
    }
 
    public function testUpdateTestModel()
    {
        $model = new TestModel();
        $model->title =  'my title';
        $model->description = 'A small desc.';
        $model->create();
        $model = new TestModel(1);
        $model->description = 'A small desc 2.';       
        $this->assertEquals(true, $model->update());
        $this->assertEquals('A small desc 2.', $model->description);
    }
 
    public function testDeleteTestModel()
    {
        $model = new TestModel();
        $model->title = 'my title';
        $model->description = 'A small desc.';
        $model->create();
        $this->assertEquals(1, $model->id);
        $this->assertEquals(true, $model->delete());
        $this->assertEquals('', $model->id);
        $this->assertEquals('', $model->title);
        $model = new TestModel();
        $this->assertEquals(false, $model->get(1));
    }
 
    public function testGetListTestModel()
    {
        for ($i=0; $i<10; $i++) {
            $model = new TestModel();
            $model->title = 'title '.$i;
            $model->description = 'A small desc '.$i;
            $model->create();
        }
        $model->title = 'update to have 11 records and 10 head';
        $model->update();
        $m = new TestModel();
        $models = $m->getList();
        $this->assertEquals(10, count($models));
        $this->assertEquals('title 0', $models[0]->title);
        $this->assertEquals('title 5', $models[5]->title);
    }
 
    public function testGetCountModel()
    {
        for ($i=0; $i<10; $i++) {
            $model = new TestModel();
            $model->title = 'title '.$i;
            $model->description = 'A small desc '.$i;
            $model->create();
        }
        $model->title = 'update to have 11 records and 10 head';
        $model->update();
        $m = new TestModel();
        $this->assertEquals(10, $m->getCount());
    }
 
 
    public function testRelatedTestModel()
    {
        $model = new TestModel();
        $model->title = 'title';
        $model->description = 'A small desc ';
        $this->assertEquals(true, $model->create());
        $m = new RelatedToTestModel();
        $m->testmodel = $model;
        $m->dummy = 'stupid values';
        $this->assertEquals(true, $m->create());
        $rel = $model->get_relatedtotestmodel_list();
        $this->assertEquals('stupid values', $rel[0]->dummy);
        $mod = $m->get_testmodel();
        $this->assertEquals('title', $mod->title);
    }
 
    public function testLimitRelatedTestModel()
    {
        $model = new TestModel();
        $model->title = 'title';
        $model->description = 'A small desc ';
        $this->assertEquals(true, $model->create());
        $m = new RelatedToTestModel();
        $m->testmodel = $model;
        $m->dummy = 'stupid values';
        $this->assertEquals(true, $m->create());
        $m = new RelatedToTestModel();
        $m->testmodel = $model;
        $m->dummy = 'stupid values 2';
        $this->assertEquals(true, $m->create());
        $m = new RelatedToTestModel();
        $m->testmodel = $model;
        $m->dummy = 'stupid values 3';
        $this->assertEquals(true, $m->create());       
        $rel = $model->get_relatedtotestmodel_list(array('filter' => "dummy='stupid values 2'"));
        $this->assertEquals('stupid values 2', $rel[0]->dummy);
        $this->assertEquals(1, count($rel));
        $rel = $model->get_relatedtotestmodel_list();
        $this->assertEquals(3, count($rel));
    }
 
    /**
     * Test if the delete() call on a model is deleting the model
     * related through a foreignkey.
     */
    public function testDeleteRelatedModels()
    {
        $model = new TestModel();
        $model->title = 'title';
        $model->description = 'A small desc ';
        $model->create();
        $m1 = new RelatedToTestModel();
        $m1->testmodel = $model;
        $m1->dummy = 'stupid values';
        $m1->create();
        $m2 = new RelatedToTestModel();
        $m2->testmodel = $model;
        $m2->dummy = 'stupid values';
        $m2->create();
        $m3 = new RelatedToTestModel();
        $m3->testmodel = $model;
        $m3->dummy = 'stupid values';
        $m3->create();
        $rel = $model->get_relatedtotestmodel_list();
        $this->assertEquals(3, count($rel));
        $this->assertEquals(0, count($m2->getDeleteSideEffect()));
        $m2->delete();
        $rel = $model->get_relatedtotestmodel_list();
        $this->assertEquals(2, count($rel));
        $this->assertEquals(2, count($model->getDeleteSideEffect()));
        $model->delete();
        $mr = new RelatedToTestModel();
        $rel = $mr->getList();
        $this->assertEquals(0, count($rel));
    }
 
    public function testManyRelatedTestModel()
    {
        $tm1 = new TestModel();
        $tm1->title = 'title tm1';
        $tm1->description = 'A small desc tm1';
        $tm1->create();
        $tm2 = new TestModel();
        $tm2->title = 'title tm2';
        $tm2->description = 'A small desc tm2';
        $tm2->create();
        $tm3 = new TestModel();
        $tm3->title = 'title tm3';
        $tm3->description = 'A small desc tm3';
        $tm3->create();
 
        $rm1 = new RelatedToTestModel2();
        $rm1->testmodel_1 = $tm1;
        $rm1->testmodel_2 = $tm2;
        $rm1->dummy = 'stupid values rm1';
        $rm1->create();
        $rm2 = new RelatedToTestModel2();
        $rm2->testmodel_1 = $tm1;
        $rm2->testmodel_2 = $tm2;
        $rm2->dummy = 'stupid values rm2';
        $rm2->create();
        $rm3 = new RelatedToTestModel2();
        $rm3->testmodel_1 = $tm1;
        $rm3->testmodel_2 = $tm3;
        $rm3->dummy = 'stupid values rm3';
        $rm3->create();
 
        $rel = $tm1->get_first_rttm_list();
        $this->assertEquals(3, count($rel));
        $this->assertEquals('stupid values rm1', $rel[0]->dummy);
        $rel = $tm2->get_first_rttm_list();
        $this->assertEquals(0, count($rel));
        $rel = $tm2->get_second_rttm_list();
        $this->assertEquals(2, count($rel));
        $this->assertEquals('stupid values rm2', $rel[1]->dummy);
        $rel = $tm3->get_second_rttm_list();
        $this->assertEquals(1, count($rel));
        $this->assertEquals('stupid values rm3', $rel[0]->dummy);
        $tm1bis = $rm2->get_testmodel_1();
        $this->assertEquals('title tm1', $tm1bis->title);
    }
 
    public function testRelatedToNotCreatedTestModel()
    {
        $db = Pluf::db();
        $schema = Pluf::factory('Pluf_DB_Schema', $db);
        $m2 = new ManyToManyTwo();
        $schema->model = $m2;
        $this->assertEquals(true, $schema->dropTables());
        $this->assertEquals(true, $schema->createTables());
        $m2->two = 'two is the best';
        $rel = $m2->get_manytomanyone_list();
        $this->assertNotEquals(false, $rel);
        $this->assertEquals(0, count($rel));
        $this->assertEquals(true, $schema->dropTables());
    }
 
    /**
     * Create the tables and test if association is working.
     */
    public function testManyToManyModels()
    {
        $db = Pluf::db();
        $schema = Pluf::factory('Pluf_DB_Schema', $db);
        $m1 = new ManyToManyOne();
        $schema->model = $m1;
        $this->assertEquals(true, $schema->dropTables());
        $this->assertEquals(true, $schema->createTables());
        $m1->one = 'one is the best';
        $this->assertEquals(true, $m1->create());
        $this->assertEquals(1, $m1->id);
        $m2 = new ManyToManyTwo();
        $schema->model = $m2;
        $this->assertEquals(true, $schema->dropTables());
        $this->assertEquals(true, $schema->createTables());
        $m2->two = 'two is the best';
        $this->assertEquals(true, $m2->create());
        $m1->setAssoc($m2);
        $m3 = new ManyToManyTwo();
        $m3->two = 'two bis is the best';
        $this->assertEquals(true, $m3->create());
        $m1->setAssoc($m3);
        $rel = $m1->get_two_list();
        $this->assertEquals(2, count($rel));
        $this->assertEquals('two is the best', $rel[0]->two);
        $this->assertEquals('two bis is the best', $rel[1]->two);
        //Has the many to many relationship is set on ManyToManyOne
        //ManyToManyTwo is accessing the other model through the
        //get_nameofthemodelclass_list syntax
        $rel = $m2->get_manytomanyone_list();
        $this->assertEquals(1, count($rel));
        $this->assertEquals('one is the best', $rel[0]->one);
        $m1->delAssoc($m3);
        $rel = $m1->get_two_list();
        $this->assertEquals(1, count($rel));
    }
 
    public function testExceptionOnProperty()
    {
        $model = new TestModel();
        $model->title = 'title';
        $model->description = 'A small desc ';
        $this->assertEquals(true, $model->create());
        try {
            $rel = $model->should_fail;
            // next line should not be called
            $this->assertEquals(true, false);
        } catch (Exception $e) {
            $this->assertEquals('Cannot get property "should_fail".',
                                $e->getMessage());
        }
    }
 
 
}
 
?>

Archive Download this file

Branches

Tags

Number of commits:
Page rendered in 0.09317s using 11 queries.