Permalink
Browse files

First tests for basic collection implementation. First experimental u…

…se of closures (Currently commented out, because the svn server makes a syntax check against 5.2.x).
  • Loading branch information...
1 parent 73ad0ac commit 34f4ee71faceab5691c2af150c9815a50e7e4235 romanb committed Jan 12, 2009
Showing with 315 additions and 219 deletions.
  1. +87 −88 lib/Doctrine/Common/Collections/Collection.php
  2. +1 −1 lib/Doctrine/DBAL/Driver.php
  3. +3 −3 lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php
  4. +3 −3 lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php
  5. +14 −14 lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
  6. +0 −1 lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
  7. 0 lib/Doctrine/DBAL/Schema/{MySqlSchemaManger.php → MySqlSchemaManager.php}
  8. +5 −5 lib/Doctrine/ORM/Internal/Hydration/ObjectDriver.php
  9. +4 −5 lib/Doctrine/ORM/Internal/Hydration/StandardHydrator.php
  10. +10 −5 lib/Doctrine/ORM/Mapping/ClassMetadata.php
  11. +8 −6 lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
  12. +2 −54 lib/Doctrine/ORM/Persisters/AbstractEntityPersister.php
  13. +3 −3 lib/Doctrine/ORM/Query.php
  14. +1 −1 lib/Doctrine/ORM/Query/ParserResultDummy.php
  15. +2 −2 lib/Doctrine/ORM/Query/SqlExecutor/Abstract.php
  16. +2 −2 lib/Doctrine/ORM/Query/SqlExecutor/SingleSelect.php
  17. +19 −10 lib/Doctrine/ORM/UnitOfWork.php
  18. +2 −0 tests/AllTests.php
  19. +30 −0 tests/Common/AllTests.php
  20. +30 −0 tests/Common/Collections/AllTests.php
  21. +44 −0 tests/Common/Collections/CollectionTest.php
  22. +2 −0 tests/Orm/AllTests.php
  23. +16 −6 tests/Orm/Functional/BasicCRUDTest.php
  24. +4 −1 tests/Orm/Mapping/ClassMetadataFactoryTest.php
  25. +6 −6 tests/Orm/Mapping/ClassMetadataTest.php
  26. +1 −0 tests/dbproperties.xml.dev
  27. +7 −0 tests/lib/Doctrine_OrmFunctionalTestCase.php
  28. +6 −1 tests/lib/Doctrine_OrmFunctionalTestSuite.php
  29. +3 −2 tests/lib/Doctrine_TestUtil.php
@@ -13,8 +13,7 @@
/**
* A Collection is a wrapper around a php array and just like a php array a
- * collection instance can be a list, a map or a hashmap, depending on how it
- * is used.
+ * collection instance can be a list, a set or a map, depending on how it is used.
*
* @author robo
*/
@@ -28,41 +27,40 @@ class Doctrine_Common_Collections_Collection implements Countable, IteratorAggre
protected $_data = array();
/**
- * Unwraps the array contained in the Collection instance.
*
- * @return array The wrapped array.
+ * @param <type> $elements
*/
- public function unwrap()
+ public function __construct(array $elements = array())
{
- return $this->_data;
+ $this->_data = $elements;
}
/**
- * returns the first record in the collection
+ * Unwraps the array contained in the Collection instance.
*
- * @return mixed
+ * @return array The wrapped array.
*/
- public function getFirst()
+ public function unwrap()
{
- return reset($this->_data);
+ return $this->_data;
}
/**
- * returns the last record in the collection
+ * returns the first entry in the collection
*
* @return mixed
*/
- public function getLast()
+ public function first()
{
- return end($this->_data);
+ return reset($this->_data);
}
/**
* returns the last record in the collection
*
* @return mixed
*/
- public function end()
+ public function last()
{
return end($this->_data);
}
@@ -78,10 +76,10 @@ public function key()
}
/**
- * Removes an entry from the collection.
+ * Removes an entry with a specific key from the collection.
*
* @param mixed $key
- * @return boolean
+ * @return mixed
*/
public function remove($key)
{
@@ -104,34 +102,32 @@ public function __isset($key)
/**
* __unset()
*
- * @param string $name
- * @since 1.0
+ * @param string $key
* @return mixed
*/
public function __unset($key)
{
return $this->remove($key);
}
+ /* ArrayAccess implementation */
+
/**
- * Check if an offsetExists.
- *
- * Part of the ArrayAccess implementation.
+ * Check if an offset exists.
*
* @param mixed $offset
- * @return boolean whether or not this object contains $offset
+ * @return boolean Whether or not this object contains $offset
*/
public function offsetExists($offset)
{
return $this->containsKey($offset);
}
/**
- * offsetGet an alias of get()
+ * Gets the element with the given key.
*
* Part of the ArrayAccess implementation.
*
- * @see get, __get
* @param mixed $offset
* @return mixed
*/
@@ -169,10 +165,12 @@ public function offsetUnset($offset)
return $this->remove($offset);
}
+ /* END ArrayAccess implementation */
+
/**
- * Checks whether the collection contains an entity.
+ * Checks whether the collection contains a specific key/index.
*
- * @param mixed $key the key of the element
+ * @param mixed $key The key to check for.
* @return boolean
*/
public function containsKey($key)
@@ -181,14 +179,30 @@ public function containsKey($key)
}
/**
- * Enter description here...
+ * Checks whether the given element is contained in the collection.
+ * Only element values are compared, not keys. The comparison of two elements
+ * is strict, that means not only the value but also the type must match.
+ * For objects this means reference equality.
*
- * @param unknown_type $entity
- * @return unknown
+ * @param mixed $element
+ * @return boolean
*/
- public function contains($entity)
+ public function contains($element)
{
- return in_array($entity, $this->_data, true);
+ return in_array($element, $this->_data, true);
+ }
+
+ /**
+ * Tests for the existance of an element that satisfies the given predicate.
+ *
+ * @param function $func
+ * @return boolean
+ */
+ public function exists($func) {
+ foreach ($this->_data as $key => $element)
+ if ($func($key, $element))
+ return true;
+ return false;
}
/**
@@ -203,20 +217,24 @@ public function containsAll($otherColl)
}
/**
+ * Searches for a given element and, if found, returns the corresponding key/index
+ * of that element. The comparison of two elements is strict, that means not
+ * only the value but also the type must match.
+ * For objects this means reference equality.
*
+ * @param mixed $element The element to search for.
+ * @return mixed The key/index of the element or FALSE if the element was not found.
*/
- public function search($record)
+ public function search($element)
{
- return array_search($record, $this->_data, true);
+ return array_search($element, $this->_data, true);
}
/**
- * returns a record for given key
- *
- * Collection also maps referential information to newly created records
+ * Gets the element with the given key/index.
*
- * @param mixed $key the key of the element
- * @return Doctrine_Entity return a specified record
+ * @param mixed $key The key.
+ * @return mixed The element or NULL, if no element exists for the given key.
*/
public function get($key)
{
@@ -227,8 +245,7 @@ public function get($key)
}
/**
- * Gets all keys.
- * (Map method)
+ * Gets all keys/indexes.
*
* @return array
*/
@@ -238,69 +255,51 @@ public function getKeys()
}
/**
- * Gets all values.
- * (Map method)
+ * Gets all elements.
*
* @return array
*/
- public function getValues()
+ public function getElements()
{
return array_values($this->_data);
}
/**
- * Returns the number of records in this collection.
+ * Returns the number of elements in the collection.
*
* Implementation of the Countable interface.
*
- * @return integer The number of records in the collection.
+ * @return integer The number of elements in the collection.
*/
public function count()
{
return count($this->_data);
}
/**
+ * Adds/sets an element in the collection at the index / with the specified key.
+ *
* When the collection is a Map this is like put(key,value)/add(key,value).
* When the collection is a List this is like add(position,value).
*
* @param integer $key
* @param mixed $value
- * @return void
*/
public function set($key, $value)
{
- if ( ! $value instanceof Doctrine_ORM_Entity) {
- throw new Doctrine_Collection_Exception('Value variable in set is not an instance of Doctrine_Entity');
- }
$this->_data[$key] = $value;
- //TODO: Register collection as dirty with the UoW if necessary
- $this->_changed();
}
/**
- * Adds an entry to the collection.
+ * Adds an element to the collection.
*
* @param mixed $value
* @param string $key
- * @return boolean
+ * @return boolean Always returns TRUE.
*/
- public function add($value, $key = null)
+ public function add($value)
{
- // TODO: Really prohibit duplicates?
- if (in_array($value, $this->_data, true)) {
- return false;
- }
-
- if (isset($key)) {
- if (isset($this->_data[$key])) {
- return false;
- }
- $this->_data[$key] = $value;
- } else {
- $this->_data[] = $value;
- }
-
+ $this->_data[] = $value;
return true;
}
@@ -316,6 +315,7 @@ public function addAll($otherCollection)
/**
* Checks whether the collection is empty.
+ * Note: This is preferrable over count() == 0.
*
* @return boolean TRUE if the collection is empty, FALSE otherwise.
*/
@@ -326,9 +326,10 @@ public function isEmpty()
}
/**
- * getIterator
+ * Gets an iterator that enables foreach() iteration over the elements in
+ * the collection.
*
- * @return object ArrayIterator
+ * @return ArrayIterator
*/
public function getIterator()
{
@@ -337,27 +338,25 @@ public function getIterator()
}
/**
- * @todo Experiment. Waiting for 5.3 closures.
- * Example usage:
- *
- * $map = $coll->mapElements(function($key, $entity) {
- * return array($entity->id, $entity->name);
- * });
+ * Applies the given function to each element in the collection and returns
+ * a new collection with the modified values.
*
- * or:
- *
- * $map = $coll->mapElements(function($key, $entity) {
- * return array($entity->name, strtoupper($entity->name));
- * });
+ * @param function $func
+ */
+ public function map($func)
+ {
+ return new Doctrine_Common_Collections_Collection(array_map($func, $this->_data));
+ }
+
+ /**
+ * Applies the given function to each element in the collection and returns
+ * a new collection with the new values.
*
+ * @param function $func
*/
- public function mapElements($lambda) {
- $result = array();
- foreach ($this->_data as $key => $entity) {
- list($key, $value) = each($lambda($key, $entity));
- $result[$key] = $value;
- }
- return $result;
+ public function filter($func)
+ {
+ return new Doctrine_Common_Collections_Collection(array_filter($this->_data, $func));
}
/**
@@ -30,7 +30,7 @@ public function getDatabasePlatform();
* Gets the SchemaManager that can be used to inspect and change the underlying
* database schema of the platform this driver connects to.
*
- * @return Doctrine::DBAL::SchemaManager
+ * @return Doctrine\DBAL\SchemaManager
*/
public function getSchemaManager(Doctrine_DBAL_Connection $conn);
}
@@ -44,12 +44,12 @@ private function _constructPdoDsn(array $params)
public function getDatabasePlatform()
{
- return new Doctrine_DatabasePlatform_MySqlPlatform();
+ return new Doctrine_DBAL_Platforms_MySqlPlatform();
}
- public function getSchemaManager(Doctrine_Connection $conn)
+ public function getSchemaManager(Doctrine_DBAL_Connection $conn)
{
- return new Doctrine_Schema_MySqlSchemaManager($conn);
+ return new Doctrine_DBAL_Schema_MySqlSchemaManager($conn);
}
}
@@ -31,12 +31,12 @@ private function _constructPdoDsn(array $params)
public function getDatabasePlatform()
{
- return new Doctrine_DatabasePlatform_PostgreSqlPlatform();
+ return new Doctrine_DBAL_Platforms_PostgreSqlPlatform();
}
- public function getSchemaManager(Doctrine_Connection $conn)
+ public function getSchemaManager(Doctrine_DBAL_Connection $conn)
{
- return new Doctrine_Schema_PostgreSqlSchemaManager($conn);
+ return new Doctrine_DBAL_Schema_PostgreSqlSchemaManager($conn);
}
}
Oops, something went wrong.

0 comments on commit 34f4ee7

Please sign in to comment.