ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / ObjectStorage_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 class Tx_ExtBase_Persistence_ObjectStorage_testcase extends Tx_ExtBase_Base_testcase {
26
27 public function setUp() {
28 }
29
30 public function test_AnObjectCanBeAttached() {
31 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
32 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
33 $objectStorage->attach($object);
34 $result = $objectStorage->offsetGet($object);
35 $this->assertEquals($result, $object, 'The retrieved object differs from the attached object.');
36 }
37
38 public function test_AttachingSomethingElseThanAnObjectThrowsAnException() {
39 $this->setExpectedException('Tx_ExtBase_Exception_InvalidArgumentType');
40 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
41 $objectStorage->attach('foo');
42 }
43
44 public function test_AnObjectCanBeDetached() {
45 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
46 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
47 $objectStorage->offsetSet($object, $object);
48 $resultBeforeDetaching = $objectStorage->offsetGet($object);
49 $this->assertEquals($resultBeforeDetaching, $object, 'The object could not be set via offsetSet().');
50 $objectStorage->detach($object);
51 $resultAfterDetaching = $objectStorage->offsetGet($object);
52 $this->assertEquals($resultAfterDetaching, NULL, 'The object could not be detached.');
53 }
54
55 public function test_DetachingSomethingElseThanAnObjectThrowsAnException() {
56 $this->setExpectedException('Tx_ExtBase_Exception_InvalidArgumentType');
57 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
58 $objectStorage->detach('foo');
59 }
60
61 public function test_AddingAnObjectWithoutAnObjectAsOffsetThrowsAnException() {
62 $this->setExpectedException('Tx_ExtBase_Exception_InvalidArgumentType');
63 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
64 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
65 $objectStorage[] = $object;
66 }
67
68 public function test_AddingAValueOtherThanAnObjectThrowsAnException() {
69 $this->setExpectedException('Tx_ExtBase_Exception_InvalidArgumentType');
70 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
71 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
72 $objectStorage[$object] = 'foo';
73 }
74
75 public function test_IfTheOffsetAndTheValueAreNotEqualAnExceptionIsThrown() {
76 $this->setExpectedException('Tx_ExtBase_Exception_InvalidArgumentType');
77 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
78 $object1 = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
79 $object2 = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
80 $objectStorage[$object1] = $object2;
81 }
82
83 public function test_AnObjectCouldBeSetViaAnOffset() {
84 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
85 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
86 $objectStorage[$object] = $object;
87 $result = $objectStorage->offsetGet($object);
88 $this->assertEquals($result, $object, 'The retrieved object differs from the attached object.');
89 }
90
91 public function test_ItCanBeTestedIfTheStorageContainsAnObject() {
92 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
93 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
94 $objectStorage->attach($object);
95 $result = $objectStorage->contains($object);
96 $this->assertEquals($result, TRUE, 'The method object differs from the attached object.');
97 }
98
99 public function test_UnsettingSomethingElseThanAnObjectThrowsAnException() {
100 $this->setExpectedException('Tx_ExtBase_Exception_InvalidArgumentType');
101 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
102 // $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
103 // $objectStorage->offsetSet($object, $object);
104 $objectStorage->offsetUnset('foo');
105 }
106
107 public function test_AnObjectCanBeUnset() {
108 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
109 $object = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
110 $objectStorage->offsetSet($object, $object);
111 $resultBeforeDetaching = $objectStorage->offsetGet($object);
112 $this->assertEquals($resultBeforeDetaching, $object, 'The object could not be set via offsetSet().');
113 $objectStorage->offsetUnset($object);
114 $resultAfterDetaching = $objectStorage->offsetGet($object);
115 $this->assertEquals($resultAfterDetaching, NULL, 'The object could not be unsetted.');
116 }
117
118 public function test_TheStorageCanBeRetrievedAsArray() {
119 $objectStorage = new Tx_ExtBase_Persistence_ObjectStorage();
120 $object1 = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
121 $objectStorage->offsetSet($object1, $object1);
122 $object2 = $this->getMock('Tx_ExtBase_DomainObject_AbstractEntity');
123 $objectStorage->offsetSet($object2, $object2);
124 $result = $objectStorage->toArray();
125 $this->assertEquals(is_array($result), TRUE, 'The result was not an array as expected.');
126 $this->assertEquals(count($result), 2, 'The retrieved array did not contain two elements.');
127 }
128
129 }
130 ?>