views = new Views(); $this->views->addComponentPath(dirname(__DIR__) . DS . 'views'); $this->mockController = $this->getMockBuilder(Controller::class)->getMock(); } /** * @covers ::get * @covers ::loadView */ public function testGetViewFromClass() { // Create mock view $mockView = $this->getMockBuilder(View::class)->getMock(); $mockViewClass = get_class($mockView); class_alias($mockViewClass, $mockViewClass . 'StandardView'); // Try and fetch this view from the Views class $this->assertInstanceOf($mockViewClass, $this->views->get($mockViewClass, $this->mockController, 'Standard', [], '\\')); } /** * 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(); } /** * @depends testGetViewFromClass * @covers ::get * @covers ::loadView */ public function testGetViewFromClassInvalidInstance() { // Create invalid mock $mockFakeView = $this->getMockBuilder(stdClass::class)->getMock(); $mockFakeViewClass = get_class($mockFakeView); class_alias($mockFakeViewClass, $mockFakeViewClass . 'StandardView'); // Try and fetch $this->expectException(ViewException::class); $this->views->get($mockFakeViewClass, $this->mockController, 'Standard', [], '\\'); } /** * @depends testGetViewFromClass * @covers ::get * @covers ::loadView */ public function testGetViewFromClassDefaultNamespace() { // Create mock view $mockView = $this->getMockBuilder(View::class)->getMock(); $mockViewClass = get_class($mockView); class_alias($mockViewClass, '\Application\View\DefaultNamespaceStandardView'); // Try and fetch $this->assertInstanceOf('\Application\View\DefaultNamespaceStandardView', $this->views->get('DefaultNamespace', $this->mockController)); } /** * @depends testGetViewFromClass * @covers ::get * @covers ::loadView * @todo Implement. Mock constructor arguments doesn't work yet */ public function testGetViewWithArguments() { // Can't be tested right now $this->assertTrue(true); } /** * @covers ::get */ public function testGetViewInvalidName() { $this->expectException(ViewException::class); $this->views->get('', $this->mockController, 'Standard', [], '\\'); } /** * @depends testGetViewFromClass * @covers ::get * @covers ::loadView */ public function testGetViewFromFile() { $this->assertInstanceOf('\Application\View\TestGetViewStandardView', $this->views->get('TestGetView', $this->mockController)); } /** * @depends testGetViewFromFile * @covers ::get * @covers ::loadView */ public function testGetViewFromFileInvalidInstance() { $this->expectException(ViewException::class); $this->views->get('ViewInvalidInstance', $this->mockController); } /** * @depends testGetViewFromFile * @covers ::get * @covers ::loadView */ public function testDifferentComponentPathPriority() { // Add the directories for this test $this->views->addComponentPath(dirname(__DIR__) . DS . 'views'.DS.'TestDifferentComponentPathPriority'.DS.'Lowest', Priority::LOWEST); $this->views->addComponentPath(dirname(__DIR__) . DS . 'views'.DS.'TestDifferentComponentPathPriority'.DS.'Highest', Priority::HIGHEST); // Load the view and assert it is the correct type $view = $this->views->get('TestDifferentComponentPathPriority', $this->mockController); $this->assertInstanceOf('\Application\View\TestDifferentComponentPathPriorityStandardView', $view); $this->assertEquals('highest', $view->type); // Clean up the test $this->views->setDirectories([]); } /** * @depends testGetViewFromFile * @covers ::get * @covers ::loadView */ public function testGetSubdirectory() { $this->assertInstanceOf('\Application\View\TestGetSubdirectoryStandardView', $this->views->get('TestGetSubdirectory', $this->mockController)); } /** * @depends testGetViewFromFile * @covers ::get * @covers ::loadView */ public function testViewNotFound() { $this->expectException(NotFoundException::class); $this->views->get('NotFound', $this->mockController); } /** * @depends testGetViewFromClass * @covers ::get * @covers \FuzeWorks\Event\ViewGetEvent::init */ public function testViewGetEvent() { // Register listener Events::addListener(function($event){ /** @var ViewGetEvent $event */ $this->assertInstanceOf('\FuzeWorks\Event\ViewGetEvent', $event); $this->assertEquals('SomeViewName', $event->viewName); $this->assertInstanceOf('\FuzeWorks\Controller', $event->controller); $this->assertEquals('Other', $event->viewType); $this->assertEquals([3 => ['some_path']], $event->viewPaths); $this->assertEquals('SomeNamespace', $event->namespace); $this->assertEquals(['Some Argument'], $event->arguments); $event->setCancelled(true); }, 'viewGetEvent', Priority::NORMAL); $this->expectException(ViewException::class); $this->views->get('SomeViewName', $this->mockController, 'Other', ['some_path'], 'SomeNamespace', 'Some Argument'); } /** * @depends testViewGetEvent * @covers ::get */ public function testCancelGetView() { // Register listener Events::addListener(function($event){ $event->setCancelled(true); }, 'viewGetEvent', Priority::NORMAL); $this->expectException(ViewException::class); $this->views->get('SomeView', $this->mockController, 'Standard', [], '\\'); } /** * @depends testViewGetEvent * @covers ::get * @covers ::loadView */ public function testViewGetEventIntervene() { // Register listener Events::addListener(function($event){ /** @var ViewGetEvent $event */ $event->viewName = 'TestViewGetEventIntervene'; $event->viewType = 'OtherType'; $event->namespace = '\Some\Other\\'; }, 'viewGetEvent', Priority::NORMAL); $this->assertInstanceOf('\Some\Other\TestViewGetEventInterveneOtherTypeView', $this->views->get('Something_Useless', $this->mockController)); } }