Skip to content

Commit 05a41fc

Browse files
authored
Merge pull request #1 from moufmouf/renaming_annotation
Switching from @Assert to @assertion
2 parents f9f5b6c + d502f32 commit 05a41fc

File tree

7 files changed

+20
-22
lines changed

7 files changed

+20
-22
lines changed

src/Annotations/Assert.php renamed to src/Annotations/Assertion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @Attribute("constraint", type = "Symfony\Component\Validator\Constraint[]|Symfony\Component\Validator\Constraint")
2121
* })
2222
*/
23-
class Assert implements ParameterAnnotationInterface
23+
class Assertion implements ParameterAnnotationInterface
2424
{
2525
/** @var string */
2626
private $for;

src/Mappers/Parameters/AssertParameterMiddleware.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use TheCodingMachine\GraphQLite\Parameters\InputTypeParameterInterface;
1717
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface;
1818
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
19+
use TheCodingMachine\Graphqlite\Validator\Annotations\Assertion;
1920
use function array_map;
2021
use function array_merge;
2122

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

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

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

48-
if (empty($assertAnnotations)) {
49+
if (empty($assertionAnnotations)) {
4950
return $parameter;
5051
}
5152

5253
if (! $parameter instanceof InputTypeParameterInterface) {
53-
throw InvalidAssertAnnotationException::canOnlyValidateInputType($refParameter);
54+
throw InvalidAssertionAnnotationException::canOnlyValidateInputType($refParameter);
5455
}
5556

5657
// Let's wrap the ParameterInterface into a ParameterValidator.
57-
$recursiveConstraints = array_map(static function (Assert $assertAnnotation) {
58+
$recursiveConstraints = array_map(static function (Assertion $assertAnnotation) {
5859
return $assertAnnotation->getConstraint();
59-
}, $assertAnnotations);
60+
}, $assertionAnnotations);
6061
$constraints = array_merge(...$recursiveConstraints);
6162

6263
return new ParameterValidator($parameter, $refParameter->getName(), $constraints, $this->constraintValidatorFactory, $this->validator, $this->translator);

src/Mappers/Parameters/InvalidAssertAnnotationException.php renamed to src/Mappers/Parameters/InvalidAssertionAnnotationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Exception;
88
use ReflectionParameter;
99

10-
class InvalidAssertAnnotationException extends Exception
10+
class InvalidAssertionAnnotationException extends Exception
1111
{
1212
public static function canOnlyValidateInputType(ReflectionParameter $refParameter): self
1313
{

tests/Annotations/AssertTest.php renamed to tests/Annotations/AssertionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
use BadMethodCallException;
66
use PHPUnit\Framework\TestCase;
77

8-
class AssertTest extends TestCase
8+
class AssertionTest extends TestCase
99
{
1010

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

1818
public function testException2()
1919
{
2020
$this->expectException(BadMethodCallException::class);
2121
$this->expectExceptionMessage('The @Assert annotation must be passed one or many constraints. For instance: "@Assert(for="$email", constraint=@Email)"');
22-
new Assert(['for'=>'foo']);
22+
new Assertion(['for'=>'foo']);
2323
}
2424
}

tests/Fixtures/Controllers/UserController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
namespace TheCodingMachine\Graphqlite\Validator\Fixtures\Controllers;
55

66

7-
use Symfony\Component\Validator\Constraints as Assertion;
7+
use Symfony\Component\Validator\Constraints as Assert;
88
use Symfony\Component\Validator\Validator\ValidatorInterface;
99
use TheCodingMachine\GraphQLite\Annotations\Mutation;
1010
use TheCodingMachine\GraphQLite\Annotations\Query;
1111
use TheCodingMachine\Graphqlite\Validator\Fixtures\Types\User;
12-
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
12+
use TheCodingMachine\Graphqlite\Validator\Annotations\Assertion;
1313
use TheCodingMachine\Graphqlite\Validator\ValidationFailedException;
1414

1515
class UserController
@@ -40,7 +40,7 @@ public function createUser(string $email, string $password): User
4040

4141
/**
4242
* @Query
43-
* @Assert(for="email", constraint=@Assertion\Email())
43+
* @Assertion(for="email", constraint=@Assert\Email())
4444
*/
4545
public function findByMail(string $email = '[email protected]'): User
4646
{

tests/Fixtures/InvalidControllers/InvalidController.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55

66

77
use GraphQL\Type\Definition\ResolveInfo;
8-
use Symfony\Component\Validator\Constraints as Assertion;
9-
use Symfony\Component\Validator\Validator\ValidatorInterface;
10-
use TheCodingMachine\GraphQLite\Annotations\Mutation;
8+
use Symfony\Component\Validator\Constraints as Assert;
119
use TheCodingMachine\GraphQLite\Annotations\Query;
12-
use TheCodingMachine\Graphqlite\Validator\Annotations\Assert;
13-
use TheCodingMachine\Graphqlite\Validator\ValidationFailedException;
10+
use TheCodingMachine\Graphqlite\Validator\Annotations\Assertion;
1411

1512
class InvalidController
1613
{
1714
/**
1815
* @Query
19-
* @Assert(for="$resolveInfo", constraint=@Assertion\Email())
16+
* @Assertion(for="$resolveInfo", constraint=@Assert\Email())
2017
*/
2118
public function invalid(ResolveInfo $resolveInfo): string
2219
{

tests/IntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use TheCodingMachine\GraphQLite\SchemaFactory;
2424
use TheCodingMachine\Graphqlite\Validator\Fixtures\Controllers\UserController;
2525
use TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\AssertParameterMiddleware;
26-
use TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\InvalidAssertAnnotationException;
26+
use TheCodingMachine\Graphqlite\Validator\Mappers\Parameters\InvalidAssertionAnnotationException;
2727
use function var_dump;
2828
use function var_export;
2929
use const JSON_PRETTY_PRINT;
@@ -162,7 +162,7 @@ public function testException(): void
162162
$schemaFactory->addControllerNamespace('TheCodingMachine\Graphqlite\Validator\Fixtures\InvalidControllers');
163163
$schema = $schemaFactory->createSchema();
164164

165-
$this->expectException(InvalidAssertAnnotationException::class);
165+
$this->expectException(InvalidAssertionAnnotationException::class);
166166
$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.');
167167
$schema->validate();
168168
}

0 commit comments

Comments
 (0)