我正在使用创建 http 服务器的 php 对我的 api 进行端到端测试。
这是我的课
<?php
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Process\Process;
class ApiTestCase extends \PHPUnit\Framework\TestCase
{
protected static string $public_directory;
protected static Process $process;
const ENVIRONMENT = 'test';
const HOST = '0.0.0.0';
const PORT = 9876; // Adjust this to a port you're sure is free
public static function setUpBeforeClass(): void
{
$command = [
'php',
'-d',
'variables_order=EGPCS',
'-S',
self::HOST . ':' . self::PORT,
'-t',
self::$public_directory,
self::$public_directory.'/index.php'
];
// Using Symfony/Process to get a handler for starting a new process
self::$process = new Process($command, null, [
'APP_ENV' => self::ENVIRONMENT
]);
// Disabling the output, otherwise the process might hang after too much output
self::$process->disableOutput();
// Actually execute the command and start the process
self::$process->start();
// Let's give the server some leeway to fully start
usleep(100000);
}
public static function tearDownAfterClass(): void
{
self::$process->stop();
}
/**
* @param array<string,mixed>|null $data
* @param string $path
* @param string $method
* @return ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function dispatch(string $path, string $method = 'POST', ?array $data = null): ResponseInterface
{
$params = [];
if ($data) {
$params['form_params'] = $data;
}
$client = new Client(['base_uri' => 'http://127.0.0.1:' . self::PORT]);
return $client->request($method, $path, $params);
}
}
所以在我的测试中,我可以这样使用它,它工作正常
class MyApiTest extends \App\Tests\ApiDbTestCase
{
public function testAuthClientSuccess()
{
// creation of $parameters
$res = $this->dispatch('/v1/user/my/url', 'POST', $parameters);
// My asserts are done after that
}
}
我正在使用 github 操作创建
name: Phpunit coverage
on: [push]
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: php-actions/composer@v6
- uses: php-actions/phpunit@v3
with:
php_extensions: pdo_pgsql xdebug
args: --coverage-html
env:
XDEBUG_MODE: coverage
- name: Archive code coverage results
uses: actions/upload-artifact@v2
with:
name: code-coverage-report
path: output/code-coverage
问题是我的 http 服务器执行的所有内容都没有考虑在内。
例如,我用它测试了一个控制器,在我的报告中有 0%。
所有在没有我的情况下测试的文件都可以,只有在服务器中使用 php 构建的测试才不算在内。
可以算吗?