diff options
author | Joas Schilling <coding@schilljs.com> | 2017-02-10 18:21:54 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2017-02-10 18:57:28 +0300 |
commit | a2c08bea51a8a4bd2881282db641054e563256c4 (patch) | |
tree | a1e323370183b59f7329a632e18739b20e508605 | |
parent | 2c7a85c3dd628e19c6892ce6e58ab1a35e87a601 (diff) |
Start unit tests
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | .travis.yml | 66 | ||||
-rw-r--r-- | tests/AppInfo/AppTest.php | 94 | ||||
-rw-r--r-- | tests/AppInfo/ApplicationTest.php | 82 | ||||
-rw-r--r-- | tests/AppInfo/RoutesTest.php | 37 | ||||
-rw-r--r-- | tests/bootstrap.php | 40 | ||||
-rw-r--r-- | tests/phpunit.xml | 24 |
6 files changed, 343 insertions, 0 deletions
diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a655392 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,66 @@ +language: php +php: + - 5.6 + - 7.0 + - 7.1 + +env: + global: + - CORE_BRANCH=master + - APP_NAME=admin_notifications + matrix: + - DB=sqlite + +branches: + only: + - master + - /^stable\d+(\.\d+)?$/ + - /^prep-release-\d+(\.\d+)?$/ + +before_install: + - wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh + - bash ./before_install.sh $APP_NAME $CORE_BRANCH $DB + + # Add some output debugging information + - cd ../server + - ./occ check + - ./occ status + - ./occ app:list + - ./occ app:enable $APP_NAME + - ./occ app:list + +script: + - cd apps/$APP_NAME/ + + # Test the app + - sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' = '1' ]; then find . -name \*.php -exec php -l \"{}\" \;; fi" + - cd ../../ + - sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' = '1' ]; then ./occ app:check-code $APP_NAME -c private -c strong-comparison; fi" + - sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' = '2' ]; then ./occ app:check-code $APP_NAME -c deprecation; fi" + - cd apps/$APP_NAME/ + + # Run phpunit tests + - cd tests/ + - sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' != '1' -a '$CODECHECK' != '2' ]; then phpunit --configuration phpunit.xml; fi" + - cd ../ + +after_success: + # Create coverage report + - cd tests/ + - sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' != '1' -a '$CODECHECK' != '2' ]; then wget https://codecov.io/bash -O codecov.sh; fi" + - sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' != '1' -a '$CODECHECK' != '2' ]; then bash codecov.sh; fi" + - cd ../ + +matrix: + include: + - php: 7.0 + env: DB=mysql + - php: 7.0 + env: DB=pgsql + - php: 7.0 + env: DB=mysql;CODECHECK=1 + - php: 7.0 + env: DB=mysql;CODECHECK=2 + allow_failures: + - env: DB=mysql;CODECHECK=2 + fast_finish: true diff --git a/tests/AppInfo/AppTest.php b/tests/AppInfo/AppTest.php new file mode 100644 index 0000000..9106661 --- /dev/null +++ b/tests/AppInfo/AppTest.php @@ -0,0 +1,94 @@ +<?php +/** + * @copyright Copyright (c) 2017 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\AdminNotifications\Tests\AppInfo; + +use OCA\AdminNotifications\AppInfo\Application; +use OCA\AdminNotifications\Notification\Notifier; +use OCP\IL10N; +use OCP\L10N\IFactory; +use OCP\Notification\IManager; +use Test\TestCase; + +/** + * Class AppTest + * + * @package OCA\AdminNotifications\Tests + * @group DB + */ +class AppTest extends TestCase { + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ + protected $language; + /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */ + protected $languageFactory; + /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + protected $notificationManager; + + protected function setUp() { + parent::setUp(); + + $this->languageFactory = $this->createMock(IFactory::class); + $this->notificationManager = $this->createMock(IManager::class); + $this->language = $this->createMock(IL10N::class); + $this->language->expects($this->any()) + ->method('t') + ->willReturnCallback(function($string, $args) { + return vsprintf($string, $args); + }); + + $this->overwriteService('NotificationManager', $this->notificationManager); + $this->overwriteService('L10NFactory', $this->languageFactory); + } + + protected function tearDown() { + $this->restoreService('L10NFactory'); + $this->restoreService('NotificationManager'); + + parent::tearDown(); + } + + public function testAppNotification() { + $this->languageFactory->expects($this->once()) + ->method('get') + ->with(Application::APP_ID) + ->willReturn($this->language); + + $this->notificationManager->expects($this->once()) + ->method('registerNotifier') + ->willReturnCallback(function($closureNotifier, $closureInfo) { + $this->assertInstanceOf(\Closure::class, $closureNotifier); + $notifier = $closureNotifier(); + $this->assertInstanceOf(Notifier::class, $notifier); + + $this->assertInstanceOf(\Closure::class, $closureInfo); + $info = $closureInfo(); + $this->assertInternalType('array', $info); + $this->assertArrayHasKey('id', $info); + $this->assertInternalType('string', $info['id']); + $this->assertArrayHasKey('name', $info); + $this->assertInternalType('string', $info['name']); + }); + + include __DIR__ . '/../../appinfo/app.php'; + } +} diff --git a/tests/AppInfo/ApplicationTest.php b/tests/AppInfo/ApplicationTest.php new file mode 100644 index 0000000..30761d9 --- /dev/null +++ b/tests/AppInfo/ApplicationTest.php @@ -0,0 +1,82 @@ +<?php +/** + * @copyright Copyright (c) 2017 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\AdminNotifications\Tests\AppInfo; + + +use OCA\AdminNotifications\AppInfo\Application; +use OCA\AdminNotifications\Command\Generate; +use OCA\AdminNotifications\Controller\APIController; +use OCA\AdminNotifications\Notification\Notifier; +use OCP\AppFramework\App; +use OCP\AppFramework\Controller; +use OCP\AppFramework\OCSController; +use OCP\Notification\INotifier; +use Symfony\Component\Console\Command\Command; +use Test\TestCase; + +/** + * Class ApplicationTest + * + * @package OCA\AdminNotifications\Tests + * @group DB + */ +class ApplicationTest extends TestCase { + /** @var \OCA\AdminNotifications\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(Application::APP_ID, $this->container->getAppName()); + } + + public function dataContainerQuery() { + return [ + [Application::class, App::class], + + [APIController::class, Controller::class], + [APIController::class, OCSController::class], + + [Generate::class, Command::class], + [Notifier::class, INotifier::class], + ]; + } + + /** + * @dataProvider dataContainerQuery + * @param string $service + * @param string $expected + */ + public function testContainerQuery($service, $expected) { + $this->assertInstanceOf($expected, $this->container->query($service)); + } +} diff --git a/tests/AppInfo/RoutesTest.php b/tests/AppInfo/RoutesTest.php new file mode 100644 index 0000000..a58d41c --- /dev/null +++ b/tests/AppInfo/RoutesTest.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2017 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\AdminNotifications\Tests\AppInfo; + +use Test\TestCase; + +class RoutesTest extends TestCase { + public function testRoutes() { + $routes = include __DIR__ . '/../../appinfo/routes.php'; + $this->assertInternalType('array', $routes); + $this->assertCount(1, $routes); + $this->assertArrayHasKey('ocs', $routes); + $this->assertInternalType('array', $routes['ocs']); + $this->assertCount(1, $routes['ocs']); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..f921af6 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,40 @@ +<?php +/** + * @copyright Copyright (c) 2017 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/>. + * + */ + +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: .../admin_notifications/tests/testcase.php" +\OC_App::loadApp('admin_notifications'); + +if(!class_exists('PHPUnit_Framework_TestCase')) { + require_once('PHPUnit/Autoload.php'); +} + +OC_Hook::clear(); diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..32201a0 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8" ?> +<phpunit bootstrap="bootstrap.php" + strict="true" + verbose="true" + timeoutForSmallTests="900" + timeoutForMediumTests="900" + timeoutForLargeTests="900" + > + <testsuite name='App Tests'> + <directory suffix='Test.php'>.</directory> + </testsuite> + <!-- filters for code coverage --> + <filter> + <whitelist> + <directory suffix=".php">../../admin_notifications/appinfo</directory> + <directory suffix=".php">../../admin_notifications/lib</directory> + </whitelist> + </filter> + <logging> + <!-- and this is where your report will be written --> + <log type="coverage-clover" target="./clover.xml"/> + </logging> +</phpunit> + |