c366a84ed762f72d0812855bde4c17d38f2d581f
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / TypeHandlingServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. 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 /**
31 * Testcase for class \TYPO3\CMS\Extbase\Service\TypeHandling
32 */
33 class TypeHandlingServiceTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Service\TypeHandlingService
37 */
38 protected $typeHandlingService;
39
40 public function setUp() {
41 $this->typeHandlingService = new \TYPO3\CMS\Extbase\Service\TypeHandlingService();
42 }
43
44 /**
45 * @test
46 * @expectedException \InvalidArgumentException
47 */
48 public function parseTypeThrowsExceptionOnInvalidType() {
49 $this->typeHandlingService->parseType('something not a type');
50 }
51
52 /**
53 * @test
54 * @expectedException \InvalidArgumentException
55 * @author Karsten Dambekalns <karsten@typo3.org>
56 */
57 public function parseTypeThrowsExceptionOnInvalidElementTypeHint() {
58 $this->typeHandlingService->parseType('string<integer>');
59 }
60
61 /**
62 * data provider for parseTypeReturnsArrayWithInformation
63 *
64 * @author Karsten Dambekalns <karsten@typo3.org>
65 * @return array
66 */
67 public function types() {
68 return array(
69 array('int', array('type' => 'integer', 'elementType' => NULL)),
70 array('string', array('type' => 'string', 'elementType' => NULL)),
71 array('DateTime', array('type' => 'DateTime', 'elementType' => NULL)),
72 array('\DateTime', array('type' => 'DateTime', 'elementType' => NULL)),
73 array('Tx_Extbase_Bar', array('type' => 'Tx_Extbase_Bar', 'elementType' => NULL)),
74 array('\\ExtbaseTeam\\BlogExample\\Foo\\Bar', array('type' => 'ExtbaseTeam\\BlogExample\\Foo\\Bar', 'elementType' => NULL)),
75 array('array<integer>', array('type' => 'array', 'elementType' => 'integer')),
76 array('ArrayObject<string>', array('type' => 'ArrayObject', 'elementType' => 'string')),
77 array('SplObjectStorage<Tx_Extbase_Bar>', array('type' => 'SplObjectStorage', 'elementType' => 'Tx_Extbase_Bar')),
78 array('SplObjectStorage<\\ExtbaseTeam\\BlogExample\\Foo\\Bar>', array('type' => 'SplObjectStorage', 'elementType' => 'ExtbaseTeam\\BlogExample\\Foo\\Bar')),
79 array('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage<Tx_Extbase_Bar>', array('type' => 'TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', 'elementType' => 'Tx_Extbase_Bar')),
80 array('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage<\\ExtbaseTeam\\BlogExample\\Foo\\Bar>', array('type' => 'TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', 'elementType' => 'ExtbaseTeam\\BlogExample\\Foo\\Bar')),
81 array('Tx_Extbase_Persistence_ObjectStorage<Tx_Extbase_Bar>', array('type' => 'Tx_Extbase_Persistence_ObjectStorage', 'elementType' => 'Tx_Extbase_Bar')),
82 array('Tx_Extbase_Persistence_ObjectStorage<\\ExtbaseTeam\\BlogExample\\Foo\\Bar>', array('type' => 'Tx_Extbase_Persistence_ObjectStorage', 'elementType' => 'ExtbaseTeam\\BlogExample\\Foo\\Bar')),
83 );
84 }
85
86 /**
87 * @test
88 * @dataProvider types
89 * @author Karsten Dambekalns <karsten@typo3.org>
90 * @param mixed $type
91 * @param mixed $expectedResult
92 */
93 public function parseTypeReturnsArrayWithInformation($type, $expectedResult) {
94 $this->assertEquals($expectedResult, $this->typeHandlingService->parseType($type));
95 }
96
97 /**
98 * data provider for normalizeTypesReturnsNormalizedType
99 *
100 * @author Karsten Dambekalns <karsten@typo3.org>
101 * @return array
102 */
103 public function normalizeTypes() {
104 return array(
105 array('int', 'integer'),
106 array('double', 'float'),
107 array('bool', 'boolean'),
108 array('string', 'string')
109 );
110 }
111
112 /**
113 * @test
114 * @dataProvider normalizeTypes
115 * @author Karsten Dambekalns <karsten@typo3.org>
116 * @param mixed $type
117 * @param mixed $normalized
118 */
119 public function normalizeTypesReturnsNormalizedType($type, $normalized) {
120 $this->assertEquals($this->typeHandlingService->normalizeType($type), $normalized);
121 }
122
123 /**
124 * data provider for isLiteralReturnsFalseForNonLiteralTypes
125 *
126 * @author Karsten Dambekalns <karsten@typo3.org>
127 * @return array
128 */
129 public function nonLiteralTypes() {
130 return array(
131 array('DateTime'),
132 array('\\Foo\\Bar'),
133 array('array'),
134 array('ArrayObject'),
135 array('stdClass')
136 );
137 }
138
139 /**
140 * @test
141 * @dataProvider nonliteralTypes
142 * @author Karsten Dambekalns <karsten@typo3.org>
143 * @param mixed $type
144 */
145 public function isLiteralReturnsFalseForNonLiteralTypes($type) {
146 $this->assertFalse($this->typeHandlingService->isLiteral($type));
147 }
148
149 /**
150 * data provider for isLiteralReturnsTrueForLiteralType
151 *
152 * @author Karsten Dambekalns <karsten@typo3.org>
153 * @return array
154 */
155 public function literalTypes() {
156 return array(
157 array('integer'),
158 array('int'),
159 array('float'),
160 array('double'),
161 array('boolean'),
162 array('bool'),
163 array('string')
164 );
165 }
166
167 /**
168 * @test
169 * @dataProvider literalTypes
170 * @author Karsten Dambekalns <karsten@typo3.org>
171 * @param mixed $type
172 */
173 public function isLiteralReturnsTrueForLiteralType($type) {
174 $this->assertTrue($this->typeHandlingService->isLiteral($type));
175 }
176
177 /**
178 * data provider for isSimpleTypeReturnsTrueForSimpleTypes
179 *
180 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
181 * @return array
182 */
183 public function simpleTypes() {
184 return array(
185 array('integer', TRUE),
186 array('int', TRUE),
187 array('float', TRUE),
188 array('double', TRUE),
189 array('boolean', TRUE),
190 array('bool', TRUE),
191 array('string', TRUE),
192 array('DateTime', FALSE),
193 );
194 }
195
196 /**
197 * @test
198 * @dataProvider simpleTypes
199 * @author Alexander Schnitzler <alex.schnitzler@typovision.de>
200 * @param string $type
201 * @param boolean $result
202 */
203 public function isSimpleTypeReturnsOnlyTrueForSimpleTypes($type, $result) {
204 $this->assertSame($result, $this->typeHandlingService->isSimpleType($type));
205 }
206 }
207
208 ?>