Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

tests for @Enum support

  • Loading branch information...
commit e0d3d352f9737801168745c1c96e2693e1b934ad 1 parent b927d78
@FabioBatSilva FabioBatSilva authored
View
46 lib/Doctrine/Common/Annotations/Annotation/Enum.php
@@ -24,9 +24,13 @@
* Annotation that can be used to signal to the parser
* to check the available values during the parsing process.
*
+ * @since 2.4
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*
* @Annotation
+ * @Attribute("value", required = true, type = "array"),
+ * @Attribute("literal", required = false, type = "array"),
+ * })
*/
final class Enum
{
@@ -35,4 +39,46 @@
*/
public $value;
+ /**
+ * Literal target declaration.
+ *
+ * @var array
+ */
+ public $literal;
+
+ /**
+ * Annotation construct
+ *
+ * @param array $values
+ *
+ * @throws \InvalidArgumentException
+ */
+ public function __construct(array $values)
+ {
+ if ( ! isset($values['literal'])) {
+ $values['literal'] = array();
+ }
+
+ foreach ($values['value'] as $var) {
+ if( ! is_scalar($var)) {
+ throw new \InvalidArgumentException(sprintf(
+ '@Enum supports only scalar values "%s" given.',
+ is_object($var) ? get_class($var) : gettype($var)
+ ));
+ }
+ }
+
+ foreach ($values['literal'] as $key => $var) {
+ if( ! in_array($key, $values['value'])) {
+ throw new \InvalidArgumentException(sprintf(
+ 'Undefined enumerator value "%s" for literal "%s".',
+ $key , $var
+ ));
+ }
+ }
+
+ $this->value = $values['value'];
+ $this->literal = $values['literal'];
+ }
+
}
View
23 lib/Doctrine/Common/Annotations/AnnotationException.php
@@ -124,4 +124,27 @@ public static function requiredError($attributeName, $annotationName, $context,
$expected
));
}
+
+ /**
+ * Creates a new AnnotationException describing a invalid enummerator.
+ *
+ * @since 2.4
+ * @param string $attributeName
+ * @param string $annotationName
+ * @param string $context
+ * @param array $available
+ * @param mixed $given
+ * @return AnnotationException
+ */
+ public static function enumeratorError($attributeName, $annotationName, $context, $available, $given)
+ {
+ throw new self(sprintf(
+ '[Enum Error] Attribute "%s" of @%s declared on %s accept only [%s], but got %s.',
+ $attributeName,
+ $annotationName,
+ $context,
+ implode(', ', $available),
+ is_object($given) ? get_class($given) : $given
+ ));
+ }
}
View
20 lib/Doctrine/Common/Annotations/DocParser.php
@@ -190,7 +190,7 @@
),
'Doctrine\Common\Annotations\Annotation\Enum' => array(
'is_annotation' => true,
- 'has_constructor' => false,
+ 'has_constructor' => true,
'targets_literal' => 'ANNOTATION_PROPERTY',
'targets' => Target::TARGET_PROPERTY,
'default_property' => 'value',
@@ -201,7 +201,11 @@
'value' => array(
'type' => 'array',
'required' => true,
- )
+ ),
+ 'literal' => array(
+ 'type' => 'array',
+ 'required' => false,
+ ),
),
),
);
@@ -523,7 +527,8 @@ private function collectAnnotationMetadata($name)
foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) {
if($annotation instanceof Enum) {
- $metadata['enum'][$property->name] = $annotation->value;
+ $metadata['enum'][$property->name]['value'] = $annotation->value;
+ $metadata['enum'][$property->name]['literal'] = ! empty($annotation->literal) ? $annotation->literal : $annotation->value;
}
}
}
@@ -680,12 +685,11 @@ private function Annotation()
}
if (isset(self::$annotationMetadata[$name]['enum'])) {
+ // checks all declared attributes
foreach (self::$annotationMetadata[$name]['enum'] as $property => $enum) {
- if(isset($values[$property]) && ! in_array($values[$property], $enum)) {
- throw new AnnotationException(sprintf(
- '[Enum Error] Attribute "%s" of @%s declared on %s accept only [%s], but got %s.',
- $property, $name, $this->context, implode(', ', $enum), $values[$property]
- ));
+ // checks if the attribute is a valid enumerator
+ if (isset($values[$property]) && ! in_array($values[$property], $enum['value'])) {
+ throw AnnotationException::enumeratorError($property, $name, $this->context, $enum['literal'], $values[$property]);
}
}
}
View
45 tests/Doctrine/Tests/Common/Annotations/DocParserTest.php
@@ -666,12 +666,10 @@ public function testAnnotationWithRequiredAttributesWithoutContructor()
}
/**
- * @TODO - change to data provider
- *
* @expectedException Doctrine\Common\Annotations\AnnotationException
* @expectedExceptionMessage Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum declared on property SomeClassName::invalidProperty. accept only [ONE, TWO, THREE], but got FOUR.
*/
- public function testAnnotationWithInvalidEnumValue()
+ public function testAnnotationEnumeratorException()
{
$parser = $this->createTestParser();
$context = 'property SomeClassName::invalidProperty.';
@@ -681,6 +679,47 @@ public function testAnnotationWithInvalidEnumValue()
$parser->setTarget(Target::TARGET_PROPERTY);
$parser->parse($docblock, $context);
}
+
+ /**
+ * @expectedException Doctrine\Common\Annotations\AnnotationException
+ * @expectedExceptionMessage Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral declared on property SomeClassName::invalidProperty. accept only [AnnotationEnumLiteral::ONE, AnnotationEnumLiteral::TWO, AnnotationEnumLiteral::THREE], but got 4.
+ */
+ public function testAnnotationEnumeratorLiteralException()
+ {
+ $parser = $this->createTestParser();
+ $context = 'property SomeClassName::invalidProperty.';
+ $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral(4)';
+
+ $parser->setIgnoreNotImportedAnnotations(false);
+ $parser->setTarget(Target::TARGET_PROPERTY);
+ $parser->parse($docblock, $context);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage @Enum supports only scalar values "array" given.
+ */
+ public function testAnnotationEnumInvalidTypeDeclarationException()
+ {
+ $parser = $this->createTestParser();
+ $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumInvalid("foo")';
+
+ $parser->setIgnoreNotImportedAnnotations(false);
+ $parser->parse($docblock);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ * @expectedExceptionMessage Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE".
+ */
+ public function testAnnotationEnumInvalidLiteralDeclarationException()
+ {
+ $parser = $this->createTestParser();
+ $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteralInvalid("foo")';
+
+ $parser->setIgnoreNotImportedAnnotations(false);
+ $parser->parse($docblock);
+ }
public function getConstantsProvider()
{
View
17 tests/Doctrine/Tests/Common/Annotations/Fixtures/AnnotationEnumInvalid.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Doctrine\Tests\Common\Annotations\Fixtures;
+
+/**
+ * @Annotation
+ * @Target("ALL")
+ */
+final class AnnotationEnumInvalid
+{
+ /**
+ * @var mixed
+ *
+ * @Enum({1, 2, "foo", "bar", {"foo":"bar"}})
+ */
+ public $value;
+}
View
34 tests/Doctrine/Tests/Common/Annotations/Fixtures/AnnotationEnumLiteral.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Doctrine\Tests\Common\Annotations\Fixtures;
+
+use Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral as SelfEnum;
+
+/**
+ * @Annotation
+ * @Target("ALL")
+ */
+final class AnnotationEnumLiteral
+{
+ const ONE = 1;
+ const TWO = 2;
+ const THREE = 3;
+
+ /**
+ * @var mixed
+ *
+ * @Enum(
+ * value = {
+ * 1,
+ * 2,
+ * 3,
+ * },
+ * literal = {
+ * 1 : "AnnotationEnumLiteral::ONE",
+ * 2 : "AnnotationEnumLiteral::TWO",
+ * 3 : "AnnotationEnumLiteral::THREE",
+ * }
+ * )
+ */
+ public $value;
+}
View
31 tests/Doctrine/Tests/Common/Annotations/Fixtures/AnnotationEnumLiteralInvalid.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Doctrine\Tests\Common\Annotations\Fixtures;
+
+/**
+ * @Annotation
+ * @Target("ALL")
+ */
+final class AnnotationEnumLiteralInvalid
+{
+ const ONE = 1;
+ const TWO = 2;
+ const THREE = 3;
+
+ /**
+ * @var mixed
+ *
+ * @Enum(
+ * value = {
+ * 1,
+ * 2
+ * },
+ * literal = {
+ * 1 : "AnnotationEnumLiteral::ONE",
+ * 2 : "AnnotationEnumLiteral::TWO",
+ * 3 : "AnnotationEnumLiteral::THREE"
+ * }
+ * )
+ */
+ public $value;
+}

0 comments on commit e0d3d35

Please sign in to comment.
Something went wrong with that request. Please try again.