From 970ed20b65156fa543bff2d18efc57a02563a146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miros=C5=82aw=20Filip?= Date: Sun, 8 Feb 2015 22:26:36 +0100 Subject: [PATCH 1/5] Rewrite tests of creating Enum from invalid values - use data provider --- src/Enum.php | 5 +++-- tests/EnumTest.php | 32 +++++++++++++++++--------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index bfeaab4..3419949 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -12,7 +12,8 @@ * Create an enum by implementing this class and adding class constants. * * @author Matthieu Napoli - * @author Daniel Costa + * @author Mirosław Filip */ abstract class Enum { @@ -22,7 +23,7 @@ abstract class Enum * @var mixed */ protected $value; - + /** * Store existing constants in a static cache per object. * diff --git a/tests/EnumTest.php b/tests/EnumTest.php index d01b20b..559d6da 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -8,7 +8,8 @@ /** * @author Matthieu Napoli - * @author Daniel Costa + * @author Mirosław Filip */ class EnumTest extends \PHPUnit_Framework_TestCase { @@ -38,27 +39,28 @@ public function testGetKey() } /** - * @expectedException \UnexpectedValueException + * @dataProvider invalidValueProvider */ - public function testInvalidValueString() + public function testCreatingEnumWithInvalidValue($value) { - new EnumFixture("test"); - } + $this->setExpectedException( + '\UnexpectedValueException', + 'Value \'' . $value . '\' is not part of the enum MyCLabs\Tests\Enum\EnumFixture' + ); - /** - * @expectedException \UnexpectedValueException - */ - public function testInvalidValueInt() - { - new EnumFixture(1234); + new EnumFixture($value); } /** - * @expectedException \UnexpectedValueException + * Contains values not existing in EnumFixture + * @return array */ - public function testInvalidValueEmpty() - { - new EnumFixture(null); + public function invalidValueProvider() { + return [ + "string" => ['test'], + "int" => [1234], + "null" => [null], + ]; } /** From e65fd092daeee9bf2d77a96dbab72098ba11fcee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miros=C5=82aw=20Filip?= Date: Sun, 8 Feb 2015 22:35:53 +0100 Subject: [PATCH 2/5] Improve __toString() tests --- tests/EnumTest.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 559d6da..1f4451d 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -65,17 +65,19 @@ public function invalidValueProvider() { /** * __toString() + * @dataProvider toStringProvider */ - public function testToString() + public function testToString($expected, $enumObject) { - $value = new EnumFixture(EnumFixture::FOO); - $this->assertEquals(EnumFixture::FOO, (string) $value); - - $value = new EnumFixture(EnumFixture::BAR); - $this->assertEquals(EnumFixture::BAR, (string) $value); + $this->assertSame($expected, (string) $enumObject); + } - $value = new EnumFixture(EnumFixture::NUMBER); - $this->assertEquals((string) EnumFixture::NUMBER, (string) $value); + public function toStringProvider() { + return [ + [EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)], + [EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)], + [(string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)], + ]; } /** From f12b80adee7ee3ae4abb7c4b8ca29745e69dc87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miros=C5=82aw=20Filip?= Date: Sun, 8 Feb 2015 23:31:40 +0100 Subject: [PATCH 3/5] Fix ability to create invalid enums. Fixes #9 --- src/Enum.php | 10 ++++----- tests/EnumFixture.php | 16 ++++++++++++- tests/EnumTest.php | 52 +++++++++++++++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/Enum.php b/src/Enum.php index 3419949..d8fc4c1 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -40,7 +40,7 @@ abstract class Enum */ public function __construct($value) { - if (!in_array($value, self::toArray())) { + if (!$this->isValid($value)) { throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class()); } @@ -95,7 +95,6 @@ public static function toArray() $reflection = new \ReflectionClass($class); self::$cache[$class] = $reflection->getConstants(); } - return self::$cache[$class]; } @@ -107,7 +106,7 @@ public static function toArray() */ public static function isValid($value) { - return in_array($value, self::toArray()); + return in_array($value, self::toArray(), true); } /** @@ -119,7 +118,7 @@ public static function isValid($value) */ public static function isValidKey($key) { - return in_array($key, self::keys()); + return in_array($key, self::keys(), true); } /** @@ -131,7 +130,8 @@ public static function isValidKey($key) */ public static function search($value) { - return array_search($value, array_combine(self::keys(), self::toArray())); + // TODO: Replace combine with self::toArray() + return array_search($value, array_combine(self::keys(), self::toArray()), true); } /** diff --git a/tests/EnumFixture.php b/tests/EnumFixture.php index 9ea26fc..d04b890 100644 --- a/tests/EnumFixture.php +++ b/tests/EnumFixture.php @@ -15,11 +15,25 @@ * @method static EnumFixture BAR() * @method static EnumFixture NUMBER() * - * @author Daniel Costa + * @author Mirosław Filip */ class EnumFixture extends Enum { const FOO = "foo"; const BAR = "bar"; const NUMBER = 42; + + /** + * Values that are known to cause problems when used with soft typing + */ + const PROBLEMATIC_NUMBER = 0; + const PROBLEMATIC_NULL = null; + const PROBLEMATIC_EMPTY_STRING = ''; + const PROBLEMATIC_BOOLEAN_FALSE = false; } diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 1f4451d..ae515da 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -59,7 +59,6 @@ public function invalidValueProvider() { return [ "string" => ['test'], "int" => [1234], - "null" => [null], ]; } @@ -86,13 +85,17 @@ public function toStringProvider() { public function testKeys() { $values = EnumFixture::keys(); - $this->assertInternalType("array", $values); $expectedValues = array( "FOO", "BAR", "NUMBER", + "PROBLEMATIC_NUMBER", + "PROBLEMATIC_NULL", + "PROBLEMATIC_EMPTY_STRING", + "PROBLEMATIC_BOOLEAN_FALSE", ); - $this->assertEquals($expectedValues, $values); + + $this->assertSame($expectedValues, $values); } /** @@ -101,13 +104,17 @@ public function testKeys() public function testToArray() { $values = EnumFixture::toArray(); - $this->assertInternalType("array", $values); $expectedValues = array( - "FOO" => EnumFixture::FOO, - "BAR" => EnumFixture::BAR, - "NUMBER" => EnumFixture::NUMBER, + "FOO" => EnumFixture::FOO, + "BAR" => EnumFixture::BAR, + "NUMBER" => EnumFixture::NUMBER, + "PROBLEMATIC_NUMBER" => EnumFixture::PROBLEMATIC_NUMBER, + "PROBLEMATIC_NULL" => EnumFixture::PROBLEMATIC_NULL, + "PROBLEMATIC_EMPTY_STRING" => EnumFixture::PROBLEMATIC_EMPTY_STRING, + "PROBLEMATIC_BOOLEAN_FALSE" => EnumFixture::PROBLEMATIC_BOOLEAN_FALSE, ); - $this->assertEquals($expectedValues, $values); + + $this->assertSame($expectedValues, $values); } /** @@ -132,11 +139,29 @@ public function testBadStaticAccess() /** * isValid() + * @dataProvider isValidProvider */ - public function testIsValid() + public function testIsValid($value, $isValid) { - $this->assertTrue(EnumFixture::isValid('foo')); - $this->assertFalse(EnumFixture::isValid('baz')); + $this->assertSame($isValid, EnumFixture::isValid($value)); + } + + public function isValidProvider() { + return [ + /** + * Valid values + */ + ['foo', true], + [42, true], + [null, true], + [0, true], + ['', true], + [false, true], + /** + * Invalid values + */ + ['baz', false] + ]; } /** @@ -154,6 +179,9 @@ public function testIsValidKey() public function testSearch() { $this->assertEquals('FOO', EnumFixture::search('foo')); - $this->assertNotEquals('FOO', EnumFixture::isValidKey('baz')); + /** + * @see https://github.com/myclabs/php-enum/issues/9 + */ + $this->assertEquals(EnumFixture::PROBLEMATIC_NUMBER, EnumFixture::search(1)); } } From 749458cf4134be80ac25ece3fbf29f065683a577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miros=C5=82aw=20Filip?= Date: Mon, 9 Feb 2015 00:00:21 +0100 Subject: [PATCH 4/5] Use PHP 5.3 compatible array() notation instead of [] --- tests/EnumTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/EnumTest.php b/tests/EnumTest.php index ae515da..b2f1657 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -57,8 +57,8 @@ public function testCreatingEnumWithInvalidValue($value) */ public function invalidValueProvider() { return [ - "string" => ['test'], - "int" => [1234], + "string" => array('test'), + "int" => array(1234), ]; } @@ -73,9 +73,9 @@ public function testToString($expected, $enumObject) public function toStringProvider() { return [ - [EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)], - [EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)], - [(string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)], + array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)), + array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)), + array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)), ]; } @@ -151,16 +151,16 @@ public function isValidProvider() { /** * Valid values */ - ['foo', true], - [42, true], - [null, true], - [0, true], - ['', true], - [false, true], + array('foo', true), + array(42, true), + array(null, true), + array(0, true), + array('', true), + array(false, true), /** * Invalid values */ - ['baz', false] + array('baz', false) ]; } From dae119a7d62b7c5e8e2791fdef598939f0b9a494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miros=C5=82aw=20Filip?= Date: Mon, 9 Feb 2015 00:05:13 +0100 Subject: [PATCH 5/5] One more fix for array() instead of [] --- tests/EnumTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/EnumTest.php b/tests/EnumTest.php index b2f1657..64270bb 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -56,10 +56,10 @@ public function testCreatingEnumWithInvalidValue($value) * @return array */ public function invalidValueProvider() { - return [ + return array( "string" => array('test'), "int" => array(1234), - ]; + ); } /** @@ -72,11 +72,11 @@ public function testToString($expected, $enumObject) } public function toStringProvider() { - return [ + return array( array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)), array(EnumFixture::BAR, new EnumFixture(EnumFixture::BAR)), array((string) EnumFixture::NUMBER, new EnumFixture(EnumFixture::NUMBER)), - ]; + ); } /** @@ -147,7 +147,7 @@ public function testIsValid($value, $isValid) } public function isValidProvider() { - return [ + return array( /** * Valid values */ @@ -161,7 +161,7 @@ public function isValidProvider() { * Invalid values */ array('baz', false) - ]; + ); } /**