From d9814674507f231977efb46dd2dd64da174b3274 Mon Sep 17 00:00:00 2001 From: Alexandr Viniychuk Date: Tue, 8 Nov 2016 21:43:36 -0500 Subject: [PATCH] test and fix for Fragment reference "fReservation" not found on model "CourtReservation" #63 --- Tests/Schema/FragmentsTest.php | 212 +++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 Tests/Schema/FragmentsTest.php diff --git a/Tests/Schema/FragmentsTest.php b/Tests/Schema/FragmentsTest.php new file mode 100644 index 00000000..f389627e --- /dev/null +++ b/Tests/Schema/FragmentsTest.php @@ -0,0 +1,212 @@ +addFields([ + 'id' => new IdType(), + 'fullName' => new StringType(), + 'reservations' => new ListType(new ReservationInterface()) + ]); + } +} + +class CourtReservation extends AbstractObjectType +{ + + public function build($config) + { + $config->addFields([ + 'id' => new IdType(), + 'players' => new ListType(new ObjectType([ + 'name' => 'Player', + 'fields' => [ + 'id' => new IdType(), + 'user' => new UserType() + ] + ])) + ]); + } + + public function getInterfaces() + { + return [new ReservationInterface()]; + } + +} + +class ClassReservation extends AbstractObjectType +{ + public function build($config) + { + $config->addFields([ + 'id' => new IdType(), + 'user' => new UserType() + ]); + } + + public function getInterfaces() + { + return [new ReservationInterface()]; + } +} + +class ReservationInterface extends AbstractInterfaceType +{ + public function resolveType($object) + { + return strpos($object['id'], 'cl') === false ? new CourtReservation() : new ClassReservation(); + } + + public function build($config) + { + $config->addFields([ + 'id' => new IdType() + ]); + } + +} + +class FragmentsTest extends \PHPUnit_Framework_TestCase +{ + + /** + * @dataProvider queries + * + * @param $query + * @param $expected + * @param $variables + */ + public function testVariables($query, $expected, $variables) + { + $schema = new Schema([ + 'query' => new ObjectType([ + 'name' => 'RootQuery', + 'fields' => [ + 'user' => [ + 'type' => new UserType(), + 'resolve' => function ($args) { + return [ + 'id' => 'user-id-1', + 'fullName' => 'Alex', + 'reservations' => [ + [ + 'id' => 'cl-1', + 'user' => [ + 'id' => 'user-id-2', + 'fullName' => 'User class1' + ], + ], + [ + 'id' => 'court-1', + 'players' => [ + [ + 'id' => 'player-id-1', + 'user' => [ + 'id' => 'user-id-3', + 'fullName' => 'User court1' + ] + ] + ] + ], + ] + ]; + }, + ], + ] + ]) + ]); + + $processor = new Processor($schema); + $processor->processPayload($query, $variables); + $result = $processor->getResponseData(); + + $this->assertEquals($expected, $result); + } + + public function queries() + { + return [ + [ + 'query { + user { + ...fUser + reservations { + ...fReservation + } + } + } + fragment fReservation on Reservation { + id + ... on CourtReservation { + players { + id + user { + ...fUser + } + } + } + ... on ClassReservation { + user { + ...fUser + } + } + } + fragment fUser on User { + id + fullName + }', + [ + 'data' => [ + 'user' => [ + 'id' => 'user-id-1', + 'fullName' => 'Alex', + 'reservations' => [ + [ + 'id' => 'cl-1', + 'user' => [ + 'id' => 'user-id-2', + 'fullName' => 'User class1' + ] + ], + [ + 'id' => 'court-1', + 'players' => [ + [ + 'id' => 'player-id-1', + 'user' => [ + 'id' => 'user-id-3', + 'fullName' => 'User court1' + ] + ] + ] + ], + ] + ] + ], + ], + [ + ] + ], + ]; + } + +} \ No newline at end of file