Skip to content

Commit

Permalink
validation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis84 committed Jul 16, 2014
1 parent 2cc288f commit fc587ec
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 182 deletions.
1 change: 0 additions & 1 deletion src/Mapped/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
class Events
{
const APPLY = 'mapped.apply';
const APPLIED = 'mapped.applied';
const UNAPPLY = 'mapped.unapply';
}
2 changes: 1 addition & 1 deletion src/Mapped/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class Extension
*
* @param Mapping $mapping The mapping object
*/
function initialize(Mapping $mapping)
public function initialize(Mapping $mapping)
{
}
}
2 changes: 2 additions & 0 deletions src/Mapped/Extension/MultipleResizeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function unapply(Event $event)
*
* @param Mapping $mapping The mapping object
* @param mixed $data The data
*
* @throw InvalidArgumentException If given data is not an array
*/
protected function prepare(Mapping $mapping, $data)
{
Expand Down
63 changes: 0 additions & 63 deletions src/Mapped/Extension/SymfonyValidation.php

This file was deleted.

14 changes: 4 additions & 10 deletions src/Mapped/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,9 @@ public function doApply($data)
$result = $transformer->transform($result);
}

if ($this->dispatcher->hasListeners(Events::APPLIED)) {
$event = new Event($this, $result, $data);
$this->dispatcher->dispatch(Events::APPLIED, $event);
$result = $event->getResult();
}

foreach ($this->constraints as $cons) {
if ($error = $cons->validate($this, $data)) {
$errors[] = $error;
if ($error = $cons->validate($this, $result)) {
array_unshift($errors, $error);
}
}

Expand Down Expand Up @@ -369,9 +363,9 @@ public function unapply($data)
}

/**
* Makes this mapping to an optional.
* Makes this mapping optional.
*
* @return Mapping
* @return Mapping
*/
public function optional()
{
Expand Down
61 changes: 0 additions & 61 deletions tests/Mapped/Tests/Extension/SymfonyValidationTest.php

This file was deleted.

21 changes: 21 additions & 0 deletions tests/Mapped/Tests/Extension/VerifyingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Mapped\Tests\Integration;

use Mapped\MappingFactory;
use Mapped\Tests\Fixtures\User;

class CustomConstraintTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -42,4 +43,24 @@ public function testB()
'password2' => 'demo',
]);
}

public function testC()
{
$factory = new MappingFactory();
$mapping = $factory->mapping([
'username' => $factory->mapping(),
'password' => $factory->mapping(),
], function ($username, $password) {
return new User($username, $password);
})->verifying('foo', function ($value) {
$this->assertInstanceOf('Mapped\Tests\Fixtures\User', $value);
return true;
});

$mapping->apply([
'username' => 'dennis',
'password' => 'password',
]);
}

}
12 changes: 0 additions & 12 deletions tests/Mapped/Tests/Fixtures/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@

namespace Mapped\Tests\Fixtures;

use Symfony\Component\Validator\Constraints as Assert;

class Address
{
/**
* @Assert\Length(min=5)
* @Assert\NotBlank
*/
public $city;

/**
* @Assert\Length(min=5)
* @Assert\NotBlank
*/
public $street;

public $location;

public function __construct($city, $street, $location = null)
Expand Down
30 changes: 0 additions & 30 deletions tests/Mapped/Tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,10 @@

namespace Mapped\Tests\Fixtures;

use Symfony\Component\Validator\Constraints as Assert;

class User
{
/**
* @Assert\Length(min=5)
* @Assert\NotBlank
*/
public $username;

/**
* @Assert\Length(min=5)
* @Assert\NotBlank
*/
public $password;

/**
* @Assert\NotBlank
*/
public $firstName;

/**
* @Assert\NotBlank
*/
public $last_name;

public $address;

public function __construct($username, $password, $address = null)
Expand All @@ -36,12 +14,4 @@ public function __construct($username, $password, $address = null)
$this->password = $password;
$this->address = $address;
}

/**
* @Assert\True(message = "foo")
*/
public function isPasswordValid()
{
return false;
}
}
46 changes: 46 additions & 0 deletions tests/Mapped/Tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mapped\MappingFactory;
use Mapped\ValidationException;
use Mapped\Tests\Fixtures\User;
use Mapped\Tests\Fixtures\Address;

class ValidationTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -35,4 +36,49 @@ public function testA()
throw $e;
}
}

public function testB()
{
$factory = new MappingFactory();
$mapping = $factory->mapping([
'username' => $factory->mapping()->nonEmptyText(),
'password' => $factory->mapping()->verifying('error.password', function ($value) {
return false;
}),
'address' => $factory->mapping([
'city' => $factory->mapping()->verifying('error.city', function ($value) {
return false;
}),
'street' => $factory->mapping(),
], function ($city, $street = null) {
return new Address($city, $street);
}),
'accept' => $factory->mapping(),
], function ($username, $password, Address $address) {
return new User($username, $password, $address);
})->verifying('foo', function ($value) {
return false;
});

$this->setExpectedException('Mapped\ValidationException');

try {
$result = $mapping->apply([
'username' => '',
'password' => 'pass',
'address' => [
'city' => 'Foobar',
],
]);
} catch (ValidationException $e) {
$errors = $e->getErrors();
$this->assertSame('foo', $errors[0]->getMessage());
$this->assertSame('error.non_empty_text', $errors[1]->getMessage());
$this->assertSame('error.password', $errors[2]->getMessage());
$this->assertSame('error.city', $errors[3]->getMessage());
$this->assertSame('error.required', $errors[4]->getMessage());
$this->assertSame('error.required', $errors[5]->getMessage());
throw $e;
}
}
}
4 changes: 0 additions & 4 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<?php

use Doctrine\Common\Annotations\AnnotationRegistry;

$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add('Mapped\Tests', __DIR__ . '/../tests');

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

0 comments on commit fc587ec

Please sign in to comment.