EnumMapTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. declare(strict_types = 1);
  3. namespace DASPRiD\EnumTest;
  4. use DASPRiD\Enum\EnumMap;
  5. use DASPRiD\Enum\Exception\ExpectationException;
  6. use DASPRiD\Enum\Exception\IllegalArgumentException;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. final class EnumMapTest extends TestCase
  10. {
  11. public function testConstructionWithInvalidEnumType() : void
  12. {
  13. $this->expectException(IllegalArgumentException::class);
  14. new EnumMap(stdClass::class, 'string', false);
  15. }
  16. public function testUnexpectedKeyType() : void
  17. {
  18. $this->expectException(ExpectationException::class);
  19. $map = new EnumMap(WeekDay::class, 'string', false);
  20. $map->expect(Planet::class, 'string', false);
  21. }
  22. public function testUnexpectedValueType() : void
  23. {
  24. $this->expectException(ExpectationException::class);
  25. $map = new EnumMap(WeekDay::class, 'string', false);
  26. $map->expect(WeekDay::class, 'int', false);
  27. }
  28. public function testUnexpectedNullableValueType() : void
  29. {
  30. $this->expectException(ExpectationException::class);
  31. $map = new EnumMap(WeekDay::class, 'string', true);
  32. $map->expect(WeekDay::class, 'string', false);
  33. }
  34. public function testExpectedTypes() : void
  35. {
  36. $map = new EnumMap(WeekDay::class, 'string', true);
  37. $map->expect(WeekDay::class, 'string', true);
  38. $this->addToAssertionCount(1);
  39. }
  40. public function testSize() : void
  41. {
  42. $map = new EnumMap(WeekDay::class, 'string', true);
  43. $this->assertSame(0, $map->size());
  44. $map->put(WeekDay::MONDAY(), 'foo');
  45. $this->assertSame(1, $map->size());
  46. }
  47. public function testContainsValue() : void
  48. {
  49. $map = new EnumMap(WeekDay::class, 'string', true);
  50. $this->assertFalse($map->containsValue('foo'));
  51. $map->put(WeekDay::TUESDAY(), 'foo');
  52. $this->assertTrue($map->containsValue('foo'));
  53. $this->assertFalse($map->containsValue(null));
  54. $map->put(WeekDay::WEDNESDAY(), null);
  55. $this->assertTrue($map->containsValue(null));
  56. }
  57. public function testContainsKey() : void
  58. {
  59. $map = new EnumMap(WeekDay::class, 'string', true);
  60. $this->assertFalse($map->containsKey(WeekDay::TUESDAY()));
  61. $map->put(WeekDay::TUESDAY(), 'foo');
  62. $this->assertTrue($map->containsKey(WeekDay::TUESDAY()));
  63. $map->put(WeekDay::WEDNESDAY(), null);
  64. $this->assertTrue($map->containsKey(WeekDay::WEDNESDAY()));
  65. }
  66. public function testPutAndGet() : void
  67. {
  68. $map = new EnumMap(WeekDay::class, 'string', true);
  69. $map->put(WeekDay::TUESDAY(), 'foo');
  70. $map->put(WeekDay::FRIDAY(), null);
  71. $this->assertSame('foo', $map->get(WeekDay::TUESDAY()));
  72. $this->assertSame(null, $map->get(WeekDay::WEDNESDAY()));
  73. $this->assertSame(null, $map->get(WeekDay::FRIDAY()));
  74. }
  75. public function testPutInvalidKey() : void
  76. {
  77. $this->expectException(IllegalArgumentException::class);
  78. $map = new EnumMap(WeekDay::class, 'string', true);
  79. $map->put(Planet::MARS(), 'foo');
  80. }
  81. public function invalidValues() : array
  82. {
  83. return [
  84. ['bool', null, false],
  85. ['bool', 0],
  86. ['boolean', 0],
  87. ['int', 2.4],
  88. ['integer', 5.3],
  89. ['float', 3],
  90. ['double', 7],
  91. ['string', 1],
  92. ['object', 1],
  93. ['array', 1],
  94. [stdClass::class, 1],
  95. ];
  96. }
  97. /**
  98. * @dataProvider invalidValues
  99. * @param mixed $value
  100. */
  101. public function testPutInvalidValue(string $valueType, $value, bool $allowNull = true) : void
  102. {
  103. $this->expectException(IllegalArgumentException::class);
  104. $map = new EnumMap(WeekDay::class, $valueType, $allowNull);
  105. $map->put(WeekDay::TUESDAY(), $value);
  106. }
  107. public function validValues() : array
  108. {
  109. return [
  110. ['bool', null],
  111. ['mixed', 'foo'],
  112. ['mixed', 1],
  113. ['mixed', new stdClass()],
  114. ['bool', true],
  115. ['boolean', false],
  116. ['int', 1],
  117. ['integer', 4],
  118. ['float', 2.5],
  119. ['double', 6.4],
  120. ['string', 'foo'],
  121. ['object', new stdClass()],
  122. ['array', ['foo']],
  123. [stdClass::class, new stdClass()],
  124. ];
  125. }
  126. /**
  127. * @dataProvider validValues
  128. * @param mixed $value
  129. */
  130. public function testPutValidValue(string $valueType, $value, bool $allowNull = true) : void
  131. {
  132. $map = new EnumMap(WeekDay::class, $valueType, $allowNull);
  133. $map->put(WeekDay::TUESDAY(), $value);
  134. $this->addToAssertionCount(1);
  135. }
  136. public function testRemove() : void
  137. {
  138. $map = new EnumMap(WeekDay::class, 'string', true);
  139. $map->put(WeekDay::TUESDAY(), 'foo');
  140. $map->remove(WeekDay::TUESDAY());
  141. $map->remove(WeekDay::WEDNESDAY());
  142. $this->assertSame(null, $map->get(WeekDay::TUESDAY()));
  143. $this->assertSame(0, $map->size());
  144. }
  145. public function testClear() : void
  146. {
  147. $map = new EnumMap(WeekDay::class, 'string', true);
  148. $map->put(WeekDay::TUESDAY(), 'foo');
  149. $map->clear();
  150. $this->assertSame(null, $map->get(WeekDay::TUESDAY()));
  151. $this->assertSame(0, $map->size());
  152. }
  153. public function testEqualsWithSameInstance() : void
  154. {
  155. $map = new EnumMap(WeekDay::class, 'string', true);
  156. $this->assertTrue($map->equals($map));
  157. }
  158. public function testEqualsWithDifferentSize() : void
  159. {
  160. $mapA = new EnumMap(WeekDay::class, 'string', true);
  161. $mapB = new EnumMap(WeekDay::class, 'string', true);
  162. $mapB->put(WeekDay::MONDAY(), 'foo');
  163. $this->assertFalse($mapA->equals($mapB));
  164. }
  165. public function testEqualsWithDifferentValues() : void
  166. {
  167. $mapA = new EnumMap(WeekDay::class, 'string', true);
  168. $mapA->put(WeekDay::MONDAY(), 'foo');
  169. $mapB = new EnumMap(WeekDay::class, 'string', true);
  170. $mapB->put(WeekDay::MONDAY(), 'bar');
  171. $this->assertFalse($mapA->equals($mapB));
  172. }
  173. public function testEqualsWithDifferentConstants() : void
  174. {
  175. $mapA = new EnumMap(WeekDay::class, 'string', true);
  176. $mapA->put(WeekDay::MONDAY(), 'foo');
  177. $mapB = new EnumMap(WeekDay::class, 'string', true);
  178. $mapB->put(WeekDay::TUESDAY(), 'foo');
  179. $this->assertFalse($mapA->equals($mapB));
  180. }
  181. public function testValues() : void
  182. {
  183. $map = new EnumMap(WeekDay::class, 'string', true);
  184. $this->assertSame([], $map->values());
  185. $map->put(WeekDay::FRIDAY(), 'foo');
  186. $map->put(WeekDay::TUESDAY(), 'bar');
  187. $map->put(WeekDay::SUNDAY(), null);
  188. $this->assertSame(['bar', 'foo', null], $map->values());
  189. }
  190. public function testSerializeAndUnserialize() : void
  191. {
  192. $mapA = new EnumMap(WeekDay::class, 'string', true);
  193. $mapA->put(WeekDay::MONDAY(), 'foo');
  194. $mapB = unserialize(serialize($mapA));
  195. $this->assertTrue($mapA->equals($mapB));
  196. }
  197. public function testIterator() : void
  198. {
  199. $map = new EnumMap(WeekDay::class, 'string', true);
  200. $map->put(WeekDay::FRIDAY(), 'foo');
  201. $map->put(WeekDay::TUESDAY(), 'bar');
  202. $map->put(WeekDay::SUNDAY(), null);
  203. $result = [];
  204. foreach ($map as $key => $value) {
  205. $result[$key->ordinal()] = $value;
  206. }
  207. $this->assertSame([1 => 'bar', 4 => 'foo', 6 => null], $result);
  208. }
  209. }