Skip to content

Commit

Permalink
Merge pull request #26 from Kdyby/annotations
Browse files Browse the repository at this point in the history
Make Kdyby/Annotations optional
  • Loading branch information
fprochazka committed Aug 27, 2016
2 parents 009d9b8 + e9d2a22 commit 2511d1c
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 37 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ Kdyby/Validator requires PHP 5.4.0 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
```


Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions src/Kdyby/Validator/DI/ValidatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -98,6 +94,12 @@ public function loadConfiguration()
->addTag(self::TAG_CONSTRAINT_VALIDATOR, [
'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);
}
}


Expand Down
64 changes: 41 additions & 23 deletions tests/KdybyTests/Validator/Extension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand All @@ -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');
Expand All @@ -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();
Expand All @@ -82,17 +110,17 @@ 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()));
}



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],
];
}

Expand All @@ -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');

Expand All @@ -118,15 +146,5 @@ class ExtensionTest extends Tester\TestCase
}


class ArticleMock extends Nette\Object
{

/**
* @Assert\NotNull()
* @var string
*/
public $title;

}

\run(new ExtensionTest());
2 changes: 2 additions & 0 deletions tests/KdybyTests/Validator/config/annotations.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extensions:
annotations: Kdyby\Annotations\DI\AnnotationsExtension
7 changes: 7 additions & 0 deletions tests/KdybyTests/Validator/config/custom-loader.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
loader:
class: Symfony\Component\Validator\Mapping\Loader\YamlFileLoader
arguments:
- %appDir%/Validator/config/mapping.yml
tags:
- kdyby.validator.loader
4 changes: 4 additions & 0 deletions tests/KdybyTests/Validator/config/mapping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
KdybyTests\ValidatorMocks\ArticleMock:
properties:
title:
- Email: ~
17 changes: 17 additions & 0 deletions tests/KdybyTests/ValidatorMocks/ArticleMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace KdybyTests\ValidatorMocks;

use Nette\Object;
use Symfony\Component\Validator\Constraints as Assert;

class ArticleMock extends Object
{

/**
* @Assert\NotNull()
* @var string
*/
public $title;

}
2 changes: 1 addition & 1 deletion tests/KdybyTests/ValidatorMocks/FooConstraint.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace KdybyTests\ValidatorMock;
namespace KdybyTests\ValidatorMocks;

use Symfony\Component\Validator\Constraint;

Expand Down
2 changes: 1 addition & 1 deletion tests/KdybyTests/ValidatorMocks/FooConstraintValidator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace KdybyTests\ValidatorMock;
namespace KdybyTests\ValidatorMocks;

use Nette\DI\Container;
use Symfony\Component\Validator\Constraint;
Expand Down
5 changes: 2 additions & 3 deletions tests/KdybyTests/nette-reset.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ php:


extensions:
annotations: Kdyby\Annotations\DI\AnnotationsExtension
translation: Kdyby\Translation\DI\TranslationExtension
validator: Kdyby\Validator\DI\ValidatorExtension

Expand All @@ -13,9 +12,9 @@ services:
class: Nette\Caching\Storages\MemoryStorage

-
class: KdybyTests\ValidatorMock\FooConstraintValidator
class: KdybyTests\ValidatorMocks\FooConstraintValidator
tags:
kdyby.validator.constraintValidator: KdybyTests\ValidatorMock\FooConstraintValidator
kdyby.validator.constraintValidator: KdybyTests\ValidatorMocks\FooConstraintValidator

http:
frames: null
Expand Down

0 comments on commit 2511d1c

Please sign in to comment.