graphql-php/tests/StarWarsValidationTest.php

159 lines
3.5 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-02 14:39:30 +03:00
declare(strict_types=1);
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
use GraphQL\Validator\DocumentValidator;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2015-07-15 20:05:46 +03:00
2018-07-29 18:43:10 +03:00
class StarWarsValidationTest extends TestCase
2015-07-15 20:05:46 +03:00
{
// Star Wars Validation Tests
// Basic Queries
/**
* @see it('Validates a complex but valid query')
*/
public function testValidatesAComplexButValidQuery() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:39:30 +03:00
$query = '
2015-07-15 20:05:46 +03:00
query NestedQueryWithFragment {
hero {
...NameAndAppearances
friends {
...NameAndAppearances
friends {
...NameAndAppearances
}
}
}
}
fragment NameAndAppearances on Character {
name
appearsIn
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(true, empty($errors));
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:39:30 +03:00
/**
* Helper function to test a query and the expected response.
*/
private function validationErrors($query)
{
$ast = Parser::parse($query);
return DocumentValidator::validate(StarWarsSchema::build(), $ast);
}
/**
* @see it('Notes that non-existent fields are invalid')
*/
public function testThatNonExistentFieldsAreInvalid() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:39:30 +03:00
$query = '
2015-07-15 20:05:46 +03:00
query HeroSpaceshipQuery {
hero {
favoriteSpaceship
}
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Requires fields on objects')
*/
public function testRequiresFieldsOnObjects() : void
2015-07-15 20:05:46 +03:00
{
$query = '
query HeroNoFieldsQuery {
hero
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Disallows fields on scalars')
*/
public function testDisallowsFieldsOnScalars() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:39:30 +03:00
$query = '
2015-07-15 20:05:46 +03:00
query HeroFieldsOnScalarQuery {
hero {
name {
firstCharacterOfName
}
}
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Disallows object fields on interfaces')
*/
public function testDisallowsObjectFieldsOnInterfaces() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:39:30 +03:00
$query = '
2015-07-15 20:05:46 +03:00
query DroidFieldOnCharacter {
hero {
name
primaryFunction
}
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Allows object fields in fragments')
*/
public function testAllowsObjectFieldsInFragments() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:39:30 +03:00
$query = '
2015-07-15 20:05:46 +03:00
query DroidFieldInFragment {
hero {
name
...DroidFields
}
}
fragment DroidFields on Droid {
primaryFunction
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(true, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Allows object fields in inline fragments')
*/
public function testAllowsObjectFieldsInInlineFragments() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:39:30 +03:00
$query = '
2015-07-15 20:05:46 +03:00
query DroidFieldInFragment {
hero {
name
... on Droid {
primaryFunction
}
}
}
';
$errors = $this->validationErrors($query);
2018-09-19 18:12:09 +03:00
self::assertEquals(true, empty($errors));
2015-07-15 20:05:46 +03:00
}
}