使用 Codeception 进行单元测试:解析错误
Unit testing with Codeception: parsing error
我正在尝试为我的简单网站(用于学习)编写一些单元测试。
项目结构:
在index.php里面我定义了一个namespace Main
。
函数在namespace Main\Logic
,类在namespace Main\Logic\Classes
。
tests/unit/BuildGalleryTest.php
的代码:
<?php
namespace Tests\Unit;
use Codeception\Test\Unit as TestCase;
use function Main\Logic\galleryBuilder;
require "/Users/l.marder/Homeworks/php-study/lesson4/logic/galleryBuilder.php";
class BuildGalleryTest extends TestCase
{
/**
* @dataProvider buildGalleryDataProvider
* @param string $imgPack
* @param string $expectedResponse
*/
public function testBuildGallery(string $imgPack, string $expectedResponse): void
{
$actualResponse = galleryBuilder("tests/unit/test_data/img/$imgPack");
self::assertEquals($expectedResponse, $actualResponse);
}
public function buildGalleryDataProvider(): array
{
return
[
"Data pack 1: one pic" => [
"1",
$this->buildTestGallery("1")
],
"Data pack 2: two pics with folder and text.txt file" => [
"2",
$this->buildTestGallery("2")
],
"Data pack 3: no images" => [
"3",
$this->buildTestGallery("3")
]
];
}
private function buildTestGallery(string $dataPack): string
{
if ($dataPack === "1") {
return "
<a href=\"picture.php?img=tests/unit/test_data/img/1/1.img\" target=\"_blank\">
<div class=\"small-pic\">
<img class=\"img-small-kit\" src= alt=\"Kitty 2\">
</div>
</a>";
}
if ($dataPack === "2") {
return "
<a href=\"picture.php?img=tests/unit/test_data/img/2/1.img\" target=\"_blank\">
<div class=\"small-pic\">
<img class=\"img-small-kit\" src=tests/unit/test_data/img/2/1.img alt=\"Kitty 2\">
</div>
</a>
<a href=\"picture.php?img=tests/unit/test_data/img/2/2.img\" target=\"_blank\">
<div class=\"small-pic\">
<img class=\"img-small-kit\" src=tests/unit/test_data/img/2/2.img alt=\"Kitty 3\">
</div>
</a>";
}
if ($dataPack === "3") {
return "<img class=\"img-big-pic\" src=tests/unit/test_data/3/404.gif alt=\"Kitty Pic\">";
}
return "";
}
}
当 运行 php vendor/bin/codecept run
我收到以下错误:
In Parser.php line 128:
Couldn't parse test '/Users/l.marder/Homeworks/php-study/lesson4/tests/unit/logic/BuildGalleryTest.php' on line 9
syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
我做错了什么?可以做些什么来解决这个问题?
我已经使用 sphp 将 php 版本更改为 7.4,现在一切正常!
我正在尝试为我的简单网站(用于学习)编写一些单元测试。
项目结构:
在index.php里面我定义了一个namespace Main
。
函数在namespace Main\Logic
,类在namespace Main\Logic\Classes
。
tests/unit/BuildGalleryTest.php
的代码:
<?php
namespace Tests\Unit;
use Codeception\Test\Unit as TestCase;
use function Main\Logic\galleryBuilder;
require "/Users/l.marder/Homeworks/php-study/lesson4/logic/galleryBuilder.php";
class BuildGalleryTest extends TestCase
{
/**
* @dataProvider buildGalleryDataProvider
* @param string $imgPack
* @param string $expectedResponse
*/
public function testBuildGallery(string $imgPack, string $expectedResponse): void
{
$actualResponse = galleryBuilder("tests/unit/test_data/img/$imgPack");
self::assertEquals($expectedResponse, $actualResponse);
}
public function buildGalleryDataProvider(): array
{
return
[
"Data pack 1: one pic" => [
"1",
$this->buildTestGallery("1")
],
"Data pack 2: two pics with folder and text.txt file" => [
"2",
$this->buildTestGallery("2")
],
"Data pack 3: no images" => [
"3",
$this->buildTestGallery("3")
]
];
}
private function buildTestGallery(string $dataPack): string
{
if ($dataPack === "1") {
return "
<a href=\"picture.php?img=tests/unit/test_data/img/1/1.img\" target=\"_blank\">
<div class=\"small-pic\">
<img class=\"img-small-kit\" src= alt=\"Kitty 2\">
</div>
</a>";
}
if ($dataPack === "2") {
return "
<a href=\"picture.php?img=tests/unit/test_data/img/2/1.img\" target=\"_blank\">
<div class=\"small-pic\">
<img class=\"img-small-kit\" src=tests/unit/test_data/img/2/1.img alt=\"Kitty 2\">
</div>
</a>
<a href=\"picture.php?img=tests/unit/test_data/img/2/2.img\" target=\"_blank\">
<div class=\"small-pic\">
<img class=\"img-small-kit\" src=tests/unit/test_data/img/2/2.img alt=\"Kitty 3\">
</div>
</a>";
}
if ($dataPack === "3") {
return "<img class=\"img-big-pic\" src=tests/unit/test_data/3/404.gif alt=\"Kitty Pic\">";
}
return "";
}
}
当 运行 php vendor/bin/codecept run
我收到以下错误:
In Parser.php line 128:
Couldn't parse test '/Users/l.marder/Homeworks/php-study/lesson4/tests/unit/logic/BuildGalleryTest.php' on line 9
syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
我做错了什么?可以做些什么来解决这个问题?
我已经使用 sphp 将 php 版本更改为 7.4,现在一切正常!