Skip to content

Commit

Permalink
Add actual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypkhantc committed Nov 23, 2023
1 parent 5dbc17a commit 7b4557a
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 62 deletions.
6 changes: 1 addition & 5 deletions src/Middlewares/MagicPropertyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ public function propertyName(): string
return $this->propertyName;
}

public function executionSource(object|null $source): object
public function executionSource(object|null $source): object|null
{
if ($source === null) {
throw new GraphQLRuntimeException('You must provide a source for MagicPropertyResolver.');
}

return $source;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Middlewares/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function toString(): string;
* the {@see ExtendedContactType::uppercaseName()} field, the source is a {@see Contact}
* object, but execution source will be an instance of {@see ExtendedContactType}.
*/
public function executionSource(object|null $source): object;
public function executionSource(object|null $source): object|null;

public function __invoke(object|null $source, mixed ...$args): mixed;
}
2 changes: 1 addition & 1 deletion src/Middlewares/SecurityFieldMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function process(QueryFieldDescriptor $queryFieldDescriptor, FieldHandler
*
* @return array<string, mixed>
*/
private function getVariables(array $args, array $parameters, object $source): array
private function getVariables(array $args, array $parameters, object|null $source): array
{
$variables = [
// If a user is not logged, we provide an empty user object to make usage easier
Expand Down
2 changes: 1 addition & 1 deletion src/Middlewares/SecurityInputFieldMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function process(InputFieldDescriptor $inputFieldDescriptor, InputFieldHa
*
* @return array<string, mixed>
*/
private function getVariables(array $args, array $parameters, object $source): array
private function getVariables(array $args, array $parameters, object|null $source): array
{
$variables = [
// If a user is not logged, we provide an empty user object to make usage easier
Expand Down
6 changes: 1 addition & 5 deletions src/Middlewares/SourceConstructorParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ public function parameterName(): string
return $this->parameterName;
}

public function executionSource(object|null $source): object
public function executionSource(object|null $source): object|null
{
if ($source === null) {
throw new GraphQLRuntimeException('You must provide a source for SourceConstructorParameterResolver.');
}

return $source;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Middlewares/SourceInputPropertyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ public function propertyReflection(): ReflectionProperty
return $this->propertyReflection;
}

public function executionSource(object|null $source): object
public function executionSource(object|null $source): object|null
{
if ($source === null) {
throw new GraphQLRuntimeException('You must provide a source for SourceInputPropertyResolver.');
}

return $source;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Middlewares/SourceMethodResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ public function methodReflection(): \ReflectionMethod
return $this->methodReflection;
}

public function executionSource(object|null $source): object
public function executionSource(object|null $source): object|null
{
if ($source === null) {
throw new GraphQLRuntimeException('You must provide a source for SourceMethodResolver.');
}

return $source;
}

Expand Down
6 changes: 1 addition & 5 deletions src/Middlewares/SourcePropertyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ public function propertyReflection(): ReflectionProperty
return $this->propertyReflection;
}

public function executionSource(object|null $source): object
public function executionSource(object|null $source): object|null
{
if ($source === null) {
throw new GraphQLRuntimeException('You must provide a source for SourcePropertyResolver.');
}

return $source;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,24 @@

namespace TheCodingMachine\GraphQLite\Fixtures\Inputs;

use Exception;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Input;
use TheCodingMachine\GraphQLite\Annotations\Right;

/**
* @Input()
*/
class TestConstructorAndPropertiesInvalid
#[Input]
class TestConstructorPromotedProperties
{

/**
* @Field()
*/
private \DateTimeImmutable $date;

/**
* @Field()
* @Right("INVALID_MIDDLEWARE")
* @var string
*/
private $foo;

/**
* @Field()
* @var int
*/
private $bar;

public function __construct(\DateTimeImmutable $date, string $foo)
#[Field]
private int $bar;

public function __construct(
#[Field]
private readonly \DateTimeImmutable $date,
#[Field]
#[Right('FOOOOO')]
public string $foo
)
{
$this->date = $date;
$this->foo = $foo;
}

public function getDate(): \DateTimeImmutable
Expand All @@ -47,11 +32,6 @@ public function setFoo(string $foo): void
throw new \RuntimeException("This should not be called");
}

public function getFoo(): string
{
return $this->foo;
}

public function setBar(int $bar): void
{
$this->bar = $bar;
Expand All @@ -61,4 +41,4 @@ public function getBar(): int
{
return $this->bar;
}
}
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Integration/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use TheCodingMachine\GraphQLite\Annotations\Query;
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Article;
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\Contact;
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\UpdateArticleInput;
use TheCodingMachine\GraphQLite\Fixtures\Integration\Models\User;

class ArticleController
Expand Down Expand Up @@ -34,4 +35,13 @@ public function createArticle(Article $article): Article
{
return $article;
}

#[Mutation]
public function updateArticle(UpdateArticleInput $input): Article
{
$article = new Article('test');
$article->magazine = $input->magazine;

return $article;
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/Integration/Models/UpdateArticleInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace TheCodingMachine\GraphQLite\Fixtures\Integration\Models;

use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Input;
use TheCodingMachine\GraphQLite\Annotations\Security;

#[Input]
class UpdateArticleInput
{
public function __construct(
#[Field]
#[Security("magazine != 'NYTimes'")]
public readonly string|null $magazine,
)
{
}
}
40 changes: 40 additions & 0 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,46 @@ public function testEndToEndInputTypeValidation(): void
$result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS);
}

public function testEndToEndInputConstructor(): void
{
$schema = $this->mainContainer->get(Schema::class);
assert($schema instanceof Schema);

$queryString = '
mutation {
updateArticle(input: {
magazine: "Test"
}) {
magazine
}
}
';

$result = GraphQL::executeQuery(
$schema,
$queryString,
);

$data = $this->getSuccessResult($result);
$this->assertSame('Test', $data['updateArticle']['magazine']);
$queryString = '
mutation {
updateArticle(input: {
magazine: "NYTimes"
}) {
magazine
}
}
';

$result = GraphQL::executeQuery(
$schema,
$queryString,
);

$this->assertSame('Access denied.', $result->toArray(DebugFlag::RETHROW_UNSAFE_EXCEPTIONS)['errors'][0]['message']);
}

public function testEndToEndSetterWithSecurity(): void
{
$container = $this->createContainer([
Expand Down
32 changes: 31 additions & 1 deletion tests/Types/InputTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use TheCodingMachine\GraphQLite\Fixtures\Inputs\InputInterface;
use TheCodingMachine\GraphQLite\Fixtures\Inputs\InputWithSetter;
use TheCodingMachine\GraphQLite\Fixtures\Inputs\TestConstructorAndProperties;
use TheCodingMachine\GraphQLite\Fixtures\Inputs\TestConstructorAndPropertiesInvalid;
use TheCodingMachine\GraphQLite\Fixtures\Inputs\TestConstructorPromotedProperties;
use TheCodingMachine\GraphQLite\Fixtures\Inputs\TestOnlyConstruct;
use TheCodingMachine\GraphQLite\Fixtures\Inputs\TypedFooBar;

Expand Down Expand Up @@ -175,6 +175,36 @@ public function testResolvesCorrectlyWithConstructorAndProperties(): void
$this->assertEquals(200, $result->getBar());
}

public function testResolvesCorrectlyWithConstructorPromotedProperties(): void
{
$input = new InputType(
TestConstructorPromotedProperties::class,
'TestConstructorPromotedPropertiesInput',
null,
false,
$this->getFieldsBuilder(),
);
$input->freeze();
$fields = $input->getFields();

$date = "2022-05-02T04:42:30Z";

$args = [
'date' => $date,
'foo' => 'Foo',
'bar' => 200,
];

$resolveInfo = $this->createMock(ResolveInfo::class);

/** @var TestConstructorPromotedProperties $result */
$result = $input->resolve(null, $args, [], $resolveInfo);

$this->assertEquals(new DateTime("2022-05-02T04:42:30Z"), $result->getDate());
$this->assertEquals('Foo', $result->foo);
$this->assertEquals(200, $result->getBar());
}

public function testFailsResolvingFieldWithoutRequiredConstructParam(): void
{
$input = new InputType(FooBar::class, 'FooBarInput', null, false, $this->getFieldsBuilder());
Expand Down

0 comments on commit 7b4557a

Please sign in to comment.