Skip to content

Commit

Permalink
Merge pull request #1 from moufmouf/renaming_annotation
Browse files Browse the repository at this point in the history
 Switching from @Assert to @assertion
  • Loading branch information
moufmouf committed Oct 16, 2019
2 parents f9f5b6c + d502f32 commit 05a41fc
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 22 deletions.
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
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
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
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
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 = 'a@a.com'): User
{
Expand Down
9 changes: 3 additions & 6 deletions tests/Fixtures/InvalidControllers/InvalidController.php
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
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

0 comments on commit 05a41fc

Please sign in to comment.