Skip to content

Commit

Permalink
Extracing a hard coded id into an static variable for the Table test
Browse files Browse the repository at this point in the history
case
  • Loading branch information
lorenzo committed Oct 28, 2013
1 parent 1df203e commit 9da6404
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -39,6 +39,14 @@ class TableTest extends \Cake\TestSuite\TestCase {
'core.tag', 'core.articles_tag'
];

/**
* Handy variable containing the next primary key that will be inserted in the
* users table
*
* @var integer
*/
public static $nextUserId = 5;

public function setUp() {
parent::setUp();
$this->connection = ConnectionManager::get('test');
Expand Down Expand Up @@ -963,9 +971,9 @@ public function testSaveNewEntity() {
]);
$table = TableRegistry::get('users');
$this->assertSame($entity, $table->save($entity));
$this->assertEquals($entity->id, 5);
$this->assertEquals($entity->id, self::$nextUserId);

$row = $table->find('all')->where(['id' => 5])->first();
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$this->assertEquals($entity->toArray(), $row->toArray());
}

Expand All @@ -985,9 +993,9 @@ public function testSaveEntityOnlySchemaFields() {
]);
$table = TableRegistry::get('users');
$this->assertSame($entity, $table->save($entity));
$this->assertEquals($entity->id, 5);
$this->assertEquals($entity->id, self::$nextUserId);

$row = $table->find('all')->where(['id' => 5])->first();
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$entity->unsetProperty('crazyness');
$this->assertEquals($entity->toArray(), $row->toArray());
}
Expand All @@ -1008,9 +1016,9 @@ public function testSaveWithFieldList() {
$table = TableRegistry::get('users');
$fieldList = ['fieldList' => ['username', 'created', 'updated']];
$this->assertSame($entity, $table->save($entity, $fieldList));
$this->assertEquals($entity->id, 5);
$this->assertEquals($entity->id, self::$nextUserId);

$row = $table->find('all')->where(['id' => 5])->first();
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$entity->set('password', null);
$this->assertEquals($entity->toArray(), $row->toArray());
}
Expand All @@ -1033,8 +1041,8 @@ public function testBeforeSaveModifyData() {
};
$table->getEventManager()->attach($listener, 'Model.beforeSave');
$this->assertSame($data, $table->save($data));
$this->assertEquals($data->id, 5);
$row = $table->find('all')->where(['id' => 5])->first();
$this->assertEquals($data->id, self::$nextUserId);
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$this->assertEquals('foo', $row->get('password'));
}

Expand All @@ -1060,9 +1068,9 @@ public function testBeforeSaveModifyOptions() {
$table->getEventManager()->attach($listener1, 'Model.beforeSave');
$table->getEventManager()->attach($listener2, 'Model.beforeSave');
$this->assertSame($data, $table->save($data));
$this->assertEquals($data->id, 5);
$this->assertEquals($data->id, self::$nextUserId);

$row = $table->find('all')->where(['id' => 5])->first();
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$data->set('username', null);
$data->set('password', null);
$this->assertEquals($data->toArray(), $row->toArray());
Expand All @@ -1088,7 +1096,7 @@ public function testBeforeSaveStopEvent() {
$table->getEventManager()->attach($listener, 'Model.beforeSave');
$this->assertSame($data, $table->save($data));
$this->assertNull($data->id);
$row = $table->find('all')->where(['id' => 5])->first();
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$this->assertNull($row);
}

Expand All @@ -1112,7 +1120,7 @@ public function testAfterSave() {
};
$table->getEventManager()->attach($listener, 'Model.afterSave');
$this->assertSame($data, $table->save($data));
$this->assertEquals($data->id, 5);
$this->assertEquals($data->id, self::$nextUserId);
$this->assertTrue($called);
}

Expand Down Expand Up @@ -1298,9 +1306,9 @@ public function testSaveOnlyDirtyProperties() {

$table = TableRegistry::get('users');
$this->assertSame($entity, $table->save($entity));
$this->assertEquals($entity->id, 5);
$this->assertEquals($entity->id, self::$nextUserId);

$row = $table->find('all')->where(['id' => 5])->first();
$row = $table->find('all')->where(['id' => self::$nextUserId])->first();
$entity->set('password', null);
$this->assertEquals($entity->toArray(), $row->toArray());
}
Expand Down

0 comments on commit 9da6404

Please sign in to comment.