温馨提示:本文翻译自stackoverflow.com,查看原文请点击:php - Unit testing with Codeception: parsing error
automated-tests codeception php phpunit

php - 使用Codeception进行单元测试:解析错误

发布于 2021-04-17 08:55:01

我正在尝试为我的简单网站(供研究)编写一些单元测试。

项目结构:

项目

在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)      

我究竟做错了什么?如何解决此问题?

查看更多

提问者
Lev Marder
被浏览
0
Lev Marder 2020-07-10 03:13

我已经使用sphp将php版本更改为7.4,现在一切正常!