-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from fogrye/master
Support of graphqlite 7 and symfony 7
- Loading branch information
Showing
11 changed files
with
84 additions
and
86 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Validator\Annotations; | ||
|
||
use BadMethodCallException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AssertionTest extends TestCase | ||
{ | ||
|
||
public function testException1() | ||
public function testException1(): void | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
$this->expectExceptionMessage('The @Assert annotation must be passed a target. For instance: "@Assert(for="$email", constraint=@Email)"'); | ||
$this->expectExceptionMessage('The Assert attribute must be passed a target. For instance: "#[Assert(for: "$email", constraint: new Email())"'); | ||
new Assertion([]); | ||
} | ||
|
||
public function testException2() | ||
public function testException2(): void | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
$this->expectExceptionMessage('The @Assert annotation must be passed one or many constraints. For instance: "@Assert(for="$email", constraint=@Email)"'); | ||
new Assertion(['for'=>'foo']); | ||
$this->expectExceptionMessage('The Assert attribute must be passed one or many constraints. For instance: "#[Assert(for: "$email", constraint: new Email())"'); | ||
new Assertion(['for' => 'foo']); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Validator\Fixtures\Controllers; | ||
|
||
|
||
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\Assertion; | ||
use TheCodingMachine\GraphQLite\Validator\Fixtures\Types\User; | ||
use TheCodingMachine\GraphQLite\Validator\ValidationFailedException; | ||
|
||
class UserController | ||
{ | ||
private $validator; | ||
|
||
public function __construct(ValidatorInterface $validator) | ||
public function __construct(private ValidatorInterface $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @Mutation() | ||
*/ | ||
#[Mutation] | ||
public function createUser(string $email, string $password): User | ||
{ | ||
$user = new User($email, $password); | ||
|
@@ -38,13 +33,10 @@ public function createUser(string $email, string $password): User | |
return $user; | ||
} | ||
|
||
/** | ||
* @Query | ||
* @Assertion(for="email", constraint=@Assert\Email()) | ||
*/ | ||
#[Query] | ||
#[Assertion(for: '$email', constraint: new Assert\Email())] | ||
public function findByMail(string $email = '[email protected]'): User | ||
{ | ||
$user = new User($email, 'foo'); | ||
return $user; | ||
return new User($email, 'foo'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Validator\Fixtures\Types; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use TheCodingMachine\GraphQLite\Annotations\Field; | ||
use TheCodingMachine\GraphQLite\Annotations\Type; | ||
|
||
/** | ||
* @Type() | ||
*/ | ||
#[Type] | ||
class User | ||
{ | ||
/** | ||
* @Assert\Email( | ||
* message = "The email '{{ value }}' is not a valid email." | ||
* ) | ||
*/ | ||
private $email; | ||
#[Assert\Email(message: "The email '{{ value }}' is not a valid email.")] | ||
private string $email; | ||
|
||
/** | ||
* The NotCompromisedPassword assertion asks the "HaveIBeenPawned" service if your password has already leaked or not. | ||
* @Assert\Length(min=8) | ||
*/ | ||
private $password; | ||
#[Assert\Length(min: 8)] | ||
private string $password; | ||
|
||
public function __construct(string $email, string $password) | ||
{ | ||
$this->email = $email; | ||
$this->password = $password; | ||
} | ||
|
||
/** | ||
* @Field() | ||
*/ | ||
#[Field] | ||
public function getEmail(): string | ||
{ | ||
return $this->email; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Validator; | ||
|
||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use GraphQL\Error\DebugFlag; | ||
use GraphQL\GraphQL; | ||
use GraphQL\Type\Schema; | ||
|
@@ -30,19 +29,19 @@ class IntegrationTest extends TestCase | |
private function getSchemaFactory(): SchemaFactory | ||
{ | ||
$container = new Picotainer([ | ||
TranslatorInterface::class => function(ContainerInterface $container) { | ||
TranslatorInterface::class => static function (ContainerInterface $container) { | ||
return new Translator('fr_FR'); | ||
}, | ||
ValidatorInterface::class => function(ContainerInterface $container) { | ||
ValidatorInterface::class => static function (ContainerInterface $container) { | ||
$build = new ValidatorBuilder(); | ||
$build->enableAnnotationMapping(); | ||
$build->setDoctrineAnnotationReader(new AnnotationReader()); | ||
$build->enableAttributeMapping(); | ||
$build->setTranslator($container->get(TranslatorInterface::class)); | ||
|
||
return $build->getValidator(); | ||
}, | ||
UserController::class => function(ContainerInterface $container) { | ||
UserController::class => static function (ContainerInterface $container) { | ||
return new UserController($container->get(ValidatorInterface::class)); | ||
} | ||
}, | ||
]); | ||
|
||
$schemaFactory = new SchemaFactory(new Psr16Cache(new ArrayAdapter()), new BasicAutoWiringContainer($container)); | ||
|
@@ -73,7 +72,7 @@ public function testEndToEndThrowException(): void | |
|
||
$result = GraphQL::executeQuery( | ||
$schema, | ||
$queryString | ||
$queryString, | ||
); | ||
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']); | ||
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']); | ||
|
@@ -102,7 +101,7 @@ public function testEndToEndAssert(): void | |
|
||
$result = GraphQL::executeQuery( | ||
$schema, | ||
$queryString | ||
$queryString, | ||
); | ||
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']); | ||
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']); | ||
|
@@ -114,7 +113,6 @@ public function testEndToEndAssert(): void | |
$this->assertSame('email', $errors[0]['extensions']['field']); | ||
$this->assertSame('Validate', $errors[0]['extensions']['category']); | ||
|
||
|
||
$queryString = ' | ||
{ | ||
findByMail(email: "[email protected]") { | ||
|
@@ -125,7 +123,7 @@ public function testEndToEndAssert(): void | |
|
||
$result = GraphQL::executeQuery( | ||
$schema, | ||
$queryString | ||
$queryString, | ||
); | ||
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']); | ||
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']); | ||
|
@@ -144,14 +142,13 @@ public function testEndToEndAssert(): void | |
|
||
$result = GraphQL::executeQuery( | ||
$schema, | ||
$queryString | ||
$queryString, | ||
); | ||
$result->setErrorsHandler([WebonyxErrorHandler::class, 'errorHandler']); | ||
$result->setErrorFormatter([WebonyxErrorHandler::class, 'errorFormatter']); | ||
|
||
$data = $result->toArray(DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)['data']; | ||
$this->assertSame('[email protected]', $data['findByMail']['email']); | ||
|
||
} | ||
|
||
public function testException(): void | ||
|
@@ -164,4 +161,4 @@ public function testException(): void | |
$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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.