graphql-php/tests/StarWarsValidationTest.php

154 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2015-07-15 20:05:46 +03:00
<?php
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;
class StartWarsValidationTest extends \PHPUnit_Framework_TestCase
{
// Star Wars Validation Tests
// Basic Queries
/**
* @it Validates a complex but valid query
*/
2015-07-15 20:05:46 +03:00
public function testValidatesAComplexButValidQuery()
{
$query = '
query NestedQueryWithFragment {
hero {
...NameAndAppearances
friends {
...NameAndAppearances
friends {
...NameAndAppearances
}
}
}
}
fragment NameAndAppearances on Character {
name
appearsIn
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(true, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @it Notes that non-existent fields are invalid
*/
2015-07-15 20:05:46 +03:00
public function testThatNonExistentFieldsAreInvalid()
{
$query = '
query HeroSpaceshipQuery {
hero {
favoriteSpaceship
}
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @it Requires fields on objects
*/
2015-07-15 20:05:46 +03:00
public function testRequiresFieldsOnObjects()
{
$query = '
query HeroNoFieldsQuery {
hero
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @it Disallows fields on scalars
*/
2015-07-15 20:05:46 +03:00
public function testDisallowsFieldsOnScalars()
{
$query = '
query HeroFieldsOnScalarQuery {
hero {
name {
firstCharacterOfName
}
}
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @it Disallows object fields on interfaces
*/
2015-07-15 20:05:46 +03:00
public function testDisallowsObjectFieldsOnInterfaces()
{
$query = '
query DroidFieldOnCharacter {
hero {
name
primaryFunction
}
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(false, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @it Allows object fields in fragments
*/
2015-07-15 20:05:46 +03:00
public function testAllowsObjectFieldsInFragments()
{
$query = '
query DroidFieldInFragment {
hero {
name
...DroidFields
}
}
fragment DroidFields on Droid {
primaryFunction
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(true, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* @it Allows object fields in inline fragments
*/
2015-07-15 20:05:46 +03:00
public function testAllowsObjectFieldsInInlineFragments()
{
$query = '
query DroidFieldInFragment {
hero {
name
... on Droid {
primaryFunction
}
}
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(true, empty($errors));
2015-07-15 20:05:46 +03:00
}
/**
* Helper function to test a query and the expected response.
*/
private function validationErrors($query)
2015-07-15 20:05:46 +03:00
{
$ast = Parser::parse($query);
return DocumentValidator::validate(StarWarsSchema::build(), $ast);
}
}