diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-09-01 14:44:31 +0300 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-09-01 14:44:31 +0300 |
commit | bf26921a4caffff92bbc209d34cf5b452511ded8 (patch) | |
tree | 9b5ad78cc9e5ff99f0011cb5da544d42d9bd4d07 /tests | |
parent | 47b4b75c33bcac3521a34c78aa572ff0a492e811 (diff) |
Add the OCP\Notification\IApp
Diffstat (limited to 'tests')
-rw-r--r-- | tests/appinfo/AppTest.php | 58 | ||||
-rw-r--r-- | tests/bootstrap.php | 13 | ||||
-rw-r--r-- | tests/lib/AppTest.php | 78 | ||||
-rw-r--r-- | tests/lib/HandlerTest.php | 110 | ||||
-rw-r--r-- | tests/phpunit.xml | 28 | ||||
-rw-r--r-- | tests/testcase.php | 65 |
6 files changed, 352 insertions, 0 deletions
diff --git a/tests/appinfo/AppTest.php b/tests/appinfo/AppTest.php new file mode 100644 index 0000000..e484739 --- /dev/null +++ b/tests/appinfo/AppTest.php @@ -0,0 +1,58 @@ +<?php + +/** + * ownCloud - Notification + * + * @author Joas Schilling + * @copyright 2014 Joas Schilling nickvergessen@owncloud.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 library. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Notification\Tests; + +use OCA\Notifications\Tests\TestCase; + +class AppTest extends TestCase { + /** @var \OCP\Notification\IManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $manager; + + protected function setUp() { + parent::setUp(); + + $this->manager = $this->getMockBuilder('OCP\Notification\IManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->overwriteService('NotificationManager', $this->manager); + } + + protected function tearDown() { + $this->restoreService('NotificationManager'); + + parent::tearDown(); + } + + public function testRegisterApp() { + $this->manager->expects($this->once()) + ->method('registerApp') + ->willReturnCallback(function($closure) { + $this->assertInstanceOf('\Closure', $closure); + $navigation = $closure(); + $this->assertInstanceOf('\OCA\Notifications\App', $navigation); + }); + + include(__DIR__ . '/../../appinfo/app.php'); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..86bfd7d --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,13 @@ +<?php + +if (!defined('PHPUNIT_RUN')) { + define('PHPUNIT_RUN', 1); +} + +require_once __DIR__.'/../../../lib/base.php'; + +if(!class_exists('PHPUnit_Framework_TestCase')) { + require_once('PHPUnit/Autoload.php'); +} + +OC_Hook::clear(); diff --git a/tests/lib/AppTest.php b/tests/lib/AppTest.php new file mode 100644 index 0000000..7dee552 --- /dev/null +++ b/tests/lib/AppTest.php @@ -0,0 +1,78 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Notifications\Tests\Lib; + + +use OCA\Notifications\App; +use OCA\Notifications\Tests\TestCase; + +class AppTest extends TestCase { + /** @var \OCA\Notifications\Handler|\PHPUnit_Framework_MockObject_MockObject */ + protected $handler; + + /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject */ + protected $notification; + + /** @var \OCA\Notifications\App */ + protected $app; + + protected function setUp() { + parent::setUp(); + + $this->handler = $this->getMockBuilder('OCA\Notifications\Handler') + ->disableOriginalConstructor() + ->getMock(); + + $this->notification = $this->getMockBuilder('OCP\Notification\INotification') + ->disableOriginalConstructor() + ->getMock(); + + $this->app = new App( + $this->handler + ); + } + + public function testNotify() { + $this->handler->expects($this->once()) + ->method('add') + ->with($this->notification); + + $this->app->notify($this->notification); + } + + public function testGetCount() { + $this->handler->expects($this->once()) + ->method('count') + ->with($this->notification) + ->willReturn(42); + + $this->assertSame(42, $this->app->getCount($this->notification)); + } + + public function testMarkProcessed() { + $this->handler->expects($this->once()) + ->method('delete') + ->with($this->notification); + + $this->app->markProcessed($this->notification); + } +} diff --git a/tests/lib/HandlerTest.php b/tests/lib/HandlerTest.php new file mode 100644 index 0000000..4a400ef --- /dev/null +++ b/tests/lib/HandlerTest.php @@ -0,0 +1,110 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Notifications\Tests\Lib; + + +use OCA\Notifications\Handler; +use OCA\Notifications\Tests\TestCase; + +class HandlerTest extends TestCase { + /** @var \OCA\Notifications\Handler */ + protected $handler; + + protected function setUp() { + parent::setUp(); + + $this->handler = new Handler( + \OC::$server->getDatabaseConnection() + ); + } + + public function testFull() { + $notification = $this->getNotification([ + 'getApp' => 'testing_notifications', + 'getUser' => 'test_user', + 'getTimestamp' => time(), + 'getObjectType' => 'notification', + 'getObjectId' => 1337, + 'getSubject' => 'subject', + 'getSubjectParameters' => [], + 'getMessage' => 'message', + 'getMessageParameters' => [], + 'getLink' => 'link', + 'getIcon' => 'icon', + 'getActions' => [ + [ + 'getLabel' => 'action_label', + 'getIcon' => 'action_icon', + 'getLink' => 'action_link', + 'getRequestType' => 'GET', + ] + ], + ]); + + // Make sure there is no notification + $this->assertSame(0, $this->handler->count($notification)); + + // Add and count + $this->handler->add($notification); + $this->assertSame(1, $this->handler->count($notification)); + + // Delete and count again + $this->handler->delete($notification); + $this->assertSame(0, $this->handler->count($notification)); + } + + /** + * @param array $values + * @return \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getNotification(array $values = []) { + $notification = $this->getMockBuilder('OCP\Notification\INotification') + ->disableOriginalConstructor() + ->getMock(); + + foreach ($values as $method => $returnValue) { + if ($method === 'getActions') { + $actions = []; + foreach ($returnValue as $actionData) { + $action = $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(); + foreach ($actionData as $actionMethod => $actionValue) { + $action->expects($this->any()) + ->method($actionMethod) + ->willReturn($actionValue); + } + $actions[] = $action; + } + $notification->expects($this->any()) + ->method($method) + ->willReturn($actions); + } else { + $notification->expects($this->any()) + ->method($method) + ->willReturn($returnValue); + } + } + + return $notification; + } +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..400c5dc --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8" ?> +<phpunit bootstrap="bootstrap.php" + strict="true" + verbose="true" + timeoutForSmallTests="900" + timeoutForMediumTests="900" + timeoutForLargeTests="900" + > + <testsuite name='ownCloud - Notifications App Tests'> + <directory suffix='.php'>.</directory> + </testsuite> + <!-- filters for code coverage --> + <filter> + <whitelist> + <directory suffix=".php">../../notifications</directory> + <exclude> + <directory suffix=".php">../../notifications/l10n</directory> + <directory suffix=".php">../../notifications/templates</directory> + <directory suffix=".php">../../notifications/tests</directory> + </exclude> + </whitelist> + </filter> + <logging> + <!-- and this is where your report will be written --> + <log type="coverage-clover" target="./clover.xml"/> + </logging> +</phpunit> + diff --git a/tests/testcase.php b/tests/testcase.php new file mode 100644 index 0000000..7a10d8a --- /dev/null +++ b/tests/testcase.php @@ -0,0 +1,65 @@ +<?php + +/** + * ownCloud - Notifications + * + * @author Joas Schilling + * @copyright 2015 Joas Schilling nickvergessen@owncloud.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 library. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Notifications\Tests; + +abstract class TestCase extends \Test\TestCase { + /** @var array */ + protected $services = []; + + /** + * @param string $name + * @param mixed $newService + * @return bool + */ + public function overwriteService($name, $newService) { + if (isset($this->services[$name])) { + return false; + } + + $this->services[$name] = \OC::$server->query($name); + \OC::$server->registerService($name, function () use ($newService) { + return $newService; + }); + + return true; + } + + /** + * @param string $name + * @return bool + */ + public function restoreService($name) { + if (isset($this->services[$name])) { + $oldService = $this->services[$name]; + \OC::$server->registerService($name, function () use ($oldService) { + return $oldService; + }); + + + unset($this->services[$name]); + return true; + } + + return false; + } +} |