/** * Test SaveAssociated with Habtm relations * * @return void */ public function testSaveAssociatedHabtm() { $this->loadFixtures('Boat', 'Tag', 'Comment', 'User', 'boatsTag'); $data = array('Boat' => array('user_id' => 1, 'title' => 'Boat Has and belongs to Many Tags'), 'Tag' => array('Tag' => array(1, 2)), 'Comment' => array(array('comment' => 'Boat comment', 'user_id' => 1))); $Boat = new Boat(); $result = $Boat->saveAssociated($data); $this->assertFalse(empty($result)); $result = $Boat->read(); $this->assertEquals(2, count($result['Tag'])); $this->assertEquals('tag1', $result['Tag'][0]['tag']); $this->assertEquals(1, count($result['Comment'])); $this->assertEquals(1, count($result['Comment'][0]['comment'])); }
/** * testRecursiveDel method * * @return void */ public function testRecursiveDel() { $this->loadFixtures('Boat', 'Comment', 'Attachment'); $TestModel = new Boat(); $result = $TestModel->delete(2); $this->assertTrue($result); $TestModel->recursive = 2; $result = $TestModel->read(null, 2); $this->assertSame(array(), $result); $result = $TestModel->Comment->read(null, 5); $this->assertSame(array(), $result); $result = $TestModel->Comment->read(null, 6); $this->assertSame(array(), $result); $result = $TestModel->Comment->Attachment->read(null, 1); $this->assertSame(array(), $result); $result = $TestModel->find('count'); $this->assertEquals(2, $result); $result = $TestModel->Comment->find('count'); $this->assertEquals(4, $result); $result = $TestModel->Comment->Attachment->find('count'); $this->assertEquals(0, $result); }
/** * testHabtmLimitOptimization method * * @return void */ public function testHabtmLimitOptimization() { $this->loadFixtures('Boat', 'User', 'Comment', 'Tag', 'boatsTag'); $TestModel = new Boat(); $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2; $result = $TestModel->read(null, 2); $expected = array('Boat' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Boat', 'body' => 'Second Boat Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'), 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'), 'Comment' => array(array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Boat', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'), array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Boat', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31'))); $this->assertEquals($expected, $result); $TestModel->hasAndBelongsToMany['Tag']['limit'] = 1; $result = $TestModel->read(null, 2); unset($expected['Tag'][1]); $this->assertEquals($expected, $result); }
/** * ensure that exists() does not persist between method calls reset on create * * @return void */ public function testResetOfExistsOnCreate() { $this->loadFixtures('Boat'); $Boat = new Boat(); $Boat->id = 1; $Boat->saveField('title', 'Reset me'); $Boat->delete(); $Boat->id = 1; $this->assertFalse($Boat->exists()); $Boat->create(); $this->assertFalse($Boat->exists()); $Boat->id = 2; $Boat->saveField('title', 'Staying alive'); $result = $Boat->read(null, 2); $this->assertEquals('Staying alive', $result['Boat']['title']); }
/** * Test nested transaction * * @return void */ public function testNestedTransaction() { $this->Dbo->useNestedTransactions = true; $this->skipIf($this->Dbo->nestedTransactionSupported() === false, 'The Postgres server do not support nested transaction'); $this->loadFixtures('Boat'); $model = new Boat(); $model->hasOne = $model->hasMany = $model->belongsTo = $model->hasAndBelongsToMany = array(); $model->cacheQueries = false; $this->Dbo->cacheMethods = false; $this->assertTrue($this->Dbo->begin()); $this->assertNotEmpty($model->read(null, 1)); $this->assertTrue($this->Dbo->begin()); $this->assertTrue($model->delete(1)); $this->assertEmpty($model->read(null, 1)); $this->assertTrue($this->Dbo->rollback()); $this->assertNotEmpty($model->read(null, 1)); $this->assertTrue($this->Dbo->begin()); $this->assertTrue($model->delete(1)); $this->assertEmpty($model->read(null, 1)); $this->assertTrue($this->Dbo->commit()); $this->assertEmpty($model->read(null, 1)); $this->assertTrue($this->Dbo->rollback()); $this->assertNotEmpty($model->read(null, 1)); }