Skip to content

Commit

Permalink
Clean up Table test a bit.
Browse files Browse the repository at this point in the history
* Don't call methods for objects the test already has references to.
* Use an alias instead of a table name.
  • Loading branch information
markstory committed Nov 1, 2013
1 parent fd83f89 commit bdbf066
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -951,7 +951,7 @@ public function testFindPersistedEntities() {
* @return void
*/
public function testExists() {
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$this->assertTrue($table->exists(['id' => 1]));
$this->assertFalse($table->exists(['id' => 501]));
$this->assertTrue($table->exists(['id' => 3, 'username' => 'larry']));
Expand Down Expand Up @@ -1026,7 +1026,7 @@ public function testSaveNewEntity() {
'created' => new \DateTime('2013-10-10 00:00'),
'updated' => new \DateTime('2013-10-10 00:00')
]);
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$this->assertSame($entity, $table->save($entity));
$this->assertEquals($entity->id, self::$nextUserId);

Expand All @@ -1048,7 +1048,7 @@ public function testSaveEntityOnlySchemaFields() {
'created' => new \DateTime('2013-10-10 00:00'),
'updated' => new \DateTime('2013-10-10 00:00'),
]);
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$this->assertSame($entity, $table->save($entity));
$this->assertEquals($entity->id, self::$nextUserId);

Expand All @@ -1070,7 +1070,7 @@ public function testSaveWithFieldList() {
'created' => new \DateTime('2013-10-10 00:00'),
'updated' => new \DateTime('2013-10-10 00:00')
]);
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$fieldList = ['fieldList' => ['username', 'created', 'updated']];
$this->assertSame($entity, $table->save($entity, $fieldList));
$this->assertEquals($entity->id, self::$nextUserId);
Expand All @@ -1086,7 +1086,7 @@ public function testSaveWithFieldList() {
* @return void
*/
public function testBeforeSaveModifyData() {
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$data = new \Cake\ORM\Entity([
'username' => 'superuser',
'created' => new \DateTime('2013-10-10 00:00'),
Expand All @@ -1109,7 +1109,7 @@ public function testBeforeSaveModifyData() {
* @return void
*/
public function testBeforeSaveModifyOptions() {
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$data = new \Cake\ORM\Entity([
'username' => 'superuser',
'password' => 'foo',
Expand Down Expand Up @@ -1140,7 +1140,7 @@ public function testBeforeSaveModifyOptions() {
* @return void
*/
public function testBeforeSaveStopEvent() {
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$data = new \Cake\ORM\Entity([
'username' => 'superuser',
'created' => new \DateTime('2013-10-10 00:00'),
Expand All @@ -1163,7 +1163,7 @@ public function testBeforeSaveStopEvent() {
* @return void
*/
public function testAfterSave() {
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$data = new \Cake\ORM\Entity([
'username' => 'superuser',
'created' => new \DateTime('2013-10-10 00:00'),
Expand All @@ -1190,7 +1190,7 @@ public function testAfterSaveNotCalled() {
$table = $this->getMock(
'\Cake\ORM\Table',
['_buildQuery', 'exists'],
[['table' => 'users', 'connection' => ConnectionManager::get('test')]]
[['table' => 'users', 'connection' => $this->connection]]
);
$query = $this->getMock(
'\Cake\ORM\Query',
Expand Down Expand Up @@ -1229,12 +1229,15 @@ public function testAfterSaveNotCalled() {
* @return void
*/
public function testAtomicSave() {
$config = ConnectionManager::config('test');

$connection = $this->getMock(
'\Cake\Database\Connection',
['begin', 'commit'],
[ConnectionManager::config('test')]
[$config]
);
$connection->driver(ConnectionManager::get('test')->driver());
$connection->driver($this->connection->driver());

$table = $this->getMock('\Cake\ORM\Table', ['connection'], [['table' => 'users']]);
$table->expects($this->any())->method('connection')
->will($this->returnValue($connection));
Expand Down Expand Up @@ -1361,7 +1364,7 @@ public function testSaveOnlyDirtyProperties() {
$entity->dirty('created', true);
$entity->dirty('updated', true);

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

Expand All @@ -1382,7 +1385,7 @@ public function testsASavedEntityIsClean() {
'created' => new \DateTime('2013-10-10 00:00'),
'updated' => new \DateTime('2013-10-10 00:00')
]);
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$this->assertSame($entity, $table->save($entity));
$this->assertFalse($entity->dirty('usermane'));
$this->assertFalse($entity->dirty('password'));
Expand All @@ -1402,7 +1405,7 @@ public function testsASavedEntityIsNotNew() {
'created' => new \DateTime('2013-10-10 00:00'),
'updated' => new \DateTime('2013-10-10 00:00')
]);
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$this->assertSame($entity, $table->save($entity));
$this->assertFalse($entity->isNew());
}
Expand All @@ -1418,9 +1421,10 @@ public function testSaveUpdateAuto() {
'id' => 2,
'username' => 'baggins'
]);
$table = TableRegistry::get('users');
$table = TableRegistry::get('user');
$original = $table->find('all')->where(['id' => 2])->first();
$this->assertSame($entity, $table->save($entity));

$row = $table->find('all')->where(['id' => 2])->first();
$this->assertEquals('baggins', $row->username);
$this->assertEquals($original->password, $row->password);
Expand Down Expand Up @@ -1462,7 +1466,7 @@ public function testSaveUpdatePrimaryKeyNotModified() {
$table = $this->getMock(
'\Cake\ORM\Table',
['_buildQuery'],
[['table' => 'users', 'connection' => ConnectionManager::get('test')]]
[['table' => 'users', 'connection' => $this->connection]]
);

$query = $this->getMock(
Expand Down Expand Up @@ -1502,7 +1506,7 @@ public function testUpdateNoChange() {
$table = $this->getMock(
'\Cake\ORM\Table',
['_buildQuery'],
[['table' => 'users', 'connection' => ConnectionManager::get('test')]]
[['table' => 'users', 'connection' => $this->connection]]
);
$table->expects($this->never())->method('_buildQuery');
$entity = new \Cake\ORM\Entity([
Expand All @@ -1521,7 +1525,7 @@ public function testUpdateNoPrimaryButOtherKeys() {
$table = $this->getMock(
'\Cake\ORM\Table',
['_buildQuery'],
[['table' => 'users', 'connection' => ConnectionManager::get('test')]]
[['table' => 'users', 'connection' => $this->connection]]
);
$table->expects($this->never())->method('_buildQuery');
$entity = new \Cake\ORM\Entity([
Expand Down

0 comments on commit bdbf066

Please sign in to comment.