From 73d196aa6c83a7fc53f0e9d4c726ab710dcc9fbe Mon Sep 17 00:00:00 2001 From: Julius Haertl Date: Wed, 18 Jan 2017 14:18:29 +0100 Subject: [PATCH] Test controllers --- lib/Controller/CardController.php | 7 +- tests/unit/controller/CardControllerTest.php | 108 ++++++++++++++++++ tests/unit/controller/LabelControllerTest.php | 84 ++++++++++++++ tests/unit/controller/StackControllerTest.php | 99 ++++++++++++++++ 4 files changed, 295 insertions(+), 3 deletions(-) create mode 100644 tests/unit/controller/CardControllerTest.php create mode 100644 tests/unit/controller/LabelControllerTest.php create mode 100644 tests/unit/controller/StackControllerTest.php diff --git a/lib/Controller/CardController.php b/lib/Controller/CardController.php index a95f5f6a0..90d5d066c 100644 --- a/lib/Controller/CardController.php +++ b/lib/Controller/CardController.php @@ -24,13 +24,14 @@ namespace OCA\Deck\Controller; use OCA\Deck\Service\CardService; - use OCP\IRequest; use OCP\AppFramework\Controller; class CardController extends Controller { + private $userId; private $cardService; + public function __construct($appName, IRequest $request, CardService $cardService, $userId){ parent::__construct($appName, $request); $this->userId = $userId; @@ -126,7 +127,7 @@ class CardController extends Controller { * @param $labelId */ public function assignLabel($cardId, $labelId) { - return $this->cardService->assignLabel($cardId, $labelId); + $this->cardService->assignLabel($cardId, $labelId); } /** @@ -135,7 +136,7 @@ class CardController extends Controller { * @param $labelId */ public function removeLabel($cardId, $labelId) { - return $this->cardService->removeLabel($cardId, $labelId); + $this->cardService->removeLabel($cardId, $labelId); } } diff --git a/tests/unit/controller/CardControllerTest.php b/tests/unit/controller/CardControllerTest.php new file mode 100644 index 000000000..1cbae245d --- /dev/null +++ b/tests/unit/controller/CardControllerTest.php @@ -0,0 +1,108 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Controller; + +use OCA\Deck\Db\Acl; +use OCA\Deck\Service\CardService; +use OCP\AppFramework\Controller; +use OCP\IRequest; + +class CardControllerTest extends \PHPUnit_Framework_TestCase { + + /** @var Controller|\PHPUnit_Framework_MockObject_MockObject */ + private $controller; + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + private $request; + /** @var CardService|\PHPUnit_Framework_MockObject_MockObject */ + private $cardService; + /** @var string */ + private $userId = 'user'; + + public function setUp() { + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->cardService = $this->getMockBuilder( + '\OCA\Deck\Service\CardService') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new CardController( + 'deck', + $this->request, + $this->cardService, + $this->userId + ); + } + + public function testRead() { + $this->cardService->expects($this->once()) + ->method('find') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->read(123)); + } + + public function testCreate() { + $this->cardService->expects($this->once()) + ->method('create') + ->with('foo', 1, 'text', 3, $this->userId) + ->willReturn(1); + $this->assertEquals(1, $this->controller->create('foo', 1, 'text', 3)); + } + + public function testUpdate() { + $this->cardService->expects($this->once()) + ->method('update') + ->with(1, 'title', 3, 'text', 5, 'foo', $this->userId) + ->willReturn(1); + $this->assertEquals(1, $this->controller->update(1, 'title', 3, 'text', 5, 'foo')); + } + + public function testDelete() { + $this->cardService->expects($this->once()) + ->method('delete') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->delete(123)); + } + + public function testArchive() { + $this->cardService->expects($this->once())->method('archive'); + $this->cardService->archive(1); + } + public function testUnarchive() { + $this->cardService->expects($this->once())->method('unarchive'); + $this->cardService->unarchive(1); + } + public function testAssignLabel() { + $this->cardService->expects($this->once())->method('assignLabel'); + $this->cardService->assignLabel(1,2); + } + public function testRemoveLabel() { + $this->cardService->expects($this->once())->method('removeLabel'); + $this->cardService->removeLabel(1,2); + } + +} diff --git a/tests/unit/controller/LabelControllerTest.php b/tests/unit/controller/LabelControllerTest.php new file mode 100644 index 000000000..ac97e9b3d --- /dev/null +++ b/tests/unit/controller/LabelControllerTest.php @@ -0,0 +1,84 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Controller; + +use OCA\Deck\Db\Acl; +use OCA\Deck\Service\CardService; +use OCA\Deck\Service\LabelService; +use OCP\AppFramework\Controller; +use OCP\IRequest; + +class LabelControllerTest extends \PHPUnit_Framework_TestCase { + + /** @var Controller|\PHPUnit_Framework_MockObject_MockObject */ + private $controller; + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + private $request; + /** @var LabelService|\PHPUnit_Framework_MockObject_MockObject */ + private $labelService; + /** @var string */ + private $userId = 'user'; + + public function setUp() { + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->labelService = $this->getMockBuilder( + '\OCA\Deck\Service\LabelService') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new LabelController( + 'deck', + $this->request, + $this->labelService + ); + } + + + public function testCreate() { + $this->labelService->expects($this->once()) + ->method('create') + ->with(1, 2, 3) + ->willReturn(1); + $this->assertEquals(1, $this->controller->create(1, 2, 3)); + } + + public function testUpdate() { + $this->labelService->expects($this->once()) + ->method('update') + ->with(1, 2, 3) + ->willReturn(1); + $this->assertEquals(1, $this->controller->update(1, 2, 3)); + } + + public function testDelete() { + $this->labelService->expects($this->once()) + ->method('delete') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->delete(123)); + } + +} diff --git a/tests/unit/controller/StackControllerTest.php b/tests/unit/controller/StackControllerTest.php new file mode 100644 index 000000000..b10e7f02b --- /dev/null +++ b/tests/unit/controller/StackControllerTest.php @@ -0,0 +1,99 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Deck\Controller; + +use OCA\Deck\Db\Acl; +use OCA\Deck\Service\CardService; +use OCA\Deck\Service\LabelService; +use OCA\Deck\Service\StackService; +use OCP\AppFramework\Controller; +use OCP\IRequest; + +class StackControllerTest extends \PHPUnit_Framework_TestCase { + + /** @var Controller|\PHPUnit_Framework_MockObject_MockObject */ + private $controller; + /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */ + private $request; + /** @var StackService|\PHPUnit_Framework_MockObject_MockObject */ + private $stackService; + /** @var string */ + private $userId = 'user'; + + public function setUp() { + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->stackService = $this->getMockBuilder( + '\OCA\Deck\Service\StackService') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new StackController( + 'deck', + $this->request, + $this->stackService, + $this->userId + ); + } + + public function testIndex() { + $this->stackService->expects($this->once())->method('findAll'); + $this->controller->index(1); + } + + public function testArchived() { + $this->stackService->expects($this->once())->method('findAllArchived'); + $this->controller->archived(1); + } + + public function testRead() { + $this->stackService->expects($this->once())->method('find'); + $this->controller->read(1); + } + public function testCreate() { + $this->stackService->expects($this->once()) + ->method('create') + ->with(1, 2, 3) + ->willReturn(1); + $this->assertEquals(1, $this->controller->create(1, 2, 3)); + } + + public function testUpdate() { + $this->stackService->expects($this->once()) + ->method('update') + ->with(1, 2, 3, 4) + ->willReturn(1); + $this->assertEquals(1, $this->controller->update(1, 2, 3, 4)); + } + + public function testDelete() { + $this->stackService->expects($this->once()) + ->method('delete') + ->with(123) + ->willReturn(1); + $this->assertEquals(1, $this->controller->delete(123)); + } + +}