phpphpspec

Why phpspec can not find classes?


I use PHPSPEC 3.4.0.When I run phpsec with

vendor/bin/phpspec run

I get this error:

class Eastsea\Service\FileStorage\DuplicateFileStorage does not exist.

Here is my composer.json file about autoload section:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "psr-0": {
        "EastSea\\": "src/"
    }
}

And here is my folder tree:

    ./src
`-- EastSea
    `-- Service
        `-- FileStorage
            |-- DuplicateFile.php
            |-- DuplicateFileStorage.php
            `-- Result.php

Classes:

<?php

namespace EastSea\Service\FileStorage;

class DuplicateFileStorage
{
    public function validate()
    {
        // TODO: write logic here
    }

    public function storage()
    {
        // TODO: write logic here
    }

    public function handle(DuplicateFile $file)
    {
        $file->hash();
    }
}

Spec:

<?php

namespace spec\Eastsea\Service\FileStorage;

use \EastSea\Service\FileStorage\DuplicateFileStorage;
use \EastSea\Service\FileStorage\Result;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class DuplicateFileStorageSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType(DuplicateFileStorage::class);
    }

}

Solution

  • Try with this:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "": "src/"
        }
    }
    

    And obviously run composer dump-autoload after your composer.json's update.