folkloreinc / laravel-graphql

Facebook GraphQL for Laravel 5. It supports Relay, eloquent models, validation and GraphiQL.
1.76k stars 234 forks source link

How to build feature tests? #99

Open sandervanhooft opened 7 years ago

sandervanhooft commented 7 years ago

I'd like to take a TDD approach on implementing a schema. Do you have a recommended way to (or an example repo) write phpunit tests for testing queries and mutations?

AlexCatch commented 7 years ago

I would also like to know this.

dmongeau commented 7 years ago

If you look at the feature/relay branch, some tests kind of does this

derotune commented 6 years ago

Is there a way now to do this? Because i couldn't find anything on the feature/relay branch

sandervanhooft commented 6 years ago

Haven't encountered a viable way yet. Let me know if you find anything :)

derotune commented 6 years ago

Well i copied some things from graphql php library and build something my own.

Its simple and maybe not the best way but it works. So first i created a new base testcase class

abstract class GraphQLTestCase extends TestCase {

    protected $queries;
    protected $mutations;
    protected $data;

    public function setUp() {
        parent::setUp();
        $this->queries = include(__DIR__ . '/fixtures/GraphQL/queries.php');
        $this->mutations = include(__DIR__ . '/fixtures/GraphQL/mutations.php');
    }

    protected function graphQlUri($query, $prefix = '') {
        return '/graphql/' . $prefix . '?query=' . urlencode($query);
    }

}

Under fixtures/GraphQL` i got two files with just queries or mutations looking like this (mutation example):


'cancelAppointment' => function ($id) {
        return '
          mutation {
              appointment: cancelAppointment(input: {
                id: ' . $id . ',
              }) { status }
          }
        ';
    }

Than test looks like this:

$response = $this->get($this->graphQlUri($this->mutations['cancelAppointment']($appointment->id), 'some_prefix_if-you_use'));
        $content = $response->getData(true);
        $content = $content['data'];

        $this->assertEquals('canceled', $content['appointment']['status']);