|
1 |
| -<?php |
2 |
| - |
3 |
| -namespace MabeEnumTest; |
4 |
| - |
5 |
| -use MabeEnum\Enum; |
6 |
| -use MabeEnum\EnumMap; |
7 |
| -use MabeEnumTest\TestAsset\EnumWithoutDefaultValue; |
8 |
| -use PHPUnit_Framework_TestCase as TestCase; |
9 |
| - |
10 |
| -/** |
11 |
| - * Unit tests for the class MabeEnum\EnumMap |
12 |
| - * |
13 |
| - * @link http://github.com/marc-mabe/php-enum for the canonical source repository |
14 |
| - * @copyright Copyright (c) 2012 Marc Bennewitz |
15 |
| - * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
16 |
| - */ |
17 |
| -class EnumMapTest extends TestCase |
18 |
| -{ |
19 |
| - public function testBasic() |
20 |
| - { |
21 |
| - $enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue'); |
22 |
| - |
23 |
| - $enum1 = EnumWithoutDefaultValue::ONE(); |
24 |
| - $value1 = 'value2'; |
25 |
| - |
26 |
| - $enum2 = EnumWithoutDefaultValue::TWO(); |
27 |
| - $value2 = 'value2'; |
28 |
| - |
29 |
| - $this->assertFalse($enumMap->contains($enum1)); |
30 |
| - $this->assertNull($enumMap->attach($enum1, $value1)); |
31 |
| - $this->assertTrue($enumMap->contains($enum1)); |
32 |
| - $this->assertSame($value1, $enumMap[$enum1]); |
33 |
| - |
34 |
| - $this->assertFalse($enumMap->contains($enum2)); |
35 |
| - $this->assertNull($enumMap->attach($enum2, $value2)); |
36 |
| - $this->assertTrue($enumMap->contains($enum2)); |
37 |
| - $this->assertSame($value2, $enumMap[$enum2]); |
38 |
| - |
39 |
| - $this->assertNull($enumMap->detach($enum1)); |
40 |
| - $this->assertFalse($enumMap->contains($enum1)); |
41 |
| - |
42 |
| - $this->assertNull($enumMap->detach($enum2)); |
43 |
| - $this->assertFalse($enumMap->contains($enum2)); |
44 |
| - } |
45 |
| - |
46 |
| - public function testBasicWithConstantValuesAsEnums() |
47 |
| - { |
48 |
| - $enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue'); |
49 |
| - |
50 |
| - $enum1 = EnumWithoutDefaultValue::ONE; |
51 |
| - $value1 = 'value2'; |
52 |
| - |
53 |
| - $enum2 = EnumWithoutDefaultValue::TWO; |
54 |
| - $value2 = 'value2'; |
55 |
| - |
56 |
| - $this->assertFalse($enumMap->contains($enum1)); |
57 |
| - $this->assertNull($enumMap->attach($enum1, $value1)); |
58 |
| - $this->assertTrue($enumMap->contains($enum1)); |
59 |
| - $this->assertSame($value1, $enumMap[$enum1]); |
60 |
| - |
61 |
| - $this->assertFalse($enumMap->contains($enum2)); |
62 |
| - $this->assertNull($enumMap->attach($enum2, $value2)); |
63 |
| - $this->assertTrue($enumMap->contains($enum2)); |
64 |
| - $this->assertSame($value2, $enumMap[$enum2]); |
65 |
| - |
66 |
| - $this->assertNull($enumMap->detach($enum1)); |
67 |
| - $this->assertFalse($enumMap->contains($enum1)); |
68 |
| - |
69 |
| - $this->assertNull($enumMap->detach($enum2)); |
70 |
| - $this->assertFalse($enumMap->contains($enum2)); |
71 |
| - } |
72 |
| - |
73 |
| - public function testIterate() |
74 |
| - { |
75 |
| - $enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue'); |
76 |
| - |
77 |
| - $enum1 = EnumWithoutDefaultValue::ONE(); |
78 |
| - $value1 = 'value2'; |
79 |
| - |
80 |
| - $enum2 = EnumWithoutDefaultValue::TWO(); |
81 |
| - $value2 = 'value2'; |
82 |
| - |
83 |
| - // an empty enum map needs to be invalid, starting by 0 |
84 |
| - $this->assertSame(0, $enumMap->count()); |
85 |
| - $this->assertFalse($enumMap->valid()); |
86 |
| - |
87 |
| - // attach in revert order shouldn't change ordering of iteration |
88 |
| - $enumMap->attach($enum2, $value2); |
89 |
| - $enumMap->attach($enum1, $value1); |
90 |
| - |
91 |
| - // a not empty enum map should be valid, starting by 0 (if not iterated) |
92 |
| - $this->assertSame(2, $enumMap->count()); |
93 |
| - $this->assertTrue($enumMap->valid()); |
94 |
| - $this->assertSame(0, $enumMap->currentPosition()); |
95 |
| - $this->assertSame($enum1, $enumMap->key()); |
96 |
| - $this->assertSame($value1, $enumMap->current()); |
97 |
| - |
98 |
| - // go to the next element (last) |
99 |
| - $this->assertNull($enumMap->next()); |
100 |
| - $this->assertTrue($enumMap->valid()); |
101 |
| - $this->assertSame(1, $enumMap->currentPosition()); |
102 |
| - $this->assertSame($enum2, $enumMap->key()); |
103 |
| - $this->assertSame($value2, $enumMap->current()); |
104 |
| - |
105 |
| - // go to the next element (out of range) |
106 |
| - $this->assertNull($enumMap->next()); |
107 |
| - $this->assertFalse($enumMap->valid()); |
108 |
| - $this->assertSame(2, $enumMap->currentPosition()); |
109 |
| - //$this->assertSame($enum2, $enumMap->currentEnum()); |
110 |
| - //$this->assertSame($value2, $enumMap->currentValue()); |
111 |
| - } |
112 |
| -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MabeEnumTest; |
| 4 | + |
| 5 | +use MabeEnum\Enum; |
| 6 | +use MabeEnum\EnumMap; |
| 7 | +use MabeEnumTest\TestAsset\EnumWithoutDefaultValue; |
| 8 | +use PHPUnit_Framework_TestCase as TestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit tests for the class MabeEnum\EnumMap |
| 12 | + * |
| 13 | + * @link http://github.com/marc-mabe/php-enum for the canonical source repository |
| 14 | + * @copyright Copyright (c) 2012 Marc Bennewitz |
| 15 | + * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
| 16 | + */ |
| 17 | +class EnumMapTest extends TestCase |
| 18 | +{ |
| 19 | + public function testBasic() |
| 20 | + { |
| 21 | + $enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue'); |
| 22 | + |
| 23 | + $enum1 = EnumWithoutDefaultValue::ONE(); |
| 24 | + $value1 = 'value2'; |
| 25 | + |
| 26 | + $enum2 = EnumWithoutDefaultValue::TWO(); |
| 27 | + $value2 = 'value2'; |
| 28 | + |
| 29 | + $this->assertFalse($enumMap->contains($enum1)); |
| 30 | + $this->assertNull($enumMap->attach($enum1, $value1)); |
| 31 | + $this->assertTrue($enumMap->contains($enum1)); |
| 32 | + $this->assertSame($value1, $enumMap[$enum1]); |
| 33 | + |
| 34 | + $this->assertFalse($enumMap->contains($enum2)); |
| 35 | + $this->assertNull($enumMap->attach($enum2, $value2)); |
| 36 | + $this->assertTrue($enumMap->contains($enum2)); |
| 37 | + $this->assertSame($value2, $enumMap[$enum2]); |
| 38 | + |
| 39 | + $this->assertNull($enumMap->detach($enum1)); |
| 40 | + $this->assertFalse($enumMap->contains($enum1)); |
| 41 | + |
| 42 | + $this->assertNull($enumMap->detach($enum2)); |
| 43 | + $this->assertFalse($enumMap->contains($enum2)); |
| 44 | + } |
| 45 | + |
| 46 | + public function testBasicWithConstantValuesAsEnums() |
| 47 | + { |
| 48 | + $enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue'); |
| 49 | + |
| 50 | + $enum1 = EnumWithoutDefaultValue::ONE; |
| 51 | + $value1 = 'value2'; |
| 52 | + |
| 53 | + $enum2 = EnumWithoutDefaultValue::TWO; |
| 54 | + $value2 = 'value2'; |
| 55 | + |
| 56 | + $this->assertFalse($enumMap->contains($enum1)); |
| 57 | + $this->assertNull($enumMap->attach($enum1, $value1)); |
| 58 | + $this->assertTrue($enumMap->contains($enum1)); |
| 59 | + $this->assertSame($value1, $enumMap[$enum1]); |
| 60 | + |
| 61 | + $this->assertFalse($enumMap->contains($enum2)); |
| 62 | + $this->assertNull($enumMap->attach($enum2, $value2)); |
| 63 | + $this->assertTrue($enumMap->contains($enum2)); |
| 64 | + $this->assertSame($value2, $enumMap[$enum2]); |
| 65 | + |
| 66 | + $this->assertNull($enumMap->detach($enum1)); |
| 67 | + $this->assertFalse($enumMap->contains($enum1)); |
| 68 | + |
| 69 | + $this->assertNull($enumMap->detach($enum2)); |
| 70 | + $this->assertFalse($enumMap->contains($enum2)); |
| 71 | + } |
| 72 | + |
| 73 | + public function testIterate() |
| 74 | + { |
| 75 | + $enumMap = new EnumMap('MabeEnumTest\TestAsset\EnumWithoutDefaultValue'); |
| 76 | + |
| 77 | + $enum1 = EnumWithoutDefaultValue::ONE(); |
| 78 | + $value1 = 'value2'; |
| 79 | + |
| 80 | + $enum2 = EnumWithoutDefaultValue::TWO(); |
| 81 | + $value2 = 'value2'; |
| 82 | + |
| 83 | + // an empty enum map needs to be invalid, starting by 0 |
| 84 | + $this->assertSame(0, $enumMap->count()); |
| 85 | + $this->assertFalse($enumMap->valid()); |
| 86 | + |
| 87 | + // attach in revert order shouldn't change ordering of iteration |
| 88 | + $enumMap->attach($enum2, $value2); |
| 89 | + $enumMap->attach($enum1, $value1); |
| 90 | + |
| 91 | + // a not empty enum map should be valid, starting by 0 (if not iterated) |
| 92 | + $this->assertSame(2, $enumMap->count()); |
| 93 | + $this->assertTrue($enumMap->valid()); |
| 94 | + $this->assertSame(0, $enumMap->currentPosition()); |
| 95 | + $this->assertSame($enum1, $enumMap->key()); |
| 96 | + $this->assertSame($value1, $enumMap->current()); |
| 97 | + |
| 98 | + // go to the next element (last) |
| 99 | + $this->assertNull($enumMap->next()); |
| 100 | + $this->assertTrue($enumMap->valid()); |
| 101 | + $this->assertSame(1, $enumMap->currentPosition()); |
| 102 | + $this->assertSame($enum2, $enumMap->key()); |
| 103 | + $this->assertSame($value2, $enumMap->current()); |
| 104 | + |
| 105 | + // go to the next element (out of range) |
| 106 | + $this->assertNull($enumMap->next()); |
| 107 | + $this->assertFalse($enumMap->valid()); |
| 108 | + $this->assertSame(2, $enumMap->currentPosition()); |
| 109 | + //$this->assertSame($enum2, $enumMap->currentEnum()); |
| 110 | + //$this->assertSame($value2, $enumMap->currentValue()); |
| 111 | + } |
| 112 | +} |
0 commit comments