Warm tip: This article is reproduced from stackoverflow.com, please click
automated-tests codeception php phpunit

Unit testing with Codeception: parsing error

发布于 2020-11-15 19:18:49

I'm trying to write some Unit Tests for my simple website (for study).

Project structure:

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 "";
    }
}

When running php vendor/bin/codecept run I'm getting the following error:

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)      

What am I doing wrong? What can be done to resolve this problem?

Questioner
Lev Marder
Viewed
4
Lev Marder 2020-07-10 03:13

I've changed php version to 7.4 using sphp and everything now works!