From: Joas Schilling Date: Mon, 16 Mar 2015 15:46:17 +0000 (+0100) Subject: Add tests for the navigation manager (closure) behaviour X-Git-Tag: v8.1.0alpha1~271^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ec1d5011b6056f7281c09f3d3eac19a01e5fe928;p=nextcloud-server.git Add tests for the navigation manager (closure) behaviour --- diff --git a/tests/lib/navigationmanagertest.php b/tests/lib/navigationmanagertest.php new file mode 100644 index 00000000000..96708786e39 --- /dev/null +++ b/tests/lib/navigationmanagertest.php @@ -0,0 +1,160 @@ +navigationManager = new NavigationManager(); + } + + public function addArrayData() { + return [ + [ + [ + 'id' => 'entry id', + 'name' => 'link text', + 'order' => 1, + 'icon' => 'optional', + 'href' => 'url', + ], + [ + 'id' => 'entry id', + 'name' => 'link text', + 'order' => 1, + 'icon' => 'optional', + 'href' => 'url', + 'active' => false, + ], + ], + [ + [ + 'id' => 'entry id', + 'name' => 'link text', + 'order' => 1, + //'icon' => 'optional', + 'href' => 'url', + 'active' => true, + ], + [ + 'id' => 'entry id', + 'name' => 'link text', + 'order' => 1, + 'icon' => '', + 'href' => 'url', + 'active' => false, + ], + ], + ]; + } + + /** + * @dataProvider addArrayData + * + * @param array $entry + * @param array $expectedEntry + */ + public function testAddArray(array $entry, array $expectedEntry) { + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists'); + $this->navigationManager->add($entry); + + $navigationEntries = $this->navigationManager->getAll(); + $this->assertEquals(1, sizeof($navigationEntries), 'Expected that 1 navigation entry exists'); + $this->assertEquals($expectedEntry, $navigationEntries[0]); + + $this->navigationManager->clear(); + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()'); + } + + /** + * @dataProvider addArrayData + * + * @param array $entry + * @param array $expectedEntry + */ + public function testAddClosure(array $entry, array $expectedEntry) { + global $testAddClosureNumberOfCalls; + $testAddClosureNumberOfCalls = 0; + + $this->navigationManager->add(function () use ($entry) { + global $testAddClosureNumberOfCalls; + $testAddClosureNumberOfCalls++; + + return $entry; + }); + + $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()'); + + $navigationEntries = $this->navigationManager->getAll(); + $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is called by getAll()'); + $this->assertEquals(1, sizeof($navigationEntries), 'Expected that 1 navigation entry exists'); + $this->assertEquals($expectedEntry, $navigationEntries[0]); + + $navigationEntries = $this->navigationManager->getAll(); + $this->assertEquals(1, $testAddClosureNumberOfCalls, 'Expected that the closure is only called once for getAll()'); + $this->assertEquals(1, sizeof($navigationEntries), 'Expected that 1 navigation entry exists'); + $this->assertEquals($expectedEntry, $navigationEntries[0]); + + $this->navigationManager->clear(); + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()'); + } + + public function testAddArrayClearGetAll() { + $entry = [ + 'id' => 'entry id', + 'name' => 'link text', + 'order' => 1, + 'icon' => 'optional', + 'href' => 'url', + ]; + + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists'); + $this->navigationManager->add($entry); + $this->navigationManager->clear(); + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()'); + } + + public function testAddClosureClearGetAll() { + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists'); + + $entry = [ + 'id' => 'entry id', + 'name' => 'link text', + 'order' => 1, + 'icon' => 'optional', + 'href' => 'url', + ]; + + global $testAddClosureNumberOfCalls; + $testAddClosureNumberOfCalls = 0; + + $this->navigationManager->add(function () use ($entry) { + global $testAddClosureNumberOfCalls; + $testAddClosureNumberOfCalls++; + + return $entry; + }); + + $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by add()'); + $this->navigationManager->clear(); + $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by clear()'); + $this->assertEmpty($this->navigationManager->getAll(), 'Expected no navigation entry exists after clear()'); + $this->assertEquals(0, $testAddClosureNumberOfCalls, 'Expected that the closure is not called by getAll()'); + } +}