Make Laravel / PHPUnit remember all executed requests
I am using Laravel and PHPUnit. In my tests, I have a lot of requests that tend to test my REST-like API, and it does the job pretty well. Now I am wondering if I can use all those tests to do some description / documentation for my API.
Formally, how can I log / access all requests and responses created $this->get(...)
, $this->post(...)
etc.?
source to share
Well, depending on how you write the test name, you can use the parameters on the PHPUnit cli. You can find out more about them here .
- testdox-html Write flexible HTML documentation to a file.
--testdox-text Write flexible documentation in Text format for the file.
When you run your tests and not just run:
phpunit
try:
phpunit --testdox-html documentation.html
A little more detailed description :
The PHPUnit TestDox functionality looks at the test class and all test method names and converts them from a camel PHP name to sentences: testBalanceIsInitiallyZero () becomes "Balance initially zero". If there are multiple test methods whose names differ only by the suffix one or more digits, such as testBalanceCannotBecomeNegative () and testBalanceCannotBecomeNegative2 (), the Balance Cannot Become Negative clause appears only once, assuming that all of these tests succeed.
The output will look like this:
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 for logging all requests, the only thing that comes to mind is to extend the test case. I tried this myself in a simple manner 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);
}
}
source to share