Skip to content

Commit

Permalink
Fix TimestampBehaviorTest with real table instance
Browse files Browse the repository at this point in the history
  • Loading branch information
o0h committed Dec 31, 2017
1 parent 896c6f2 commit 47db642
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\I18n\Time;
use Cake\ORM\Behavior\TimestampBehavior;
use Cake\ORM\Entity;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -88,7 +89,7 @@ public function testImplementedEventsCustom()
*/
public function testCreatedAbsent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -110,7 +111,7 @@ public function testCreatedAbsent()
*/
public function testCreatedPresent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -132,7 +133,7 @@ public function testCreatedPresent()
*/
public function testCreatedNotNew()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -154,7 +155,7 @@ public function testCreatedNotNew()
*/
public function testModifiedAbsent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -177,7 +178,7 @@ public function testModifiedAbsent()
*/
public function testModifiedPresent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -204,7 +205,7 @@ public function testInvalidEventConfig()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('When should be one of "always", "new" or "existing". The passed value "fat fingers" is invalid');
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$settings = ['events' => ['Model.beforeSave' => ['created' => 'fat fingers']]];
$this->Behavior = new TimestampBehavior($table, $settings);

Expand All @@ -220,7 +221,7 @@ public function testInvalidEventConfig()
*/
public function testGetTimestamp()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$behavior = new TimestampBehavior($table);

$return = $behavior->timestamp();
Expand All @@ -241,7 +242,7 @@ public function testGetTimestamp()
*/
public function testGetTimestampPersists()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$behavior = new TimestampBehavior($table);

$initialValue = $behavior->timestamp();
Expand All @@ -261,7 +262,7 @@ public function testGetTimestampPersists()
*/
public function testGetTimestampRefreshes()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$behavior = new TimestampBehavior($table);

$initialValue = $behavior->timestamp();
Expand All @@ -281,7 +282,7 @@ public function testGetTimestampRefreshes()
*/
public function testSetTimestampExplicit()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);

$ts = new \DateTime();
Expand All @@ -302,7 +303,7 @@ public function testSetTimestampExplicit()
*/
public function testTouch()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$ts = new \DateTime('2000-01-01');
$this->Behavior->timestamp($ts);
Expand All @@ -325,7 +326,7 @@ public function testTouch()
*/
public function testTouchNoop()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$config = [
'events' => [
'Model.beforeSave' => [
Expand All @@ -352,7 +353,7 @@ public function testTouchNoop()
*/
public function testTouchCustomEvent()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$table = $this->getTable();
$settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]];
$this->Behavior = new TimestampBehavior($table, $settings);
$ts = new \DateTime('2000-01-01');
Expand Down Expand Up @@ -398,4 +399,21 @@ public function testSaveTriggersInsert()
$this->assertEquals($now->toDateTimeString(), $row->created->toDateTimeString());
$this->assertEquals($now->toDateTimeString(), $row->updated->toDateTimeString());
}

/**
* Helper method to get Table instance with created/modified column
*
* @return \Cake\ORM\Table
*/
protected function getTable()
{
$schema = [
'created' => ['type' => 'datetime'],
'modified' => ['type' => 'timestamp'],
'date_specialed' => ['type' => 'datetime'],
];
$table = new Table(['schema' => $schema]);

return $table;
}
}

0 comments on commit 47db642

Please sign in to comment.