[TASK] Adjust namespaces of persistence unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / ObjectStorageTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
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.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 class ObjectStorageTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
31
32 /**
33 * @test
34 */
35 public function anObjectCanBeAttached() {
36 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
37 $object1 = new \StdClass();
38 $object2 = new \StdClass();
39 $objectStorage->attach($object1);
40 $objectStorage->attach($object2, 'foo');
41 $this->assertEquals($objectStorage[$object1], NULL);
42 $this->assertEquals($objectStorage[$object2], 'foo');
43 }
44
45 /**
46 * @test
47 */
48 public function anObjectCanBeDetached() {
49 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
50 $object1 = new \StdClass();
51 $object2 = new \StdClass();
52 $objectStorage->attach($object1);
53 $objectStorage->attach($object2, 'foo');
54 $this->assertEquals(count($objectStorage), 2);
55 $objectStorage->detach($object1);
56 $this->assertEquals(count($objectStorage), 1);
57 $objectStorage->detach($object2);
58 $this->assertEquals(count($objectStorage), 0);
59 }
60
61 /**
62 * @test
63 */
64 public function offsetSetAssociatesDataToAnObjectInTheStorage() {
65 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
66 $object1 = new \StdClass();
67 $object2 = new \StdClass();
68 $objectStorage->offsetSet($object1, 'foo');
69 $this->assertEquals(count($objectStorage), 1);
70 $objectStorage[$object2] = 'bar';
71 $this->assertEquals(count($objectStorage), 2);
72 }
73
74 /**
75 * @test
76 */
77 public function offsetUnsetRemovesAnObjectFromTheStorage() {
78 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
79 $object1 = new \StdClass();
80 $object2 = new \StdClass();
81 $objectStorage->attach($object1);
82 $objectStorage->attach($object2, 'foo');
83 $this->assertEquals(count($objectStorage), 2);
84 $objectStorage->offsetUnset($object2);
85 $this->assertEquals(count($objectStorage), 1);
86 $objectStorage->offsetUnset($object1);
87 $this->assertEquals(count($objectStorage), 0);
88 }
89
90 /**
91 * @test
92 */
93 public function offsetGetReturnsTheDataAssociatedWithAnObject() {
94 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
95 $object1 = new \StdClass();
96 $object2 = new \StdClass();
97 $objectStorage[$object1] = 'foo';
98 $objectStorage->attach($object2);
99 $this->assertEquals($objectStorage->offsetGet($object1), 'foo');
100 $this->assertEquals($objectStorage->offsetGet($object2), NULL);
101 }
102
103 /**
104 * @test
105 */
106 public function offsetExistsChecksWhetherAnObjectExistsInTheStorage() {
107 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
108 $object1 = new \StdClass();
109 $object2 = new \StdClass();
110 $objectStorage->attach($object1);
111 $this->assertEquals($objectStorage->offsetExists($object1), TRUE);
112 $this->assertEquals($objectStorage->offsetExists($object2), FALSE);
113 }
114
115 /**
116 * @test
117 */
118 public function getInfoReturnsTheDataAssociatedWithTheCurrentIteratorEntry() {
119 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
120 $object1 = new \StdClass();
121 $object2 = new \StdClass();
122 $object3 = new \StdClass();
123 $objectStorage->attach($object1, 42);
124 $objectStorage->attach($object2, 'foo');
125 $objectStorage->attach($object3, array('bar', 'baz'));
126 $objectStorage->rewind();
127 $this->assertEquals($objectStorage->getInfo(), 42);
128 $objectStorage->next();
129 $this->assertEquals($objectStorage->getInfo(), 'foo');
130 $objectStorage->next();
131 $this->assertEquals($objectStorage->getInfo(), array('bar', 'baz'));
132 }
133
134 /**
135 * @test
136 */
137 public function setInfoSetsTheDataAssociatedWithTheCurrentIteratorEntry() {
138 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
139 $object1 = new \StdClass();
140 $object2 = new \StdClass();
141 $objectStorage->attach($object1);
142 $objectStorage->attach($object2, 'foo');
143 $objectStorage->rewind();
144 $objectStorage->setInfo(42);
145 $objectStorage->next();
146 $objectStorage->setInfo('bar');
147 $this->assertEquals($objectStorage[$object1], 42);
148 $this->assertEquals($objectStorage[$object2], 'bar');
149 }
150
151 /**
152 * @test
153 */
154 public function removeAllRemovesObjectsContainedInAnotherStorageFromTheCurrentStorage() {
155 $object1 = new \StdClass();
156 $object2 = new \StdClass();
157 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
158 $objectStorageA->attach($object1, 'foo');
159 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
160 $objectStorageB->attach($object1, 'bar');
161 $objectStorageB->attach($object2, 'baz');
162 $this->assertEquals(count($objectStorageB), 2);
163 $objectStorageB->removeAll($objectStorageA);
164 $this->assertEquals(count($objectStorageB), 1);
165 }
166
167 /**
168 * @test
169 */
170 public function addAllAddsAllObjectsFromAnotherStorage() {
171 $object1 = new \StdClass();
172 $object2 = new \StdClass();
173 $objectStorageA = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
174 // It might be better to mock this
175 $objectStorageA->attach($object1, 'foo');
176 $objectStorageB = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
177 $objectStorageB->attach($object2, 'baz');
178 $this->assertEquals($objectStorageB->offsetExists($object1), FALSE);
179 $objectStorageB->addAll($objectStorageA);
180 $this->assertEquals($objectStorageB[$object1], 'foo');
181 $this->assertEquals($objectStorageB[$object2], 'baz');
182 }
183
184 /**
185 * @test
186 */
187 public function theStorageCanBeRetrievedAsArray() {
188 $objectStorage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
189 $object1 = new \StdClass();
190 $object2 = new \StdClass();
191 $objectStorage->attach($object1, 'foo');
192 $objectStorage->attach($object2, 'bar');
193 $this->assertEquals($objectStorage->toArray(), array($object1, $object2));
194 }
195 }
196
197 ?>