View = new View(); } /** * tearDown method */ public function tearDown(): void { parent::tearDown(); Configure::delete('Asset'); $this->clearPlugins(); unset($this->View); } /** * Test settings merging */ public function testSettingsMerging(): void { $Helper = new TestHelper($this->View, [ 'key3' => 'val3', 'key2' => ['key2.2' => 'newval'], ]); $expected = [ 'key1' => 'val1', 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'newval'], 'key3' => 'val3', ]; $this->assertEquals($expected, $Helper->getConfig()); } /** * test lazy loading helpers is seamless */ public function testLazyLoadingHelpers(): void { $this->loadPlugins(['TestPlugin']); $Helper = new TestHelper($this->View); $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $Helper->OtherHelper); $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $Helper->Html); } /** * test that a helpers Helper is not 'attached' to the collection */ public function testThatHelperHelpersAreNotAttached(): void { $events = $this->getMockBuilder('Cake\Event\EventManager')->getMock(); $this->View->setEventManager($events); $events->expects($this->never()) ->method('on'); $Helper = new TestHelper($this->View); $Helper->OtherHelper; } /** * test that the lazy loader doesn't duplicate objects on each access. */ public function testLazyLoadingUsesReferences(): void { $Helper = new TestHelper($this->View); $resultA = $Helper->Html; $resultB = $Helper->Html; $resultA->testprop = 1; $this->assertSame($resultA->testprop, $resultB->testprop); } /** * test getting view instance */ public function testGetView(): void { $Helper = new TestHelper($this->View); $this->assertSame($this->View, $Helper->getView()); } /** * Tests __debugInfo */ public function testDebugInfo(): void { $Helper = new TestHelper($this->View); $expected = [ 'helpers' => [ 'Html', 'TestPlugin.OtherHelper', ], 'implementedEvents' => [ ], '_config' => [ 'key1' => 'val1', 'key2' => ['key2.1' => 'val2.1', 'key2.2' => 'val2.2'], ], ]; $result = $Helper->__debugInfo(); $this->assertEquals($expected, $result); } /** * Test addClass() with 'class' => array */ public function testAddClassArray(): void { $helper = new TestHelper($this->View); $input = ['class' => ['element1', 'element2']]; $expected = ['class' => [ 'element1', 'element2', 'element3', ]]; $this->assertEquals($expected, $helper->addClass($input, 'element3')); } /** * Test addClass() with 'class' => string */ public function testAddClassString(): void { $helper = new TestHelper($this->View); $input = ['class' => 'element1 element2']; $expected = ['class' => 'element1 element2 element3']; $this->assertEquals($expected, $helper->addClass($input, 'element3')); } /** * Test addClass() with no class element */ public function testAddClassEmpty(): void { $helper = new TestHelper($this->View); $input = []; $expected = ['class' => 'element3']; $this->assertEquals($expected, $helper->addClass($input, 'element3')); } }