forked from mandango/MandangoBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
427 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Model; | ||
|
||
/** | ||
* Model\Article document. | ||
*/ | ||
class Article extends \Model\Base\Article | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Model; | ||
|
||
/** | ||
* Query of Model\Article document. | ||
*/ | ||
class ArticleQuery extends \Model\Base\ArticleQuery | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Model; | ||
|
||
/** | ||
* Repository of Model\Article document. | ||
*/ | ||
class ArticleRepository extends \Model\Base\ArticleRepository | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
Tests/Validator/Constraint/UniqueDocumentValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Mandango. | ||
* | ||
* (c) Pablo Díez <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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.