Skip to content

Switching from @Assert to @Assertion #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @Attribute("constraint", type = "Symfony\Component\Validator\Constraint[]|Symfony\Component\Validator\Constraint")
* })
*/
class Assert implements ParameterAnnotationInterface
class Assertion implements ParameterAnnotationInterface
{
/** @var string */
private $for;
Expand Down
13 changes: 7 additions & 6 deletions src/Mappers/Parameters/AssertParameterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameterInterface;
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assertion;
use function array_map;
use function array_merge;

Expand All @@ -40,23 +41,23 @@ public function __construct(ConstraintValidatorFactoryInterface $constraintValid

public function mapParameter(ReflectionParameter $refParameter, DocBlock $docBlock, ?Type $paramTagType, ParameterAnnotations $parameterAnnotations, ParameterHandlerInterface $next): ParameterInterface
{
/** @var Assert[] $assertAnnotations */
$assertAnnotations = $parameterAnnotations->getAnnotationsByType(Assert::class);
/** @var Assertion[] $assertionAnnotations */
$assertionAnnotations = $parameterAnnotations->getAnnotationsByType(Assertion::class);

$parameter = $next->mapParameter($refParameter, $docBlock, $paramTagType, $parameterAnnotations);

if (empty($assertAnnotations)) {
if (empty($assertionAnnotations)) {
return $parameter;
}

if (! $parameter instanceof InputTypeParameterInterface) {
throw InvalidAssertAnnotationException::canOnlyValidateInputType($refParameter);
throw InvalidAssertionAnnotationException::canOnlyValidateInputType($refParameter);
}

// Let's wrap the ParameterInterface into a ParameterValidator.
$recursiveConstraints = array_map(static function (Assert $assertAnnotation) {
$recursiveConstraints = array_map(static function (Assertion $assertAnnotation) {
return $assertAnnotation->getConstraint();
}, $assertAnnotations);
}, $assertionAnnotations);
$constraints = array_merge(...$recursiveConstraints);

return new ParameterValidator($parameter, $refParameter->getName(), $constraints, $this->constraintValidatorFactory, $this->validator, $this->translator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Exception;
use ReflectionParameter;

class InvalidAssertAnnotationException extends Exception
class InvalidAssertionAnnotationException extends Exception
{
public static function canOnlyValidateInputType(ReflectionParameter $refParameter): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
use BadMethodCallException;
use PHPUnit\Framework\TestCase;

class AssertTest extends TestCase
class AssertionTest extends TestCase
{

public function testException1()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('The @Assert annotation must be passed a target. For instance: "@Assert(for="$email", constraint=@Email)"');
new Assert([]);
new Assertion([]);
}

public function testException2()
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('The @Assert annotation must be passed one or many constraints. For instance: "@Assert(for="$email", constraint=@Email)"');
new Assert(['for'=>'foo']);
new Assertion(['for'=>'foo']);
}
}
6 changes: 3 additions & 3 deletions tests/Fixtures/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
namespace TheCodingMachine\Graphqlite\Validator\Fixtures\Controllers;


use Symfony\Component\Validator\Constraints as Assertion;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\Graphqlite\Validator\Fixtures\Types\User;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assertion;
use TheCodingMachine\Graphqlite\Validator\ValidationFailedException;

class UserController
Expand Down Expand Up @@ -40,7 +40,7 @@ public function createUser(string $email, string $password): User

/**
* @Query
* @Assert(for="email", constraint=@Assertion\Email())
* @Assertion(for="email", constraint=@Assert\Email())
*/
public function findByMail(string $email = '[email protected]'): User
{
Expand Down
9 changes: 3 additions & 6 deletions tests/Fixtures/InvalidControllers/InvalidController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@


use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\Validator\Constraints as Assertion;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use TheCodingMachine\GraphQLite\Annotations\Mutation;
use Symfony\Component\Validator\Constraints as Assert;
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
use TheCodingMachine\Graphqlite\Validator\ValidationFailedException;
use TheCodingMachine\Graphqlite\Validator\Annotations\Assertion;

class InvalidController
{
/**
* @Query
* @Assert(for="$resolveInfo", constraint=@Assertion\Email())
* @Assertion(for="$resolveInfo", constraint=@Assert\Email())
*/
public function invalid(ResolveInfo $resolveInfo): string
{
Expand Down
4 changes: 2 additions & 2 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use TheCodingMachine\GraphQLite\SchemaFactory;
use TheCodingMachine\Graphqlite\Validator\Fixtures\Controllers\UserController;
use TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\AssertParameterMiddleware;
use TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\InvalidAssertAnnotationException;
use TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\InvalidAssertionAnnotationException;
use function var_dump;
use function var_export;
use const JSON_PRETTY_PRINT;
Expand Down Expand Up @@ -162,7 +162,7 @@ public function testException(): void
$schemaFactory->addControllerNamespace('TheCodingMachine\Graphqlite\Validator\Fixtures\InvalidControllers');
$schema = $schemaFactory->createSchema();

$this->expectException(InvalidAssertAnnotationException::class);
$this->expectException(InvalidAssertionAnnotationException::class);
$this->expectExceptionMessage('In method TheCodingMachine\Graphqlite\Validator\Fixtures\InvalidControllers\InvalidController::invalid(), the @Assert annotation is targeting parameter "$resolveInfo". You cannot target this parameter because it is not part of the GraphQL Input type. You can only assert parameters coming from the end user.');
$schema->validate();
}
Expand Down