Модульное тестирование с помощью Codeception: ошибка парсинга - PullRequest
0 голосов
/ 09 июля 2020

Я пытаюсь написать несколько модульных тестов для своего простого веб-сайта (для изучения).

Структура проекта:

project

Inside index.php I have a namespace Main defined.

Functions lie in namespace Main\Logic and Classes lie in namespace Main\Logic\Classes.

Code for 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)      

Что я делаю не так? Что можно сделать для решения этой проблемы?

1 Ответ

0 голосов
/ 09 июля 2020

Я изменил версию php на 7.4, используя s php, и теперь все работает!

...