From 5c6494ca67674b3c35b3d00841b828e155b64f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Mon, 25 Apr 2016 09:48:41 +0200 Subject: [PATCH 1/2] Make Kdyby/Annotations optional --- composer.json | 3 +- src/Kdyby/Validator/DI/ValidatorExtension.php | 10 +-- tests/KdybyTests/Validator/Extension.phpt | 64 ++++++++++++------- .../Validator/config/annotations.neon | 2 + .../Validator/config/custom-loader.neon | 7 ++ tests/KdybyTests/Validator/config/mapping.yml | 4 ++ .../KdybyTests/ValidatorMocks/ArticleMock.php | 17 +++++ .../ValidatorMocks/FooConstraint.php | 2 +- .../ValidatorMocks/FooConstraintValidator.php | 2 +- tests/KdybyTests/nette-reset.neon | 5 +- 10 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 tests/KdybyTests/Validator/config/annotations.neon create mode 100644 tests/KdybyTests/Validator/config/custom-loader.neon create mode 100644 tests/KdybyTests/Validator/config/mapping.yml create mode 100644 tests/KdybyTests/ValidatorMocks/ArticleMock.php diff --git a/composer.json b/composer.json index e9acbba..18e5e39 100644 --- a/composer.json +++ b/composer.json @@ -28,11 +28,12 @@ "nette/utils": "~2.3@dev", "symfony/validator": ">=2.5.3", - "kdyby/annotations": "~2.2@dev", "kdyby/doctrine-cache": "~2.4@dev", "kdyby/translation": "~2.2@dev" }, "require-dev": { + "kdyby/annotations": "~2.2@dev", + "symfony/yaml": "^2.8|^3.0", "nette/application": "~2.3@dev", "nette/bootstrap": "~2.3@dev", "nette/caching": "~2.3@dev", diff --git a/src/Kdyby/Validator/DI/ValidatorExtension.php b/src/Kdyby/Validator/DI/ValidatorExtension.php index 7e65769..0298eb4 100644 --- a/src/Kdyby/Validator/DI/ValidatorExtension.php +++ b/src/Kdyby/Validator/DI/ValidatorExtension.php @@ -51,10 +51,6 @@ public function loadConfiguration() ->setClass('Symfony\Component\Validator\Mapping\Loader\LoaderInterface') ->setFactory('Symfony\Component\Validator\Mapping\Loader\LoaderChain'); - $builder->addDefinition($this->prefix('annotationsLoader')) - ->setFactory('Symfony\Component\Validator\Mapping\Loader\AnnotationLoader') - ->addTag(self::TAG_LOADER); - $cacheService = $builder->addDefinition($this->prefix('cache')) ->setClass('Symfony\Component\Validator\Mapping\Cache\CacheInterface'); @@ -101,6 +97,12 @@ public function loadConfiguration() 'Symfony\Component\Validator\Constraints\ExpressionValidator', 'validator.expression', // @link https://github.com/symfony/symfony/pull/16166 ]); + + if ($this->compiler->getExtensions('Kdyby\Annotations\DI\AnnotationsExtension')) { + $builder->addDefinition($this->prefix('annotationsLoader')) + ->setFactory('Symfony\Component\Validator\Mapping\Loader\AnnotationLoader') + ->addTag(self::TAG_LOADER); + } } diff --git a/tests/KdybyTests/Validator/Extension.phpt b/tests/KdybyTests/Validator/Extension.phpt index aa3f573..0ccff8e 100644 --- a/tests/KdybyTests/Validator/Extension.phpt +++ b/tests/KdybyTests/Validator/Extension.phpt @@ -11,9 +11,9 @@ namespace KdybyTests\Validator; use Kdyby; +use KdybyTests\ValidatorMocks\ArticleMock; use Nette; use Symfony; -use Symfony\Component\Validator\Constraints as Assert; use Tester; require_once __DIR__ . '/../bootstrap.php'; @@ -30,14 +30,18 @@ class ExtensionTest extends Tester\TestCase /** * @return Nette\DI\Container */ - public function createContainer($configFile = NULL) + public function createContainer(array $files = []) { + $rootDir = __DIR__ . '/..'; + $config = new Nette\Configurator(); - $config->setTempDirectory(TEMP_DIR); - $config->addParameters(['container' => ['class' => 'SystemContainer_' . md5($configFile)]]); + $config->setTempDirectory(TEMP_DIR) + ->addParameters([ + 'appDir' => $rootDir, + ]); $config->addConfig(__DIR__ . '/../nette-reset.neon'); - if ($configFile) { - $config->addConfig(__DIR__ . '/config/' . $configFile . '.neon'); + foreach ($files as $file) { + $config->addConfig($file); } return $config->createContainer(); @@ -47,7 +51,7 @@ class ExtensionTest extends Tester\TestCase public function testFunctionality() { - $container = $this->createContainer(); + $container = $this->createContainer([__DIR__ . '/config/annotations.neon']); /** @var Symfony\Component\Validator\Validator\ValidatorInterface $validator */ $validator = $container->getByType('Symfony\Component\Validator\Validator\ValidatorInterface'); @@ -69,6 +73,30 @@ class ExtensionTest extends Tester\TestCase + public function testWithoutAnnotations() + { + $container = $this->createContainer([__DIR__ . '/config/custom-loader.neon']); + + /** @var Symfony\Component\Validator\Validator\ValidatorInterface $validator */ + $validator = $container->getByType('Symfony\Component\Validator\Validator\ValidatorInterface'); + Tester\Assert::true($validator instanceof Symfony\Component\Validator\Validator\RecursiveValidator); + + $article = new ArticleMock(); + + /** @var Symfony\Component\Validator\ConstraintViolationInterface[] $violations */ + $violations = $validator->validate($article); + Tester\Assert::same(0, count($violations)); + + $article->title = "Nette Framework + Symfony/Validator"; + + /** @var Symfony\Component\Validator\ConstraintViolationInterface[] $violations */ + $violations = $validator->validate($article); + Tester\Assert::same(1, count($violations)); + Tester\Assert::same('This value is not a valid email address.', $violations[0]->getMessage()); + } + + + public function testConstraintValidatorFactory() { $container = $this->createContainer(); @@ -82,7 +110,7 @@ class ExtensionTest extends Tester\TestCase Tester\Assert::type('Symfony\Component\Validator\Constraints\ExpressionValidator', $factory->getInstance(new \Symfony\Component\Validator\Constraints\Expression(['expression' => '']))); // Custom validator with dependency (haa to be created by DIC). - Tester\Assert::type('KdybyTests\ValidatorMock\FooConstraintValidator', $factory->getInstance(new \KdybyTests\ValidatorMock\FooConstraint())); + Tester\Assert::type('KdybyTests\ValidatorMocks\FooConstraintValidator', $factory->getInstance(new \KdybyTests\ValidatorMocks\FooConstraint())); } @@ -90,9 +118,9 @@ class ExtensionTest extends Tester\TestCase public function strictEmailDataProvider() { return [ - [NULL, FALSE], - ['strict-email', TRUE], - ['non-strict-email', FALSE], + [[], FALSE], + [[__DIR__ . '/config/strict-email.neon'], TRUE], + [[__DIR__ . '/config/non-strict-email.neon'], FALSE], ]; } @@ -101,9 +129,9 @@ class ExtensionTest extends Tester\TestCase /** * @dataProvider strictEmailDataProvider */ - public function testStrictEmail($configFile, $strict) + public function testStrictEmail($configFiles, $strict) { - $container = $this->createContainer($configFile); + $container = $this->createContainer($configFiles); $factory = $container->getByType('Symfony\Component\Validator\ConstraintValidatorFactoryInterface'); @@ -118,15 +146,5 @@ class ExtensionTest extends Tester\TestCase } -class ArticleMock extends Nette\Object -{ - - /** - * @Assert\NotNull() - * @var string - */ - public $title; - -} \run(new ExtensionTest()); diff --git a/tests/KdybyTests/Validator/config/annotations.neon b/tests/KdybyTests/Validator/config/annotations.neon new file mode 100644 index 0000000..97213c7 --- /dev/null +++ b/tests/KdybyTests/Validator/config/annotations.neon @@ -0,0 +1,2 @@ +extensions: + annotations: Kdyby\Annotations\DI\AnnotationsExtension diff --git a/tests/KdybyTests/Validator/config/custom-loader.neon b/tests/KdybyTests/Validator/config/custom-loader.neon new file mode 100644 index 0000000..dbceb90 --- /dev/null +++ b/tests/KdybyTests/Validator/config/custom-loader.neon @@ -0,0 +1,7 @@ +services: + loader: + class: Symfony\Component\Validator\Mapping\Loader\YamlFileLoader + arguments: + - %appDir%/Validator/config/mapping.yml + tags: + - kdyby.validator.loader diff --git a/tests/KdybyTests/Validator/config/mapping.yml b/tests/KdybyTests/Validator/config/mapping.yml new file mode 100644 index 0000000..75d5464 --- /dev/null +++ b/tests/KdybyTests/Validator/config/mapping.yml @@ -0,0 +1,4 @@ +KdybyTests\ValidatorMocks\ArticleMock: + properties: + title: + - Email: ~ diff --git a/tests/KdybyTests/ValidatorMocks/ArticleMock.php b/tests/KdybyTests/ValidatorMocks/ArticleMock.php new file mode 100644 index 0000000..9705dfe --- /dev/null +++ b/tests/KdybyTests/ValidatorMocks/ArticleMock.php @@ -0,0 +1,17 @@ + Date: Mon, 2 May 2016 10:34:26 +0200 Subject: [PATCH 2/2] Update documentation --- README.md | 5 +++-- docs/en/index.md | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6184151..bde3614 100755 --- a/README.md +++ b/README.md @@ -20,10 +20,11 @@ Kdyby/Validator requires PHP 5.3.2 or higher. Installation ------------ -The best way to install Kdyby/Validator is using [Composer](http://getcomposer.org/): +The best way to install Kdyby/Validator is using [Composer](http://getcomposer.org/). It is recommended to install [Kdyby/Annotations](https://github.com/Kdyby/Annotations) as well. ```sh -$ composer require kdyby/validator:@dev +$ composer require kdyby/annotations +$ composer require kdyby/validator ``` diff --git a/docs/en/index.md b/docs/en/index.md index 2471122..be5d3a7 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -7,10 +7,11 @@ This extension is here to integrate [Symfony/Validator](https://github.com/Symfo Installation ----------- -The best way to install Kdyby/Validator is using [Composer](http://getcomposer.org/): +The best way to install Kdyby/Validator is using [Composer](http://getcomposer.org/). It is recommended to install [Kdyby/Annotations](https://github.com/Kdyby/Annotations) as well. ```sh -$ composer require Kdyby/Validator:@dev +$ composer require kdyby/annotations +$ composer require kdyby/validator ``` Now you need to register Kdyby/Validator, [Kdyby/Annotations](https://github.com/Kdyby/Annotations)