Skip to content

Commit

Permalink
added validatables command, query and event
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannis committed May 19, 2016
1 parent 1aea554 commit 3a5ed65
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 6 deletions.
21 changes: 21 additions & 0 deletions Command/CommandValidatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus\Command;

use Cubiche\Core\Bus\MessageValidatableInterface;

/**
* CommandValidatable interface.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
interface CommandValidatableInterface extends CommandInterface, MessageValidatableInterface
{
}
21 changes: 21 additions & 0 deletions Event/EventValidatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus\Event;

use Cubiche\Core\Bus\MessageValidatableInterface;

/**
* EventValidatable interface.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
interface EventValidatableInterface extends EventInterface, MessageValidatableInterface
{
}
27 changes: 27 additions & 0 deletions MessageValidatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus;

use Cubiche\Core\Validator\ValidatableInterface;
use Cubiche\Core\Validator\Validator;

/**
* MessageValidatable interface.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
interface MessageValidatableInterface extends MessageInterface, ValidatableInterface
{
/**
* @param Validator $validator
*/
public function addValidationConstraints(Validator $validator);
}
39 changes: 39 additions & 0 deletions Middlewares/Validator/ValidatorMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus\Middlewares\Validator;

use Cubiche\Core\Bus\MessageValidatableInterface;
use Cubiche\Core\Bus\Middlewares\MiddlewareInterface;
use Cubiche\Core\Validator\Validator;

/**
* ValidatorMiddleware class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class ValidatorMiddleware implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function handle($message, callable $next)
{
if ($message instanceof MessageValidatableInterface) {
$validator = Validator::create();

$message->addValidationConstraints($validator);

$validator->assert($message);
}

$next($message);
}
}
22 changes: 22 additions & 0 deletions Query/QueryValidatableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus\Query;

use Cubiche\Core\Bus\MessageValidatableInterface;

/**
* QueryValidatable interface.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
interface QueryValidatableInterface extends QueryInterface, MessageValidatableInterface
{
}
20 changes: 17 additions & 3 deletions Tests/Fixtures/Command/LoginUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Bus\Tests\Fixtures\Command;

use Cubiche\Core\Bus\Command\CommandInterface;
use Cubiche\Core\Bus\Command\CommandValidatableInterface;
use Cubiche\Core\Validator\Assert;
use Cubiche\Core\Validator\Validator;

/**
* LoginUserCommand class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class LoginUserCommand implements CommandInterface
class LoginUserCommand implements CommandValidatableInterface
{
/**
* @var string
Expand Down Expand Up @@ -93,4 +94,17 @@ public function setLogin($login)
{
$this->login = $login;
}

/**
* {@inheritdoc}
*/
public function addValidationConstraints(Validator $validator)
{
$assert = Assert::create()
->attribute('email', Assert::email())
->attribute('password', Assert::stringType()->notBlank())
;

$validator->addConstraint($assert);
}
}
12 changes: 10 additions & 2 deletions Tests/Fixtures/Command/LogoutUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Bus\Tests\Fixtures\Command;

use Cubiche\Core\Bus\Command\CommandNamedInterface;
use Cubiche\Core\Bus\Command\CommandValidatableInterface;
use Cubiche\Core\Validator\Validator;

/**
* LogoutUserCommand class.
*
* @author Ivannis Suárez Jerez <ivannis.suarez@gmail.com>
*/
class LogoutUserCommand implements CommandNamedInterface
class LogoutUserCommand implements CommandNamedInterface, CommandValidatableInterface
{
/**
* @var string
Expand Down Expand Up @@ -65,4 +66,11 @@ public function name()
{
return 'logout_user';
}

/**
* {@inheritdoc}
*/
public function addValidationConstraints(Validator $validator)
{
}
}
66 changes: 66 additions & 0 deletions Tests/Units/Middlewares/Validator/ValidatorMiddlewareTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cubiche\Core\Bus\Tests\Units\Middlewares\Validator;

use Cubiche\Core\Bus\Middlewares\Validator\ValidatorMiddleware;
use Cubiche\Core\Bus\Tests\Fixtures\Command\LoginUserCommand;
use Cubiche\Core\Bus\Tests\Fixtures\Command\LogoutUserCommand;
use Cubiche\Core\Bus\Tests\Units\TestCase;
use Cubiche\Core\Validator\Exception\ValidationException;

/**
* ValidatorMiddleware class.
*
* Generated by TestGenerator on 2016-04-11 at 15:18:25.
*/
class ValidatorMiddlewareTests extends TestCase
{
/**
* Test handle method.
*/
public function testHandle()
{
$this
->given($middleware = new ValidatorMiddleware())
->and($command = new LoginUserCommand('ivan@cubiche.com', 'plainpassword'))
->and($callable = function (LoginUserCommand $command) {
$command->setEmail('info@cubiche.org');
})
->when($middleware->handle($command, $callable))
->then()
->string($command->email())
->isEqualTo('info@cubiche.org')
;

$this
->given($middleware = new ValidatorMiddleware())
->and($command = new LoginUserCommand('invalid.email.com', 'plainpassword'))
->and($callable = function () {
})
->then()
->exception(function () use ($middleware, $command, $callable) {
$middleware->handle($command, $callable);
})->isInstanceOf(ValidationException::class)
;

$this
->given($middleware = new ValidatorMiddleware())
->and($command = new LogoutUserCommand('invalid.email.com'))
->and($callable = function (LogoutUserCommand $command) {
$command->setEmail('info@cubiche.org');
})
->when($middleware->handle($command, $callable))
->then()
->string($command->email())
->isEqualTo('info@cubiche.org')
;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"cubiche/comparable": "dev-master",
"cubiche/delegate": "dev-master",
"cubiche/event-dispatcher": "dev-master",
"cubiche/serializer": "dev-master"
"cubiche/serializer": "dev-master",
"cubiche/validator": "dev-master"
},
"require-dev": {
"cubiche/tests": "dev-master"
Expand Down

0 comments on commit 3a5ed65

Please sign in to comment.