0

For an application we use configuration files in which a large number of endpoint characteristics are determined (relations, fillables, visibles, roles, etc.) We would like to loop through these files and conduct automatic tests with PHPUnit, simply to see if we receive a response, if validation errors are being triggered, if the response is in line with the files, etc.

We load the configuration and perform the tests for each endpoint configuration:

public function testConfigurationFiles()
{
    $config = resolve('App\Contracts\ConfigInterface');

    foreach ($config->resources as $resource=>$configuration) {
        foreach ($configuration->endpoints() as $method=>$rules) {
            $this->endpoint($method, $resource, $configuration);
        }
    }
}

After which we use a switch, to test each type of method differently (index, show, create, update, delete). In total this comes down to dozens of tests with hundreds of assertions.

However, if even one of these endpoints fails, the entire tests fails without showing explicit information what went wrong. Is there a way to automatically generate a "test{$resource}{$method}" method for each endpoint, so they will be handled like individual tests?

Besides these tests we also conduct units tests & e2e tests, so we are fully aware of the disadvantages of this way of testing.

1 Answer 1

2

After studying PHPUnit some more, I found my answer in dataProviders:

https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers

This way you can indicate a data provider for a method, which should return an array with all cases you want to iterate over.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.