Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The 'Validate' attribute works with a string passed #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/vendor
/build
/.phpunit.result.cache
/.phpunit.cache
tags
5 changes: 2 additions & 3 deletions src/Annotations/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Attribute;
use BadMethodCallException;
use TheCodingMachine\GraphQLite\Annotations\MiddlewareAnnotationInterface;
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotationInterface;
use function is_string;
use function ltrim;
Expand All @@ -30,7 +29,7 @@ class Validate implements ParameterAnnotationInterface
private $rule;

/**
* @param array<string, mixed> $values
* @param array<string, mixed>|string $rule
*/
public function __construct($rule = [])
{
Expand All @@ -43,7 +42,7 @@ public function __construct($rule = [])
$this->for = ltrim($values['for'], '$');
}
}
if (! isset($values['rule'])) {
if (empty($this->rule)) {
throw new BadMethodCallException('The @Validate annotation must be passed a rule. For instance: "#Validate("email")" in PHP 8+ or "@Validate(for="$email", rule="email")" in PHP 7+');
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Annotations/ValidateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace TheCodingMachine\GraphQLite\Laravel\Providers;

use BadMethodCallException;
use Orchestra\Testbench\TestCase;
use TheCodingMachine\GraphQLite\Laravel\Annotations\Validate;

class ValidateTest extends TestCase
{
public function testStringArgument(): void
{
$validator = new Validate('any-rule');
$this->assertEquals('any-rule', $validator->getRule());
}

public function testArrayArgument(): void
{
$validator = new Validate(['rule' => 'any-rule']);
$this->assertEquals('any-rule', $validator->getRule());
}

public function testArrayArgumentWithRuleAndForProperties(): void
{
$validator = new Validate([
'rule' => 'any-rule',
'for' => 'any-for',
]);
$this->assertEquals('any-rule', $validator->getRule());
$this->assertEquals('any-for', $validator->getTarget());
}

public function testRuleShouldNotBeEmpty(): void
{
$this->expectException(BadMethodCallException::class);
$validator = new Validate('');
}
}
10 changes: 10 additions & 0 deletions tests/Fixtures/App/Http/Controllers/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public function testValidatorMultiple(string $foo): string
{
return 'success';
}

/** @Query() */
public function testValidatorForParameterPHP8(
#[Validate("required")]
string $foo,
#[Validate("sometimes")]
null|string $bar,
): string {
return 'success';
}
}
29 changes: 26 additions & 3 deletions tests/Providers/GraphQLiteServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
use TheCodingMachine\GraphQLite\Http\HttpCodeDeciderInterface;
use TheCodingMachine\GraphQLite\Laravel\Listeners\CachePurger;
use TheCodingMachine\GraphQLite\Schema;
use TheCodingMachine\TDBM\TDBMService;
use function json_decode;
use Illuminate\Http\Request;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use TheCodingMachine\GraphQLite\Laravel\Controllers\GraphQLiteController;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;


class GraphQLiteServiceProviderTest extends TestCase
{
protected function getPackageProviders($app)
Expand Down Expand Up @@ -168,6 +165,32 @@ public function testValidatorMultiple()
$this->assertSame(200, $response->getStatusCode(), $response->getContent());
}

/**
* This test the 'Validate' Attribute. In older versions the 'rule' property
* of the Validate constructor was normalized but wrong value was checked.
* Now the Validate attribute should works properly if it's initialized
* with something like this: "#[Validate('some-rule')]".
*/
public function testValidatorForParameterPHP8()
{
$response = $this->json('POST', '/graphql', ['query' => '{ testValidatorForParameterPHP8(foo:"") }']);
$response->assertJson([
'errors' => [
[
'extensions' => [
'argument' => 'foo',
],
],
],
]);

$this->assertStringContainsString('The foo field is required.', $response->json('errors')[0]['message']);
$this->assertSame(400, $response->getStatusCode(), $response->getContent());

$response = $this->json('POST', '/graphql', ['query' => '{ testValidatorForParameterPHP8(foo:"not-empty") }']);
$this->assertSame(200, $response->getStatusCode(), $response->getContent());
}

public function testCachePurger(): void
{
$cachePurger = $this->app->make(CachePurger::class);
Expand Down
Loading