2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 class Tx_Extbase_Persistence_ObjectStorage_testcase
extends Tx_Extbase_BaseTestCase
{
33 public function anObjectCanBeAttached() {
34 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
35 $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
36 $objectStorage->attach($object);
37 $result = $objectStorage->offsetGet($object);
39 $this->assertEquals($result, $object, 'The retrieved object differs from the attached object.');
44 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentType
46 public function attachingSomethingElseThanAnObjectThrowsAnException() {
47 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
48 $objectStorage->attach('foo');
54 public function anObjectCanBeDetached() {
55 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
56 $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
57 $objectStorage->offsetSet($object, $object);
58 $resultBeforeDetaching = $objectStorage->offsetGet($object);
60 $this->assertEquals($resultBeforeDetaching, $object, 'The object could not be set via offsetSet().');
62 $objectStorage->detach($object);
63 $resultAfterDetaching = $objectStorage->offsetGet($object);
65 $this->assertEquals($resultAfterDetaching, NULL, 'The object could not be detached.');
70 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentType
72 public function detachingSomethingElseThanAnObjectThrowsAnException() {
73 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
74 $objectStorage->detach('foo');
79 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentType
81 public function addingAnObjectWithoutAnObjectAsOffsetThrowsAnException() {
82 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
83 $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
84 $objectStorage[] = $object;
90 public function anObjectCouldBeSetViaAnOffset() {
91 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
92 $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
93 $objectStorage[$object] = $object;
94 $result = $objectStorage->offsetGet($object);
96 $this->assertEquals($result, $object, 'The retrieved object differs from the attached object.');
102 public function itCanBeTestedIfTheStorageContainsAnObject() {
103 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
104 $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
105 $objectStorage->attach($object);
106 $result = $objectStorage->contains($object);
108 $this->assertEquals($result, TRUE, 'The method object differs from the attached object.');
113 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentType
115 public function unsettingSomethingElseThanAnObjectThrowsAnException() {
116 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
117 // $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
118 // $objectStorage->offsetSet($object, $object);
119 $objectStorage->offsetUnset('foo');
125 public function anObjectCanBeUnset() {
126 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
127 $object = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
128 $objectStorage->offsetSet($object, $object);
129 $resultBeforeDetaching = $objectStorage->offsetGet($object);
131 $this->assertEquals($resultBeforeDetaching, $object, 'The object could not be set via offsetSet().');
133 $objectStorage->offsetUnset($object);
134 $resultAfterDetaching = $objectStorage->offsetGet($object);
136 $this->assertEquals($resultAfterDetaching, NULL, 'The object could not be unsetted.');
142 public function theStorageCanBeRetrievedAsArray() {
143 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
144 $object1 = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
145 $objectStorage->offsetSet($object1, $object1);
146 $object2 = $this->getMock('Tx_Extbase_DomainObject_AbstractEntity');
147 $objectStorage->offsetSet($object2, $object2);
148 $result = $objectStorage->toArray();
150 $this->assertEquals(is_array($result), TRUE, 'The result was not an array as expected.');
151 $this->assertEquals(count($result), 2, 'The retrieved array did not contain two elements.');