diff options
author | Joas Schilling <coding@schilljs.com> | 2016-09-22 01:03:42 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2016-09-22 01:03:42 +0300 |
commit | b6df50eada48421ea415b0964981c473567d0ff9 (patch) | |
tree | 0b1f1518c13f52703261a1f828472893a9d1b91c /tests | |
parent | 1b98a543a128d885ed5afa9663243a60f56ec6ff (diff) |
Add unit tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/AppInfo/ApplicationTest.php | 85 | ||||
-rw-r--r-- | tests/AppInfo/RoutesTest.php | 45 | ||||
-rw-r--r-- | tests/Controller/WizardControllerTest.php | 127 | ||||
-rw-r--r-- | tests/Notification/BackgroundJobTest.php | 119 | ||||
-rw-r--r-- | tests/bootstrap.php | 16 | ||||
-rw-r--r-- | tests/phpunit.xml | 10 |
6 files changed, 391 insertions, 11 deletions
diff --git a/tests/AppInfo/ApplicationTest.php b/tests/AppInfo/ApplicationTest.php new file mode 100644 index 00000000..ace4d333 --- /dev/null +++ b/tests/AppInfo/ApplicationTest.php @@ -0,0 +1,85 @@ +<?php +/** + * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\FirstRunWizard\Tests\AppInfo; + + +use OCA\FirstRunWizard\AppInfo\Application; +use OCA\FirstRunWizard\Controller\WizardController; +use OCA\FirstRunWizard\Notification\BackgroundJob; +use OCA\FirstRunWizard\Notification\Notifier; +use OCP\AppFramework\App; +use OCP\AppFramework\Controller; +use OCP\BackgroundJob\IJob; +use OCP\Notification\INotifier; +use Test\TestCase; + +/** + * Class ApplicationTest + * + * @package OCA\FirstRunWizard\Tests\AppInfo + * @group DB + */ +class ApplicationTest extends TestCase { + /** @var \OCA\FirstRunWizard\AppInfo\Application */ + protected $app; + + /** @var \OCP\AppFramework\IAppContainer */ + protected $container; + + protected function setUp() { + parent::setUp(); + $this->app = new Application(); + $this->container = $this->app->getContainer(); + } + + public function testContainerAppName() { + $this->app = new Application(); + $this->assertEquals('firstrunwizard', $this->container->getAppName()); + } + + public function dataContainerQuery() { + return [ + [Application::class, Application::class], + [Application::class, App::class], + + [WizardController::class, WizardController::class], + [WizardController::class, Controller::class], + + [Notifier::class, Notifier::class], + [Notifier::class, INotifier::class], + + [BackgroundJob::class, BackgroundJob::class], + [BackgroundJob::class, IJob::class], + ]; + } + + /** + * @dataProvider dataContainerQuery + * @param string $service + * @param string $expected + */ + public function testContainerQuery($service, $expected) { + $this->assertTrue($this->container->query($service) instanceof $expected); + } +} diff --git a/tests/AppInfo/RoutesTest.php b/tests/AppInfo/RoutesTest.php new file mode 100644 index 00000000..4d6633c7 --- /dev/null +++ b/tests/AppInfo/RoutesTest.php @@ -0,0 +1,45 @@ +<?php +/** + * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\FirstRunWizard\Tests\AppInfo; + +use Test\TestCase; + +/** + * Class RoutesTest + * + * @package OCA\FirstRunWizard\Tests\AppInfo + */ +class RoutesTest extends TestCase { + public function testRoutes() { + $routes = include(__DIR__ . '/../../appinfo/routes.php'); + $this->assertInternalType('array', $routes); + $this->assertCount(1, $routes); + $this->assertArrayHasKey('routes', $routes); + $this->assertInternalType('array', $routes['routes']); + $this->assertSame([ + ['name' => 'Wizard#show', 'url' => '/wizard', 'verb' => 'GET'], + ['name' => 'Wizard#disable', 'url' => '/wizard', 'verb' => 'DELETE'], + ], $routes['routes']); + } +} diff --git a/tests/Controller/WizardControllerTest.php b/tests/Controller/WizardControllerTest.php new file mode 100644 index 00000000..264dad4a --- /dev/null +++ b/tests/Controller/WizardControllerTest.php @@ -0,0 +1,127 @@ +<?php +/** + * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\FirstRunWizard\Tests\Controller; + + +use OCA\FirstRunWizard\AppInfo\Application; +use OCA\FirstRunWizard\Controller\WizardController; +use OCA\FirstRunWizard\Notification\BackgroundJob; +use OCA\FirstRunWizard\Notification\Notifier; +use OCP\AppFramework\App; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\BackgroundJob\IJob; +use OCP\IConfig; +use OCP\IRequest; +use OCP\Notification\INotifier; +use Test\TestCase; + +/** + * Class WizardControllerTest + * + * @package OCA\FirstRunWizard\Tests\Controller + * @group DB + */ +class WizardControllerTest extends TestCase { + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + protected function setUp() { + parent::setUp(); + $this->config = $this->createMock(IConfig::class); + } + + /** + * @param string $user + * @return WizardController + */ + protected function getController($user = 'test') { + return new WizardController( + 'firstrunwizard', + $this->createMock(IRequest::class), + $this->config, + $user + ); + } + + public function dataDisable() { + return [ + ['test1'], + ['test2'], + ]; + } + + /** + * @dataProvider dataDisable + * @param string $user + */ + public function testDisable($user) { + $controller = $this->getController($user); + + $this->config->expects($this->once()) + ->method('setUserValue') + ->with($user, 'firstrunwizard', 'show', 0); + + $response = $controller->disable(); + + $this->assertInstanceOf(DataResponse::class, $response); + $this->assertSame(Http::STATUS_OK, $response->getStatus()); + } + + public function dataShow() { + return [ + ['desktop1', 'android1', 'ios1'], + ['desktop2', 'android2', 'ios2'], + ]; + } + + /** + * @dataProvider dataShow + * @param string $desktopUrl + * @param string $androidUrl + * @param string $iosUrl + */ + public function testShow($desktopUrl, $androidUrl, $iosUrl) { + $controller = $this->getController(); + + $this->config->expects($this->exactly(3)) + ->method('getSystemValue') + ->willReturnMap([ + ['customclient_desktop', 'https://nextcloud.com/install', $desktopUrl], + ['customclient_android', 'https://play.google.com/store/apps/details?id=com.nextcloud.client', $androidUrl], + ['customclient_ios', 'https://itunes.apple.com/us/app/nextcloud/id1125420102?mt=8', $iosUrl], + ]); + + $response = $controller->show(); + + $this->assertInstanceOf(Http\TemplateResponse::class, $response); + $this->assertSame(Http::STATUS_OK, $response->getStatus()); + $this->assertSame([ + 'desktop' => $desktopUrl, + 'android' => $androidUrl, + 'ios' => $iosUrl, + ], $response->getParams()); + } +} diff --git a/tests/Notification/BackgroundJobTest.php b/tests/Notification/BackgroundJobTest.php new file mode 100644 index 00000000..4663a6d2 --- /dev/null +++ b/tests/Notification/BackgroundJobTest.php @@ -0,0 +1,119 @@ +<?php +/** + * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\FirstRunWizard\Tests\Notification; + + +use OCA\FirstRunWizard\Controller\WizardController; +use OCA\FirstRunWizard\Notification\BackgroundJob; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; +use OCP\IURLGenerator; +use OCP\Notification\IManager; +use OCP\Notification\INotification; +use Test\TestCase; + +/** + * Class BackgroundJobTest + * + * @package OCA\FirstRunWizard\Tests\Notification + * @group DB + */ +class BackgroundJobTest extends TestCase { + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $notificationManager; + + /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ + protected $urlGenerator; + + /** @var BackgroundJob */ + protected $job; + + protected function setUp() { + parent::setUp(); + $this->notificationManager = $this->createMock(IManager::class); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->job = new BackgroundJob( + $this->notificationManager, + $this->urlGenerator + ); + } + + public function dataRun() { + return [ + ['user1', 1, false], + ['user2', 0, true], + ]; + } + + /** + * @dataProvider dataRun + * @param string $user + * @param int $count + * @param bool $notify + */ + public function testRun($user, $count, $notify) { + + $notification = $this->createMock(INotification::class); + $notification->expects($this->once()) + ->method('setApp') + ->with('firstrunwizard') + ->willReturnSelf(); + $notification->expects($this->once()) + ->method('setSubject') + ->with('profile') + ->willReturnSelf(); + $notification->expects($this->once()) + ->method('setObject') + ->with('user', $user) + ->willReturnSelf(); + $notification->expects($this->once()) + ->method('setUser') + ->with($user) + ->willReturnSelf(); + + if ($notify) { + $notification->expects($this->once()) + ->method('setDateTime') + ->willReturnSelf(); + $notification->expects($this->once()) + ->method('setLink') + ->willReturnSelf(); + } else { + $notification->expects($this->never()) + ->method('setDateTime'); + $notification->expects($this->never()) + ->method('setLink'); + } + + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + $this->notificationManager->expects($this->once()) + ->method('getCount') + ->willReturn($count); + + $this->invokePrivate($this->job, 'run', [['uid' => $user]]); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6c881956..5b2ac441 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,8 +1,8 @@ <?php /** - + * @copyright Copyright (c) 2016, Joas Schilling <coding@schilljs.com> * - * @author Georg Ehrke <georg@owncloud.com> + * @author Joas Schilling <coding@schilljs.com> * * @license GNU AGPL version 3 or any later version * @@ -20,13 +20,19 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ -global $RUNTIME_NOAPPS; -$RUNTIME_NOAPPS = true; -define('PHPUNIT_RUN', 1); +if (!defined('PHPUNIT_RUN')) { + define('PHPUNIT_RUN', 1); +} require_once __DIR__.'/../../../lib/base.php'; +// Fix for "Autoload path not allowed: .../tests/lib/testcase.php" +\OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); + +// Fix for "Autoload path not allowed: .../firstrunwizard/*.php" +\OC_App::loadApp('firstrunwizard'); + if(!class_exists('PHPUnit_Framework_TestCase')) { require_once('PHPUnit/Autoload.php'); } diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 3e66f993..9c630723 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -6,16 +6,14 @@ timeoutForLargeTests="900" > <testsuite name='FirstRunWizard App Tests'> - <directory suffix='test.php'>.</directory> + <directory suffix='Test.php'>.</directory> </testsuite> <!-- filters for code coverage --> <filter> <whitelist> - <directory suffix=".php">../firstrunwizard</directory> - <exclude> - <directory suffix=".php">../firstrunwizard/l10n</directory> - <directory suffix=".php">../firstrunwizard/tests</directory> - </exclude> + <directory suffix=".php">../../firstrunwizard/appinfo</directory> + <directory suffix=".php">../../firstrunwizard/lib</directory> + <directory suffix=".php">../../firstrunwizard/template</directory> </whitelist> </filter> <logging> |