/** * Test SaveAssociated with Habtm relations * * @return void */ public function testSaveAssociatedHabtm() { $this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag'); $data = array('Article' => array('user_id' => 1, 'title' => 'Article Has and belongs to Many Tags'), 'Tag' => array('Tag' => array(1, 2)), 'Comment' => array(array('comment' => 'Article comment', 'user_id' => 1))); $Article = new Article(); $result = $Article->saveAssociated($data); $this->assertFalse(empty($result)); $result = $Article->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'])); }
/** * testHabtmLimitOptimization method * * @return void */ public function testHabtmLimitOptimization() { $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag'); $TestModel = new Article(); $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2; $result = $TestModel->read(null, 2); $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article 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 Article', '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 Article', '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); }
/** * 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('Article'); $model = new Article(); $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)); }
/** * testRecursiveDel method * * @return void */ public function testRecursiveDel() { $this->loadFixtures('Article', 'Comment', 'Attachment'); $TestModel = new Article(); $result = $TestModel->delete(2); $this->assertTrue($result); $TestModel->recursive = 2; $result = $TestModel->read(null, 2); $this->assertFalse($result); $result = $TestModel->Comment->read(null, 5); $this->assertFalse($result); $result = $TestModel->Comment->read(null, 6); $this->assertFalse($result); $result = $TestModel->Comment->Attachment->read(null, 1); $this->assertFalse($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); }
/** * ensure that exists() does not persist between method calls reset on create * * @return void */ public function testResetOfExistsOnCreate() { $this->loadFixtures('Article'); $Article = new Article(); $Article->id = 1; $Article->saveField('title', 'Reset me'); $Article->delete(); $Article->id = 1; $this->assertFalse($Article->exists()); $Article->create(); $this->assertFalse($Article->exists()); $Article->id = 2; $Article->saveField('title', 'Staying alive'); $result = $Article->read(null, 2); $this->assertEquals('Staying alive', $result['Article']['title']); }
<?php class Page { public function read($name = "Page") { echo "{$name} read.\n"; } } class Article extends Page { public function read($name = "Article") { parent::read($name); } } $article = new Article(); echo $article->read();
/** * This test ensures that multiple requests, which are sent with multiple requests are executed in the correct order. * Thus it ensures that a request with a higher transaction ID is not executed before a request with a lower TID. * */ public function testEditEditMultipleRequests() { $this->markTestSkipped("Consistancy is not yet implemented."); // Preparation: create article $article = new Article(); $article->create(); $article->save(array('title' => 'foo')); // Execute two requests in parallel. $clientId = uniqid(); // The syntax of the fake_request script is // php _fake_request.php client_id article_id tid new_title sleep_time // These processes are executed in the background and we do not need the output. exec('php ' . dirname(__FILE__) . '/_fake_request.php ' . $clientId . ' ' . $article->id . ' 1 foobar 5 ' . '>/dev/null &'); sleep(3); exec('php ' . dirname(__FILE__) . '/_fake_request.php ' . $clientId . ' ' . $article->id . ' 2 barfoo 0 ' . ' >/dev/null &'); // Wait some seconds until the backround process are executed. sleep(8); // Read article from database and check if the value is correct. $data = $article->read(null, $article->id); $this->assertEquals('foobar', $data['Article']['title']); // Clean up operations: delete article $article->delete(); }