3
votes

I use Laravel and PHPUnit. In my tests, I have a lot of requests which tend to test my REST-like API, and it's doing the job quite nicely. Now I wonder if I could use all those tests to make some description/documentation for my API.

Formally, how can I log/access all requests and responses created by $this->get(...), $this->post(...) and so on?

1

1 Answers

1
votes

Well depending on how you write your test name you can use the options on PHPUnit cli. You can read more about them here.

--testdox-html Write agile documentation in HTML format to file.
--testdox-text Write agile documentation in Text format to file.

When you run your tests, instead of just running:

phpunit 

try:

phpunit --testdox-html documentation.html

A little more detailed description:

PHPUnit's TestDox functionality looks at a test class and all the test method names and converts them from camel case PHP names to sentences: testBalanceIsInitiallyZero() becomes "Balance is initially zero". If there are several test methods whose names only differ in a suffix of one or more digits, such as testBalanceCannotBecomeNegative() and testBalanceCannotBecomeNegative2(), the sentence "Balance cannot become negative" will appear only once, assuming that all of these tests succeed.

The output will look as follows:

phpunit --testdox BankAccountTest
PHPUnit 6.1.0 by Sebastian Bergmann and contributors.

BankAccount
 [x] Balance is initially zero
 [x] Balance cannot become negative

Update

As far as logging all requests, the only thing that comes to mind is to extend test case. I tried it out myself in a simple form and it seemed to work:

abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{

    public function get($uri, array $headers = [])
    {
        parent::get($uri, $headers);
        $txt = "GET REQUEST: ".$uri." HEADERS: ".json_encode($headers)."\n";
        file_put_contents('logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
    }

}