PHP: Add Enum methods for converting to/from strings (#5342)
* adds string-to-int and int-to-string methods to enums
* remove check for valueToName property in EnumTrait
* Remove unused imports
* Update to avoid using EnumTrait
* Remove EnumTrait
* Update enum types
* Move name and value methods into generated classes
* Remove functions from GPBUtil
* Test well known enums
* Implement enum value to/from name in c extension
* Only generate use statement when namespace is present
diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php
index 8bac4e5..96dad22 100644
--- a/php/tests/generated_class_test.php
+++ b/php/tests/generated_class_test.php
@@ -232,6 +232,28 @@
// Set string.
$m->setOptionalEnum("1");
$this->assertEquals(TestEnum::ONE, $m->getOptionalEnum());
+
+ // Test Enum methods
+ $this->assertEquals('ONE', TestEnum::name(1));
+ $this->assertEquals(1, TestEnum::value('ONE'));
+ }
+
+ /**
+ * @expectedException UnexpectedValueException
+ * @expectedExceptionMessage Enum Foo\TestEnum has no name defined for value -1
+ */
+ public function testInvalidEnumValueThrowsException()
+ {
+ TestEnum::name(-1);
+ }
+
+ /**
+ * @expectedException UnexpectedValueException
+ * @expectedExceptionMessage Enum Foo\TestEnum has no value defined for name DOES_NOT_EXIST
+ */
+ public function testInvalidEnumNameThrowsException()
+ {
+ TestEnum::value('DOES_NOT_EXIST');
}
public function testNestedEnum()