Permalink
Browse files

added unique validator

  • Loading branch information...
pablodip committed Mar 5, 2012
1 parent e8708f8 commit 2d9baf91e894bffc16e77e16322f6a8ae8b71948
View
@@ -1,5 +1,7 @@
/phpunit.xml
/vendor/
+/Tests/Model/Base/
+/Tests/Model/Mapping/
/TestsProject/app/cache/
/TestsProject/app/log/
/TestsProject/src/Model/Base/
@@ -28,6 +28,9 @@
<!-- form -->
<parameter key="form.type.mandango_document.class">Mandango\MandangoBundle\Form\Type\MandangoDocumentType</parameter>
<parameter key="form.type_guesser.mandango.class">Mandango\MandangoBundle\Form\MandangoTypeGuesser</parameter>
+
+ <!-- validator -->
+ <parameter key="mandango.validator.unique_document.class">Mandango\MandangoBundle\Validator\Constraint\UniqueDocumentValidator</parameter>
</parameters>
<services>
@@ -70,5 +73,11 @@
<tag name="form.type_guesser" />
<argument type="service" id="mandango.metadata_factory" />
</service>
+
+ <!-- validator -->
+ <service id="mandango.validator.unique_document" class="%mandango.validator.unique_document.class%">
+ <tag name="validator.constraint_validator" alias="mandango.validator.unique_document" />
+ <argument type="service" id="mandango" />
+ </service>
</services>
</container>
View
@@ -0,0 +1,10 @@
+<?php
+
+namespace Model;
+
+/**
+ * Model\Article document.
+ */
+class Article extends \Model\Base\Article
+{
+}
@@ -0,0 +1,10 @@
+<?php
+
+namespace Model;
+
+/**
+ * Query of Model\Article document.
+ */
+class ArticleQuery extends \Model\Base\ArticleQuery
+{
+}
@@ -0,0 +1,10 @@
+<?php
+
+namespace Model;
+
+/**
+ * Repository of Model\Article document.
+ */
+class ArticleRepository extends \Model\Base\ArticleRepository
+{
+}
View
@@ -0,0 +1,28 @@
+<?php
+
+namespace Mandango\MandangoBundle\Tests;
+
+use Mandango\Mandango;
+use Mandango\Cache\ArrayCache;
+use Mandango\Connection;
+use Model\Mapping\Metadata;
+
+class TestCase extends \PHPUnit_Framework_TestCase
+{
+ protected $mandango;
+
+ protected function setUp()
+ {
+ if (!class_exists('Mongo')) {
+ $this->markTestSkipped('Mongo is not available.');
+ }
+
+ $this->mandango = new Mandango(new Metadata(), new ArrayCache());
+ $this->mandango->setConnection('global', new Connection('mongodb://localhost:27017', 'mandango_bundle'));
+ $this->mandango->setDefaultConnectionName('global');
+
+ foreach ($this->mandango->getAllRepositories() as $repository) {
+ $repository->getCollection()->drop();
+ }
+ }
+}
@@ -0,0 +1,128 @@
+<?php
+
+namespace Mandango\MandangoBundle\Tests\Validator\Constraint;
+
+use Mandango\MandangoBundle\Tests\TestCase;
+use Mandango\MandangoBundle\Validator\Constraint\UniqueDocument;
+use Mandango\MandangoBundle\Validator\Constraint\UniqueDocumentValidator;
+
+class UniqueDocumentValidatorTest extends TestCase
+{
+ private $validator;
+
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->validator = new UniqueDocumentValidator($this->mandango);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ * @dataProvider IsValidNotMandangoDocumentProvider
+ */
+ public function testIsValidNotMandangoDocument($document)
+ {
+ $constraint = new UniqueDocument(array('fields' => array('title')));
+ $this->validator->isValid($document, $constraint);
+ }
+
+ public function IsValidNotMandangoDocumentProvider()
+ {
+ return array(
+ array('foo'),
+ array(1),
+ array(1.1),
+ array(true)
+ );
+ }
+
+ /**
+ * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
+ * @dataProvider isValidFieldsNotValidProvider
+ */
+ public function testIsValidFieldsNotValid($fields)
+ {
+ $this->validator->isValid($this->createArticle(), $this->createConstraint($fields));
+ }
+
+ public function isValidFieldsNotValidProvider()
+ {
+ return array(
+ array(1),
+ array(1.1),
+ array(true),
+ array(new \ArrayObject())
+ );
+ }
+
+ /**
+ * @expectedException Symfony\Component\Validator\Exception\ConstraintDefinitionException
+ */
+ public function testIsValidAtLeastOneField()
+ {
+ $this->validator->isValid($this->createArticle(), $this->createConstraint(array()));
+ }
+
+ /**
+ * @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
+ * @dataProvider isValidCaseInsensitiveNotValidProvider
+ */
+ public function testIsValidCaseInsensitiveNotValid($caseInsensitive)
+ {
+ $constraint = $this->createConstraint('title');
+ $constraint->caseInsensitive = $caseInsensitive;
+ $this->validator->isValid($this->createArticle(), $constraint);
+ }
+
+ public function isValidCaseInsensitiveNotValidProvider()
+ {
+ return array(
+ array('foo'),
+ array(1),
+ array(1.1),
+ array(true),
+ array(new \ArrayObject())
+ );
+ }
+
+ public function testIsValidWithoutResults()
+ {
+ $article = $this->createArticle()->setTitle('foo');
+ $this->assertTrue($this->validator->isValid($article, $this->createConstraint('title')));
+ }
+
+ public function testIsValidSameResult()
+ {
+ $article = $this->createArticle()->setTitle('foo')->save();
+ $this->assertTrue($this->validator->isValid($article, $this->createConstraint('title')));
+ }
+
+ public function testIsValidOneField()
+ {
+ $article1 = $this->createArticle()->setTitle('foo')->save();
+ $article2 = $this->createArticle()->setTitle('foo');
+ $this->assertFalse($this->validator->isValid($article2, $this->createConstraint('title')));
+ }
+
+ public function testIsValidCaseInsensitive()
+ {
+ $article1 = $this->createArticle()->setTitle('foo')->save();
+ $article2 = $this->createArticle()->setTitle('foO');
+
+ $constraint = $this->createConstraint('title');
+ $constraint->caseInsensitive = array('title');
+
+ $this->assertFalse($this->validator->isValid($article2, $constraint));
+ }
+
+ private function createConstraint($fields)
+ {
+ return new UniqueDocument(array('fields' => $fields));
+ }
+
+ private function createArticle()
+ {
+ return $this->mandango->create('Model\Article');
+ }
+}
View
@@ -15,7 +15,36 @@
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
- 'Symfony' => $vendorDir.'/symfony/src',
- 'Mandango' => $vendorDir.'/mandango/src'
+ 'Symfony' => $vendorDir.'/symfony/src',
+ 'Mandango\Mondator' => $vendorDir.'/mondator/src',
+ 'Mandango' => $vendorDir.'/mandango/src',
+ 'Model' => __DIR__
));
-$loader->register();
+$loader->registerPrefixes(array(
+ 'Twig_' => $vendorDir.'/twig/lib',
+));
+$loader->register();
+
+/*
+ * Generate Mandango model.
+ */
+$configClasses = array(
+ 'Model\Article' => array(
+ 'fields' => array(
+ 'title' => array('type' => 'string'),
+ ),
+ ),
+);
+
+use Mandango\Mondator\Mondator;
+
+$mondator = new Mondator();
+$mondator->setConfigClasses($configClasses);
+$mondator->setExtensions(array(
+ new Mandango\Extension\Core(array(
+ 'metadata_factory_class' => 'Model\Mapping\Metadata',
+ 'metadata_factory_output' => __DIR__.'/Model/Mapping',
+ 'default_output' => __DIR__.'/Model'
+ )),
+));
+$mondator->process();
@@ -0,0 +1,61 @@
+<?php
+
+/*
+ * This file is part of Mandango.
+ *
+ * (c) Pablo Díez <pablodip@gmail.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Mandango\MandangoBundle\Validator\Constraint;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * UniqueConstraint.
+ *
+ * @author Pablo Díez <pablodip@gmail.com>
+ */
+class UniqueDocument extends Constraint
+{
+ public $message = 'This value is already used.';
+ public $service = 'mandango.validator.unique_document';
+ public $fields = array();
+ public $caseInsensitive = array();
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDefaultOption()
+ {
+ return 'fields';
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getRequiredOptions()
+ {
+ return array('fields');
+ }
+
+ /**
+ * The validator must be defined as a service with this name.
+ *
+ * @return string
+ */
+ public function validatedBy()
+ {
+ return $this->service;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getTargets()
+ {
+ return self::CLASS_CONSTRAINT;
+ }
+}
Oops, something went wrong.

0 comments on commit 2d9baf9

Please sign in to comment.