models = new Models(); $this->models->addComponentPath(dirname(__DIR__) . DS . 'models'); } /** * Remove all listeners before the next test starts. */ public function tearDown(): void { // Clear all events created by tests Events::$listeners = array(); // Reset all config files Factory::getInstance()->config->discardConfigFiles(); } /** * @covers ::get * @covers ::loadModel */ public function testGetModelFromClass() { // Create mock model $mockModel = $this->getMockBuilder(Model::class)->getMock(); $mockModelClass = get_class($mockModel); class_alias($mockModelClass, $mockModelClass . 'Model'); // Try and fetch this model from the Models class $this->assertInstanceOf($mockModelClass, $this->models->get($mockModelClass, [], '\\')); } /** * @depends testGetModelFromClass * @covers ::get * @covers ::loadModel */ public function testGetModelFromClassInvalidInstance() { // Create invalid mock $mockFakeModel = $this->getMockBuilder(stdClass::class)->getMock(); $mockFakeModelClass = get_class($mockFakeModel); class_alias($mockFakeModelClass, $mockFakeModelClass . 'Model'); // Try and fetch $this->expectException(ModelException::class); $this->models->get($mockFakeModelClass, [], '\\'); } /** * @depends testGetModelFromClass * @covers ::get * @covers ::loadModel */ public function testGetModelFromClassDefaultNamespace() { // Create mock model $mockModel = $this->getMockBuilder(Model::class)->getMock(); $mockModelClass = get_class($mockModel); class_alias($mockModelClass, '\Application\Model\DefaultNamespaceModel'); // Try and fetch $this->assertInstanceOf('\Application\Model\DefaultNamespaceModel', $this->models->get('DefaultNamespace')); } /** * @depends testGetModelFromClass * @covers ::get * @covers ::loadModel * @todo Implement. Mock constructor arguments doesn't work yet */ public function testGetModelWithArguments() { // Can't be tested right now $this->assertTrue(true); } /** * @covers ::get */ public function testGetModelInvalidName() { $this->expectException(ModelException::class); $this->models->get('', [], '\\'); } /** * @depends testGetModelFromClass * @covers ::get * @covers ::loadModel */ public function testGetModelFromFile() { $this->assertInstanceOf('\Application\Model\TestGetModelModel', $this->models->get('TestGetModel')); } /** * @depends testGetModelFromFile * @covers ::get * @covers ::loadModel */ public function testGetModelFromFileInvalidInstance() { $this->expectException(ModelException::class); $this->models->get('ModelInvalidInstance'); } /** * @depends testGetModelFromFile * @covers ::get * @covers ::loadModel */ public function testDifferentComponentPathPriority() { // Add the directories for this test $this->models->addComponentPath(dirname(__DIR__) . DS . 'models'.DS.'TestDifferentComponentPathPriority'.DS.'Lowest', Priority::LOWEST); $this->models->addComponentPath(dirname(__DIR__) . DS . 'models'.DS.'TestDifferentComponentPathPriority'.DS.'Highest', Priority::HIGHEST); // Load the model and assert it is the correct type $model = $this->models->get('TestDifferentComponentPathPriority'); $this->assertInstanceOf('\Application\Model\TestDifferentComponentPathPriorityModel', $model); $this->assertEquals('highest', $model->type); // Clean up the test $this->models->setDirectories([]); } /** * @depends testGetModelFromFile * @covers ::get * @covers ::loadModel */ public function testGetSubdirectory() { $this->assertInstanceOf('\Application\Model\TestGetSubdirectoryModel', $this->models->get('TestGetSubdirectory')); } /** * @depends testGetModelFromFile * @covers ::get * @covers ::loadModel */ public function testModelNotFound() { $this->expectException(NotFoundException::class); $this->models->get('NotFound'); } /** * @depends testGetModelFromClass * @covers ::get * @covers \FuzeWorks\Event\ModelGetEvent::init */ public function testModelGetEvent() { // Register listener Events::addListener(function($event){ /** @var ModelGetEvent $event */ $this->assertInstanceOf('\FuzeWorks\Event\ModelGetEvent', $event); $this->assertEquals('SomeModelName', $event->modelName); $this->assertEquals([3 => ['some_path']], $event->modelPaths); $this->assertEquals('SomeNamespace', $event->namespace); $this->assertEquals(['Some Argument'], $event->arguments); $event->setCancelled(true); }, 'modelGetEvent', Priority::NORMAL); $this->expectException(ModelException::class); $this->models->get('SomeModelName', ['some_path'], 'SomeNamespace', 'Some Argument'); } /** * @depends testModelGetEvent * @covers ::get */ public function testCancelGetModel() { // Register listener Events::addListener(function($event){ $event->setCancelled(true); }, 'modelGetEvent', Priority::NORMAL); $this->expectException(ModelException::class); $this->models->get('SomeModel', [], '\\'); } /** * @depends testModelGetEvent * @covers ::get * @covers ::loadModel */ public function testModelGetEventIntervene() { // Register listener Events::addListener(function($event){ /** @var ModelGetEvent $event */ $event->modelName = 'TestModelGetEventIntervene'; $event->namespace = '\Some\Other\\'; }, 'modelGetEvent', Priority::NORMAL); $this->assertInstanceOf('\Some\Other\TestModelGetEventInterveneModel', $this->models->get('Something_Useless')); } }