1
0
mirror of synced 2024-12-13 06:46:03 +03:00
doctrine2/tests_old/RecordTestCase.php

929 lines
32 KiB
PHP
Raw Normal View History

2006-10-13 20:28:40 +04:00
<?php
2007-02-17 15:38:02 +03:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.org>.
2007-02-17 15:38:02 +03:00
*/
/**
* Doctrine_Record_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.org
2007-02-17 15:38:02 +03:00
* @since 1.0
* @version $Revision$
*/
2007-06-15 01:40:22 +04:00
class Doctrine_Record_TestCase extends Doctrine_UnitTestCase
{
2007-05-17 01:28:33 +04:00
2007-06-15 01:40:22 +04:00
public function prepareTables()
{
$this->tables[] = 'enumTest';
$this->tables[] = 'fieldNameTest';
$this->tables[] = 'GzipTest';
$this->tables[] = 'Book';
$this->tables[] = 'EntityAddress';
2006-10-13 20:28:40 +04:00
parent::prepareTables();
}
2007-08-09 21:09:20 +04:00
public function testOne2OneForeign()
{
$user = new User();
$user->name = "Richard Linklater";
2007-09-02 20:56:44 +04:00
2007-08-09 21:09:20 +04:00
$rel = $user->getTable()->getRelation('Account');
$this->assertTrue($rel instanceof Doctrine_Relation_ForeignKey);
$account = $user->Account;
$account->amount = 1000;
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->state(), Doctrine_Record::STATE_TDIRTY);
$this->assertEqual($account->entity_id->getOid(), $user->getOid());
$this->assertEqual($account->amount, 1000);
$this->assertEqual($user->name, "Richard Linklater");
$user->save();
$this->assertEqual($account->entity_id, $user->id);
$user->refresh();
$account = $user->Account;
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->state(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($account->entity_id, $user->id);
$this->assertEqual($account->amount, 1000);
$this->assertEqual($user->name, "Richard Linklater");
$user = new User();
$user->name = 'John Rambo';
$account = $user->Account;
$account->amount = 2000;
2007-08-09 21:09:20 +04:00
$this->assertEqual($account->getTable()->getColumnNames(), array('id', 'entity_id', 'amount'));
$this->connection->unitOfWork->saveAll();
2007-08-09 21:09:20 +04:00
$this->assertEqual($user->state(), Doctrine_Record::STATE_CLEAN);
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->getTable()->getColumnNames(), array('id', 'entity_id', 'amount'));
2007-08-09 21:09:20 +04:00
$this->assertEqual($account->entity_id, $user->id);
$this->assertEqual($account->amount, 2000);
$user = $user->getRepository()->find($user->id);
2007-08-09 21:09:20 +04:00
$this->assertEqual($user->state(), Doctrine_Record::STATE_CLEAN);
$account = $user->Account;
$this->assertTrue($account instanceof Account);
$this->assertEqual($account->state(), Doctrine_Record::STATE_CLEAN);
$this->assertEqual($account->getTable()->getColumnNames(), array('id', 'entity_id', 'amount'));
$this->assertEqual($account->entity_id, $user->id);
$this->assertEqual($account->amount, 2000);
$this->assertEqual($user->name, "John Rambo");
}
2007-06-15 01:40:22 +04:00
public function testIssetForPrimaryKey()
{
2006-10-13 20:28:40 +04:00
$this->assertTrue(isset($this->users[0]->id));
$this->assertTrue(isset($this->users[0]['id']));
$this->assertTrue($this->users[0]->contains('id'));
$user = new User();
2008-05-08 18:17:35 +04:00
$this->assertTrue(!isset($user->id));
$this->assertTrue(!isset($user['id']));
$this->assertTrue(!$user->contains('id'));
2006-10-13 20:28:40 +04:00
}
2007-05-17 01:28:33 +04:00
2007-06-15 01:40:22 +04:00
public function testNotNullConstraint()
{
2006-10-13 20:28:40 +04:00
$null = new NotNullTest();
$null->name = null;
$null->type = 1;
try {
$this->connection->beginTransaction();
2006-10-13 20:28:40 +04:00
$null->save();
$this->fail();
} catch (Exception $e) {
2006-10-13 20:28:40 +04:00
$this->pass();
$this->connection->rollback();
}
}
2007-06-15 01:40:22 +04:00
public function testGzipType()
{
2006-10-13 20:28:40 +04:00
$gzip = new GzipTest();
$gzip->gzip = "compressed";
$this->assertEqual($gzip->gzip, "compressed");
$gzip->save();
$this->assertEqual($gzip->gzip, "compressed");
$gzip->refresh();
$this->assertEqual($gzip->gzip, "compressed");
$this->connection->clear();
$gzip = $gzip->getRepository()->find($gzip->id);
2006-10-13 20:28:40 +04:00
$this->assertEqual($gzip->gzip, "compressed");
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$gzip->gzip = "compressed 2";
$this->assertEqual($gzip->gzip, "compressed 2");
$gzip->save();
$this->assertEqual($gzip->gzip, "compressed 2");
$gzip->refresh();
$this->assertEqual($gzip->gzip, "compressed 2");
}
2007-06-15 01:40:22 +04:00
public function testDefaultValues()
{
2006-10-13 20:28:40 +04:00
$test = new FieldNameTest;
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$this->assertEqual($test->someColumn, 'some string');
$this->assertEqual($test->someEnum, 'php');
$this->assertEqual($test->someArray, array());
$this->assertTrue(is_object($test->someObject));
$this->assertEqual($test->someInt, 11);
}
2007-06-15 01:40:22 +04:00
public function testToArray()
{
2006-10-13 20:28:40 +04:00
$user = new User();
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$a = $user->toArray();
$this->assertTrue(is_array($a));
$this->assertTrue(array_key_exists('name', $a));
2007-09-02 20:56:44 +04:00
2007-07-06 03:47:48 +04:00
2006-10-13 20:28:40 +04:00
$this->assertEqual($a['name'], null);
$this->assertTrue(array_key_exists('id', $a));
$this->assertEqual($a['id'], null);
2007-07-06 03:47:48 +04:00
2006-10-13 20:28:40 +04:00
$user->name = 'Someone';
$user->save();
$a = $user->toArray();
$this->assertTrue(is_array($a));
$this->assertTrue(array_key_exists('name', $a));
$this->assertEqual($a['name'], 'Someone');
$this->assertTrue(array_key_exists('id', $a));
$this->assertTrue(is_numeric($a['id']));
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$user->refresh();
$a = $user->toArray();
$this->assertTrue(is_array($a));
$this->assertTrue(array_key_exists('name', $a));
$this->assertEqual($a['name'], 'Someone');
$this->assertTrue(array_key_exists('id', $a));
$this->assertTrue(is_numeric($a['id']));
$this->connection->clear();
$user = $user->getRepository()->find($user->id);
2006-10-13 20:28:40 +04:00
$a = $user->toArray();
$this->assertTrue(is_array($a));
$this->assertTrue(array_key_exists('name', $a));
$this->assertEqual($a['name'], 'Someone');
$this->assertTrue(array_key_exists('id', $a));
$this->assertTrue(is_numeric($a['id']));
}
2007-06-15 01:40:22 +04:00
public function testReferences2()
{
2006-10-13 20:28:40 +04:00
$user = new User();
$user->Phonenumber[0]->phonenumber = '123 123';
2007-05-24 01:02:19 +04:00
$ref = $user->Phonenumber[0]->entity_id;
2006-10-13 20:28:40 +04:00
2007-05-24 01:02:19 +04:00
$this->assertEqual($ref->getOid(), $user->getOid());
2006-10-13 20:28:40 +04:00
}
2007-05-24 01:02:19 +04:00
2007-06-15 01:40:22 +04:00
public function testUpdatingWithNullValue()
{
$user = $this->connection->getRepository('User')->find(5);
2006-10-13 20:28:40 +04:00
$user->name = null;
$this->assertEqual($user->name, null);
$user->save();
$this->assertEqual($user->name, null);
2007-05-24 01:02:19 +04:00
2006-10-13 20:28:40 +04:00
$this->connection->clear();
$user = $this->connection->getRepository('User')->find(5);
2007-10-22 20:32:07 +04:00
2006-10-13 20:28:40 +04:00
$this->assertEqual($user->name, null);
}
2007-05-24 01:02:19 +04:00
2007-06-15 01:40:22 +04:00
public function testSerialize()
{
$user = $this->connection->getRepository("User")->find(4);
2006-10-13 20:28:40 +04:00
$str = serialize($user);
$user2 = unserialize($str);
$this->assertTrue($user2 instanceof User);
$this->assertEqual($user2->identifier(), $user->identifier());
2006-10-13 20:28:40 +04:00
}
2007-06-15 01:40:22 +04:00
public function testCallback()
{
2006-10-13 20:28:40 +04:00
$user = new User();
$user->name = " zYne ";
$user->call('trim', 'name');
$this->assertEqual($user->name, 'zYne');
$user->call('substr', 'name', 0, 1);
$this->assertEqual($user->name, 'z');
}
2007-05-24 01:02:19 +04:00
2006-10-13 20:28:40 +04:00
public function testCompositePK() {
$record = new EntityReference();
$this->assertEqual((array)$record->getTable()->getIdentifier(), array("entity1","entity2"));
2007-06-27 03:01:41 +04:00
$this->assertEqual($record->getTable()->getIdentifierType(), Doctrine::IDENTIFIER_COMPOSITE);
$this->assertEqual($record->identifier(), array("entity1" => null, "entity2" => null));
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_TCLEAN);
2006-10-13 20:28:40 +04:00
$record->entity1 = 3;
$record->entity2 = 4;
$this->assertEqual($record->entity2, 4);
$this->assertEqual($record->entity1, 3);
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_TDIRTY);
$this->assertEqual($record->identifier(), array("entity1" => 3, "entity2" => 4));
2006-10-13 20:28:40 +04:00
$record->save();
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertEqual($record->entity2, 4);
$this->assertEqual($record->entity1, 3);
$this->assertEqual($record->identifier(), array("entity1" => 3, "entity2" => 4));
2006-10-13 20:28:40 +04:00
$record = $record->getRepository()->find($record->identifier());
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertEqual($record->entity2, 4);
$this->assertEqual($record->entity1, 3);
$this->assertEqual($record->identifier(), array("entity1" => 3, "entity2" => 4));
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$record->entity2 = 5;
$record->entity1 = 2;
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_DIRTY);
2006-10-13 20:28:40 +04:00
$this->assertEqual($record->entity2, 5);
$this->assertEqual($record->entity1, 2);
$this->assertEqual($record->identifier(), array("entity1" => 3, "entity2" => 4));
2006-10-13 20:28:40 +04:00
$record->save();
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertEqual($record->entity2, 5);
$this->assertEqual($record->entity1, 2);
$this->assertEqual($record->identifier(), array("entity1" => 2, "entity2" => 5));
$record = $record->getRepository()->find($record->identifier());
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertEqual($record->entity2, 5);
$this->assertEqual($record->entity1, 2);
$this->assertEqual($record->identifier(), array("entity1" => 2, "entity2" => 5));
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$record->refresh();
2007-01-21 21:31:51 +03:00
$this->assertEqual($record->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertEqual($record->entity2, 5);
$this->assertEqual($record->entity1, 2);
$this->assertEqual($record->identifier(), array("entity1" => 2, "entity2" => 5));
2006-10-13 20:28:40 +04:00
$record = new EntityReference();
$record->entity2 = 6;
$record->entity1 = 2;
$record->save();
2007-09-02 20:56:44 +04:00
2007-05-24 01:02:19 +04:00
$coll = $this->connection->query("FROM EntityReference");
2006-10-13 20:28:40 +04:00
$this->assertTrue($coll[0] instanceof EntityReference);
2007-01-21 21:31:51 +03:00
$this->assertEqual($coll[0]->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertTrue($coll[1] instanceof EntityReference);
2007-01-21 21:31:51 +03:00
$this->assertEqual($coll[1]->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
2007-05-24 01:02:19 +04:00
$coll = $this->connection->query("FROM EntityReference WHERE EntityReference.entity2 = 5");
2006-10-13 20:28:40 +04:00
$this->assertEqual($coll->count(), 1);
}
2007-06-15 01:40:22 +04:00
public function testManyToManyTreeStructure()
{
2006-10-13 20:28:40 +04:00
$task = $this->connection->create("Task");
$task->name = "Task 1";
$task->ResourceAlias[0]->name = "Resource 1";
$this->connection->unitOfWork->saveAll();
2006-10-13 20:28:40 +04:00
$this->assertTrue($task->ResourceAlias[0] instanceof Resource);
$this->assertEqual($task->ResourceAlias[0]->name, "Resource 1");
$this->assertEqual($this->dbh->query("SELECT COUNT(*) FROM assignment")->fetch(PDO::FETCH_NUM),array(1));
$task = new Task();
$this->assertTrue($task instanceof Task);
2007-01-21 21:31:51 +03:00
$this->assertEqual($task->state(), Doctrine_Record::STATE_TCLEAN);
2006-10-13 20:28:40 +04:00
$this->assertTrue($task->Subtask[0] instanceof Task);
2007-01-21 21:31:51 +03:00
//$this->assertEqual($task->Subtask[0]->state(), Doctrine_Record::STATE_TDIRTY);
2006-10-13 20:28:40 +04:00
$this->assertTrue($task->ResourceAlias[0] instanceof Resource);
2007-01-21 21:31:51 +03:00
$this->assertEqual($task->ResourceAlias[0]->state(), Doctrine_Record::STATE_TCLEAN);
2006-10-13 20:28:40 +04:00
$task->name = "Task 1";
$task->ResourceAlias[0]->name = "Resource 1";
$task->Subtask[0]->name = "Subtask 1";
$this->assertEqual($task->name, "Task 1");
$this->assertEqual($task->ResourceAlias[0]->name, "Resource 1");
$this->assertEqual($task->ResourceAlias->count(), 1);
$this->assertEqual($task->Subtask[0]->name, "Subtask 1");
$this->connection->unitOfWork->saveAll();
$task = $task->getRepository()->find($task->identifier());
2006-10-13 20:28:40 +04:00
$this->assertEqual($task->name, "Task 1");
$this->assertEqual($task->ResourceAlias[0]->name, "Resource 1");
$this->assertEqual($task->ResourceAlias->count(), 1);
$this->assertEqual($task->Subtask[0]->name, "Subtask 1");
}
2007-06-15 01:40:22 +04:00
public function testGet()
{
2006-10-13 20:28:40 +04:00
$user = new User();
$user->name = "Jack Daniels";
$this->assertEqual($user->name, "Jack Daniels");
$this->assertEqual($user->created, null);
$this->assertEqual($user->updated, null);
$user->save();
$id = $user->identifier();
$user = $user->getRepository()->find($id);
2006-10-13 20:28:40 +04:00
$this->assertEqual($user->name, "Jack Daniels");
$this->assertEqual($user->created, null);
$this->assertEqual($user->updated, null);
}
2007-06-15 01:40:22 +04:00
public function testNewOperator()
{
$table = $this->connection->getClassMetadata("User");
2006-10-13 20:28:40 +04:00
$user = new User();
2007-01-21 21:31:51 +03:00
$this->assertEqual(Doctrine_Lib::getRecordStateAsString($user->state()), Doctrine_Lib::getRecordStateAsString(Doctrine_Record::STATE_TCLEAN));
2006-10-13 20:28:40 +04:00
$user->name = "John Locke";
$this->assertTrue($user->name,"John Locke");
2007-01-21 21:31:51 +03:00
$this->assertTrue($user->state() == Doctrine_Record::STATE_TDIRTY);
2006-10-13 20:28:40 +04:00
$user->save();
2007-01-21 21:31:51 +03:00
$this->assertTrue($user->state() == Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertTrue($user->name,"John Locke");
}
2007-06-15 01:40:22 +04:00
public function testTreeStructure()
{
2006-10-13 20:28:40 +04:00
$e = new Element();
$fk = $e->getTable()->getRelation("Child");
$this->assertTrue($fk instanceof Doctrine_Relation_ForeignKey);
$this->assertEqual($fk->getType(), Doctrine_Relation::MANY_AGGREGATE);
$this->assertEqual($fk->getForeign(), "parent_id");
$this->assertEqual($fk->getLocal(), "id");
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$e->name = "parent";
$e->Child[0]->name = "child 1";
$e->Child[1]->name = "child 2";
$e->Child[1]->Child[0]->name = "child 1's child 1";
$e->Child[1]->Child[1]->name = "child 1's child 1";
$this->assertEqual($e->name,"parent");
$this->assertEqual($e->Child[0]->name,"child 1");
$this->assertEqual($e->Child[1]->name,"child 2");
$this->assertEqual($e->Child[1]->Child[0]->name,"child 1's child 1");
$this->assertEqual($e->Child[1]->Child[1]->name,"child 1's child 1");
$this->connection->unitOfWork->saveAll();
2007-05-24 01:02:19 +04:00
$elements = $this->connection->query("FROM Element");
2006-10-13 20:28:40 +04:00
$this->assertEqual($elements->count(), 5);
$e = $e->getRepository()->find(1);
2006-10-13 20:28:40 +04:00
$this->assertEqual($e->name,"parent");
$this->assertEqual($e->Child[0]->name,"child 1");
$c = $e->getRepository()->find(2);
2006-10-13 20:28:40 +04:00
$this->assertEqual($c->name, "child 1");
$this->assertEqual($e->Child[0]->parent_id, 1);
$this->assertEqual($e->Child[0]->Parent->identifier(), $e->identifier());
2006-10-13 20:28:40 +04:00
$this->assertEqual($e->Child[1]->parent_id, 1);
$this->assertEqual($e->Child[1]->Child[0]->name,"child 1's child 1");
$this->assertEqual($e->Child[1]->Child[1]->name,"child 1's child 1");
$this->assertEqual($e->Child[1]->Child[0]->parent_id, 3);
$this->assertEqual($e->Child[1]->Child[1]->parent_id, 3);
}
2007-06-15 01:40:22 +04:00
public function testUniqueKeyComponent()
{
2006-10-13 20:28:40 +04:00
$e = new Error();
2007-05-24 01:02:19 +04:00
$e->message = 'user error';
2006-10-13 20:28:40 +04:00
$e->file_md5 = md5(0);
$e->code = 1;
// ADDING NEW RECORD
$this->assertEqual($e->code,1);
$this->assertEqual($e->file_md5, md5(0));
2007-05-24 01:02:19 +04:00
$this->assertEqual($e->message, 'user error');
2006-10-13 20:28:40 +04:00
$e2 = new Error();
2007-05-24 01:02:19 +04:00
$e2->message = 'user error2';
2006-10-13 20:28:40 +04:00
$e2->file_md5 = md5(1);
$e2->code = 2;
$this->assertEqual($e2->code,2);
$this->assertEqual($e2->file_md5, md5(1));
2007-05-24 01:02:19 +04:00
$this->assertEqual($e2->message, 'user error2');
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
2007-05-24 01:02:19 +04:00
$fk = $e->getTable()->getRelation('Description');
2006-10-13 20:28:40 +04:00
$this->assertTrue($fk instanceof Doctrine_Relation_ForeignKey);
2007-05-24 01:02:19 +04:00
$this->assertEqual($fk->getLocal(),'file_md5');
$this->assertEqual($fk->getForeign(),'file_md5');
$this->assertTrue($fk->getTable() instanceof Doctrine_ClassMetadata);
2006-10-13 20:28:40 +04:00
2007-05-24 01:02:19 +04:00
$e->Description[0]->description = 'This is the 1st description';
$e->Description[1]->description = 'This is the 2nd description';
$this->assertEqual($e->Description[0]->description, 'This is the 1st description');
$this->assertEqual($e->Description[1]->description, 'This is the 2nd description');
2006-10-13 20:28:40 +04:00
$this->assertEqual($e->Description[0]->file_md5, $e->file_md5);
$this->assertEqual($e->Description[1]->file_md5, $e->file_md5);
2007-05-24 01:02:19 +04:00
2006-10-13 20:28:40 +04:00
$this->assertEqual($e2->Description[0]->description, null);
$this->assertEqual($e2->Description[1]->description, null);
$this->assertEqual($e2->Description[0]->file_md5, $e2->file_md5);
$this->assertEqual($e2->Description[1]->file_md5, $e2->file_md5);
$e->save();
2007-09-02 20:56:44 +04:00
2007-05-24 01:02:19 +04:00
$coll = $this->connection->query('FROM Error');
2006-10-13 20:28:40 +04:00
$e = $coll[0];
$this->assertEqual($e->code,1);
$this->assertEqual($e->file_md5, md5(0));
2007-05-24 01:02:19 +04:00
$this->assertEqual($e->message, 'user error');
2006-10-13 20:28:40 +04:00
$this->assertTrue($e->Description instanceof Doctrine_Collection);
$this->assertTrue($e->Description[0] instanceof Description);
$this->assertTrue($e->Description[1] instanceof Description);
2007-05-24 01:02:19 +04:00
$this->assertEqual($e->Description[0]->description, 'This is the 1st description');
$this->assertEqual($e->Description[1]->description, 'This is the 2nd description');
2006-10-13 20:28:40 +04:00
// UPDATING
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$e->code = 2;
2007-05-24 01:02:19 +04:00
$e->message = 'changed message';
$e->Description[0]->description = '1st changed description';
$e->Description[1]->description = '2nd changed description';
2006-10-13 20:28:40 +04:00
$this->assertEqual($e->code,2);
2007-05-24 01:02:19 +04:00
$this->assertEqual($e->message,'changed message');
$this->assertEqual($e->Description[0]->description, '1st changed description');
$this->assertEqual($e->Description[1]->description, '2nd changed description');
2006-10-13 20:28:40 +04:00
$e->save();
$this->assertEqual($e->code,2);
2007-05-24 01:02:19 +04:00
$this->assertEqual($e->message,'changed message');
$this->assertEqual($e->Description[0]->description, '1st changed description');
$this->assertEqual($e->Description[1]->description, '2nd changed description');
2006-10-13 20:28:40 +04:00
}
2007-06-15 01:40:22 +04:00
public function testInsert()
{
2006-10-13 20:28:40 +04:00
$user = new User();
$user->name = "John Locke";
$user->save();
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$this->assertTrue(is_numeric($user->id) && $user->id > 0);
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$this->assertTrue($user->getModified() == array());
2007-01-21 21:31:51 +03:00
$this->assertTrue($user->state() == Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$user->delete();
2007-01-21 21:31:51 +03:00
$this->assertEqual($user->state(), Doctrine_Record::STATE_TCLEAN);
2006-10-13 20:28:40 +04:00
}
2007-06-15 01:40:22 +04:00
public function testUpdate()
{
$user = $this->connection->getRepository("User")->find(4);
2006-10-13 20:28:40 +04:00
$user->set("name","Jack Daniels",true);
$user->save();
//print $this->old->name;
$this->assertEqual($user->getModified(), array());
$this->assertEqual($user->name, "Jack Daniels");
}
2006-10-16 20:17:07 +04:00
2007-06-15 01:40:22 +04:00
public function testCopy()
{
$user = $this->connection->getRepository("User")->find(4);
2006-10-13 20:28:40 +04:00
$new = $user->copy();
2006-10-16 20:17:07 +04:00
2006-10-13 20:28:40 +04:00
$this->assertTrue($new instanceof Doctrine_Record);
2007-01-21 21:31:51 +03:00
$this->assertTrue($new->state() == Doctrine_Record::STATE_TDIRTY);
2006-10-16 20:17:07 +04:00
$new = $user->copy();
2006-10-16 20:17:07 +04:00
$new->save();
$this->assertEqual($user->name, $new->name);
2006-10-16 20:17:07 +04:00
$this->assertTrue(is_numeric($new->id) && $new->id > 0);
$new->refresh();
$this->assertEqual($user->name, $new->name);
$this->assertTrue(is_numeric($new->id) && $new->id > 0);
}
2007-06-15 01:40:22 +04:00
public function testCopyAndModify()
{
$user = $this->connection->getRepository("User")->find(4);
$new = $user->copy();
$this->assertTrue($new instanceof Doctrine_Record);
2007-01-21 21:31:51 +03:00
$this->assertTrue($new->state() == Doctrine_Record::STATE_TDIRTY);
$new->loginname = 'jackd';
$this->assertEqual($user->name, $new->name);
$this->assertEqual($new->loginname, 'jackd');
$new->save();
$this->assertTrue(is_numeric($new->id) && $new->id > 0);
$new->refresh();
$this->assertEqual($user->name, $new->name);
$this->assertEqual($new->loginname, 'jackd');
2006-10-13 20:28:40 +04:00
}
2007-05-17 01:28:33 +04:00
public function testReferences()
{
$user = $this->connection->getRepository('User')->find(5);
2006-10-13 20:28:40 +04:00
$this->assertTrue($user->Phonenumber instanceof Doctrine_Collection);
$this->assertEqual($user->Phonenumber->count(), 3);
2007-05-24 01:02:19 +04:00
$coll = new Doctrine_Collection('Phonenumber');
2006-10-13 20:28:40 +04:00
$user->Phonenumber = $coll;
$this->assertEqual($user->Phonenumber->count(), 0);
$user->save();
$user->getEntityManager()->clear('User');
2006-10-13 20:28:40 +04:00
$user = $this->objTable->find(5);
2006-10-13 20:28:40 +04:00
$this->assertEqual($user->Phonenumber->count(), 0);
2007-05-24 01:02:19 +04:00
$this->assertEqual(get_class($user->Phonenumber), 'Doctrine_Collection');
2006-10-13 20:28:40 +04:00
$user->Phonenumber[0]->phonenumber;
$this->assertEqual($user->Phonenumber->count(), 1);
// ADDING REFERENCES
$user->Phonenumber[0]->phonenumber = "123 123";
$this->assertEqual($user->Phonenumber->count(), 1);
$user->Phonenumber[1]->phonenumber = "123 123";
$this->assertEqual($user->Phonenumber->count(), 2);
$user->save();
$this->assertEqual($user->Phonenumber->count(), 2);
unset($user);
$user = $this->objTable->find(5);
$this->assertEqual($user->Phonenumber->count(), 2);
$user->Phonenumber[3]->phonenumber = "123 123";
$user->save();
$this->assertEqual($user->Phonenumber->count(), 3);
unset($user);
$user = $this->objTable->find(5);
$this->assertEqual($user->Phonenumber->count(), 3);
// DELETING REFERENCES
$user->Phonenumber->delete();
$this->assertEqual($user->Phonenumber->count(), 0);
unset($user);
$user = $this->objTable->find(5);
$this->assertEqual($user->Phonenumber->count(), 0);
// ADDING REFERENCES WITH STRING KEYS
$user->Phonenumber["home"]->phonenumber = "123 123";
$user->Phonenumber["work"]->phonenumber = "444 444";
$user->save();
$this->assertEqual($user->Phonenumber->count(), 2);
unset($user);
$user = $this->objTable->find(5);
$this->assertEqual($user->Phonenumber->count(), 2);
// REPLACING ONE-TO-MANY REFERENCE
unset($coll);
2007-05-24 01:02:19 +04:00
$coll = new Doctrine_Collection('Phonenumber');
2006-10-13 20:28:40 +04:00
$coll[0]->phonenumber = "123 123";
$coll["home"]->phonenumber = "444 444";
$coll["work"]->phonenumber = "444 444";
$user->Phonenumber = $coll;
$user->save();
$this->assertEqual($user->Phonenumber->count(), 3);
$user = $this->objTable->find(5);
//$this->assertEqual($user->Phonenumber->count(), 3);
// ONE-TO-ONE REFERENCES
$user->Email->address = "drinker@drinkmore.info";
$this->assertTrue($user->Email instanceof Email);
$this->assertEqual($user->Email->address, "drinker@drinkmore.info");
$user->save();
$this->assertTrue($user->Email instanceof Email);
$this->assertEqual($user->Email->address, "drinker@drinkmore.info");
$this->assertEqual($user->Email->id, $user->email_id);
$user = $this->objTable->find(5);
$this->assertTrue($user->Email instanceof Email);
$this->assertEqual($user->Email->id, $user->email_id);
2007-01-21 21:31:51 +03:00
$this->assertEqual($user->Email->state(), Doctrine_Record::STATE_CLEAN);
2006-10-13 20:28:40 +04:00
$this->assertEqual($user->Email->address, "drinker@drinkmore.info");
$id = $user->Email->id;
// REPLACING ONE-TO-ONE REFERENCES
$email = $this->connection->create("Email");
$email->address = "absolutist@nottodrink.com";
$user->Email = $email;
$this->assertTrue($user->Email instanceof Email);
$this->assertEqual($user->Email->address, "absolutist@nottodrink.com");
$user->save();
unset($user);
$user = $this->objTable->find(5);
$this->assertTrue($user->Email instanceof Email);
$this->assertEqual($user->Email->address, "absolutist@nottodrink.com");
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$emails = $this->connection->query("FROM Email WHERE Email.id = $id");
//$this->assertEqual(count($emails),0);
}
2007-06-15 01:40:22 +04:00
public function testDeleteReference()
{
2006-10-13 20:28:40 +04:00
$user = $this->objTable->find(5);
$int = $user->Phonenumber->delete();
$this->assertTrue($user->Phonenumber->count() == 0);
}
2007-05-24 01:02:19 +04:00
2007-06-15 01:40:22 +04:00
public function testSaveAssociations()
{
$userMapper = $this->connection->getRepository('User');
$user = $userMapper->find(5);
$userTable = $this->connection->getClassMetadata('User');
/*echo get_class($rel1) . "<br />";
echo get_class($rel2) . "<br />";
echo get_class($userTable->getRelation('Group'));
echo "........<br />";
echo "local:" . $rel1->getLocal() . "---foreign:" . $rel1->getForeign() . "<br />";
echo "local:" . $rel2->getLocal() . "---foreign:" . $rel2->getForeign() . "<br />";
echo "........<br />";*/
$gf = $this->connection->getRepository("Group");
//echo "start";
2006-10-13 20:28:40 +04:00
$this->assertTrue($user->Group instanceof Doctrine_Collection);
//echo "end";
/*$xrefMapper = $this->connection->getMapper('Groupuser');
$xrefs = $xrefMapper->findAll();
foreach ($xrefs as $xref) {
echo $xref->group_id . " -- ". $xref->user_id ."(state:".$xref->state().")<br />";
}*/
$this->assertEqual($user->Group->count(), 1);
$this->assertEqual($user->Group[0]->id, 3);
2006-10-13 20:28:40 +04:00
// ADDING ASSOCIATED REFERENCES
$group1 = $gf->find(1);
$group2 = $gf->find(2);
$user->Group[1] = $group1;
$user->Group[2] = $group2;
2006-10-13 20:28:40 +04:00
$this->assertEqual($user->Group->count(), 3);
2006-10-13 20:28:40 +04:00
$user->save();
/*$xrefMapper = $this->connection->getMapper('Groupuser');
$xrefs = $xrefMapper->findAll();
foreach ($xrefs as $xref) {
echo $xref->group_id . " -- ". $xref->user_id ."(state:".$xref->state().")<br />";
}*/
2007-05-24 01:02:19 +04:00
$coll = $user->Group;
2006-10-13 20:28:40 +04:00
// UNSETTING ASSOCIATED REFERENCES
2006-10-13 20:28:40 +04:00
unset($user);
$query = new Doctrine_Query($this->conn);
2006-10-13 20:28:40 +04:00
$user = $this->objTable->find(5);
//echo get_class($user->Group);
$this->assertEqual($user->Group->count(), 3);
$pks = $user->Group->getPrimaryKeys();
$this->assertTrue(in_array(1, $pks));
$this->assertTrue(in_array(2, $pks));
$this->assertTrue(in_array(3, $pks));
2007-05-24 01:02:19 +04:00
$user->unlink('Group', array($group1->id, $group2->id));
$this->assertEqual($user->Group->count(), 1);
2006-10-13 20:28:40 +04:00
$user->save();
unset($user);
// CHECKING THE PERSISTENCE OF UNSET ASSOCIATED REFERENCES
2007-05-24 01:02:19 +04:00
$this->connection->clear();
2006-10-13 20:28:40 +04:00
$user = $this->objTable->find(5);
2007-05-24 01:02:19 +04:00
$this->assertEqual($user->Group->count(), 1);
$this->assertEqual($user->Group[0]->id, 3);
$this->assertEqual($gf->findAll()->count(), 3);
2006-10-13 20:28:40 +04:00
// REPLACING OLD ASSOCIATED REFERENCE
$user->unlink('Group', 3); // you MUST first unlink old relationship
$user->Group[1] = $group1;
$user->Group[0] = $group2;
2006-10-13 20:28:40 +04:00
$user->save();
$user = $this->objTable->find(5);
2007-05-24 01:02:19 +04:00
$this->assertEqual($user->Group->count(), 2);
$this->assertEqual($user->Group[0]->identifier(), $group2->identifier());
$this->assertEqual($user->Group[1]->identifier(), $group1->identifier());
2006-10-13 20:28:40 +04:00
2007-09-13 02:07:57 +04:00
$user->unlink('Group');
$user->save();
$user->free();
2007-09-13 02:07:57 +04:00
$user = $this->objTable->find(5);
$this->assertEqual($user->Group->count(), 0);
2007-05-24 01:02:19 +04:00
2006-10-13 20:28:40 +04:00
// ACCESSING ASSOCIATION OBJECT PROPERTIES
$user = new User();
$this->assertTrue($user->getTable()->getRelation("Groupuser") instanceof Doctrine_Relation_ForeignKey);
2007-05-24 01:02:19 +04:00
$this->assertTrue($user->Groupuser instanceof Doctrine_Collection);
$this->assertTrue($user->Groupuser[0] instanceof Groupuser);
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$user->name = "Jack Daniels";
$user->Group[0]->name = "Group #1";
$user->Group[1]->name = "Group #2";
$t1 = time();
$t2 = time();
$user->Groupuser[0]->added = $t1;
$user->Groupuser[1]->added = $t2;
2007-09-02 20:56:44 +04:00
$this->assertEqual($user->Groupuser[0]->added, $t1);
$this->assertEqual($user->Groupuser[1]->added, $t2);
2007-09-02 20:56:44 +04:00
2006-10-13 20:28:40 +04:00
$user->save();
$user->refresh();
$this->assertEqual($user->Groupuser[0]->added, $t1);
$this->assertEqual($user->Groupuser[1]->added, $t2);
2006-10-13 20:28:40 +04:00
}
2007-05-17 01:28:33 +04:00
public function testCount()
{
$user = $this->connection->getRepository("User")->find(4);
2006-10-13 20:28:40 +04:00
$this->assertTrue(is_integer($user->count()));
}
2007-05-17 01:28:33 +04:00
public function testGetReference()
{
$user = $this->connection->getRepository("User")->find(4);
2006-10-13 20:28:40 +04:00
$this->assertTrue($user->Email instanceof Doctrine_Record);
$this->assertTrue($user->Phonenumber instanceof Doctrine_Collection);
$this->assertTrue($user->Group instanceof Doctrine_Collection);
$this->assertTrue($user->Phonenumber->count() == 1);
}
2007-05-17 01:28:33 +04:00
public function testGetIterator()
{
$user = $this->connection->getRepository("User")->find(4);
2006-10-13 20:28:40 +04:00
$this->assertTrue($user->getIterator() instanceof ArrayIterator);
}
2007-09-02 20:56:44 +04:00
public function testRefreshRelated()
{
$user = $this->connection->getRepository("User")->find(4);
2007-09-02 20:56:44 +04:00
$user->Address[0]->address = "Address #1";
$user->Address[1]->address = "Address #2";
$user->save();
$this->assertEqual(count($user->Address), 2);
Doctrine_Query::create()->delete()->from('EntityAddress')->where('user_id = ? AND address_id = ?', array($user->id, $user->Address[1]->id))->execute();
$user->refreshRelated('Address');
$this->assertEqual(count($user->Address), 1);
Doctrine_Query::create()->delete()->from('EntityAddress')->where('user_id = ? AND address_id = ?', array($user->id, $user->Address[0]->id))->execute();
$user->refreshRelated();
$this->assertEqual(count($user->Address), 0);
}
2007-09-02 20:56:44 +04:00
public function testRefreshDeep()
{
$user = $this->connection->getRepository("User")->find(4);
$user->Address[0]->address = "Address #1";
$user->Address[1]->address = "Address #2";
$user->save();
$this->assertEqual(count($user->Address), 2);
Doctrine_Query::create()->delete()->from('EntityAddress')->where('user_id = ? AND address_id = ?', array($user->id, $user->Address[1]->id))->execute();
$user->refresh(true);
$this->assertEqual(count($user->Address), 1);
$address = $user->Address[0];
Doctrine_Query::create()->delete()->from('EntityAddress')->where('user_id = ? AND address_id = ?', array($user->id, $user->Address[0]->id))->execute();
$user->refresh(true);
$this->assertEqual(count($user->Address), 0);
$entity_address = new EntityAddress();
$entity_address->user_id = $user->id;
$entity_address->address_id = $address->id;
$entity_address->save();
$this->assertNotEqual(count($user->Address), 1);
$user->refresh(true);
$this->assertEqual(count($user->Address), 1);
}
2006-10-13 20:28:40 +04:00
}