Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schiessle committed Jul 26, 2016
1 parent b83feb4 commit 6af3830
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 73 deletions.
6 changes: 6 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
imports:
- javascript
- php

tools:
external_code_coverage: true
45 changes: 45 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7

env:
global:
- CORE_BRANCH=master
matrix:
- DB=sqlite

branches:
only:
- master
- /^stable\d+(\.\d+)?$/

sudo: true
before_install:
- wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh
- bash ./before_install.sh serverinfo $CORE_BRANCH $DB

script:
# Test lint
- cd ../server
- cd apps/serverinfo
- find . -name \*.php -exec php -l "{}" \;

# Run phpunit tests
- cd tests
- phpunit --configuration phpunit.xml

# Create coverage report
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover clover.xml

matrix:
include:
- php: 5.4
env: DB=mysql
- php: 5.4
env: DB=pgsql
fast_finish: true
1 change: 0 additions & 1 deletion lib/SystemStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function getSystemStatistics() {
'enable_previews' => $this->config->getSystemValue('enable_previews', true) ? 'yes' : 'no',
'memcache.local' => $this->config->getSystemValue('memcache.local', 'none'),
'memcache.distributed' => $this->config->getSystemValue('memcache.distributed', 'none'),
'asset-pipeline.enabled' => $this->config->getSystemValue('asset-pipeline.enabled') ? 'yes' : 'no',
'filelocking.enabled' => $this->config->getSystemValue('filelocking.enabled', true) ? 'yes' : 'no',
'memcache.locking' => $this->config->getSystemValue('memcache.locking', 'none'),
'debug' => $this->config->getSystemValue('debug', false) ? 'yes' : 'no',
Expand Down
33 changes: 12 additions & 21 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
<?php
/**
* @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
*
* @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/>.
*
*/

require_once __DIR__ . '/../../../tests/bootstrap.php';
require_once __DIR__ . '/../appinfo/autoload.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_App::loadApp('serverinfo');
OC_Hook::clear();

124 changes: 124 additions & 0 deletions tests/lib/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
*
* @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\ServerInfo\Tests\lib\Controller;


use OCA\ServerInfo\Controller\ApiController;
use OCA\ServerInfo\DatabaseStatistics;
use OCA\ServerInfo\PhpStatistics;
use OCA\ServerInfo\ShareStatistics;
use OCA\ServerInfo\StorageStatistics;
use OCA\ServerInfo\SystemStatistics;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
use Test\TestCase;

class ApiControllerTest extends TestCase {

/** @var IRequest | \PHPUnit_Framework_MockObject_MockObject */
private $request;

/** @var SystemStatistics | \PHPUnit_Framework_MockObject_MockObject */
private $systemStatistics;

/** @var StorageStatistics | \PHPUnit_Framework_MockObject_MockObject */
private $storageStatistics;

/** @var PhpStatistics | \PHPUnit_Framework_MockObject_MockObject */
private $phpStatistics;

/** @var DatabaseStatistics | \PHPUnit_Framework_MockObject_MockObject */
private $databaseStatistics;

/** @var ShareStatistics | \PHPUnit_Framework_MockObject_MockObject */
private $shareStatistics;

/** @var ApiController */
private $instance;

public function setUp() {
parent::setUp();

$this->request = $this->getMockBuilder('OCP\IRequest')
->disableOriginalConstructor()
->getMock();

$this->systemStatistics = $this->getMockBuilder('OCA\ServerInfo\SystemStatistics')
->disableOriginalConstructor()
->getMock();

$this->storageStatistics = $this->getMockBuilder('OCA\ServerInfo\StorageStatistics')
->disableOriginalConstructor()
->getMock();

$this->phpStatistics = $this->getMockBuilder('OCA\ServerInfo\PhpStatistics')
->disableOriginalConstructor()
->getMock();

$this->databaseStatistics = $this->getMockBuilder('OCA\ServerInfo\DatabaseStatistics')
->disableOriginalConstructor()
->getMock();

$this->shareStatistics = $this->getMockBuilder('OCA\ServerInfo\ShareStatistics')
->disableOriginalConstructor()
->getMock();

$this->instance = new ApiController(
'ServerInfoTest',
$this->request,
$this->systemStatistics,
$this->storageStatistics,
$this->phpStatistics,
$this->databaseStatistics,
$this->shareStatistics
);
}

public function testInfo() {

$this->systemStatistics->expects($this->once())->method('getSystemStatistics')
->willReturn('systemStatistics');
$this->storageStatistics->expects($this->once())->method('getStorageStatistics')
->willReturn('storageStatistics');
$this->phpStatistics->expects($this->once())->method('getPhpStatistics')
->willReturn('phpStatistics');
$this->databaseStatistics->expects($this->once())->method('getDatabaseStatistics')
->willReturn('databaseStatistics');
$this->shareStatistics->expects($this->once())->method('getShareStatistics')
->willReturn('shareStatistics');

$result = $this->instance->info();
$this->assertTrue($result instanceof DataResponse);
$data = $result->getData();
$this->assertTrue(isset($data['data']['nextcloud']));
$this->assertTrue(isset($data['data']['server']));

$this->assertSame('systemStatistics', $data['data']['nextcloud']['system']);
$this->assertSame('storageStatistics', $data['data']['nextcloud']['storage']);
$this->assertSame('shareStatistics', $data['data']['nextcloud']['shares']);
$this->assertSame('unknown', $data['data']['server']['webserver']);
$this->assertSame('databaseStatistics', $data['data']['server']['database']);
$this->assertSame('phpStatistics', $data['data']['server']['php']);
}

}
26 changes: 26 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="bootstrap.php"
verbose="true"
timeoutForSmallTests="900"
timeoutForMediumTests="900"
timeoutForLargeTests="900"
>
<testsuite name='Nextcloud - ServerInfo Tests'>
<directory suffix='Test.php'>.</directory>
</testsuite>
<!-- filters for code coverage -->
<filter>
<whitelist>
<directory suffix=".php">../../serverinfo/lib</directory>
<exclude>
<directory suffix=".php">../../serverinfo/l10n</directory>
<directory suffix=".php">../../serverinfo/lists</directory>
<directory suffix=".php">../../serverinfo/tests</directory>
</exclude>
</whitelist>
</filter>
<logging>
<!-- and this is where your report will be written -->
<log type="coverage-clover" target="./clover.xml"/>
</logging>
</phpunit>
51 changes: 0 additions & 51 deletions tests/unit/controller/PageControllerTest.php

This file was deleted.

0 comments on commit 6af3830

Please sign in to comment.