我正在使用的帮助下对代码进行单元测试
phpunit 9.3.8
. 测试在我的本地开发环境中运行良好(我运行的是Windows 10,
PHP 7.4.2
具有
Xdebug 2.9.2
)但由于尝试运行时出现分析错误而失败
phpunit
在Gitlab CI上(CI运行最新的alpine linux映像)。
错误是:
$ composer run test
> phpunit
PHPUnit 9.3.8 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.23 with Xdebug 2.9.8
Configuration: /builds/gaspacchio/back-to-the-future/phpunit.xml
Reporter
â Can set short message [6.05 ms]
â
â ParseError: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
â
â /builds/gaspacchio/back-to-the-future/src/api/utilities/Reporter.php:21
â /builds/gaspacchio/back-to-the-future/src/api/tests/ReporterTest.php:12
â
// More errors
Time: 00:01.970, Memory: 310.00 MB
ERRORS!
Tests: 6, Assertions: 0, Errors: 6.
Generating code coverage report in Clover XML format ... done [00:00.330]
PHP Warning: fopen(/builds/gaspacchio/back-to-the-future/): failed to open stream: Is a directory in /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/Util/Printer.php on line 89
PHP Stack trace:
PHP 1. {main}() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit\TextUI\Command::main() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/phpunit:61
PHP 3. PHPUnit\TextUI\Command->run() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/Command.php:100
PHP 4. PHPUnit\TextUI\TestRunner->run() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/Command.php:147
PHP 5. PHPUnit\Util\Printer->__construct() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:756
PHP 6. fopen() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/Util/Printer.php:89
Code Coverage Report:
2020-10-13 10:12:09
Summary:
Classes: 0.00% (0/11)
Methods: 0.00% (0/38)
Paths: 0.00% (0/7)
Branches: 0.00% (0/7)
Lines: 0.00% (0/397)
Script phpunit handling the test event returned with error code 2
报告第21行
Reporter.php
档案如下:
<?php namespace utilities\Reporter;
/**
* The Reporter class is responsible for returning data to the client.
*/
class Reporter
{
/** This is the code of the answer.
* (more comments)
* @var int The status code.
*/
private int $code; //<-- This is line number 21
第12行
ReporterTest.php
<?php
use PHPUnit\Framework\TestCase;
use utilities\Reporter\Reporter;
class ReporterTest extends TestCase
{
protected $reporter;
protected function setUp(): void
{
$this->reporter = new Reporter(); // <-- Line 12 is here
}
}
我正在使用PHPunit的fixtures函数,定义为:
PHPUnit支持共享设置代码。在运行测试方法之前,将调用名为setUp()的模板方法。setUp()用于创建要测试的对象。