Commit efaaf83e authored by romanb's avatar romanb

test and collection cleanup

parent b9ecb8de
......@@ -18,7 +18,7 @@
*
* @author robo
*/
class Doctrine_Common_Collection implements Countable, IteratorAggregate, Serializable, ArrayAccess {
class Doctrine_Common_Collections_Collection implements Countable, IteratorAggregate, Serializable, ArrayAccess {
/**
* An array containing the entries of this collection.
* This is the wrapped php array.
......@@ -205,7 +205,7 @@ class Doctrine_Common_Collection implements Countable, IteratorAggregate, Serial
/**
*
*/
public function search(Doctrine_ORM_Entity $record)
public function search($record)
{
return array_search($record, $this->_data, true);
}
......@@ -287,11 +287,6 @@ class Doctrine_Common_Collection implements Countable, IteratorAggregate, Serial
*/
public function add($value, $key = null)
{
//TODO: really only allow entities?
if ( ! $value instanceof Doctrine_ORM_Entity) {
throw new Doctrine_Record_Exception('Value variable in collection is not an instance of Doctrine_Entity.');
}
// TODO: Really prohibit duplicates?
if (in_array($value, $this->_data, true)) {
return false;
......@@ -306,16 +301,6 @@ class Doctrine_Common_Collection implements Countable, IteratorAggregate, Serial
$this->_data[] = $value;
}
if ($this->_hydrationFlag) {
if ($this->_backRefFieldName) {
// set back reference to owner
$value->_internalSetReference($this->_backRefFieldName, $this->_owner);
}
} else {
//TODO: Register collection as dirty with the UoW if necessary
$this->_changed();
}
return true;
}
......@@ -351,12 +336,36 @@ class Doctrine_Common_Collection implements Countable, IteratorAggregate, Serial
return new ArrayIterator($data);
}
/**
* @todo Experiment. Waiting for 5.3 closures.
* Example usage:
*
* $map = $coll->mapElements(function($key, $entity) {
* return array($entity->id, $entity->name);
* });
*
* or:
*
* $map = $coll->mapElements(function($key, $entity) {
* return array($entity->name, strtoupper($entity->name));
* });
*
*/
public function mapElements($lambda) {
$result = array();
foreach ($this->_data as $key => $entity) {
list($key, $value) = each($lambda($key, $entity));
$result[$key] = $value;
}
return $result;
}
/**
* returns a string representation of this object
*/
public function __toString()
{
return Doctrine_Lib::getCollectionAsString($this);
return __CLASS__ . '@' . spl_object_hash($this);
}
/**
......@@ -368,5 +377,39 @@ class Doctrine_Common_Collection implements Countable, IteratorAggregate, Serial
{
$this->_data = array();
}
/* Serializable implementation */
/**
* Serializes the collection.
* This method is automatically called when the Collection is serialized.
*
* Part of the implementation of the Serializable interface.
*
* @return array
*/
public function serialize()
{
$vars = get_object_vars($this);
//TODO
return serialize($vars);
}
/**
* Reconstitutes the collection object from it's serialized form.
* This method is automatically called everytime the Collection object is unserialized.
*
* Part of the implementation of the Serializable interface.
*
* @param string $serialized The serialized data
*
* @return void
*/
public function unserialize($serialized)
{
//TODO
}
}
?>
This diff is collapsed.
......@@ -131,16 +131,28 @@ class Doctrine_ORM_EntityManager
* @var EventManager
*/
private $_eventManager;
/**
* The maintained (cached) Id generators.
*
* @var <type>
*/
private $_idGenerators = array();
/** Whether the EntityManager is closed or not. */
private $_closed = false;
/**
* Creates a new EntityManager that operates on the given database connection.
* Creates a new EntityManager that operates on the given database connection
* and uses the given Configuration and EventManager implementations.
*
* @param Doctrine_Connection $conn
* @param Doctrine\DBAL\Connection $conn
* @param string $name
*/
protected function __construct(Doctrine_DBAL_Connection $conn, $name, Doctrine_ORM_Configuration $config,
protected function __construct(
Doctrine_DBAL_Connection $conn,
$name,
Doctrine_ORM_Configuration $config,
Doctrine_Common_EventManager $eventManager)
{
$this->_conn = $conn;
......@@ -183,7 +195,7 @@ class Doctrine_ORM_EntityManager
}
/**
* Starts a database transaction.
* Starts a transaction on the underlying connection.
*/
public function beginTransaction()
{
......@@ -191,11 +203,11 @@ class Doctrine_ORM_EntityManager
}
/**
* Commits a running database transaction.
* Commits a running transaction.
* This causes a flush() of the EntityManager if the flush mode is set to
* AUTO or COMMIT.
*
* @return unknown
* @return boolean
*/
public function commit()
{
......@@ -275,7 +287,7 @@ class Doctrine_ORM_EntityManager
{
if ( ! isset($this->_persisters[$entityName])) {
$class = $this->getClassMetadata($entityName);
if ($class->getInheritanceType() == Doctrine_ORM_Mapping_ClassMetadata::INHERITANCE_TYPE_JOINED) {
if ($class->isInheritanceTypeJoined()) {
$persister = new Doctrine_EntityPersister_JoinedSubclass($this, $class);
} else {
$persister = new Doctrine_ORM_Persisters_StandardEntityPersister($this, $class);
......@@ -288,8 +300,8 @@ class Doctrine_ORM_EntityManager
/**
* Detaches an entity from the manager. It's lifecycle is no longer managed.
*
* @param Doctrine_Entity $entity
* @return unknown
* @param Doctrine\ORM\Entity $entity
* @return boolean
*/
public function detach(Doctrine_ORM_Entity $entity)
{
......@@ -342,11 +354,11 @@ class Doctrine_ORM_EntityManager
/**
* Finds an Entity by its identifier.
* This is just a convenient shortcut for getRepository()->find().
* This is just a convenient shortcut for getRepository($entityName)->find($id).
*
* @param string $entityName
* @param mixed $identifier
* @return Doctrine::ORM::Entity
* @return Doctrine\ORM\Entity
*/
public function find($entityName, $identifier)
{
......@@ -391,23 +403,19 @@ class Doctrine_ORM_EntityManager
}
/**
* Clears the persistence context, detaching all entities.
*
* @return void
* @todo package:orm
* Clears the persistence context, effectively detaching all managed entities.
*/
public function clear($entityName = null)
{
if ($entityName === null) {
$this->_unitOfWork->detachAll();
} else {
//...
//TODO
}
}
/**
* Closes the EntityManager.
*
*/
public function close()
{
......@@ -418,7 +426,6 @@ class Doctrine_ORM_EntityManager
* Saves the given entity, persisting it's state.
*
* @param Doctrine\ORM\Entity $entity
* @return void
*/
public function save(Doctrine_ORM_Entity $entity)
{
......@@ -430,10 +437,9 @@ class Doctrine_ORM_EntityManager
}
/**
* Removes the given entity from the persistent store.
* Deletes the persistent state of the given entity.
*
* @param Doctrine\ORM\Entity $entity
* @return void
*/
public function delete(Doctrine_ORM_Entity $entity)
{
......@@ -448,8 +454,7 @@ class Doctrine_ORM_EntityManager
* Refreshes the persistent state of the entity from the database,
* overriding any local changes that have not yet been persisted.
*
* @param Doctrine::ORM::Entity $entity
* @return void
* @param Doctrine\ORM\Entity $entity
* @todo FIX Impl
*/
public function refresh(Doctrine_ORM_Entity $entity)
......@@ -462,8 +467,8 @@ class Doctrine_ORM_EntityManager
/**
* Creates a copy of the given entity. Can create a shallow or a deep copy.
*
* @param Doctrine::ORM::Entity $entity The entity to copy.
* @return Doctrine::ORM::Entity The new entity.
* @param Doctrine\ORM\Entity $entity The entity to copy.
* @return Doctrine\ORM\Entity The new entity.
*/
public function copy(Doctrine_ORM_Entity $entity, $deep = false)
{
......@@ -474,7 +479,7 @@ class Doctrine_ORM_EntityManager
* Gets the repository for an Entity.
*
* @param string $entityName The name of the Entity.
* @return Doctrine::ORM::EntityRepository The repository.
* @return Doctrine\ORM\EntityRepository The repository.
*/
public function getRepository($entityName)
{
......@@ -497,7 +502,7 @@ class Doctrine_ORM_EntityManager
/**
* Checks if the instance is managed by the EntityManager.
*
* @param Doctrine::ORM::Entity $entity
* @param Doctrine\ORM\Entity $entity
* @return boolean TRUE if this EntityManager currently manages the given entity
* (and has it in the identity map), FALSE otherwise.
*/
......@@ -552,6 +557,8 @@ class Doctrine_ORM_EntityManager
/**
* Checks whether this EntityManager is the currently active one.
*
* Note:This is only useful in scenarios where {@link ActiveEntity}s are used.
*
* @return boolean
*/
public function isActive()
......@@ -562,7 +569,7 @@ class Doctrine_ORM_EntityManager
/**
* Makes this EntityManager the currently active one.
*
* @return void
* Note: This is only useful in scenarios where {@link ActiveEntity}s are used.
*/
public function activate()
{
......@@ -582,7 +589,10 @@ class Doctrine_ORM_EntityManager
* @param EventManager $eventManager The EventManager instance to use.
* @return EntityManager The created EntityManager.
*/
public static function create($conn, $name, Doctrine_ORM_Configuration $config = null,
public static function create(
$conn,
$name,
Doctrine_ORM_Configuration $config = null,
Doctrine_Common_EventManager $eventManager = null)
{
if (is_array($conn)) {
......@@ -607,6 +617,8 @@ class Doctrine_ORM_EntityManager
/**
* Static lookup to get the currently active EntityManager.
*
* Note: Used by {@link ActiveEntity}s to actively lookup an EntityManager.
*
* @return Doctrine\ORM\EntityManager
*/
public static function getActiveEntityManager()
......
......@@ -55,7 +55,7 @@ class Doctrine_ORM_Internal_Hydration_ObjectDriver
public function getElementCollection($component)
{
$coll = new Doctrine_ORM_Collection($component);
$coll = new Doctrine_ORM_Collection($this->_em, $component);
$this->_collections[] = $coll;
return $coll;
}
......
<?php
#namespace Doctrine::ORM;
#namespace Doctrine\ORM;
/**
* @todo Migrate the old RawSql to NativeQuery.
......
......@@ -20,7 +20,7 @@
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine::ORM;
#namespace Doctrine\ORM;
/**
* A Doctrine_ORM_Query object represents a DQL query. It is used to query databases for
......@@ -28,7 +28,7 @@
* and is dbms independant.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @link www.doctrine-project.org
* @since 1.0
* @version $Revision: 3938 $
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
......@@ -60,17 +60,17 @@ class Doctrine_ORM_Query extends Doctrine_ORM_Query_Abstract
const HYDRATE_NONE = 5;
/**
* @var Doctrine_ORM_EntityManager The entity manager used by this query object.
* @var Doctrine\ORM\EntityManager The entity manager used by this query object.
*/
protected $_entityManager;
/**
* @var Doctrine_ORM_Internal_Hydration The hydrator object used to hydrate query results.
* @var Doctrine\ORM\Internal\Hydrator The hydrator object used to hydrate query results.
*/
protected $_hydrator;
/**
* @var Doctrine_ORM_Query_ParserResult The parser result that holds DQL => SQL information.
* @var Doctrine\ORM\Query\ParserResult The parser result that holds DQL => SQL information.
*/
protected $_parserResult;
......@@ -120,20 +120,19 @@ class Doctrine_ORM_Query extends Doctrine_ORM_Query_Abstract
/**
* Initializes a new instance of the Query class.
*
* @param EntityManager $entityManager
* @param Doctrine\ORM\EntityManager $entityManager
*/
public function __construct(Doctrine_ORM_EntityManager $entityManager)
{
$this->_entityManager = $entityManager;
$this->_hydrator = new Doctrine_ORM_Internal_Hydration_StandardHydrator($entityManager);
$this->free();
}
/**
* Retrieves the assocated EntityManager to this Doctrine_ORM_Query
*
* @return Doctrine_EntityManager
* @return Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
......@@ -143,7 +142,7 @@ class Doctrine_ORM_Query extends Doctrine_ORM_Query_Abstract
/**
* Returns the hydrator associated with this query object
*
* @return Doctrine_ORM_Internal_Hydration The hydrator associated with this query object
* @return Doctrine\ORM\Internal\StandardHydrator The hydrator associated with this query object
*/
public function getHydrator()
{
......@@ -166,7 +165,7 @@ class Doctrine_ORM_Query extends Doctrine_ORM_Query_Abstract
*
* @param string $params Parameters
* @param int $hydrationMode Hydration mode
* @return mixed Array or Doctrine_Collection or false if no result.
* @return mixed Array or Doctrine\Common\Collection or false if no result.
*/
public function fetchOne($params = array(), $hydrationMode = null)
{
......@@ -191,7 +190,6 @@ class Doctrine_ORM_Query extends Doctrine_ORM_Query_Abstract
* @param string $query DQL query
* @param array $params prepared statement parameters
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
* @see Doctrine::FETCH_* constants
* @return mixed
*/
public function query($query, $params = array(), $hydrationMode = null)
......
<?php
/*
* $Id: Cache.php 3938 2008-03-06 19:36:50Z romanb $
*
......@@ -20,12 +19,14 @@
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine\ORM\Query;
/**
* Doctrine_ORM_Query_CacheHandler
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @link www.doctrine-project.com
* @since 2.0
* @version $Revision: 1393 $
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
......
......@@ -650,19 +650,28 @@ class Doctrine_ORM_UnitOfWork
public function getEntityState($entity)
{
$oid = spl_object_hash($entity);
return isset($this->_entityStates[$oid]) ? $this->_entityStates[$oid] : self::STATE_NEW;
if ( ! isset($this->_entityStates[$oid])) {
if (isset($this->_entityIdentifiers[$oid])) {
$this->_entityStates[$oid] = self::STATE_DETACHED;
} else {
$this->_entityStates[$oid] = self::STATE_NEW;
}
}
return $this->_entityStates[$oid];
}
/**
* Removes an entity from the identity map.
* Removes an entity from the identity map. This effectively detaches the
* entity from the persistence management of Doctrine.
*
* @param Doctrine\ORM\Entity $entity
* @return boolean
*/
public function removeFromIdentityMap($entity)
{
$oid = spl_object_hash($entity);
$classMetadata = $this->_em->getClassMetadata(get_class($entity));
$idHash = $this->getIdentifierHash($this->_entityIdentifiers[spl_object_hash($entity)]);
$idHash = $this->getIdentifierHash($this->_entityIdentifiers[$oid]);
if ($idHash === '') {
throw new Doctrine_Exception("Entity with oid '" . spl_object_hash($entity)
. "' has no identity and therefore can't be removed from the identity map.");
......@@ -670,6 +679,7 @@ class Doctrine_ORM_UnitOfWork
$className = $classMetadata->getRootClassName();
if (isset($this->_identityMap[$className][$idHash])) {
unset($this->_identityMap[$className][$idHash]);
$this->_entityStates[$oid] = self::STATE_DETACHED;
return true;
}
......@@ -677,7 +687,7 @@ class Doctrine_ORM_UnitOfWork
}
/**
* Finds an entity in the identity map by its identifier hash.
* Gets an entity in the identity map by its identifier hash.
*
* @param string $idHash
* @param string $rootClassName
......@@ -692,8 +702,8 @@ class Doctrine_ORM_UnitOfWork
* Tries to get an entity by its identifier hash. If no entity is found for
* the given hash, FALSE is returned.
*
* @param <type> $idHash
* @param <type> $rootClassName
* @param string $idHash
* @param string $rootClassName
* @return mixed The found entity or FALSE.
*/
public function tryGetByIdHash($idHash, $rootClassName)
......@@ -760,7 +770,7 @@ class Doctrine_ORM_UnitOfWork
/**
* Saves an entity as part of the current unit of work.
*
* @param Doctrine_ORM_Entity $entity The entity to save.
* @param Doctrine\ORM\Entity $entity The entity to save.
*/
public function save($entity)
{
......@@ -775,7 +785,7 @@ class Doctrine_ORM_UnitOfWork
foreach ($commitOrder as $class) {
$this->_executeInserts($class);
}
// remove them from _newEntities
// remove them from _newEntities and _dataChangeSets
$this->_newEntities = array_diff_key($this->_newEntities, $insertNow);
$this->_dataChangeSets = array_diff_key($this->_dataChangeSets, $insertNow);
}
......@@ -786,7 +796,7 @@ class Doctrine_ORM_UnitOfWork
* This method is internally called during save() cascades as it tracks
* the already visited entities to prevent infinite recursions.
*
* @param Doctrine_ORM_Entity $entity The entity to save.
* @param Doctrine\ORM\Entity $entity The entity to save.
* @param array $visited The already visited entities.
*/
private function _doSave($entity, array &$visited, array &$insertNow)
......@@ -909,33 +919,61 @@ class Doctrine_ORM_UnitOfWork
}
}
/**
* Cascades the delete operation to associated entities.
*
* @param Doctrine\ORM\Entity $entity
*/
private function _cascadeDelete($entity)
{
$class = $this->_em->getClassMetadata(get_class($entity));
foreach ($class->getAssociationMappings() as $assocMapping) {
if ( ! $assocMapping->isCascadeDelete()) {
continue;
}
$relatedEntities = $class->getReflectionProperty($assocMapping->getSourceFieldName())
->getValue($entity);
if ($relatedEntities instanceof Doctrine_ORM_Collection &&
count($relatedEntities) > 0) {
foreach ($relatedEntities as $relatedEntity) {
$this->_doDelete($relatedEntity, $visited, $insertNow);
}
} else if (is_object($relatedEntities)) {
$this->_doDelete($relatedEntities, $visited, $insertNow);
}
}
}
/**
* Gets the CommitOrderCalculator used by the UnitOfWork to order commits.
*
* @return Doctrine\ORM\Internal\CommitOrderCalculator
*/
public function getCommitOrderCalculator()
{
return $this->_commitOrderCalculator;
}
/**
* Closes the UnitOfWork.
*/
public function close()
{
//...
$this->_commitOrderCalculator->clear();
}
public function scheduleCollectionUpdate(Doctrine_Collection $coll)
public function scheduleCollectionUpdate(Doctrine_ORM_Collection $coll)
{
$this->_collectionUpdates[] = $coll;
}
public function isCollectionScheduledForUpdate(Doctrine_Collection $coll)
public function isCollectionScheduledForUpdate(Doctrine_ORM_Collection $coll)
{
//...
}
public function scheduleCollectionDeletion(Doctrine_Collection $coll)
public function scheduleCollectionDeletion(Doctrine_ORM_Collection $coll)
{
//TODO: if $coll is already scheduled for recreation ... what to do?
// Just remove $coll from the scheduled recreations?
......@@ -1097,7 +1135,9 @@ class Doctrine_ORM_UnitOfWork
* INTERNAL:
* For hydration purposes only.
*
* Adds a managed collection to the UnitOfWork.
* Adds a managed collection to the UnitOfWork. On commit time, the UnitOfWork
* checks all these managed collections for modifications and then initiates
* the appropriate database synchronization.
*
* @param Doctrine\ORM\Collection $coll
*/
......
<?php
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'AllTests::main');
}
......
......@@ -6,7 +6,6 @@ if (!defined('PHPUnit_MAIN_METHOD')) {
require_once 'lib/DoctrineTestInit.php';
// Suites
require_once 'Orm/Component/AllTests.php';
require_once 'Orm/Query/AllTests.php';
require_once 'Orm/Hydration/AllTests.php';
require_once 'Orm/Ticket/AllTests.php';
......@@ -17,6 +16,7 @@ require_once 'Orm/Associations/AllTests.php';
require_once 'Orm/UnitOfWorkTest.php';
require_once 'Orm/EntityManagerTest.php';
require_once 'Orm/EntityPersisterTest.php';
require_once 'Orm/CommitOrderCalculatorTest.php';
class Orm_AllTests
{
......@@ -32,8 +32,8 @@ class Orm_AllTests
$suite->addTestSuite('Orm_UnitOfWorkTest');
$suite->addTestSuite('Orm_EntityManagerTest');
$suite->addTestSuite('Orm_EntityPersisterTest');
$suite->addTestSuite('Orm_CommitOrderCalculatorTest');
$suite->addTest(Orm_Component_AllTests::suite());
$suite->addTest(Orm_Query_AllTests::suite());
$suite->addTest(Orm_Hydration_AllTests::suite());
$suite->addTest(Orm_Entity_AllTests::suite());
......
......@@ -8,20 +8,21 @@ require_once 'lib/DoctrineTestInit.php';
* can have many valid orderings, so you may want to build a graph that has only
* 1 valid order to simplify your tests.
*/
class Orm_Internal_CommitOrderCalculatorTest extends Doctrine_OrmTestCase
class Orm_CommitOrderCalculatorTest extends Doctrine_OrmTestCase
{
private $_calc;
protected function setUp()
{
$this->_calc = new Doctrine_Internal_CommitOrderCalculator();
$this->_calc = new Doctrine_ORM_Internal_CommitOrderCalculator();
}
/** Helper to create an array of nodes */
private function _createNodes(array $names)
{
$nodes = array();
foreach ($names as $name) {
$node = new Doctrine_Internal_CommitOrderNode($name, $this->_calc);
$node = new Doctrine_ORM_Internal_CommitOrderNode($name, $this->_calc);
$nodes[$name] = $node;
$this->_calc->addNode($node->getClass(), $node);
}
......@@ -46,6 +47,4 @@ class Orm_Internal_CommitOrderCalculatorTest extends Doctrine_OrmTestCase
$this->assertSame($correctOrder, $sorted);
}
}
<?php
/*
* $Id: Doctrine.php 3754 2008-02-13 10:53:07Z romanb $
*
* 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>.
*/
/**
* Testcase for basic accessor/mutator functionality.
*
* @package Doctrine
* @author Bjarte Stien Karlsen <doctrine@bjartek.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @since 2.0
* @version $Revision: 3754 $
*/
require_once 'lib/DoctrineTestInit.php';
class AccessStub extends Doctrine_Access {}
class Orm_Component_AccessTest extends Doctrine_OrmTestCase
{
private $user;
public function setUp()
{
parent::setUp();
$this->user = new ForumUser();
}
/*public function testAccessorOverridePerformance() {
$this->user->username;
$start = microtime(true);
for ($i = 0; $i < 1; $i++) {
$this->user->username;
}
$end = microtime(true);
echo ($end - $start) . " seconds" . PHP_EOL;
}*/
/**
* @test
*/
public function shouldMarkEmptyFieldAsNotSetOnNewRecord()
{
$this->assertFalse(isset($this->user->username));
$this->assertFalse(isset($this->user['username']));
}
/**
* @test
*/
public function shouldMarkNonExistantFieldAsNotSetOnNewRecord()
{
$this->assertFalse(isset($this->user->rat));
$this->assertFalse(isset($this->user['rat']));
}
/**
* @test
*/
public function shouldSetSingleValueInRecord()
{
$this->user->username = 'meus';
$this->assertEquals('meus', $this->user->username);
$this->assertEquals('meus', $this->user['username']);
}
/**
* @test
*/
public function shouldSetSingleValueInRecordWithOffset()
{
$this->user['username'] ='meus';
$this->assertEquals('meus', $this->user->username);
$this->assertEquals('meus', $this->user['username']);
}
/**
* @test
* @expectedException Doctrine_ORM_Exceptions_EntityException
*/
public function shouldNotBeAbleToSetNonExistantField()
{
$this->user->rat = 'meus';
}
/**
* @test
* @expectedException Doctrine_ORM_Exceptions_EntityException
*/
public function shouldNotBeAbleToSetNonExistantFieldWithOffset()
{
$this->user['rat'] = 'meus';
}
/**
* @test
*/
public function newCollectionShouldBeEmpty()
{
$col = new Doctrine_Collection('ForumUser');
$this->assertEquals(0, count($col));
$this->assertFalse(isset($coll[0]));
}
/**
* @test
*/
public function shouldBeAbleToUnsetWithOffsetFromCollection()
{
$col = new Doctrine_Collection('ForumUser');
$col[0] = new ForumUser();
$this->assertTrue(isset($col[0]));
unset($col[0]);
$this->assertFalse(isset($col[0]));
}
/**
* @test
*/
public function shouldBeAbleToUnsetFromCollection()
{
$col = new Doctrine_Collection('ForumUser');
$col->test = new ForumUser();
$this->assertTrue(isset($col->test));
unset($col->test);
$this->assertFalse(isset($col->test));
}
/**
* @test
* @expectedException Doctrine_Exception
*/
public function shouldNotBeAbleToUseContainsWhenNotImplemented()
{
$stub = new AccessStub();
isset($stub['foo']);
}
/**
* @test
* @expectedException Doctrine_Exception
*/
public function shouldNotBeAbleToUseSetWhenNotImplemented()
{
$stub = new AccessStub();
$stub['foo'] = 'foo';
}
/**
* @test
* @expectedException Doctrine_Exception
*/
public function shouldNotBeAbleToUseUnsetWhenNotImplemented()
{
$stub = new AccessStub();
unset($stub['foo']);
}
/**
* @test
* @expectedException Doctrine_Exception
*/
public function shouldNotBeAbleToUseGetWhenNotImplemented()
{
$stub = new AccessStub();
$stub['foo'];
}
}
<?php
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Component_AllTests::main');
}
require_once 'lib/DoctrineTestInit.php';
// Tests
//require_once 'Orm/Component/AccessTest.php';
require_once 'Orm/Component/CollectionTest.php';
class Orm_Component_AllTests
{
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new Doctrine_TestSuite('Doctrine Orm Component');
//$suite->addTestSuite('Orm_Component_AccessTest');
$suite->addTestSuite('Orm_Component_CollectionTest');
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Component_AllTests::main') {
Orm_Component_AllTests::main();
}
<?php /* vim: set et sw=4 ts=4: */
/*
* $Id: Doctrine.php 3754 2008-02-13 10:53:07Z romanb $
*
* 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>.
*/
/**
* Doctrine
* the base class of Doctrine framework
*
* @package Doctrine
* @author Bjarte Stien Karlsen <doctrine@bjartek.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @since 2.0
* @version $Revision: 3754 $
*/
require_once 'lib/DoctrineTestInit.php';
class Orm_Component_CollectionTest extends Doctrine_OrmTestCase
{
private $coll;
public function setUp()
{
parent::setUp();
$this->coll = new Doctrine_ORM_Collection('ForumUser');
//we create a CmsUser with username as key column and add a user to it
$cmsColl = new Doctrine_ORM_Collection('CmsUser', 'username');
$user = new CmsUser();
$user->username ='test';
$cmsColl[] = $user;
$this->cmsColl = $cmsColl;
$this->cmsUser = $user;
}
/**
* @test
*/
public function shouldHaveBlankAsDefaultKeyColumn()
{
$this->assertEquals('', $this->coll->getKeyField());
}
/**
* @test
*/
public function shouldUseSpecifiedKeyColumn()
{
$coll = new Doctrine_ORM_Collection('ForumUser', 'id');
$this->assertEquals('id', $coll->getKeyField());
}
/**
* This test is currently failing. I do not understand why it should be
* possible to set this to something that is not valid.
*
* @test
* @expectedException Doctrine_Exception
*/
public function shouldThrowExceptionIfNonValidFieldSetAsKey()
{
$coll = new Doctrine_ORM_Collection('ForumUser', 'xxNonValidFieldxx');
}
/**
* @test
*/
public function shouldSerializeEmptyCollection()
{
$serialized = serialize($this->coll);
$this->assertTrue(is_string($serialized));
}
/**
* @test
*/
public function shouldUnserializeEmptyCollectionIntoObject()
{
$serialized = serialize($this->coll);
$coll = unserialize($serialized);
$this->assertEquals('Doctrine_ORM_Collection', get_class($coll));
}
/**
* @test
*/
/*public function shouldSetKeyColumnWhenAddingNewRowAsArray()
{
$this->assertTrue(isset($this->cmsColl['test']));
$this->assertEquals($this->cmsUser, $this->cmsColl['test']);
}*/
/**
* @test
*/
/*public function shouldSerializeAndUnserializeCollectionWithData()
{
$serialized = serialize($this->cmsColl);
$coll = unserialize($serialized);
$this->assertEquals('username', $coll->getKeyField());
$this->assertTrue(isset($coll['test']));
$user = $coll['test'];
$this->assertTrue($user instanceOf CmsUser);
$this->assertEquals('test', $user['username']);
}*/
}
<?php
/* CURRENTLY NOT USED */
require_once 'lib/DoctrineTestInit.php';
class Orm_Entity_AccessorTest extends Doctrine_OrmTestCase
......
......@@ -12,7 +12,7 @@ class Orm_Entity_ConstructorTest extends Doctrine_OrmTestCase
class ConstructorTestEntity1
{
public $id;
private $id;
public $username;
public function __construct($username = null)
......@@ -45,4 +45,3 @@ class ConstructorTestEntity1
}
}
?>
\ No newline at end of file
<?php
require_once 'lib/DoctrineTestInit.php';
#namespace Doctrine::Tests::ORM;
/**
* EntityManagerFactory tests.
*/
class Orm_EntityManagerFactoryTest extends Doctrine_OrmTestCase
{
private $_mockOptions = array('driver' => 'mock', 'user' => '', 'password' => '');
protected function tearDown() {
parent::tearDown();
}
private function _createNamedManager($name)
{
return $this->_emf->createEntityManager($this->_mockOptions, $name);
}
/*public function testBindingEntityToNamedManager()
{
$myEM = $this->_createNamedManager('myEM');
$this->_emf->bindEntityToManager('SomeEntity', 'myEM');
$this->assertSame($myEM, $this->_emf->getEntityManager('SomeEntity'));
$this->_emf->releaseEntityManager($myEM);
}
public function testStaticLookup()
{
$this->assertTrue(Doctrine_EntityManagerFactory::getManager() instanceof Doctrine_EntityManager);
}*/
}
\ No newline at end of file
<?php
require_once 'lib/DoctrineTestInit.php';
#namespace Doctrine::Tests::ORM;
#namespace Doctrine\Tests\ORM;
/**
* EntityManager tests.
*/
class Orm_EntityManagerTest extends Doctrine_OrmTestCase
{
private $_em;
function setUp() {
parent::setUp();
$this->_em = $this->_getTestEntityManager();
}
public function testSettingInvalidFlushModeThrowsException()
{
$prev = $this->_em->getFlushMode();
......
......@@ -7,9 +7,12 @@ require_once 'lib/mocks/Doctrine_HydratorMockStatement.php';
class Orm_Hydration_BasicHydrationTest extends Doctrine_OrmTestCase
{
private $_em;
protected function setUp()
{
parent::setUp();
$this->_em = $this->_getTestEntityManager();
}
/** Getter for the hydration mode dataProvider */
......
......@@ -36,6 +36,12 @@ require_once 'lib/DoctrineTestInit.php';
*/
class Orm_Query_IdentifierRecognitionTest extends Doctrine_OrmTestCase
{
private $_em;
protected function setUp() {
parent::setUp();
$this->_em = $this->_getTestEntityManager();
}
public function testSingleAliasDeclarationIsSupported()
{
......
<?php
#namespace Doctrine\Tests\ORM;
require_once 'lib/DoctrineTestInit.php';
require_once 'lib/mocks/Doctrine_EntityManagerMock.php';
require_once 'lib/mocks/Doctrine_ConnectionMock.php';
......@@ -7,25 +10,20 @@ require_once 'lib/mocks/Doctrine_IdentityIdGeneratorMock.php';
/**
* UnitOfWork tests.
* These tests run without a database through mocking the
* persister/connection/sequence used by the UnitOfWork.
*/
class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
{
// SUT
private $_unitOfWork;
// Mocks
// Provides a sequence mock to the UnitOfWork
private $_connectionMock;
// The EntityManager mock that provides the mock persister
// The EntityManager mock that provides the mock persisters
private $_emMock;
protected function setUp() {
parent::setUp();
$this->_connectionMock = new Doctrine_ConnectionMock(array());
$this->_emMock = Doctrine_EntityManagerMock::create($this->_connectionMock, "uowMockEm");
// SUT
$this->_unitOfWork = new Doctrine_UnitOfWorkMock($this->_emMock);
$this->_emMock->setUnitOfWork($this->_unitOfWork);
......@@ -136,6 +134,7 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
// Fake managed state
$this->_unitOfWork->setEntityState($user2, Doctrine_ORM_UnitOfWork::STATE_MANAGED);
// Fake original entity date
$this->_unitOfWork->setOriginalEntityData($user1, array(
'id' => 1, 'username' => 'roman'
));
......@@ -143,8 +142,10 @@ class Orm_UnitOfWorkTest extends Doctrine_OrmTestCase
'id' => 2, 'username' => 'jon'
));
// Go
$this->_unitOfWork->computeDataChangeSet(array($user1, $user2));
// Verify
$user1ChangeSet = $this->_unitOfWork->getDataChangeSet($user1);
$this->assertTrue(is_array($user1ChangeSet));
$this->assertEquals(2, count($user1ChangeSet));
......
<?php
/*
* This file bootstraps the test environment.
*/
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
//require_once '../lib/Doctrine.php';
require_once '../lib/Doctrine/Common/ClassLoader.php';
$classLoader = new Doctrine_Common_ClassLoader();
......
<?php
#namespace Doctrine\Tests;
/**
* Base testcase class for all dbal testcases.
*/
class Doctrine_DbalTestCase extends Doctrine_TestCase
{
protected $_conn;
/**
* setUp()
*/
protected function setUp()
{
// Setup a db connection if there is none, yet. This makes it possible
// to run tests that use a connection standalone.
if (isset($this->sharedFixture['conn'])) {
$this->_conn = $this->sharedFixture['conn'];
} else {
$this->sharedFixture['conn'] = Doctrine_TestUtil::getConnection();
$this->_conn = $this->sharedFixture['conn'];
}
}
}
\ No newline at end of file
<?php
#namespace Doctrine\Tests;
/**
* The outermost test suite for all dbal related testcases & suites.
*
* Currently the dbal suite uses a normal connection object, too, just like the orm suite.
* Upon separation of the DBAL and ORM package this suite should just use a DBAL
* connection in the shared fixture.
*/
class Doctrine_DbalTestSuite extends Doctrine_TestSuite
{
protected function setUp()
{
$this->sharedFixture['conn'] = Doctrine_TestUtil::getConnection();
}
protected function tearDown()
{}
}
\ No newline at end of file
<?php
#namespace Doctrine\Tests;
/**
* Base testcase class for all orm testcases.
*
......
<?php
#namespace Doctrine\Tests;
/**
* The outermost test suite for all orm related testcases & suites.
*
......
<?php
#namespace Doctrine\Tests;
require_once 'lib/mocks/Doctrine_DriverMock.php';
require_once 'lib/mocks/Doctrine_ConnectionMock.php';
/**
* Base testcase class for all orm testcases.
*
* Base testcase class for all ORM testcases.
*/
class Doctrine_OrmTestCase extends Doctrine_TestCase
{
protected $_em;
protected function setUp() {
if (isset($this->sharedFixture['em'])) {
$this->_em = $this->sharedFixture['em'];
} else {
/**
* Creates an EntityManager for testing purposes.
*
* @return Doctrine\ORM\EntityManager
*/
protected function _getTestEntityManager($conf = null, $eventManager = null) {
$config = new Doctrine_ORM_Configuration();
$eventManager = new Doctrine_Common_EventManager();
$connectionOptions = array(
......@@ -23,9 +24,6 @@ class Doctrine_OrmTestCase extends Doctrine_TestCase
'user' => 'john',
'password' => 'wayne'
);
$em = Doctrine_ORM_EntityManager::create($connectionOptions, 'mockEM', $config, $eventManager);
$this->_em = $em;
}
$this->_em->activate();
return Doctrine_ORM_EntityManager::create($connectionOptions, 'mockEM', $config, $eventManager);
}
}
<?php
#namespace Doctrine\Tests;
/**
* The outermost test suite for all orm related testcases & suites.
*
* Currently the orm suite uses a normal connection object.
* Upon separation of the DBAL and ORM package this suite should just use a orm
* connection/session/manager instance as the shared fixture.
*/
class Doctrine_OrmTestSuite extends Doctrine_TestSuite
{
protected function setUp()
{
$config = new Doctrine_ORM_Configuration();
$eventManager = new Doctrine_Common_EventManager();
$connectionOptions = array(
'driverClass' => 'Doctrine_DriverMock',
'wrapperClass' => 'Doctrine_ConnectionMock',
'user' => 'john',
'password' => 'wayne'
);
$em = Doctrine_ORM_EntityManager::create($connectionOptions, 'mockEM', $config, $eventManager);
$this->sharedFixture['em'] = $em;
}
protected function tearDown()
{}
}
\ No newline at end of file
<?php
#namespace Doctrine\Tests;
/**
* Base testcase class for all Doctrine testcases.
*/
class Doctrine_TestCase extends PHPUnit_Framework_TestCase
{
}
\ No newline at end of file
<?php
#namespace Doctrine\Tests;
/**
* Doctrine's basic test suite implementation. Provides functionality needed by all
* test suites.
*/
class Doctrine_TestSuite extends PHPUnit_Framework_TestSuite
{
}
\ No newline at end of file
<?php
#namespace Doctrine\Tests;
class Doctrine_TestUtil
{
public static function getConnection()
......
<?php
#namespace Doctrine\Tests\Mocks;
class Doctrine_ClassMetadataMock extends Doctrine_ORM_Mapping_ClassMetadata
{
/* Mock API */
......@@ -10,4 +12,3 @@ class Doctrine_ClassMetadataMock extends Doctrine_ORM_Mapping_ClassMetadata
}
?>
\ No newline at end of file
<?php
#namespace Doctrine\Tests\Mocks;
#use Doctrine\DBAL\Connection;
require_once 'lib/mocks/Doctrine_SequenceMock.php';
require_once 'lib/mocks/Doctrine_DatabasePlatformMock.php';
......@@ -73,4 +77,3 @@ class Doctrine_ConnectionMock extends Doctrine_DBAL_Connection
}
}
?>
\ No newline at end of file
<?php
#namespace Doctrine\Tests\Mocks;
#use Doctrine\DBAL\Platforms\AbstractPlatform;
class Doctrine_DatabasePlatformMock extends Doctrine_DBAL_Platforms_AbstractPlatform
{
private $_prefersIdentityColumns = false;
......@@ -30,4 +34,3 @@ class Doctrine_DatabasePlatformMock extends Doctrine_DBAL_Platforms_AbstractPlat
}
?>
\ No newline at end of file
<?php
#namespace Doctrine\Tests\Mocks;
class Doctrine_DriverConnectionMock implements Doctrine_DBAL_Driver_Connection
{
public function prepare($prepareString) {}
......@@ -14,4 +16,3 @@ class Doctrine_DriverConnectionMock implements Doctrine_DBAL_Driver_Connection
public function errorInfo() {}
}
?>
\ No newline at end of file
<?php
#namespace Doctrine\Tests\Mocks;
require_once 'lib/mocks/Doctrine_DriverConnectionMock.php';
require_once 'lib/mocks/Doctrine_DatabasePlatformMock.php';
......
<?php
#namespace Doctrine\Tests\Mocks;
require_once 'lib/mocks/Doctrine_EntityPersisterMock.php';
/**
......@@ -88,4 +90,3 @@ class Doctrine_EntityManagerMock extends Doctrine_ORM_EntityManager
}
}
?>
\ No newline at end of file
<?php
#namespace Doctrine\Tests\Mocks;
/**
* EntityPersister implementation used for mocking during tests.
*/
......
<?php
#namespace Doctrine\Tests\Mocks;
/**
* This class is a mock of the PDOStatement class that can be passed in to the Hydrator
* to test the hydration standalone with faked result sets.
......
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
#namespace Doctrine\Tests\Mocks;
/**
* Description of Doctrine_IdentityIdGeneratorMock
......@@ -17,4 +15,4 @@ class Doctrine_IdentityIdGeneratorMock extends Doctrine_ORM_Id_IdentityGenerator
$this->_mockPostInsertId = $id;
}
}
?>
<?php
#namespace Doctrine\Tests\Mocks;
class Doctrine_SequenceMock extends Doctrine_ORM_Id_SequenceGenerator
{
private $_sequenceNumber = 0;
......@@ -52,4 +54,3 @@ class Doctrine_SequenceMock extends Doctrine_ORM_Id_SequenceGenerator
}
}
?>
\ No newline at end of file
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
#namespace Doctrine\Tests\Mocks;
/**
* Description of Doctrine_UnitOfWorkMock
......
<?php
#namespace Doctrine\Tests\Models\Forum;
class ForumAdministrator extends ForumUser
{
public $accessLevel;
......
<?php
#namespace Doctrine\Tests\ORM\Models\Forum;
#namespace Doctrine\Tests\Models\Forum;
#use Doctrine\ORM\Entity;
#use Doctrine\Common\VirtualPropertySystem;
class ForumAvatar
{
public $id;
/*static function construct() {
Doctrine_Common_VirtualPropertySystem::register(__CLASS__, 'id', 'int');
}*/
public static function initMetadata($mapping)
{
$mapping->mapField(array(
......
<?php
#namespace Doctrine\Tests\Models\Forum;
class ForumBoard
{
public $id;
......
<?php
#namespace Doctrine\Tests\Models\Forum;
class ForumCategory
{
private $id;
......
<?php
#namespace Doctrine::Test::ORM::Models;
#namespace Doctrine\Tests\Models\Forum;
#use Doctrine::ORM::Entity;
#use Doctrine\ORM\Entity;
class ForumEntry extends Doctrine_ORM_Entity
class ForumEntry implements Doctrine_ORM_Entity
{
public $id;
public $topic;
static function construct() {
Doctrine_Common_VirtualPropertySystem::register(__CLASS__, 'id', 'int');
Doctrine_Common_VirtualPropertySystem::register(__CLASS__, 'topic', 'string');
}
public static function initMetadata($mapping)
{
$mapping->mapField(array(
......@@ -33,4 +28,3 @@ class ForumEntry extends Doctrine_ORM_Entity
}
?>
\ No newline at end of file
<?php
#namespace Doctrine\Tests\ORM\Models\Forum;
#namespace Doctrine\Tests\Models\Forum;
#use Doctrine\ORM\Entity;
#use Doctrine\Common\VirtualPropertySystem;
class ForumUser
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment