From f4d03c393e51fefb1a3077c1085b7a869a75b4a2 Mon Sep 17 00:00:00 2001 From: Donatas Martinkus Date: Sat, 29 Apr 2017 19:45:04 +0100 Subject: [PATCH 1/6] types available as singletons --- src/Parser.php | 37 ++++++++++++++---- src/TypeContainer.php | 77 ++++++++++++++++++++++++++++++++++++++ src/Types/AbstractType.php | 13 +++++++ src/Types/ConcreteType.php | 24 +++++++++--- src/Types/DynamicType.php | 17 +++++++-- src/Types/FloatType.php | 16 +++++++- src/Types/IntegerType.php | 16 +++++++- src/Types/JsonType.php | 16 +++++++- src/Types/StringType.php | 17 +++++++-- test/suite/ParserTest.php | 2 +- 10 files changed, 209 insertions(+), 26 deletions(-) create mode 100644 src/TypeContainer.php create mode 100644 src/Types/AbstractType.php diff --git a/src/Parser.php b/src/Parser.php index 62314bf..a9e798a 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -19,13 +19,20 @@ class Parser extends DynamicParser * @var array */ protected $types = [ - '\Minime\Annotations\Types\IntegerType' => 'integer', - '\Minime\Annotations\Types\StringType' => 'string', - '\Minime\Annotations\Types\FloatType' => 'float', - '\Minime\Annotations\Types\JsonType' => 'json', - '\Minime\Annotations\Types\ConcreteType' => '->' + 'integerType' => 'integer', + 'stringType' => 'string', + 'floatType' => 'float', + 'jsonType' => 'json', + 'concreteType' => '->' ]; + /** + * A fallback type if no strong type declaration found. + * + * @var string + */ + protected $typeFallback = 'dynamicType'; + /** * The regex equivalent of $types * @@ -33,12 +40,24 @@ class Parser extends DynamicParser */ protected $typesPattern; + /** + * @var TypeContainer + */ + private $typeContainer; + /** * Parser constructor * */ public function __construct() { + $this->typeContainer = new TypeContainer(); + $this->typeContainer->add($this->typeFallback); + + foreach ($this->types as $key => $value) { + $this->typeContainer->add($key); + } + $this->buildTypesPattern(); parent::__construct(); } @@ -46,12 +65,14 @@ public function __construct() public function registerType($class, $token) { $this->types[$class] = $token; + $this->typeContainer->add($class); $this->buildTypesPattern(); } public function unregisterType($class) { unset($this->types[$class]); + $this->typeContainer->remove($class); $this->buildTypesPattern(); } @@ -65,16 +86,18 @@ public function unregisterType($class) protected function parseValue($value, $key = null) { $value = trim($value); - $type = '\Minime\\Annotations\\Types\\DynamicType'; + $type = $this->typeFallback; + if (preg_match($this->typesPattern, $value, $found)) { // strong typed $type = $found[1]; $value = trim(substr($value, strlen($type))); + if (in_array($type, $this->types)) { $type = array_search($type, $this->types); } } - return (new $type)->parse($value, $key); + return $this->typeContainer->{$type}->parse($value, $key); } /** diff --git a/src/TypeContainer.php b/src/TypeContainer.php new file mode 100644 index 0000000..a3b794d --- /dev/null +++ b/src/TypeContainer.php @@ -0,0 +1,77 @@ +builders[$name] = function() use($name) { + $type = 'Minime\\Annotations\\Types\\' . ucfirst($name); + + // do we have in default configuration setup? + if (!class_exists($type)) { + $type = $name; + } + + if (is_callable($type . '::getType')) { + $typeClass = call_user_func($type . '::getType'); + } else { + $typeClass = new $type; + } + + return new $typeClass; + }; + } + + /** + * Remove a lambda function. + * + * @param string $name + */ + public function remove($name) + { + unset($this->builders[$name]); + unset($this->types[$name]); + } + + /** + * @param string $name + * + * @return TypeInterface + */ + public function __get($name) + { + if (!isset($this->types[$name]) && isset($this->builders[$name])) { + $this->types[$name] = $this->builders[$name](); + } + + return $this->types[$name]; + } +} diff --git a/src/Types/AbstractType.php b/src/Types/AbstractType.php new file mode 100644 index 0000000..19f1683 --- /dev/null +++ b/src/Types/AbstractType.php @@ -0,0 +1,13 @@ +parse($value); @@ -79,7 +91,7 @@ protected function doMethodConfiguration($instance, stdClass $prototype) { foreach ($prototype as $method => $args) { call_user_func_array([$instance, $method], $args); - } + } return $instance; } @@ -92,7 +104,7 @@ protected function doMethodConfiguration($instance, stdClass $prototype) */ protected function isPrototypeSchemaValid(stdclass $prototype) { - foreach ($prototype as $method => $args) { + foreach ($prototype as $args) { if (! is_array($args)) { return false; } diff --git a/src/Types/DynamicType.php b/src/Types/DynamicType.php index 2f5bd49..5371c46 100644 --- a/src/Types/DynamicType.php +++ b/src/Types/DynamicType.php @@ -2,10 +2,21 @@ namespace Minime\Annotations\Types; -use Minime\Annotations\Interfaces\TypeInterface; - -class DynamicType implements TypeInterface +class DynamicType extends AbstractType { + /** + * @var TypeInterface + */ + private static $instance; + + public static function getType() + { + if (!isset(self::$instance)) { + self::$instance = new DynamicType(); + } + + return self::$instance; + } /** * Parse a given undefined type value diff --git a/src/Types/FloatType.php b/src/Types/FloatType.php index 9fafde4..7242c38 100644 --- a/src/Types/FloatType.php +++ b/src/Types/FloatType.php @@ -2,11 +2,23 @@ namespace Minime\Annotations\Types; -use Minime\Annotations\Interfaces\TypeInterface; use Minime\Annotations\ParserException; -class FloatType implements TypeInterface +class FloatType extends AbstractType { + /** + * @var TypeInterface + */ + private static $instance; + + public static function getType() + { + if (!isset(self::$instance)) { + self::$instance = new FloatType(); + } + + return self::$instance; + } /** * Filter a value to be a Float diff --git a/src/Types/IntegerType.php b/src/Types/IntegerType.php index a18ba03..de63e01 100644 --- a/src/Types/IntegerType.php +++ b/src/Types/IntegerType.php @@ -2,11 +2,23 @@ namespace Minime\Annotations\Types; -use Minime\Annotations\Interfaces\TypeInterface; use Minime\Annotations\ParserException; -class IntegerType implements TypeInterface +class IntegerType extends AbstractType { + /** + * @var TypeInterface + */ + private static $instance; + + public static function getType() + { + if (!isset(self::$instance)) { + self::$instance = new IntegerType(); + } + + return self::$instance; + } /** * Filter a value to be an Integer diff --git a/src/Types/JsonType.php b/src/Types/JsonType.php index ac9d90e..cbceb87 100644 --- a/src/Types/JsonType.php +++ b/src/Types/JsonType.php @@ -2,11 +2,23 @@ namespace Minime\Annotations\Types; -use Minime\Annotations\Interfaces\TypeInterface; use Minime\Annotations\ParserException; -class JsonType implements TypeInterface +class JsonType extends AbstractType { + /** + * @var TypeInterface + */ + private static $instance; + + public static function getType() + { + if (!isset(self::$instance)) { + self::$instance = new JsonType(); + } + + return self::$instance; + } /** * Filter a value to be a Json diff --git a/src/Types/StringType.php b/src/Types/StringType.php index b813f90..f78d8b2 100644 --- a/src/Types/StringType.php +++ b/src/Types/StringType.php @@ -2,10 +2,21 @@ namespace Minime\Annotations\Types; -use Minime\Annotations\Interfaces\TypeInterface; - -class StringType implements TypeInterface +class StringType extends AbstractType { + /** + * @var TypeInterface + */ + private static $instance; + + public static function getType() + { + if (!isset(self::$instance)) { + self::$instance = new StringType(); + } + + return self::$instance; + } /** * Parse a given value as string diff --git a/test/suite/ParserTest.php b/test/suite/ParserTest.php index 55fdb1d..b1aafbc 100644 --- a/test/suite/ParserTest.php +++ b/test/suite/ParserTest.php @@ -7,7 +7,7 @@ /** * ParserTest - * + * * @group parser */ class ParserTest extends DynamicParserTest From f8302b135254d9de6e8b8d8faf10ec50238efb0a Mon Sep 17 00:00:00 2001 From: Donatas Martinkus Date: Sat, 29 Apr 2017 22:02:11 +0100 Subject: [PATCH 2/6] adding user defined namespaces for ConcreteType --- src/DynamicParser.php | 5 +++ src/Interfaces/ParserInterface.php | 7 ++++ src/Parser.php | 4 ++ src/Types/ConcreteType.php | 61 +++++++++++++++++++++++----- test/fixtures/AnnotationsFixture.php | 35 ++++++++++------ test/suite/BaseTest.php | 4 ++ test/suite/DynamicParserTest.php | 4 +- test/suite/ParserTest.php | 50 +++++++++++++++++++++-- 8 files changed, 142 insertions(+), 28 deletions(-) diff --git a/src/DynamicParser.php b/src/DynamicParser.php index 4e3c967..59c79e1 100644 --- a/src/DynamicParser.php +++ b/src/DynamicParser.php @@ -121,4 +121,9 @@ protected function sanitizeKey($key) { return $key; } + + public function registerConcreteNamespaceLookup(array $namespaces) + { + } + } diff --git a/src/Interfaces/ParserInterface.php b/src/Interfaces/ParserInterface.php index 89da9af..a93b6b7 100644 --- a/src/Interfaces/ParserInterface.php +++ b/src/Interfaces/ParserInterface.php @@ -17,4 +17,11 @@ interface ParserInterface * @return array */ public function parse($docBlock); + + /** + * Register set of namespaces for ConcreteType to autolookup. + * + * @param array $namespaces + */ + public function registerConcreteNamespaceLookup(array $namespaces); } diff --git a/src/Parser.php b/src/Parser.php index a9e798a..a806fc9 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -119,4 +119,8 @@ protected function buildTypesPattern() { $this->typesPattern = '/^('.implode('|', $this->types).')(\s+)/'; } + + public function registerConcreteNamespaceLookup(array $namespaces) { + $this->typeContainer->concreteType->setNamespaces($namespaces); + } } diff --git a/src/Types/ConcreteType.php b/src/Types/ConcreteType.php index e01dea4..14bbd34 100644 --- a/src/Types/ConcreteType.php +++ b/src/Types/ConcreteType.php @@ -22,35 +22,76 @@ public static function getType() return self::$instance; } + /** + * @var array + */ + private $namespaceLookup = []; + + /** + * Set of user defined namespaces to lookup for class autoloading. + * + * @param array $namespaces + */ + public function setNamespaces(array $namespaces) + { + $this->namespaceLookup = $namespaces; + } + + /** + * @param string $class + * + * @return string + * + * @throws ParserException + */ + protected function checkClassExistence($class) + { + $found = class_exists($class); + $classname = $class; + $i = 0; + + while (!$found && $i < count($this->namespaceLookup)) { + $classname = $this->namespaceLookup[$i] . $class; + $found = class_exists($classname); + $i++; + } + + if (!$found) { + throw new ParserException("Concrete annotation expects '{$class}' to exist."); + } + + return $classname; + } + /** * Process a value to be a concrete annotation * - * @param string $value json string - * @param string $class name of concrete annotation type (class) - * @throws \Minime\Annotations\ParserException + * @param string $value json string + * @param string $class name of concrete annotation type (class) + * + * @throws ParserException + * * @return object */ public function parse($value, $class = null) { - if (!class_exists($class)) { - throw new ParserException("Concrete annotation expects '{$class}' to exist."); - } + $classname = $this->checkClassExistence($class); $prototype = (new JsonType)->parse($value); if ($prototype instanceof stdClass) { - if (! $this->isPrototypeSchemaValid($prototype)) { + if (!$this->isPrototypeSchemaValid($prototype)) { throw new ParserException("Only arrays should be used to configure concrete annotation method calls."); } - return $this->makeInstance($class, $prototype); + return $this->makeInstance($classname, $prototype); } if (is_array($prototype)) { - return $this->makeConstructSugarInjectionInstance($class, $prototype); + return $this->makeConstructSugarInjectionInstance($classname, $prototype); } - throw new ParserException("Json value for annotation({$class}) must be of type object or array."); + throw new ParserException("Json value for annotation({$classname}) must be of type object or array."); } protected function makeConstructSugarInjectionInstance($class, array $prototype) { diff --git a/test/fixtures/AnnotationsFixture.php b/test/fixtures/AnnotationsFixture.php index d48e047..06d546f 100644 --- a/test/fixtures/AnnotationsFixture.php +++ b/test/fixtures/AnnotationsFixture.php @@ -36,9 +36,9 @@ class AnnotationsFixture private $implicit_boolean_fixture; /** - * @value abc - * @value "abc" - * @value "abc " + * @value abc + * @value "abc" + * @value "abc " * @value "123" */ private $string_fixture; @@ -82,6 +82,15 @@ class AnnotationsFixture */ private $concrete_fixture; + /** + * @AnnotationConstructInjection -> { "__construct" : ["bar"] } + * + * @AnnotationConstructSugarInjection -> ["foo", "bar"] + * + * @AnnotationSetterInjection -> { "setFoo" : ["bar"] } + */ + private $short_concrete_fixture; + /** * @SomeUndefinedClass -> {} */ @@ -121,17 +130,17 @@ class AnnotationsFixture /** * @value string abc - * @value string 45 + * @value string 45 * @value integer 45 - * @value integer -45 + * @value integer -45 * @value float .45 * @value float 0.45 * @value float 45. * @value float -4.5 - * @value float 4 + * @value float 4 * @json_value json ["x", "y"] * @json_value json {"x": {"y": "z"}} - * @json_value json {"x": {"y": ["z", "p"]}} + * @json_value json {"x": {"y": ["z", "p"]}} */ private $strong_typed_fixture; @@ -145,14 +154,14 @@ class AnnotationsFixture * @multiline_indented_string * ------ * < moo > - * ------ + * ------ * \ ^__^ * \ (oo)\_______ * (__)\ )\/\ * ||----w | * || || * - * + * * @multiline_json json { * "x": { * "y": [ @@ -168,10 +177,10 @@ class AnnotationsFixture * @value integer * @value float * @value json - * @value_with_trailing_space string - * @value_with_trailing_space integer - * @value_with_trailing_space float - * @value_with_trailing_space json + * @value_with_trailing_space string + * @value_with_trailing_space integer + * @value_with_trailing_space float + * @value_with_trailing_space json */ private $reserved_words_as_value_fixture; diff --git a/test/suite/BaseTest.php b/test/suite/BaseTest.php index 0bee04e..11d372f 100644 --- a/test/suite/BaseTest.php +++ b/test/suite/BaseTest.php @@ -4,6 +4,7 @@ use \ReflectionProperty; use Minime\Annotations\Fixtures\AnnotationsFixture; +use Minime\Annotations\Interfaces\ParserInterface; /** * BaseTest @@ -13,6 +14,9 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase { protected $fixture; + /** + * @var ParserInterface + */ protected $parser; public function setUp() diff --git a/test/suite/DynamicParserTest.php b/test/suite/DynamicParserTest.php index 7052d2d..25fff07 100644 --- a/test/suite/DynamicParserTest.php +++ b/test/suite/DynamicParserTest.php @@ -4,7 +4,7 @@ /** * DynamicParserTest - * + * * @group parser */ class DynamicParserTest extends BaseTest @@ -153,7 +153,7 @@ public function parseMultilineValueFixture() ."Suspendisse egestas orci a felis imperdiet, non consectetur est suscipit."; $this->assertSame($string, $annotations['multiline_string']); - $cowsay = "------\n< moo >\n------ \n \ ^__^\n ". + $cowsay = "------\n< moo >\n------\n \ ^__^\n ". "\ (oo)\_______\n (__)\ )\/\\\n ". "||----w |\n || ||"; $this->assertSame($cowsay, $annotations['multiline_indented_string']); diff --git a/test/suite/ParserTest.php b/test/suite/ParserTest.php index b1aafbc..66c96c6 100644 --- a/test/suite/ParserTest.php +++ b/test/suite/ParserTest.php @@ -2,9 +2,6 @@ namespace Minime\Annotations; -use \ReflectionProperty; -use Minime\Annotations\Fixtures\AnnotationsFixture; - /** * ParserTest * @@ -40,6 +37,14 @@ public function parseConcreteFixture() '{"foo":"bar","bar":"baz"}', json_encode($annotations['Minime\Annotations\Fixtures\AnnotationConstructInjection'][1]) ); + $this->assertInstanceOf( + 'Minime\Annotations\Fixtures\AnnotationConstructSugarInjection', + $annotations['Minime\Annotations\Fixtures\AnnotationConstructSugarInjection'][0] + ); + $this->assertInstanceOf( + 'Minime\Annotations\Fixtures\AnnotationConstructSugarInjection', + $annotations['Minime\Annotations\Fixtures\AnnotationConstructSugarInjection'][1] + ); $this->assertSame( '{"foo":"foo","bar":"bar"}', json_encode($annotations['Minime\Annotations\Fixtures\AnnotationConstructSugarInjection'][0]) @@ -173,4 +178,43 @@ public function testTypeRegister() $this->parser->unregisterType('\Minime\Annotations\Fixtures\FooType'); $this->assertSame(['value' => 'foo bar'], $this->parser->parse($docblock)); } + + /** + * @test + */ + public function testConcreteTypeNamespaceLookup() + { + $this->parser->registerConcreteNamespaceLookup([ + 'Minime\\Annotations\\Types\\Dummy\\', + 'Minime\\Annotations\\Fixtures\\' + ]); + + $annotations = $this->getFixture('short_concrete_fixture'); + + $this->assertInstanceOf( + 'Minime\Annotations\Fixtures\AnnotationConstructInjection', + $annotations['AnnotationConstructInjection'] + ); + $this->assertInstanceOf( + 'Minime\Annotations\Fixtures\AnnotationConstructSugarInjection', + $annotations['AnnotationConstructSugarInjection'] + ); + $this->assertInstanceOf( + 'Minime\Annotations\Fixtures\AnnotationSetterInjection', + $annotations['AnnotationSetterInjection'] + ); + + $this->assertSame( + '{"foo":"bar","bar":"baz"}', + json_encode($annotations['AnnotationConstructInjection']) + ); + $this->assertSame( + '{"foo":"foo","bar":"bar"}', + json_encode($annotations['AnnotationConstructSugarInjection']) + ); + $this->assertSame( + '{"foo":"bar"}', + json_encode($annotations['AnnotationSetterInjection']) + ); + } } From 1048336abff5df8175562b5cbdd67e97a0d65e08 Mon Sep 17 00:00:00 2001 From: Donatas Martinkus Date: Sat, 29 Apr 2017 22:14:09 +0100 Subject: [PATCH 3/6] rolling back IDE's config auto-changes for trailing whitespace --- test/fixtures/AnnotationsFixture.php | 26 +++++++++++++------------- test/suite/DynamicParserTest.php | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test/fixtures/AnnotationsFixture.php b/test/fixtures/AnnotationsFixture.php index 06d546f..934db61 100644 --- a/test/fixtures/AnnotationsFixture.php +++ b/test/fixtures/AnnotationsFixture.php @@ -36,9 +36,9 @@ class AnnotationsFixture private $implicit_boolean_fixture; /** - * @value abc - * @value "abc" - * @value "abc " + * @value abc + * @value "abc" + * @value "abc " * @value "123" */ private $string_fixture; @@ -130,17 +130,17 @@ class AnnotationsFixture /** * @value string abc - * @value string 45 + * @value string 45 * @value integer 45 - * @value integer -45 + * @value integer -45 * @value float .45 * @value float 0.45 * @value float 45. * @value float -4.5 - * @value float 4 + * @value float 4 * @json_value json ["x", "y"] * @json_value json {"x": {"y": "z"}} - * @json_value json {"x": {"y": ["z", "p"]}} + * @json_value json {"x": {"y": ["z", "p"]}} */ private $strong_typed_fixture; @@ -154,14 +154,14 @@ class AnnotationsFixture * @multiline_indented_string * ------ * < moo > - * ------ + * ------ * \ ^__^ * \ (oo)\_______ * (__)\ )\/\ * ||----w | * || || * - * + * * @multiline_json json { * "x": { * "y": [ @@ -177,10 +177,10 @@ class AnnotationsFixture * @value integer * @value float * @value json - * @value_with_trailing_space string - * @value_with_trailing_space integer - * @value_with_trailing_space float - * @value_with_trailing_space json + * @value_with_trailing_space string + * @value_with_trailing_space integer + * @value_with_trailing_space float + * @value_with_trailing_space json */ private $reserved_words_as_value_fixture; diff --git a/test/suite/DynamicParserTest.php b/test/suite/DynamicParserTest.php index 25fff07..7052d2d 100644 --- a/test/suite/DynamicParserTest.php +++ b/test/suite/DynamicParserTest.php @@ -4,7 +4,7 @@ /** * DynamicParserTest - * + * * @group parser */ class DynamicParserTest extends BaseTest @@ -153,7 +153,7 @@ public function parseMultilineValueFixture() ."Suspendisse egestas orci a felis imperdiet, non consectetur est suscipit."; $this->assertSame($string, $annotations['multiline_string']); - $cowsay = "------\n< moo >\n------\n \ ^__^\n ". + $cowsay = "------\n< moo >\n------ \n \ ^__^\n ". "\ (oo)\_______\n (__)\ )\/\\\n ". "||----w |\n || ||"; $this->assertSame($cowsay, $annotations['multiline_indented_string']); From 7dc246931a937ec9e8681986d944bfc487a9978e Mon Sep 17 00:00:00 2001 From: Donatas Martinkus Date: Sun, 30 Apr 2017 22:23:09 +0100 Subject: [PATCH 4/6] roling back unnecessary abstraction --- src/Interfaces/TypeInterface.php | 5 +++++ src/Types/AbstractType.php | 13 ------------- src/Types/ConcreteType.php | 3 ++- src/Types/DynamicType.php | 4 +++- src/Types/FloatType.php | 3 ++- src/Types/IntegerType.php | 3 ++- src/Types/JsonType.php | 3 ++- src/Types/StringType.php | 4 +++- test/fixtures/FooType.php | 5 +++++ 9 files changed, 24 insertions(+), 19 deletions(-) delete mode 100644 src/Types/AbstractType.php diff --git a/src/Interfaces/TypeInterface.php b/src/Interfaces/TypeInterface.php index 538bff3..3f8a78f 100644 --- a/src/Interfaces/TypeInterface.php +++ b/src/Interfaces/TypeInterface.php @@ -9,6 +9,11 @@ */ interface TypeInterface { + /** + * @return TypeInterface + */ + public static function getType(); + /** * Parses a type * @param string $value value to be processed diff --git a/src/Types/AbstractType.php b/src/Types/AbstractType.php deleted file mode 100644 index 19f1683..0000000 --- a/src/Types/AbstractType.php +++ /dev/null @@ -1,13 +0,0 @@ - Date: Wed, 3 Jan 2024 15:07:36 +0200 Subject: [PATCH 5/6] Update versions --- composer.json | 13 ++++++---- phpunit.xml.dist | 40 ++++--------------------------- test/suite/AnnotationsBagTest.php | 4 ++-- test/suite/BaseTest.php | 38 ----------------------------- test/suite/CacheTest.php | 13 +++++----- test/suite/DynamicParserTest.php | 31 ++++++++++++++++++++---- test/suite/ParserTest.php | 16 ++++++++----- test/suite/ReaderTest.php | 13 ++-------- 8 files changed, 59 insertions(+), 109 deletions(-) delete mode 100644 test/suite/BaseTest.php diff --git a/composer.json b/composer.json index 659ba64..83af31e 100644 --- a/composer.json +++ b/composer.json @@ -12,19 +12,22 @@ } ], "require": { - "php": ">=5.4.0", + "php": "~8", "ext-json":"*", - "regex-guard/regex-guard": "~1.0", - "nikic/php-parser" : ">=2.0 <5.0.0" + "regex-guard/regex-guard": "~1.1", + "nikic/php-parser" : ">=4.0 <5.0.0" }, "require-dev": { - "mockery/mockery": "^0.9", - "phpunit/phpunit": ">=4.8 <6.0.0" + "mockery/mockery": "~1.6", + "phpunit/phpunit": "~10" }, "autoload": { "psr-4": { "Minime\\Annotations\\": "src/" } }, "autoload-dev": { "psr-4": { "Minime\\Annotations\\Fixtures\\": "test/fixtures/" } + }, + "scripts": { + "test": "phpunit" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a1772a1..621e1fc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,50 +2,18 @@ - + - + test/suite/ - - - src/ - - - - - - - - + diff --git a/test/suite/AnnotationsBagTest.php b/test/suite/AnnotationsBagTest.php index 527e29b..f658058 100644 --- a/test/suite/AnnotationsBagTest.php +++ b/test/suite/AnnotationsBagTest.php @@ -7,12 +7,12 @@ /** * @group bag */ -class AnnotationsBagTest extends \PHPUnit_Framework_TestCase +class AnnotationsBagTest extends \PHPUnit\Framework\TestCase { private $Bag; - public function setUp() + protected function setUp(): void { $this->Bag = new AnnotationsBag( [ diff --git a/test/suite/BaseTest.php b/test/suite/BaseTest.php deleted file mode 100644 index 11d372f..0000000 --- a/test/suite/BaseTest.php +++ /dev/null @@ -1,38 +0,0 @@ -fixture = new AnnotationsFixture; - } - - protected function getDocblock($fixture) - { - $reflection = new ReflectionProperty($this->fixture, $fixture); - - return $reflection->getDocComment(); - } - - protected function getFixture($fixture) - { - return $this->parser->parse($this->getDocblock($fixture)); - } -} diff --git a/test/suite/CacheTest.php b/test/suite/CacheTest.php index 0f294a4..9574ab4 100644 --- a/test/suite/CacheTest.php +++ b/test/suite/CacheTest.php @@ -10,12 +10,12 @@ use Minime\Annotations\Cache\ApcCache; use Minime\Annotations\Fixtures\AnnotationsFixture; -class CacheTest extends \PHPUnit_Framework_TestCase +class CacheTest extends \PHPUnit\Framework\TestCase { protected $fixtureClass = 'Minime\Annotations\Fixtures\AnnotationsFixture'; - public function tearDown() + protected function tearDown(): void { Mockery::close(); } @@ -54,14 +54,13 @@ public function testFileCache(){ } public function testFileCacheWithDefaultStoragePath(){ - new FileCache(); + $this->assertCacheHandlerWorks(new FileCache()); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessageRegExp #^Cache path is not a writable/readable directory: .+\.# - */ public function testFileCacheWithBadStoragePath(){ + $this->expectException('InvalidArgumentException'); + $this->expectExceptionMessage('Cache path is not a writable/readable directory:'); + new FileCache(__DIR__ . '/invalid/path/'); } diff --git a/test/suite/DynamicParserTest.php b/test/suite/DynamicParserTest.php index 7052d2d..996fdc6 100644 --- a/test/suite/DynamicParserTest.php +++ b/test/suite/DynamicParserTest.php @@ -2,16 +2,39 @@ namespace Minime\Annotations; +use \ReflectionProperty; +use Minime\Annotations\Fixtures\AnnotationsFixture; +use Minime\Annotations\Interfaces\ParserInterface; + /** * DynamicParserTest - * + * * @group parser */ -class DynamicParserTest extends BaseTest +class DynamicParserTest extends \PHPUnit\Framework\TestCase { - public function setUp() + protected $fixture; + + /** + * @var ParserInterface + */ + protected $parser; + + protected function getDocblock($fixture) + { + $reflection = new ReflectionProperty($this->fixture, $fixture); + + return $reflection->getDocComment(); + } + + protected function getFixture($fixture) + { + return $this->parser->parse($this->getDocblock($fixture)); + } + + protected function setUp(): void { - parent::setup(); + $this->fixture = new AnnotationsFixture; $this->parser = new DynamicParser; } diff --git a/test/suite/ParserTest.php b/test/suite/ParserTest.php index 66c96c6..2e3a7cc 100644 --- a/test/suite/ParserTest.php +++ b/test/suite/ParserTest.php @@ -9,7 +9,7 @@ */ class ParserTest extends DynamicParserTest { - public function setUp() + protected function setUp(): void { parent::setup(); $this->parser = new Parser; @@ -73,15 +73,16 @@ public function parseConcreteFixture() /** * @test - * @expectedException \Minime\Annotations\ParserException * @dataProvider invalidConcreteAnnotationFixtureProvider */ public function parseInvalidConcreteFixture($fixture) { + $this->expectException(ParserException::class); + $this->getFixture($fixture); } - public function invalidConcreteAnnotationFixtureProvider() + public static function invalidConcreteAnnotationFixtureProvider() { return [ ['bad_concrete_fixture'], @@ -140,28 +141,31 @@ public function tolerateUnrecognizedTypes() /** * @test - * @expectedException \Minime\Annotations\ParserException */ public function exceptionWithBadJsonValue() { + $this->expectException(ParserException::class); + $this->getFixture('bad_json_fixture'); } /** * @test - * @expectedException \Minime\Annotations\ParserException */ public function exceptionWithBadIntegerValue() { + $this->expectException(ParserException::class); + $this->getFixture('bad_integer_fixture'); } /** * @test - * @expectedException \Minime\Annotations\ParserException */ public function exceptionWithBadFloatValue() { + $this->expectException(ParserException::class); + $this->getFixture('bad_float_fixture'); } diff --git a/test/suite/ReaderTest.php b/test/suite/ReaderTest.php index 34bb0a0..4ac9013 100644 --- a/test/suite/ReaderTest.php +++ b/test/suite/ReaderTest.php @@ -3,11 +3,11 @@ use Minime\Annotations\Fixtures\AnnotationsFixture; -class ReaderTest extends \PHPUnit_Framework_TestCase +class ReaderTest extends \PHPUnit\Framework\TestCase { private $fixture; - public function setUp() + protected function setUp(): void { $this->fixture = new AnnotationsFixture; } @@ -32,15 +32,6 @@ public function testGetAnnotations() $this->assertTrue($annotations->get('get')); } - public function testReadFunctionAnnotations() - { - if(! function_exists($fn = __NAMESPACE__ . '\\fn')){ - /** @bar */ function fn(){} - } - - $this->assertTrue($this->getReader()->getFunctionAnnotations($fn)->get('bar')); - } - public function testReadClosureAnnotations() { /** @foo */ From 1f440e3b101c6e5059ad25336bb64b71f8e1e4f3 Mon Sep 17 00:00:00 2001 From: Donatas Martinkus Date: Mon, 15 Jan 2024 13:42:52 +0200 Subject: [PATCH 6/6] WIP: fixing return types --- src/AnnotationsBag.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/AnnotationsBag.php b/src/AnnotationsBag.php index b27043d..7c7ba26 100644 --- a/src/AnnotationsBag.php +++ b/src/AnnotationsBag.php @@ -153,7 +153,7 @@ public function union(AnnotationsBagInterface $bag) /** * Countable */ - public function count() + public function count(): int { return count($this->attributes); } @@ -161,7 +161,7 @@ public function count() /** * JsonSerializable */ - public function jsonSerialize() + public function jsonSerialize(): array { return $this->toArray(); } @@ -169,7 +169,7 @@ public function jsonSerialize() /** * IteratorAggregate */ - public function getIterator() + public function getIterator(): \Traversable { return new ArrayIterator($this->attributes); } @@ -177,7 +177,7 @@ public function getIterator() /** * ArrayAccess - Whether or not an offset exists. */ - public function offsetExists($key) + public function offsetExists($key): bool { return $this->has($key); } @@ -185,7 +185,7 @@ public function offsetExists($key) /** * ArrayAccess - Returns the value at specified offset. */ - public function offsetGet($key) + public function offsetGet($key): mixed { return $this->get($key); } @@ -193,17 +193,15 @@ public function offsetGet($key) /** * ArrayAccess - Assigns a value to the specified offset. */ - public function offsetSet($key, $value) + public function offsetSet($key, $value): void { $this->set($key, $value); - - return true; } /** * ArrayAccess - Unsets an offset. */ - public function offsetUnset($key) + public function offsetUnset($key): void { unset($this->attributes[$key]); }