root/trunk/generator/test/classes/propel/FieldnameRelatedTest.php

Revision 1082, 10.3 kB (checked in by tony, 3 months ago)

Refs #683: Added support for PHP 5.3 namespaces

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1 <?php
2
3 use bookstore::Peer as PropelPeer;
4 use bookstore::Model as PropelModel;
5
6 /*
7  *  $Id$
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
13  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
16  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
17  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20  *
21  * This software consists of voluntary contributions made by many individuals
22  * and is licensed under version 3 of the LGPL. For more information please see
23  * <http://propel.phpdb.org>.
24  */
25
26 require_once 'PHPUnit/Framework/TestCase.php';
27 require_once 'bookstore/BookstoreTestBase.php';
28
29 /**
30  * Tests some of the methods of generated Object classes. These are:
31  *
32  * - Base[Object]Peer::getFieldNames()
33  * - Base[Object]Peer::translateFieldName()
34  * - ::BasePeer::getFieldNames()
35  * - ::BasePeer::translateFieldName()
36  * - Base[Object]::getByName()
37  * - Base[Object]::setByName()
38  * - Base[Object]::fromArray()
39  * - Base[Object]::toArray()
40  *
41  * I've pulled these tests from the GeneratedObjectTest because the don't
42  * need the BookstoreTestBase's setUp and tearDown (database de/population)
43  * behaviour. The tests will run faster this way.
44  *
45  * @author     Sven Fuchs <svenfuchs@artweb-design.de>
46  */
47 class FieldnameRelatedTest extends PHPUnit_Framework_TestCase {
48
49     /**
50      * Tests if fieldname type constants are defined
51      */
52     public function testFieldNameTypeConstants () {
53
54         $result = defined('::BasePeer::TYPE_PHPNAME');
55         $this->assertTrue($result);
56     }
57
58     /**
59      * Tests the Base[Object]Peer::getFieldNames() method
60      */
61     public function testGetFieldNames () {
62
63         $types = array(
64             ::BasePeer::TYPE_PHPNAME,
65             ::BasePeer::TYPE_COLNAME,
66             ::BasePeer::TYPE_FIELDNAME,
67             ::BasePeer::TYPE_NUM
68         );
69         $expecteds = array (
70             ::BasePeer::TYPE_PHPNAME => array(
71                 0 => 'Id',
72                 1 => 'Title',
73                 2 => 'ISBN',
74                 3 => 'Price',
75                 4 => 'PublisherId',
76                 5 => 'AuthorId'
77             ),
78             ::BasePeer::TYPE_STUDLYPHPNAME => array(
79                 0 => 'id',
80                 1 => 'title',
81                 2 => 'iSBN',
82                 3 => 'price',
83                 4 => 'publisherId',
84                 5 => 'authorId'
85             ),
86             ::BasePeer::TYPE_COLNAME => array(
87                 0 => 'book.ID',
88                 1 => 'book.TITLE',
89                 2 => 'book.ISBN',
90                 3 => 'book.PRICE',
91                 4 => 'book.PUBLISHER_ID',
92                 5 => 'book.AUTHOR_ID'
93             ),
94             ::BasePeer::TYPE_FIELDNAME => array(
95                 0 => 'id',
96                 1 => 'title',
97                 2 => 'isbn',
98                 3 => 'price',
99                 4 => 'publisher_id',
100                 5 => 'author_id'
101             ),
102             ::BasePeer::TYPE_NUM => array(
103                 0 => 0,
104                 1 => 1,
105                 2 => 2,
106                 3 => 3,
107                 4 => 4,
108                 5 => 5
109             )
110         );
111
112         foreach ($types as $type) {
113             $results[$type] = PropelPeer::BookPeer::getFieldnames($type);
114             $this->assertEquals(
115                 $expecteds[$type],
116                 $results[$type],
117                 'expected was: ' . print_r($expecteds[$type], 1) .
118                 'but getFieldnames() returned ' . print_r($results[$type], 1)
119             );
120         }
121     }
122
123     /**
124      * Tests the Base[Object]Peer::translateFieldName() method
125      */
126     public function testTranslateFieldName () {
127
128         $types = array(
129             ::BasePeer::TYPE_PHPNAME,
130             ::BasePeer::TYPE_STUDLYPHPNAME,
131             ::BasePeer::TYPE_COLNAME,
132             ::BasePeer::TYPE_FIELDNAME,
133             ::BasePeer::TYPE_NUM
134         );
135         $expecteds = array (
136             ::BasePeer::TYPE_PHPNAME => 'AuthorId',
137             ::BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
138             ::BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
139             ::BasePeer::TYPE_FIELDNAME => 'author_id',
140             ::BasePeer::TYPE_NUM => 5,
141         );
142         foreach ($types as $fromType) {
143             foreach ($types as $toType) {
144                 $name = $expecteds[$fromType];
145                 $expected = $expecteds[$toType];
146                 $result = PropelPeer::BookPeer::translateFieldName($name, $fromType, $toType);
147                 $this->assertEquals($expected, $result);
148             }
149         }
150     }
151
152     /**
153      * Tests the ::BasePeer::getFieldNames() method
154      */
155     public function testGetFieldNamesStatic () {
156
157         $types = array(
158             ::BasePeer::TYPE_PHPNAME,
159             ::BasePeer::TYPE_STUDLYPHPNAME,
160             ::BasePeer::TYPE_COLNAME,
161             ::BasePeer::TYPE_FIELDNAME,
162             ::BasePeer::TYPE_NUM
163         );
164         $expecteds = array (
165             ::BasePeer::TYPE_PHPNAME => array(
166                 0 => 'Id',
167                 1 => 'Title',
168                 2 => 'ISBN',
169                 3 => 'Price',
170                 4 => 'PublisherId',
171                 5 => 'AuthorId'
172             ),
173             ::BasePeer::TYPE_STUDLYPHPNAME => array(
174                 0 => 'id',
175                 1 => 'title',
176                 2 => 'iSBN',
177                 3 => 'price',
178                 4 => 'publisherId',
179                 5 => 'authorId'
180             ),
181             ::BasePeer::TYPE_COLNAME => array(
182                 0 => 'book.ID',
183                 1 => 'book.TITLE',
184                 2 => 'book.ISBN',
185                 3 => 'book.PRICE',
186                 4 => 'book.PUBLISHER_ID',
187                 5 => 'book.AUTHOR_ID'
188             ),
189             ::BasePeer::TYPE_FIELDNAME => array(
190                 0 => 'id',
191                 1 => 'title',
192                 2 => 'isbn',
193                 3 => 'price',
194                 4 => 'publisher_id',
195                 5 => 'author_id'
196             ),
197             ::BasePeer::TYPE_NUM => array(
198                 0 => 0,
199                 1 => 1,
200                 2 => 2,
201                 3 => 3,
202                 4 => 4,
203                 5 => 5
204             )
205         );
206
207         foreach ($types as $type) {
208             $results[$type] = ::BasePeer::getFieldnames('Book', $type);
209             $this->assertEquals(
210                 $expecteds[$type],
211                 $results[$type],
212                 'expected was: ' . print_r($expecteds[$type], 1) .
213                 'but getFieldnames() returned ' . print_r($results[$type], 1)
214             );
215         }
216     }
217
218     /**
219      * Tests the ::BasePeer::translateFieldName() method
220      */
221     public function testTranslateFieldNameStatic () {
222
223         $types = array(
224             ::BasePeer::TYPE_PHPNAME,
225             ::BasePeer::TYPE_STUDLYPHPNAME,
226             ::BasePeer::TYPE_COLNAME,
227             ::BasePeer::TYPE_FIELDNAME,
228             ::BasePeer::TYPE_NUM
229         );
230         $expecteds = array (
231             ::BasePeer::TYPE_PHPNAME => 'AuthorId',
232             ::BasePeer::TYPE_STUDLYPHPNAME => 'authorId',
233             ::BasePeer::TYPE_COLNAME => 'book.AUTHOR_ID',
234             ::BasePeer::TYPE_FIELDNAME => 'author_id',
235             ::BasePeer::TYPE_NUM => 5,
236         );
237         foreach ($types as $fromType) {
238             foreach ($types as $toType) {
239                 $name = $expecteds[$fromType];
240                 $expected = $expecteds[$toType];
241                 $result = ::BasePeer::translateFieldName('Book', $name, $fromType, $toType);
242                 $this->assertEquals($expected, $result);
243             }
244         }
245     }
246
247     /**
248      * Tests the Base[Object]::getByName() method
249      */
250     public function testGetByName() {
251
252         $types = array(
253             ::BasePeer::TYPE_PHPNAME => 'Title',
254             ::BasePeer::TYPE_STUDLYPHPNAME => 'title',
255             ::BasePeer::TYPE_COLNAME => 'book.TITLE',
256             ::BasePeer::TYPE_FIELDNAME => 'title',
257             ::BasePeer::TYPE_NUM => 1
258         );
259
260         $book = new PropelModel::Book();
261         $book->setTitle('Harry Potter and the Order of the Phoenix');
262
263         $expected = 'Harry Potter and the Order of the Phoenix';
264         foreach ($types as $type => $name) {
265             $result = $book->getByName($name, $type);
266             $this->assertEquals($expected, $result);
267         }
268     }
269
270     /**
271      * Tests the Base[Object]::setByName() method
272      */
273     public function testSetByName() {
274
275         $book = new PropelModel::Book();
276         $types = array(
277             ::BasePeer::TYPE_PHPNAME => 'Title',
278             ::BasePeer::TYPE_STUDLYPHPNAME => 'title',
279             ::BasePeer::TYPE_COLNAME => 'book.TITLE',
280             ::BasePeer::TYPE_FIELDNAME => 'title',
281             ::BasePeer::TYPE_NUM => 1
282         );
283
284         $title = 'Harry Potter and the Order of the Phoenix';
285         foreach ($types as $type => $name) {
286             $book->setByName($name, $title, $type);
287             $result = $book->getTitle();
288             $this->assertEquals($title, $result);
289         }
290     }
291
292     /**
293      * Tests the Base[Object]::fromArray() method
294      *
295      * this also tests populateFromArray() because that's an alias
296      */
297     public function testFromArray(){
298
299         $types = array(
300             ::BasePeer::TYPE_PHPNAME,
301             ::BasePeer::TYPE_STUDLYPHPNAME,
302             ::BasePeer::TYPE_COLNAME,
303             ::BasePeer::TYPE_FIELDNAME,
304             ::BasePeer::TYPE_NUM
305         );
306         $expecteds = array (
307             ::BasePeer::TYPE_PHPNAME => array (
308                 'Title' => 'Harry Potter and the Order of the Phoenix',
309                 'ISBN' => '043935806X'
310             ),
311             ::BasePeer::TYPE_STUDLYPHPNAME => array (
312                 'title' => 'Harry Potter and the Order of the Phoenix',
313                 'iSBN' => '043935806X'
314             ),
315             ::BasePeer::TYPE_COLNAME => array (
316                 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
317                 'book.ISBN' => '043935806X'
318             ),
319             ::BasePeer::TYPE_FIELDNAME => array (
320                 'title' => 'Harry Potter and the Order of the Phoenix',
321                 'isbn' => '043935806X'
322             ),
323             ::BasePeer::TYPE_NUM => array (
324                 '1' => 'Harry Potter and the Order of the Phoenix',
325                 '2' => '043935806X'
326             )
327         );
328
329         $book = new PropelModel::Book();
330
331         foreach ($types as $type) {
332             $expected = $expecteds[$type];
333             $book->fromArray($expected, $type);
334             $result = array();
335             foreach (array_keys($expected) as $key) {
336                 $result[$key] = $book->getByName($key, $type);
337             }
338             $this->assertEquals(
339                 $expected,
340                 $result,
341                 'expected was: ' . print_r($expected, 1) .
342                 'but fromArray() returned ' . print_r($result, 1)
343             );
344         }
345     }
346
347     /**
348      * Tests the Base[Object]::toArray() method
349      */
350     public function testToArray(){
351
352         $types = array(
353             ::BasePeer::TYPE_PHPNAME,
354             ::BasePeer::TYPE_STUDLYPHPNAME,
355             ::BasePeer::TYPE_COLNAME,
356             ::BasePeer::TYPE_FIELDNAME,
357             ::BasePeer::TYPE_NUM
358         );
359
360         $book = new PropelModel::Book();
361         $book->fromArray(array (
362             'Title' => 'Harry Potter and the Order of the Phoenix',
363             'ISBN' => '043935806X'
364         ));
365
366         $expecteds = array (
367             ::BasePeer::TYPE_PHPNAME => array (
368                 'Title' => 'Harry Potter and the Order of the Phoenix',
369                 'ISBN' => '043935806X'
370             ),
371             ::BasePeer::TYPE_STUDLYPHPNAME => array (
372                 'title' => 'Harry Potter and the Order of the Phoenix',
373                 'iSBN' => '043935806X'
374             ),
375             ::BasePeer::TYPE_COLNAME => array (
376                 'book.TITLE' => 'Harry Potter and the Order of the Phoenix',
377                 'book.ISBN' => '043935806X'
378             ),
379             ::BasePeer::TYPE_FIELDNAME => array (
380                 'title' => 'Harry Potter and the Order of the Phoenix',
381                 'isbn' => '043935806X'
382             ),
383             ::BasePeer::TYPE_NUM => array (
384                 '1' => 'Harry Potter and the Order of the Phoenix',
385                 '2' => '043935806X'
386             )
387         );
388
389         foreach ($types as $type) {
390             $expected = $expecteds[$type];
391             $result = $book->toArray($type);
392             // remove ID since its autoincremented at each test iteration
393             $result = array_slice($result, 1, 2, true);
394             $this->assertEquals(
395                 $expected,
396                 $result,
397                 'expected was: ' . print_r($expected, 1) .
398                 'but toArray() returned ' . print_r($result, 1)
399             );
400         }
401     }
402 }
403
404 ?>
Note: See TracBrowser for help on using the browser.