From 1312bde85ea4802a1b48d0ea712a9a8444dd1f0e Mon Sep 17 00:00:00 2001 From: AD7six Date: Mon, 4 Nov 2013 16:00:55 +0000 Subject: [PATCH] add an integration test --- .../Model/Behavior/TimestampBehaviorTest.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php b/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php index fd56bf5cc44..102669effe4 100644 --- a/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php +++ b/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php @@ -17,6 +17,7 @@ use Cake\Event\Event; use Cake\Model\Behavior\TimestampBehavior; use Cake\ORM\Entity; +use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; /** @@ -24,6 +25,25 @@ */ class TimestampBehaviorTest extends TestCase { +/** + * autoFixtures + * + * Don't load fixtures for all tests + * + * @var bool + */ + public $autoFixtures = false; + +/** + * fixtures + * + * @var array + */ + public $fixtures = [ + 'core.user' + ]; + + /** * Sanity check Implemented events * @@ -239,4 +259,39 @@ public function testSetTimestampExplicit() { 'Should return the same value as initially set' ); } + +/** + * Test that calling save, triggers an insert including the created and updated field values + * + * @return void + */ + public function testSaveTriggersInsert() { + $this->loadFixtures('User'); //@TODO make fixtures more consistent/easier to use + + $table = TableRegistry::get('users'); + $table->addBehavior('Timestamp', [ + 'events' => [ + 'Model.beforeSave' => [ + 'created' => 'new', + 'updated' => 'always' + ] + ] + ]); + + $entity = new Entity([ + 'username' => 'timestamp', + ]); + $return = $table->save($entity); + $this->assertSame($return, $entity); + + $row = $table->find('all')->where(['id' => $entity->id])->first(); + + $now = time(); + + $storedValue = $row->created->getTimestamp(); + $this->assertLessThan(3, abs($storedValue - $now), "The stored created timestamp is expected to within 3 seconds of the current timestamp"); + + $storedValue = $row->updated->getTimestamp(); + $this->assertLessThan(3, abs($storedValue - $now), "The stored updated timestamp is expected to within 3 seconds of the current timestamp"); + } }