-
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.
added validatables command, query and event
- Loading branch information
Showing
9 changed files
with
225 additions
and
6 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
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 | ||
{ | ||
} |
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,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 | ||
{ | ||
} |
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,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); | ||
} |
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,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); | ||
} | ||
} |
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,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 | ||
{ | ||
} |
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
66 changes: 66 additions & 0 deletions
66
Tests/Units/Middlewares/Validator/ValidatorMiddlewareTests.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,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') | ||
; | ||
} | ||
} |
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