From 9da6404ded0e9f3f452eb91d534702ea92c73a2f Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 28 Oct 2013 10:53:54 +0100 Subject: [PATCH] Extracing a hard coded id into an static variable for the Table test case --- Cake/Test/TestCase/ORM/TableTest.php | 36 +++++++++++++++++----------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/Cake/Test/TestCase/ORM/TableTest.php b/Cake/Test/TestCase/ORM/TableTest.php index 27edd2ff3b2..b606ba73d6d 100644 --- a/Cake/Test/TestCase/ORM/TableTest.php +++ b/Cake/Test/TestCase/ORM/TableTest.php @@ -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'); @@ -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()); } @@ -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()); } @@ -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()); } @@ -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')); } @@ -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()); @@ -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); } @@ -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); } @@ -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()); }