1. Go to this page and download the library: Download goez/open-api-tester library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
goez / open-api-tester example snippets
use Goez\OpenAPI\Tester\TestSuite;
use Goez\OpenAPI\Tester\TestCase;
// Parse the entire API document to get the test suite
$testSuite = TestSuite::loadFromYamlFile(
__DIR__ . '/docs/api.yaml', // Document location
'My API Tests', // Test suite name
__DIR__ . '/test_files/' // External file path (Where to find the test files for testing uploads)
);
// Get warnings from the parsing process, which can be echoed to notify developers.
// Currently, warnings are generated when:
// - No tests are defined for an Operation
$warnings = $testSuite->getWarning();
// Retrieve the test cases
// An array of Goez\OpenAPI\Tester\TestCase
$testCases = $testSuite->getTestCases();
foreach ($testCases as $testName => $testCase) {
// Use the information contained in `$request` to make an HTTP call to your API or mock the Controller directly.
$request = $testCase->getRequest();
// Here, we use the fictitious function `callMyApi` to represent it. You'll need to implement it yourself.
// If you're using Laravel, more information is provided below.
$response = callMyApi($request);
// Here are some commonly used methods:
//
// $request->getPath();
// $request->getMethod();
// $request->getQuery();
// $request->getRequestBody();
// $request->getHeader();
// Verify if the actual response matches the definition
$result = $testCase->validate($response->code, $response->body, TestCase::FORMAT_JSON);
// Check if the validation was successful
assert($result->isValid(), true);
// Print out the validation details
echo $result->renderReport();
}
public function extractRequestInfo(\Goez\OpenAPI\Tester\Request\Request $request): array
{
$files = [];
$body = [];
$server = $this->transformHeadersToServerVars($request->getHeader());
$requestBody = $request->getRequestBody();
if ($requestBody === null) {
return [$body, $files, $server];
}
$body = collect($requestBody->getStructuredData())
->map(function ($item, $name) use (&$files) {
if (is_a($item, \Goez\OpenAPI\Tester\Request\UploadedFile::class)) {
// Convert the `UploadedFile` object into the `Illuminate\Http\UploadedFile`
$uploadedFile = new \Illuminate\Http\UploadedFile(
$item->getPath(),
$item->getClientOriginalName(),
$item->getClientMimeType(),
null,
true
);
$files[$name] = $uploadedFile;
return $uploadedFile;
}
return $item;
})->toArray();
return [$body, $files, $server];
}