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