PageRenderTime 88ms CodeModel.GetById 50ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/test/testsuite/generator/behavior/TimestampableBehaviorTest.php

http://github.com/propelorm/Propel
PHP | 259 lines | 187 code | 42 blank | 30 comment | 2 complexity | caa61b1623ce7f02bda95781d6c3c3bb MD5 | raw file
  1<?php
  2
  3/*
  4 * This file is part of the Propel package.
  5 * For the full copyright and license information, please view the LICENSE
  6 * file that was distributed with this source code.
  7 *
  8 * @license    MIT License
  9 */
 10
 11require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
 12require_once dirname(__FILE__) . '/../../../../generator/lib/config/GeneratorConfig.php';
 13require_once dirname(__FILE__) . '/../../../../generator/lib/model/Behavior.php';
 14require_once dirname(__FILE__) . '/../../../../generator/lib/behavior/TimestampableBehavior.php';
 15require_once dirname(__FILE__) . '/../../../../generator/lib/util/PropelQuickBuilder.php';
 16
 17/**
 18 * Tests for TimestampableBehavior class
 19 *
 20 * @author     Fran├žois Zaninotto
 21 * @version    $Revision$
 22 * @package    generator.behavior
 23 */
 24class TimestampableBehaviorTest extends BookstoreTestBase
 25{
 26    public function testParameters()
 27    {
 28        $table2 = Table2Peer::getTableMap();
 29        $this->assertEquals(count($table2->getColumns()), 4, 'Timestampable adds two columns by default');
 30        $this->assertTrue(method_exists('Table2', 'getCreatedAt'), 'Timestamplable adds a created_at column by default');
 31        $this->assertTrue(method_exists('Table2', 'getUpdatedAt'), 'Timestamplable adds an updated_at column by default');
 32        $table1 = Table1Peer::getTableMap();
 33        $this->assertEquals(count($table1->getColumns()), 4, 'Timestampable does not add two columns when they already exist');
 34        $this->assertTrue(method_exists('Table1', 'getCreatedOn'), 'Timestamplable allows customization of create_column name');
 35        $this->assertTrue(method_exists('Table1', 'getUpdatedOn'), 'Timestamplable allows customization of update_column name');
 36    }
 37
 38    public function testPreSave()
 39    {
 40        $t1 = new Table2();
 41        $this->assertNull($t1->getUpdatedAt());
 42
 43        $tsave = time();
 44        $t1->save();
 45
 46        // Do not use assertEquals or similar, the second may have just changed!
 47        // If both are integers, the update_at has been set.
 48        $this->assertGreaterThanOrEqual($tsave, $t1->getUpdatedAt('U'), 'Timestampable sets updated_column to time() on creation');
 49
 50        // Ensure the timestamp will change.
 51        sleep(1);
 52
 53        $t1->setTitle('foo');
 54        $t1->save();
 55
 56        // Do not use assertEquals or similar, time goes by, here too!
 57        $this->assertGreaterThan($tsave, $t1->getUpdatedAt('U'), 'Timestampable changes updated_column to time() on update');
 58    }
 59
 60    public function testPreSaveNoChange()
 61    {
 62        $t1 = new Table2();
 63        $t1->save();
 64
 65        $date = $t1->getUpdatedAt('U');
 66
 67        // Ensure the timestamp will change.
 68        sleep(1);
 69
 70        $t1->save();
 71
 72        $this->assertEquals($date, $t1->getUpdatedAt('U'), 'Timestampable only changes updated_column if the object was modified');
 73    }
 74
 75    public function testPreSaveManuallyUpdated()
 76    {
 77        $t1 = new Table2();
 78        $t1->setUpdatedAt(time() - 10);
 79        $tsave = time();
 80        $t1->save();
 81        $this->assertNotEquals($t1->getUpdatedAt('U'), $tsave, 'Timestampable does not set updated_column to time() on creation when it is set by the user');
 82        // tip: if I set it to time()-10 a second time, the object sees that I want to change it to the same value
 83        // and skips the update, therefore the updated_at is not in the list of modified columns,
 84        // and the behavior changes it to the current date... let's say it's an edge case
 85        $t1->setUpdatedAt(time() - 15);
 86        $tupdate = time();
 87        $t1->save();
 88        $this->assertNotEquals($t1->getUpdatedAt('U'), $tupdate, 'Timestampable does not change updated_column to time() on update when it is set by the user');
 89    }
 90
 91    public function testPreInsert()
 92    {
 93        $t1 = new Table2();
 94        $this->assertNull($t1->getCreatedAt());
 95
 96        $tsave = time();
 97        $t1->save();
 98
 99        // Do not use assertEquals or similar, the second may have just changed!
100        // If both are integers, the created_at has been set.
101        $this->assertGreaterThanOrEqual($tsave, $t1->getCreatedAt('U'), 'Timestampable sets created_column to time() on creation');
102
103        $tcreated = $t1->getCreatedAt('U');
104
105        // Ensure the timestamp will change.
106        sleep(1);
107
108        $t1->setTitle('foo');
109        $t1->save();
110
111        $this->assertEquals($tcreated, $t1->getCreatedAt('U'), 'Timestampable does not update created_column on update');
112    }
113
114    public function testPreInsertManuallyUpdated()
115    {
116        $tcreated = time() - 10;
117
118        $t1 = new Table2();
119        $t1->setCreatedAt($tcreated);
120        $t1->save();
121
122        $this->assertEquals($tcreated, $t1->getCreatedAt('U'), 'Timestampable does not set created_column to time() on creation when it is set by the user');
123    }
124
125    /**
126     * @depends testPreSave
127     * @depends testPreSaveManuallyUpdated
128     */
129    public function testObjectKeepUpdateDateUnchanged()
130    {
131        $tsave = time() - 10;
132
133        $t1 = new Table2();
134        $t1->setUpdatedAt($tsave);
135        $t1->save();
136
137        $t1->keepUpdateDateUnchanged();
138        $t1->setTitle('foo');
139        $t1->save();
140
141        $this->assertEquals($tsave, $t1->getUpdatedAt('U'), 'keepUpdateDateUnchanged() prevents the behavior from updating the update date');
142    }
143
144    protected function populateUpdatedAt()
145    {
146        Table2Query::create()->deleteAll();
147        $ts = new PropelObjectCollection();
148        $ts->setModel('Table2');
149        for ($i=0; $i < 10; $i++) {
150            $t = new Table2();
151            $t->setTitle('UpdatedAt' . $i);
152            /* additional -30 in case the check is done in the same second (which we can't guarantee, so no assert(8 ...) below).*/
153            $t->setUpdatedAt(time() - $i * 24 * 60 * 60 - 30);
154            $ts[]= $t;
155        }
156        $ts->save();
157    }
158
159    protected function populateCreatedAt()
160    {
161        Table2Query::create()->deleteAll();
162        $ts = new PropelObjectCollection();
163        $ts->setModel('Table2');
164        for ($i=0; $i < 10; $i++) {
165            $t = new Table2();
166            $t->setTitle('CreatedAt' . $i);
167            $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
168            $ts[]= $t;
169        }
170        $ts->save();
171    }
172
173    public function testQueryRecentlyUpdated()
174    {
175        $q = Table2Query::create()->recentlyUpdated();
176        $this->assertTrue($q instanceof Table2Query, 'recentlyUpdated() returns the current Query object');
177        $this->populateUpdatedAt();
178        $ts = Table2Query::create()->recentlyUpdated()->count();
179        $this->assertEquals(7, $ts, 'recentlyUpdated() returns the elements updated in the last 7 days by default');
180        $ts = Table2Query::create()->recentlyUpdated(5)->count();
181        $this->assertEquals(5, $ts, 'recentlyUpdated() accepts a number of days as parameter');
182    }
183
184    public function testQueryRecentlyCreated()
185    {
186        $q = Table2Query::create()->recentlyCreated();
187        $this->assertTrue($q instanceof Table2Query, 'recentlyCreated() returns the current Query object');
188        $this->populateCreatedAt();
189        $ts = Table2Query::create()->recentlyCreated()->count();
190        $this->assertEquals(7, $ts, 'recentlyCreated() returns the elements created in the last 7 days by default');
191        $ts = Table2Query::create()->recentlyCreated(5)->count();
192        $this->assertEquals(5, $ts, 'recentlyCreated() accepts a number of days as parameter');
193    }
194
195    public function testQueryLastUpdatedFirst()
196    {
197        $q = Table2Query::create()->lastUpdatedFirst();
198        $this->assertTrue($q instanceof Table2Query, 'lastUpdatedFirst() returns the current Query object');
199        $this->populateUpdatedAt();
200        $t = Table2Query::create()->lastUpdatedFirst()->findOne();
201        $this->assertEquals('UpdatedAt0', $t->getTitle(), 'lastUpdatedFirst() returns element with most recent update date first');
202    }
203
204    public function testQueryFirstUpdatedFirst()
205    {
206        $q = Table2Query::create()->firstUpdatedFirst();
207        $this->assertTrue($q instanceof Table2Query, 'firstUpdatedFirst() returns the current Query object');
208        $this->populateUpdatedAt();
209        $t = Table2Query::create()->firstUpdatedFirst()->findOne();
210        $this->assertEquals('UpdatedAt9', $t->getTitle(), 'firstUpdatedFirst() returns the element with oldest updated date first');
211    }
212
213    public function testQueryLastCreatedFirst()
214    {
215        $q = Table2Query::create()->lastCreatedFirst();
216        $this->assertTrue($q instanceof Table2Query, 'lastCreatedFirst() returns the current Query object');
217        $this->populateCreatedAt();
218        $t = Table2Query::create()->lastCreatedFirst()->findOne();
219        $this->assertEquals('CreatedAt0', $t->getTitle(), 'lastCreatedFirst() returns element with most recent create date first');
220    }
221
222    public function testQueryFirstCreatedFirst()
223    {
224        $q = Table2Query::create()->firstCreatedFirst();
225        $this->assertTrue($q instanceof Table2Query, 'firstCreatedFirst() returns the current Query object');
226        $this->populateCreatedAt();
227        $t = Table2Query::create()->firstCreatedFirst()->findOne();
228        $this->assertEquals('CreatedAt9', $t->getTitle(), 'firstCreatedFirst() returns the element with oldest create date first');
229    }
230
231    public function testDisableUpdatedAt()
232    {
233        $schema = <<<EOF
234<database name="timestampable_database">
235    <table name="table_without_updated_at">
236        <column name="id" type="INTEGER" primaryKey="true" />
237
238        <behavior name="timestampable">
239            <parameter name="disable_updated_at" value="true" />
240        </behavior>
241    </table>
242</database>
243EOF;
244
245        $builder = new PropelQuickBuilder();
246        $builder->setSchema($schema);
247        $builder->build();
248
249        $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'getCreatedAt'));
250        $this->assertTrue(method_exists('TableWithoutUpdatedAt', 'setCreatedAt'));
251        $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'getUpdatedAt'));
252        $this->assertFalse(method_exists('TableWithoutUpdatedAt', 'setUpdatedAt'));
253
254        $obj = new TableWithoutUpdatedAt();
255        $this->assertNull($obj->getCreatedAt());
256        $this->assertEquals(1, $obj->save());
257        $this->assertNotNull($obj->getCreatedAt());
258    }
259}