laravel-graphql icon indicating copy to clipboard operation
laravel-graphql copied to clipboard

How to build feature tests?

Open sandervanhooft opened this issue 7 years ago • 5 comments

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?

sandervanhooft avatar Mar 16 '17 12:03 sandervanhooft

I would also like to know this.

AlexCatch avatar Jun 02 '17 07:06 AlexCatch

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

dmongeau avatar Jun 02 '17 19:06 dmongeau

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

derotune avatar Apr 18 '18 10:04 derotune

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

sandervanhooft avatar Jun 20 '18 13:06 sandervanhooft

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']);

derotune avatar Jul 06 '18 13:07 derotune