Класс PhpUnit Reflection не существует, событие, хотя я правильно автозагрузка PSR-4 - PullRequest
0 голосов
/ 22 апреля 2019

У меня есть следующий тест (расположенный в tests/DropboxFactoryTest.php):

namespace PcMagas\Tests;

use PcMagas\DropBoxFactory;
use PcMagas\Exceptions\FileNotFoundException;
use phpmock\MockBuilder;
use phpmock\functions\FixedValueFunction;
use PHPUnit\Framework\TestCase;

class DropboxFactoryTest extends TestCase
{
    /**
     * Test whether The correct exception is thrown
     * when the configuration file does not exist.
     */
    public function testIfExceptionIsThrownWhenFileDoesNotExist()
    {
        //Kind Ugly But this mock is Isolated and tesat specific.
        $builder = new MockBuilder();
        $builder->setNamespace(__NAMESPACE__)
         ->setName("file_exists")
         ->setFunctionProvider(new FixedValueFunction(FALSE));

        $mockGuzzleClient = $this->getMockBuilder(\GuzzleHttp\Client::class)
            ->disableOriginalConstructor()
            ->disableOriginalClone()
            ->disableArgumentCloning()
            ->disallowMockingUnknownTypes()
            ->getMock();

        $this->expectException(FileNotFoundException::class);
        DropboxFactory::fromIniFile('example.ini', $mockGuzzleClient);
    }
}

, который тестирует следующий класс (расположенный в DropboxFactory.php):

namespace PCMagas;

use \GuzzleHttp\Client; 
use PCMagas\Exceptions\FileNotFoundException;

class DropboxFactory
{
    /**
     * @param String $path File containing the configuration
     * @param Client $client The Required Http Client
     * @return Dropbox
     * @throws Exception
     */
    public static function fromIniFile($path,Client $client)
    {
        if( !file_exists($path) ){
         throw new FileNotFoundException($path);
        }

        $iniArray = parse_ini_file($path);
        if($iniArray === FALSE){
            throw new Exception("Could not parse the Ini file", 244);
        } 

        return new Dropbox($iniArray['key'],$iniArray['secret'],$client);
    }
}

И как видновыдает следующее исключение (находится в FileNotFoundException.php):

namespace PCMagas\Exceptions;

class FileNotFoundException extends \Exception {

    /**
     * @param String $file The path that Does not exist
     */
    public function __construct($file){
      parent::__construct("The file ${file} does not exist.");
    }
}

Файлы структурированы так, как в моем проекте:

|- src
|-- PCMagas
|---- Exceptions
|------ FileNotFoundException.php
|---- DropboxFactory.php
|- tests
|-- DropboxFactoryTest.php
|- phpunit.xml
|- composer.json

Классы автоматически загружаются через psr-4 композитораавтозагрузчик:

"autoload": {
    "psr-4":{
        "PCMagas\\": "src/PCMagas"
    }
},
"autoload-dev": {
    "psr-4": { "PCMagas\\Tests\\": "tests/" }
}

Но при запуске модульного теста появляется следующая ошибка:

ReflectionException: класс PcMagas \ Exceptions \ FileNotFoundException не существует

Я банально исправляю ошибку, запустив:

composer dump-autoload -o

Но проблема все еще остается.

...