Skip to content

Commit

Permalink
Include StarWars fixture
Browse files Browse the repository at this point in the history
It's no longer exported by webonyx/graphql-php
  • Loading branch information
simPod committed Mar 13, 2021
1 parent cdbcf30 commit bd113e2
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 5 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
"psr-4": {
"Overblog\\DataLoader\\Test\\": "tests/",
"Overblog\\PromiseAdapter\\Test\\": "lib/promise-adapter/tests/"
},
"files": [
"vendor/webonyx/graphql-php/tests/StarWarsData.php"
]
}
},
"replace": {
"overblog/promise-adapter": "self.version"
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Webonyx/GraphQL/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use GraphQL\Executor\Promise\Promise;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\GraphQL;
use GraphQL\Tests\StarWarsData;
use Overblog\DataLoader\DataLoader;
use Overblog\DataLoader\Test\Functional\Webonyx\GraphQL\fixtures\StarWarsData;
use Overblog\PromiseAdapter\PromiseAdapterInterface;

abstract class TestCase extends \PHPUnit\Framework\TestCase
Expand Down
174 changes: 174 additions & 0 deletions tests/Functional/Webonyx/GraphQL/fixtures/StarWarsData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare(strict_types=1);

namespace Overblog\DataLoader\Test\Functional\Webonyx\GraphQL\fixtures;

use function array_map;

class StarWarsData
{
/**
* Helper function to get a character by ID.
*/
public static function getCharacter($id)
{
$humans = self::humans();
$droids = self::droids();
if (isset($humans[$id])) {
return $humans[$id];
}
if (isset($droids[$id])) {
return $droids[$id];
}

return null;
}

public static function humans()
{
return [
'1000' => self::luke(),
'1001' => self::vader(),
'1002' => self::han(),
'1003' => self::leia(),
'1004' => self::tarkin(),
];
}

private static function luke()
{
return [
'id' => '1000',
'name' => 'Luke Skywalker',
'friends' => ['1002', '1003', '2000', '2001'],
'appearsIn' => [4, 5, 6],
'homePlanet' => 'Tatooine',
];
}

private static function vader()
{
return [
'id' => '1001',
'name' => 'Darth Vader',
'friends' => ['1004'],
'appearsIn' => [4, 5, 6],
'homePlanet' => 'Tatooine',
];
}

private static function han()
{
return [
'id' => '1002',
'name' => 'Han Solo',
'friends' => ['1000', '1003', '2001'],
'appearsIn' => [4, 5, 6],
];
}

private static function leia()
{
return [
'id' => '1003',
'name' => 'Leia Organa',
'friends' => ['1000', '1002', '2000', '2001'],
'appearsIn' => [4, 5, 6],
'homePlanet' => 'Alderaan',
];
}

private static function tarkin()
{
return [
'id' => '1004',
'name' => 'Wilhuff Tarkin',
'friends' => ['1001'],
'appearsIn' => [4],
];
}

public static function droids()
{
return [
'2000' => self::threepio(),
'2001' => self::artoo(),
];
}

private static function threepio()
{
return [
'id' => '2000',
'name' => 'C-3PO',
'friends' => ['1000', '1002', '1003', '2001'],
'appearsIn' => [4, 5, 6],
'primaryFunction' => 'Protocol',
];
}

/**
* We export artoo directly because the schema returns him
* from a root field, and hence needs to reference him.
*/
private static function artoo()
{
return [

'id' => '2001',
'name' => 'R2-D2',
'friends' => ['1000', '1002', '1003'],
'appearsIn' => [4, 5, 6],
'primaryFunction' => 'Astromech',
];
}

/**
* Allows us to query for a character's friends.
*/
public static function getFriends($character)
{
return array_map([self::class, 'getCharacter'], $character['friends']);
}

/**
* @param int $episode
*
* @return mixed[]
*/
public static function getHero($episode)
{
if ($episode === 5) {
// Luke is the hero of Episode V.
return self::luke();
}

// Artoo is the hero otherwise.
return self::artoo();
}

/**
* @param string $id
*
* @return mixed|null
*/
public static function getHuman($id)
{
$humans = self::humans();

return $humans[$id] ?? null;
}

/**
* @param string $id
*
* @return mixed|null
*/
public static function getDroid($id)
{
$droids = self::droids();

return $droids[$id] ?? null;
}
}

0 comments on commit bd113e2

Please sign in to comment.