I have the following test (located in 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);
}
}
That Tests the following Class (located in 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);
}
}
And as seen throws the following Exception (located in 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.");
}
}
The files are structured like that in my project:
|- src
|-- PCMagas
|---- Exceptions
|------ FileNotFoundException.php
|---- DropboxFactory.php
|- tests
|-- DropboxFactoryTest.php
|- phpunit.xml
|- composer.json
The classes are autoloaded via a composer's psr-4 autoloader:
"autoload": {
"psr-4":{
"PCMagas\\": "src/PCMagas"
}
},
"autoload-dev": {
"psr-4": { "PCMagas\\Tests\\": "tests/" }
}
But when I run the unit test I get the following error:
ReflectionException: Class PcMagas\Exceptions\FileNotFoundException does not exist
I trite to mitigate the error via running:
composer dump-autoload -o
But the problem still remains.