graphql-php/tests/StarWarsValidationTest.php

134 lines
3.0 KiB
PHP
Raw 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
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
}
public function testThatNonExistentFieldsAreInvalid()
{
// Notes that non-existent fields are invalid
$query = '
query HeroSpaceshipQuery {
hero {
favoriteSpaceship
}
}
';
$errors = $this->validationErrors($query);
$this->assertEquals(false, empty($errors));
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
}
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
}
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
}
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
}
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);
}
}