diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index e9cc4ca1a..4087d3942 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -184,7 +184,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator unset($vars['expanded']); unset($vars['generator']); - $vars['_table'] = $vars['_table']->getComponentName(); + $vars['_mapper'] = $vars['_mapper']->getComponentName(); return serialize($vars); } @@ -195,6 +195,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator * * Part of the implementation of the Serializable interface. * + * @param string $serialized The serialized data + * * @return void */ public function unserialize($serialized) @@ -241,7 +243,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator */ public function getKeyColumn() { - return $this->column; + return $this->keyColumn; } /** @@ -515,6 +517,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator return true; } + // why is this not checked when the keyColumn is set? if (isset($this->keyColumn)) { $value = $record->get($this->keyColumn); if ($value === null) { diff --git a/tests/Orm/Component/AccessTest.php b/tests/Orm/Component/AccessTest.php index d793a2d4a..da01e18c8 100644 --- a/tests/Orm/Component/AccessTest.php +++ b/tests/Orm/Component/AccessTest.php @@ -1,4 +1,35 @@ . + */ + +/** + * Doctrine + * the base class of Doctrine framework + * + * @package Doctrine + * @author Bjarte Stien Karlsen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision: 3754 $ + */ require_once 'lib/DoctrineTestInit.php'; class Orm_Component_AccessTest extends Doctrine_OrmTestCase diff --git a/tests/Orm/Component/AllTests.php b/tests/Orm/Component/AllTests.php index 6785cfd38..49a898ce3 100644 --- a/tests/Orm/Component/AllTests.php +++ b/tests/Orm/Component/AllTests.php @@ -8,6 +8,7 @@ require_once 'lib/DoctrineTestInit.php'; // Tests require_once 'Orm/Component/TestTest.php'; require_once 'Orm/Component/AccessTest.php'; +require_once 'Orm/Component/CollectionTest.php'; class Orm_Component_AllTests { @@ -22,6 +23,7 @@ class Orm_Component_AllTests // $suite->addTestSuite('Orm_Component_TestTest'); $suite->addTestSuite('Orm_Component_AccessTest'); + $suite->addTestSuite('Orm_Component_CollectionTest'); return $suite; } diff --git a/tests/Orm/Component/CollectionTest.php b/tests/Orm/Component/CollectionTest.php new file mode 100644 index 000000000..154ce571b --- /dev/null +++ b/tests/Orm/Component/CollectionTest.php @@ -0,0 +1,130 @@ +. + */ + +/** + * Doctrine + * the base class of Doctrine framework + * + * @package Doctrine + * @author Bjarte Stien Karlsen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.phpdoctrine.com + * @since 1.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_Collection('ForumUser'); + + //we create a CmsUser with username as key column and add a user to it + $cmsColl = new Doctrine_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->getKeyColumn()); + } + + + /** + * @test + */ + public function shouldUseSpecifiedKeyColumn() + { + $coll = new Doctrine_Collection('ForumUser', 'id'); + $this->assertEquals('id', $coll->getKeyColumn()); + } + + /** + * 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_Collection('ForumUser', 'rat'); + } + + /** + * @test + */ + public function shouldSerializeEmptyCollection() + { + $serializedFormCollection='C:19:"Doctrine_Collection":158:{a:7:{s:4:"data";a:0:{}s:7:"_mapper";s:9:"ForumUser";s:9:"_snapshot";a:0:{}s:14:"referenceField";N;s:9:"keyColumn";N;s:8:"_locator";N;s:10:"_resources";a:0:{}}}'; + $this->assertEquals($serializedFormCollection, serialize($this->coll)); + } + + /** + * @test + */ + public function shouldUnserializeEmptyCollectionIntoObject() + { + $serializedFormCollection='C:19:"Doctrine_Collection":158:{a:7:{s:4:"data";a:0:{}s:7:"_mapper";s:9:"ForumUser";s:9:"_snapshot";a:0:{}s:14:"referenceField";N;s:9:"keyColumn";N;s:8:"_locator";N;s:10:"_resources";a:0:{}}}'; + $coll = unserialize($serializedFormCollection); + $this->assertEquals(Doctrine_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->getKeyColumn()); + $this->assertTrue(isset($coll['test'])); + $user = $coll['test']; + $this->assertTrue($user instanceOf CmsUser); + $this->assertEquals('test', $user['username']); + + } + +} diff --git a/tests/models/cms/CmsUser.php b/tests/models/cms/CmsUser.php index 144989756..b6cb0607f 100644 --- a/tests/models/cms/CmsUser.php +++ b/tests/models/cms/CmsUser.php @@ -5,6 +5,6 @@ class CmsUser extends Doctrine_Record { $class->setColumn('id', 'integer', 4, array('primary' => true, 'autoincrement' => true)); $class->setColumn('username', 'string', 255); - $class->setColumn('username', 'string', 255); + $class->setColumn('name', 'string', 255); } -} \ No newline at end of file +}