TYPO3 CMS  TYPO3_7-6
EnumerationTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
23 {
29  {
30  new Enumeration\MissingConstantsEnumeration();
31  }
32 
38  {
39  new Enumeration\CompleteEnumeration('bar');
40  }
41 
47  {
48  new Enumeration\MissingConstantsEnumeration(2);
49  }
50 
56  {
57  new Enumeration\InvalidConstantEnumeration(1);
58  }
59 
65  {
66  new Enumeration\MissingDefaultEnumeration();
67  }
68 
74  {
75  new Enumeration\DuplicateConstantValueEnumeration(1);
76  }
77 
82  {
83  $enumeration = $this->getAccessibleMock(
84  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
85  ['dummy']
86  );
87 
88  $enumClassName = get_class($enumeration);
89 
90  $expectedValue = [
91  'INTEGER_VALUE' => 1,
92  'STRING_VALUE' => 'foo',
93  '__default' => 1
94  ];
95 
96  $result = $enumeration->_getStatic('enumConstants');
97  $this->assertArrayHasKey($enumClassName, $result);
98  $this->assertSame($expectedValue, $result[$enumClassName]);
99  }
100 
104  public function constructorSetsValue()
105  {
106  $enumeration = $this->getAccessibleMock(
107  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
108  ['dummy'],
109  [1]
110  );
111  $this->assertEquals(1, $enumeration->_get('value'));
112  }
113 
117  public function setValueSetsValue()
118  {
119  $enumeration = $this->getAccessibleMock(
120  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
121  ['dummy'],
122  [1]
123  );
124  $enumeration->_call('setValue', 'foo');
125  $this->assertEquals('foo', $enumeration->_get('value'));
126  }
127 
133  {
134  $enumeration = $this->getAccessibleMock(
135  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
136  ['dummy'],
137  [1]
138  );
139  $enumeration->_call('setValue', 2);
140  $this->assertEquals(2, $enumeration->_get('value'));
141  }
142 
147  {
148  return [
149  [
150  1,
151  1,
152  true
153  ],
154  [
155  1,
156  '1',
157  true
158  ],
159  [
160  '1',
161  1,
162  true
163  ],
164  [
165  'a1',
166  1,
167  false
168  ],
169  [
170  1,
171  'a1',
172  false
173  ],
174  [
175  '1a',
176  1,
177  false
178  ],
179  [
180  1,
181  '1a',
182  false
183  ],
184  [
185  'foo',
186  'foo',
187  true
188  ],
189  [
190  'foo',
191  'bar',
192  false
193  ],
194  [
195  'foo',
196  'foobar',
197  false
198  ]
199  ];
200  }
201 
206  public function isValidDoesTypeLooseComparison($enumerationValue, $testValue, $expectation)
207  {
208  $mockName = $this->getUniqueId('CompleteEnumerationMock');
209  $enumeration = $this->getAccessibleMock(
210  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
211  ['dummy'],
212  [],
213  $mockName,
214  false
215  );
216  $enumeration->_setStatic('enumConstants', [$mockName => ['CONSTANT_NAME' => $enumerationValue]]);
217  $enumeration->_set('value', $enumerationValue);
218  $this->assertSame($expectation, $enumeration->_call('isValid', $testValue));
219  }
220 
225  {
226  $this->assertEquals(['INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'], Enumeration\CompleteEnumeration::getConstants());
227  }
228 
233  {
234  $this->assertEquals(['INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo', '__default' => 1], Enumeration\CompleteEnumeration::getConstants(true));
235  }
236 
241  {
242  $enumeration = new Enumeration\CompleteEnumeration();
243  $this->assertEquals(['INTEGER_VALUE' => 1, 'STRING_VALUE' => 'foo'], $enumeration->getConstants());
244  }
245 
250  {
251  $enumeration = new Enumeration\CompleteEnumeration();
252  $this->assertSame('1', $enumeration->__toString());
253  }
254 
259  {
260  $enumeration = Enumeration\CompleteEnumeration::cast(1);
261  $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
262  }
263 
268  {
269  $initialEnumeration = new Enumeration\MissingDefaultEnumeration(1);
270  $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
271  $this->assertInstanceOf(\TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class, $enumeration);
272  }
273 
278  {
279  $initialEnumeration = new Enumeration\CompleteEnumeration(1);
280  $enumeration = Enumeration\CompleteEnumeration::cast($initialEnumeration);
281  $this->assertSame($initialEnumeration, $enumeration);
282  }
283 
288  {
289  $enumeration = $this->getAccessibleMock(
290  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
291  ['dummy'],
292  ['1']
293  );
294  $this->assertSame(1, $enumeration->_get('value'));
295  }
296 
301  {
302  $enumeration = $this->getAccessibleMock(
303  \TYPO3\CMS\Core\Tests\Unit\Type\Fixture\Enumeration\CompleteEnumeration::class,
304  ['dummy'],
305  [1]
306  );
307  $this->assertSame(1, $enumeration->_get('value'));
308  }
309 
314  {
315  $enumeration = new Enumeration\CompleteEnumeration(1);
316  $this->assertTrue($enumeration->equals(1));
317  }
318 
323  {
324  $enumeration = new Enumeration\CompleteEnumeration(1);
325  $this->assertTrue($enumeration->equals('1'));
326  }
327 
332  {
333  $enumerationFoo = new Enumeration\CompleteEnumeration(1);
334  $enumerationBar = new Enumeration\CompleteEnumeration(1);
335  $this->assertTrue($enumerationFoo->equals($enumerationBar));
336  }
337 
342  {
343  $enumerationFoo = new Enumeration\CompleteEnumeration(1);
344  $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
345  $this->assertTrue($enumerationFoo->equals($enumerationBar));
346  }
347 
352  {
353  $enumerationFoo = new Enumeration\CompleteEnumeration('foo');
354  $enumerationBar = new Enumeration\MissingDefaultEnumeration(1);
355  $this->assertFalse($enumerationFoo->equals($enumerationBar));
356  }
357 
362  {
363  $enumerationFoo = new Enumeration\CompleteEnumeration(1);
364  $enumerationBar = new Enumeration\CompleteEnumeration('foo');
365  $this->assertFalse($enumerationFoo->equals($enumerationBar));
366  }
367 }
static getConstants($include_default=false)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
isValidDoesTypeLooseComparison($enumerationValue, $testValue, $expectation)