libraries = new Libraries(); // And then set all paths $this->libraries->setDirectories([3 => ['test'.DS.'libraries']]); } /** * @coversNothing */ public function testLibrariesClass() { $this->assertInstanceOf('FuzeWorks\Libraries', $this->libraries); } /* ---------------------------------- Load library from directories ------------------- */ /** * @depends testLibrariesClass * @covers ::get * @covers ::initLibrary */ public function testGetLibraryFromDirectory() { $this->assertInstanceOf('Application\Library\TestGetLibraryFromDirectory', $this->libraries->get('TestGetLibraryFromDirectory')); } /** * @depends testGetLibraryFromDirectory * @covers ::get * @covers ::initLibrary */ public function testGetLibraryFromSubdirectory() { // Add test directory path $this->libraries->addComponentPath('test'.DS.'libraries'.DS.'TestGetLibraryFromSubdirectory'); $this->assertInstanceOf('Application\Library\TestGetLibraryFromSubdirectory', $this->libraries->get('TestGetLibraryFromSubdirectory')); } /** * @depends testGetLibraryFromDirectory * @covers ::get * @covers ::initLibrary */ public function testGetLibraryFromAltDirectory() { // Simple test of loading a library and checking if it exists $this->assertInstanceOf('Application\Library\TestGetLibraryFromAltDirectory', $this->libraries->get('TestGetLibraryFromAltDirectory', [], ['test'.DS.'libraries'.DS.'TestGetLibraryFromAltDirectory'])); } /** * @depends testGetLibraryFromDirectory * @covers ::get * @covers ::initLibrary */ public function testGetLibraryFail() { $this->expectException(LibraryException::class); $this->libraries->get('FailLoadLibrary'); } /** * @depends testGetLibraryFromDirectory * @covers ::get * @covers ::initLibrary */ public function testGetLibraryNoName() { $this->expectException(LibraryException::class); $this->libraries->get(''); } /** * @depends testGetLibraryFromDirectory * @covers ::get * @covers ::initLibrary */ public function testGetLibraryNoClass() { $this->expectException(LibraryException::class); $this->libraries->get('TestGetLibraryNoClass'); } /** * @depends testGetLibraryFromDirectory * @covers ::get * @covers ::initLibrary */ public function testGetLibraryParametersFromConfig() { // Prepare the config file $libraryName = 'TestGetLibraryParametersFromConfig'; $libraryDir = 'test'.DS.'libraries'.DS.'TestGetLibraryParametersFromConfig'; Factory::getInstance()->config->getConfig(strtolower($libraryName), [$libraryDir]); // Load the library $lib = $this->libraries->get('TestGetLibraryParametersFromConfig'); $this->assertInstanceOf('Application\Library\TestGetLibraryParametersFromConfig', $lib); // And check the parameters $this->assertEquals(5, $lib->parameters['provided']); } /* ---------------------------------- Add libraries --------------------------------------------- */ /** * @covers ::addLibraryObject * @covers ::get */ public function testAddLibraryObject() { $z = new stdClass(); $this->libraries->addLibraryObject('TestAddLibraryObject', $z); $this->assertEquals($z, $this->libraries->get('TestAddLibraryObject')); } /** * @covers ::addLibraryClass * @covers ::get */ public function testAddLibraryClass() { require_once('test'.DS.'libraries'.DS.'TestAddLibraryClass'.DS.'TestAddLibraryClass.php'); $this->libraries->addLibraryClass('LibraryClass', '\Custom\Spaces\TestAddLibraryClass'); $this->assertInstanceOf('\Custom\Spaces\TestAddLibraryClass', $this->libraries->get('LibraryClass')); } /** * @depends testAddLibraryClass * @covers ::addLibraryClass */ public function testAddLibraryClassFail() { $this->expectException(LibraryException::class); $this->libraries->addLibraryClass('LibraryClassFail', '\Case\Not\Exist'); } /** * @depends testAddLibraryClass * @covers ::initLibrary */ public function testAddLibraryWithAutoloader() { // First assert the extra class can't be autoloaded $this->assertFalse(class_exists('FuzeWorks\Test\TestAddLibraryWithAutoloader\SomeExtraClass', true)); // Load the library and test the instance type $this->assertInstanceOf('Application\Library\TestAddLibraryWithAutoloader', $this->libraries->get('TestAddLibraryWithAutoloader')); // Afterwards test if the loader has been correctly added $this->assertTrue(class_exists('FuzeWorks\Test\TestAddLibraryWithAutoloader\SomeExtraClass', true)); } /** * @depends testAddLibraryWithAutoloader * @covers ::initLibrary */ public function testAddBadAutoloader() { $this->expectException(LibraryException::class); $this->assertInstanceOf('Application\Library\TestAddBadAutoloader', $this->libraries->get('TestAddBadAutoloader')); } public function tearDown(): void { parent::tearDown(); Factory::getInstance()->config->getConfig('error')->revert(); } }