Skip to content

Commit 2f677f6

Browse files
authored
Merge pull request #70 from fogrye/master
Support of graphqlite 7 and symfony 7
2 parents 433380b + f469df2 commit 2f677f6

11 files changed

+84
-86
lines changed

composer.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818
],
1919
"require" : {
2020
"php" : ">=8.1",
21-
"thecodingmachine/graphqlite" : "^6.0 || ^7.0",
22-
"symfony/validator": "^6" ,
23-
"doctrine/annotations": "^1.13 || ^2.0.1"
21+
"thecodingmachine/graphqlite" : "^7.0",
22+
"symfony/validator": "^7"
2423
},
2524
"require-dev": {
2625
"phpunit/phpunit": "^9.6.5 || ^10.0.0",
2726
"mouf/picotainer": "^1.1",
2827
"phpstan/phpstan": "^1.8",
2928
"php-coveralls/php-coveralls": "^2.1.0",
30-
"symfony/translation": "^6",
31-
"doctrine/coding-standard": "^11.1 || ^12.0"
29+
"symfony/translation": "^7",
30+
"doctrine/coding-standard": "^11.1|^12.0"
3231
},
3332
"scripts": {
3433
"phpstan": "phpstan analyse src/ -c phpstan.neon --level=7 --no-progress",
@@ -48,7 +47,7 @@
4847
},
4948
"extra": {
5049
"branch-alias": {
51-
"dev-master": "6.0.x-dev"
50+
"dev-master": "7.0.x-dev"
5251
}
5352
},
5453
"minimum-stability": "dev",

phpcs.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<!-- Directories to be checked -->
1313
<file>src</file>
14-
<!-- <file>tests</file> -->
14+
<file>tests</file>
1515

1616
<!-- Include full Doctrine Coding Standard -->
1717
<rule ref="Doctrine">

src/Annotations/Assertion.php

+21-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TheCodingMachine\GraphQLite\Validator\Annotations;
66

7+
use Attribute;
78
use BadMethodCallException;
89
use Symfony\Component\Validator\Constraint;
910
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotationInterface;
@@ -21,26 +22,34 @@
2122
* @Attribute("constraint", type = "Symfony\Component\Validator\Constraint[]|Symfony\Component\Validator\Constraint")
2223
* })
2324
*/
25+
#[Attribute(Attribute::TARGET_METHOD)]
2426
class Assertion implements ParameterAnnotationInterface
2527
{
26-
/** @var string */
27-
private $for;
28+
private string $for;
2829
/** @var Constraint[] */
29-
private $constraint;
30+
private array $constraint;
3031

31-
/** @param array<string, mixed> $values */
32-
public function __construct(array $values)
33-
{
34-
if (! isset($values['for'])) {
35-
throw new BadMethodCallException('The @Assert annotation must be passed a target. For instance: "@Assert(for="$email", constraint=@Email)"');
32+
/**
33+
* @param array<string, mixed> $values
34+
* @param Constraint[]|Constraint|null $constraint
35+
*/
36+
public function __construct(
37+
array $values = [],
38+
string|null $for = null,
39+
array|Constraint|null $constraint = null,
40+
) {
41+
$for = $for ?? $values['for'] ?? null;
42+
$constraint = $constraint ?? $values['constraint'] ?? null;
43+
if ($for === null) {
44+
throw new BadMethodCallException('The Assert attribute must be passed a target. For instance: "#[Assert(for: "$email", constraint: new Email())"');
3645
}
3746

38-
if (! isset($values['constraint'])) {
39-
throw new BadMethodCallException('The @Assert annotation must be passed one or many constraints. For instance: "@Assert(for="$email", constraint=@Email)"');
47+
if ($constraint === null) {
48+
throw new BadMethodCallException('The Assert attribute must be passed one or many constraints. For instance: "#[Assert(for: "$email", constraint: new Email())"');
4049
}
4150

42-
$this->for = ltrim($values['for'], '$');
43-
$this->constraint = is_array($values['constraint']) ? $values['constraint'] : [$values['constraint']];
51+
$this->for = ltrim($for, '$');
52+
$this->constraint = is_array($constraint) ? $constraint : [$constraint];
4453
}
4554

4655
public function getTarget(): string

src/Mappers/Parameters/ParameterValidator.php

+10
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,14 @@ public function getDefaultValue(): mixed
7777
{
7878
return $this->parameter->getDefaultValue();
7979
}
80+
81+
public function getName(): string
82+
{
83+
return $this->parameter->getName();
84+
}
85+
86+
public function getDescription(): string
87+
{
88+
return $this->parameter->getDescription();
89+
}
8090
}

tests/Annotations/AssertionTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace TheCodingMachine\GraphQLite\Validator\Annotations;
46

57
use BadMethodCallException;
68
use PHPUnit\Framework\TestCase;
79

810
class AssertionTest extends TestCase
911
{
10-
11-
public function testException1()
12+
public function testException1(): void
1213
{
1314
$this->expectException(BadMethodCallException::class);
14-
$this->expectExceptionMessage('The @Assert annotation must be passed a target. For instance: "@Assert(for="$email", constraint=@Email)"');
15+
$this->expectExceptionMessage('The Assert attribute must be passed a target. For instance: "#[Assert(for: "$email", constraint: new Email())"');
1516
new Assertion([]);
1617
}
1718

18-
public function testException2()
19+
public function testException2(): void
1920
{
2021
$this->expectException(BadMethodCallException::class);
21-
$this->expectExceptionMessage('The @Assert annotation must be passed one or many constraints. For instance: "@Assert(for="$email", constraint=@Email)"');
22-
new Assertion(['for'=>'foo']);
22+
$this->expectExceptionMessage('The Assert attribute must be passed one or many constraints. For instance: "#[Assert(for: "$email", constraint: new Email())"');
23+
new Assertion(['for' => 'foo']);
2324
}
2425
}

tests/ConstraintValidationExceptionTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace TheCodingMachine\GraphQLite\Validator;
46

57
use PHPUnit\Framework\TestCase;
68
use Symfony\Component\Validator\ConstraintViolation;
79

810
class ConstraintValidationExceptionTest extends TestCase
911
{
10-
11-
public function testException()
12+
public function testException(): void
1213
{
1314
$exception = new ConstraintViolationException(new ConstraintViolation('foo', 'foo {bar}', ['bar' => 'baz'], null, null, 'invalidValue', null, 'myCode'));
1415
$this->assertSame(400, $exception->getCode());
+7-15
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
<?php
22

3+
declare(strict_types=1);
34

45
namespace TheCodingMachine\GraphQLite\Validator\Fixtures\Controllers;
56

6-
77
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;
11-
use TheCodingMachine\GraphQLite\Validator\Fixtures\Types\User;
1211
use TheCodingMachine\GraphQLite\Validator\Annotations\Assertion;
12+
use TheCodingMachine\GraphQLite\Validator\Fixtures\Types\User;
1313
use TheCodingMachine\GraphQLite\Validator\ValidationFailedException;
1414

1515
class UserController
1616
{
17-
private $validator;
18-
19-
public function __construct(ValidatorInterface $validator)
17+
public function __construct(private ValidatorInterface $validator)
2018
{
21-
$this->validator = $validator;
2219
}
2320

24-
/**
25-
* @Mutation()
26-
*/
21+
#[Mutation]
2722
public function createUser(string $email, string $password): User
2823
{
2924
$user = new User($email, $password);
@@ -38,13 +33,10 @@ public function createUser(string $email, string $password): User
3833
return $user;
3934
}
4035

41-
/**
42-
* @Query
43-
* @Assertion(for="email", constraint=@Assert\Email())
44-
*/
36+
#[Query]
37+
#[Assertion(for: '$email', constraint: new Assert\Email())]
4538
public function findByMail(string $email = '[email protected]'): User
4639
{
47-
$user = new User($email, 'foo');
48-
return $user;
40+
return new User($email, 'foo');
4941
}
5042
}

tests/Fixtures/InvalidControllers/InvalidController.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?php
22

3+
declare(strict_types=1);
34

45
namespace TheCodingMachine\GraphQLite\Validator\Fixtures\InvalidControllers;
56

6-
77
use GraphQL\Type\Definition\ResolveInfo;
88
use Symfony\Component\Validator\Constraints as Assert;
99
use TheCodingMachine\GraphQLite\Annotations\Query;
1010
use TheCodingMachine\GraphQLite\Validator\Annotations\Assertion;
1111

1212
class InvalidController
1313
{
14-
/**
15-
* @Query
16-
* @Assertion(for="$resolveInfo", constraint=@Assert\Email())
17-
*/
14+
#[Query]
15+
#[Assertion(for: '$resolveInfo', constraint: new Assert\Email())]
1816
public function invalid(ResolveInfo $resolveInfo): string
1917
{
2018
return 'foo';

tests/Fixtures/Types/User.php

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
34

45
namespace TheCodingMachine\GraphQLite\Validator\Fixtures\Types;
6+
57
use Symfony\Component\Validator\Constraints as Assert;
68
use TheCodingMachine\GraphQLite\Annotations\Field;
79
use TheCodingMachine\GraphQLite\Annotations\Type;
810

9-
/**
10-
* @Type()
11-
*/
11+
#[Type]
1212
class User
1313
{
14-
/**
15-
* @Assert\Email(
16-
* message = "The email '{{ value }}' is not a valid email."
17-
* )
18-
*/
19-
private $email;
14+
#[Assert\Email(message: "The email '{{ value }}' is not a valid email.")]
15+
private string $email;
2016

2117
/**
2218
* The NotCompromisedPassword assertion asks the "HaveIBeenPawned" service if your password has already leaked or not.
23-
* @Assert\Length(min=8)
2419
*/
25-
private $password;
20+
#[Assert\Length(min: 8)]
21+
private string $password;
2622

2723
public function __construct(string $email, string $password)
2824
{
2925
$this->email = $email;
3026
$this->password = $password;
3127
}
3228

33-
/**
34-
* @Field()
35-
*/
29+
#[Field]
3630
public function getEmail(): string
3731
{
3832
return $this->email;
3933
}
40-
}
34+
}

tests/IntegrationTest.php

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
34

45
namespace TheCodingMachine\GraphQLite\Validator;
56

6-
7-
use Doctrine\Common\Annotations\AnnotationReader;
87
use GraphQL\Error\DebugFlag;
98
use GraphQL\GraphQL;
109
use GraphQL\Type\Schema;
@@ -30,19 +29,19 @@ class IntegrationTest extends TestCase
3029
private function getSchemaFactory(): SchemaFactory
3130
{
3231
$container = new Picotainer([
33-
TranslatorInterface::class => function(ContainerInterface $container) {
32+
TranslatorInterface::class => static function (ContainerInterface $container) {
3433
return new Translator('fr_FR');
3534
},
36-
ValidatorInterface::class => function(ContainerInterface $container) {
35+
ValidatorInterface::class => static function (ContainerInterface $container) {
3736
$build = new ValidatorBuilder();
38-
$build->enableAnnotationMapping();
39-
$build->setDoctrineAnnotationReader(new AnnotationReader());
37+
$build->enableAttributeMapping();
4038
$build->setTranslator($container->get(TranslatorInterface::class));
39+
4140
return $build->getValidator();
4241
},
43-
UserController::class => function(ContainerInterface $container) {
42+
UserController::class => static function (ContainerInterface $container) {
4443
return new UserController($container->get(ValidatorInterface::class));
45-
}
44+
},
4645
]);
4746

4847
$schemaFactory = new SchemaFactory(new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer($container));
@@ -73,7 +72,7 @@ public function testEndToEndThrowException(): void
7372

7473
$result = GraphQL::executeQuery(
7574
$schema,
76-
$queryString
75+
$queryString,
7776
);
7877
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']);
7978
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']);
@@ -102,7 +101,7 @@ public function testEndToEndAssert(): void
102101

103102
$result = GraphQL::executeQuery(
104103
$schema,
105-
$queryString
104+
$queryString,
106105
);
107106
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']);
108107
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']);
@@ -114,7 +113,6 @@ public function testEndToEndAssert(): void
114113
$this->assertSame('email', $errors[0]['extensions']['field']);
115114
$this->assertSame('Validate', $errors[0]['extensions']['category']);
116115

117-
118116
$queryString = '
119117
{
120118
findByMail(email: "[email protected]") {
@@ -125,7 +123,7 @@ public function testEndToEndAssert(): void
125123

126124
$result = GraphQL::executeQuery(
127125
$schema,
128-
$queryString
126+
$queryString,
129127
);
130128
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']);
131129
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']);
@@ -144,14 +142,13 @@ public function testEndToEndAssert(): void
144142

145143
$result = GraphQL::executeQuery(
146144
$schema,
147-
$queryString
145+
$queryString,
148146
);
149147
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']);
150148
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']);
151149

152150
$data = $result->toArray(DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)['data'];
153151
$this->assertSame('[email protected]', $data['findByMail']['email']);
154-
155152
}
156153

157154
public function testException(): void
@@ -164,4 +161,4 @@ public function testException(): void
164161
$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.');
165162
$schema->validate();
166163
}
167-
}
164+
}

0 commit comments

Comments
 (0)