|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
| 4 | + * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 5 | + * |
| 6 | + * Licensed under The MIT License |
| 7 | + * For full copyright and license information, please see the LICENSE.txt |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
| 11 | + * @link http://cakephp.org CakePHP(tm) Project |
| 12 | + * @since CakePHP(tm) v 3.0.0 |
| 13 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
| 14 | + */ |
| 15 | +namespace Cake\Test\TestCase\Model\Behavior; |
| 16 | + |
| 17 | +use Cake\Event\Event; |
| 18 | +use Cake\Model\Behavior\TimestampBehavior; |
| 19 | +use Cake\ORM\Entity; |
| 20 | +use Cake\TestSuite\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Behavior test case |
| 24 | + */ |
| 25 | +class TimestampBehaviorTest extends TestCase { |
| 26 | + |
| 27 | +/** |
| 28 | + * Sanity check Implemented events |
| 29 | + * |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function testImplementedEvents() { |
| 33 | + $table = $this->getMock('Cake\ORM\Table'); |
| 34 | + $this->Behavior = new TimestampBehavior($table); |
| 35 | + |
| 36 | + $expected = [ |
| 37 | + 'Model.beforeSave' => 'beforeSave' |
| 38 | + ]; |
| 39 | + $this->assertEquals($expected, $this->Behavior->implementedEvents()); |
| 40 | + } |
| 41 | + |
| 42 | +/** |
| 43 | + * testCreatedAbsent |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + public function testCreatedAbsent() { |
| 48 | + $table = $this->getMock('Cake\ORM\Table'); |
| 49 | + $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); |
| 50 | + $ts = new \DateTime('2000-01-01'); |
| 51 | + $this->Behavior->setTimestamp($ts); |
| 52 | + |
| 53 | + $event = new Event('Model.beforeSave'); |
| 54 | + $entity = new Entity(['name' => 'Foo']); |
| 55 | + |
| 56 | + $return = $this->Behavior->beforeSave($event, $entity); |
| 57 | + $this->assertTrue($return, 'Handle Event is expected to always return true'); |
| 58 | + $this->assertSame($ts, $entity->created, 'Created timestamp is expected to be the mocked value'); |
| 59 | + } |
| 60 | + |
| 61 | +/** |
| 62 | + * testCreatedPresent |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public function testCreatedPresent() { |
| 67 | + $table = $this->getMock('Cake\ORM\Table'); |
| 68 | + $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); |
| 69 | + $ts = new \DateTime('2000-01-01'); |
| 70 | + $this->Behavior->setTimestamp($ts); |
| 71 | + |
| 72 | + $event = new Event('Model.beforeSave'); |
| 73 | + $existingValue = new \DateTime('2011-11-11'); |
| 74 | + $entity = new Entity(['name' => 'Foo', 'created' => $existingValue]); |
| 75 | + |
| 76 | + $return = $this->Behavior->beforeSave($event, $entity); |
| 77 | + $this->assertTrue($return, 'Handle Event is expected to always return true'); |
| 78 | + $this->assertSame($existingValue, $entity->created, 'Created timestamp is expected to be unchanged'); |
| 79 | + } |
| 80 | + |
| 81 | +/** |
| 82 | + * testCreatedNotNew |
| 83 | + * |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function testCreatedNotNew() { |
| 87 | + $table = $this->getMock('Cake\ORM\Table'); |
| 88 | + $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); |
| 89 | + $ts = new \DateTime('2000-01-01'); |
| 90 | + $this->Behavior->setTimestamp($ts); |
| 91 | + |
| 92 | + $event = new Event('Model.beforeSave'); |
| 93 | + $entity = new Entity(['name' => 'Foo']); |
| 94 | + $entity->isNew(false); |
| 95 | + |
| 96 | + $return = $this->Behavior->beforeSave($event, $entity); |
| 97 | + $this->assertTrue($return, 'Handle Event is expected to always return true'); |
| 98 | + $this->assertNull($entity->created, 'Created timestamp is expected to be untouched if the entity is not new'); |
| 99 | + } |
| 100 | + |
| 101 | +/** |
| 102 | + * testModifiedAbsent |
| 103 | + * |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + public function testModifiedAbsent() { |
| 107 | + $table = $this->getMock('Cake\ORM\Table'); |
| 108 | + $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); |
| 109 | + $ts = new \DateTime('2000-01-01'); |
| 110 | + $this->Behavior->setTimestamp($ts); |
| 111 | + |
| 112 | + $event = new Event('Model.beforeSave'); |
| 113 | + $entity = new Entity(['name' => 'Foo']); |
| 114 | + $entity->isNew(false); |
| 115 | + |
| 116 | + $return = $this->Behavior->beforeSave($event, $entity); |
| 117 | + $this->assertTrue($return, 'Handle Event is expected to always return true'); |
| 118 | + $this->assertSame($ts, $entity->modified, 'Modified timestamp is expected to be the mocked value'); |
| 119 | + } |
| 120 | + |
| 121 | +/** |
| 122 | + * testModifiedPresent |
| 123 | + * |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function testModifiedPresent() { |
| 127 | + $table = $this->getMock('Cake\ORM\Table'); |
| 128 | + $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); |
| 129 | + $ts = new \DateTime('2000-01-01'); |
| 130 | + $this->Behavior->setTimestamp($ts); |
| 131 | + |
| 132 | + $event = new Event('Model.beforeSave'); |
| 133 | + $existingValue = new \DateTime('2011-11-11'); |
| 134 | + $entity = new Entity(['name' => 'Foo', 'modified' => $existingValue]); |
| 135 | + $entity->clean(); |
| 136 | + $entity->isNew(false); |
| 137 | + |
| 138 | + $return = $this->Behavior->beforeSave($event, $entity); |
| 139 | + $this->assertTrue($return, 'Handle Event is expected to always return true'); |
| 140 | + $this->assertSame($ts, $entity->modified, 'Modified timestamp is expected to be updated'); |
| 141 | + } |
| 142 | + |
| 143 | +/** |
| 144 | + * testGetTimestamp |
| 145 | + * |
| 146 | + * @return void |
| 147 | + */ |
| 148 | + public function testGetTimestamp() { |
| 149 | + $table = $this->getMock('Cake\ORM\Table'); |
| 150 | + $this->Behavior = new TimestampBehavior($table); |
| 151 | + |
| 152 | + $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); |
| 153 | + $property->setAccessible(true); |
| 154 | + |
| 155 | + $this->assertNull($property->getValue($this->Behavior), 'Should be null be default'); |
| 156 | + |
| 157 | + $return = $this->Behavior->getTimestamp(); |
| 158 | + $this->assertInstanceOf( |
| 159 | + 'DateTime', |
| 160 | + $return, |
| 161 | + 'After calling for the first time, should be a date time object' |
| 162 | + ); |
| 163 | + |
| 164 | + return $this->Behavior; |
| 165 | + } |
| 166 | + |
| 167 | +/** |
| 168 | + * testGetTimestampPersists |
| 169 | + * |
| 170 | + * @depends testGetTimestamp |
| 171 | + * @return void |
| 172 | + */ |
| 173 | + public function testGetTimestampPersists($behavior) { |
| 174 | + $this->Behavior = $behavior; |
| 175 | + |
| 176 | + $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); |
| 177 | + $property->setAccessible(true); |
| 178 | + |
| 179 | + $initialValue = $property->getValue($this->Behavior); |
| 180 | + $this->Behavior->getTimestamp(); |
| 181 | + $postValue = $property->getValue($this->Behavior); |
| 182 | + |
| 183 | + $this->assertSame( |
| 184 | + $initialValue, |
| 185 | + $postValue, |
| 186 | + 'The timestamp should be exactly the same value' |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | +/** |
| 191 | + * testGetTimestampRefreshes |
| 192 | + * |
| 193 | + * @depends testGetTimestamp |
| 194 | + * @return void |
| 195 | + */ |
| 196 | + public function testGetTimestampRefreshes($behavior) { |
| 197 | + $this->Behavior = $behavior; |
| 198 | + |
| 199 | + $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); |
| 200 | + $property->setAccessible(true); |
| 201 | + |
| 202 | + $initialValue = $property->getValue($this->Behavior); |
| 203 | + $this->Behavior->getTimestamp(true); |
| 204 | + $postValue = $property->getValue($this->Behavior); |
| 205 | + |
| 206 | + $this->assertNotSame( |
| 207 | + $initialValue, |
| 208 | + $postValue, |
| 209 | + 'The timestamp should be a different object if refreshTimestamp is truthy' |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | +/** |
| 214 | + * testSetTimestampDefault |
| 215 | + * |
| 216 | + * @return void |
| 217 | + */ |
| 218 | + public function testSetTimestampDefault() { |
| 219 | + $table = $this->getMock('Cake\ORM\Table'); |
| 220 | + $this->Behavior = new TimestampBehavior($table); |
| 221 | + |
| 222 | + $this->Behavior->setTimestamp(); |
| 223 | + |
| 224 | + $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); |
| 225 | + $property->setAccessible(true); |
| 226 | + $set = $property->getValue($this->Behavior); |
| 227 | + |
| 228 | + $this->assertInstanceOf( |
| 229 | + 'DateTime', |
| 230 | + $set, |
| 231 | + 'After calling for the first time, should be a date time object' |
| 232 | + ); |
| 233 | + } |
| 234 | + |
| 235 | +/** |
| 236 | + * testSetTimestampExplicit |
| 237 | + * |
| 238 | + * @return void |
| 239 | + */ |
| 240 | + public function testSetTimestampExplicit() { |
| 241 | + $table = $this->getMock('Cake\ORM\Table'); |
| 242 | + $this->Behavior = new TimestampBehavior($table); |
| 243 | + |
| 244 | + $ts = new \DateTime(); |
| 245 | + $this->Behavior->setTimestamp($ts); |
| 246 | + |
| 247 | + $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); |
| 248 | + $property->setAccessible(true); |
| 249 | + $set = $property->getValue($this->Behavior); |
| 250 | + |
| 251 | + $this->assertInstanceOf( |
| 252 | + 'DateTime', |
| 253 | + $set, |
| 254 | + 'After calling for the first time, should be a date time object' |
| 255 | + ); |
| 256 | + |
| 257 | + $this->assertSame( |
| 258 | + $ts, |
| 259 | + $set, |
| 260 | + 'Should have set the same object passed in' |
| 261 | + ); |
| 262 | + } |
| 263 | +} |
0 commit comments