Commit 409101b0 authored by Yuliya Bozhko's avatar Yuliya Bozhko Committed by Gerrit Code Review

PHPUnit improvements and fixes

Change-Id: Ia83d2cd8b5f7b971098daf580839bd61f08be354
Signed-off-by: default avatarYuliya Bozhko <yuliya.bozhko@totaralms.com>
parent e73abfd5
all: all:
@echo "Run 'make initcomposer' to install Composer and phpunit"
@echo "Run 'make phpunit' to execute phpunit tests"
@echo "Run 'make imageoptim' to losslessly optimise all images" @echo "Run 'make imageoptim' to losslessly optimise all images"
@echo "Run 'make minaccept' to run the quick pre-commit tests" @echo "Run 'make minaccept' to run the quick pre-commit tests"
@echo "Run 'make checksignoff' to check that your commits are all Signed-off-by" @echo "Run 'make checksignoff' to check that your commits are all Signed-off-by"
...@@ -10,15 +12,45 @@ imageoptim: ...@@ -10,15 +12,45 @@ imageoptim:
find . -iname '*.jpg' -exec jpegoptim -q -p --strip-all {} \; find . -iname '*.jpg' -exec jpegoptim -q -p --strip-all {} \;
find . -iname '*.jpeg' -exec jpegoptim -q -p --strip-all {} \; find . -iname '*.jpeg' -exec jpegoptim -q -p --strip-all {} \;
composer := $(shell ls external/composer.phar 2>/dev/null)
initcomposer:
ifdef composer
@echo "Updating Composer..."
@php external/composer.phar --working-dir=external update
else
@echo "Installing Composer..."
@curl -sS https://getcomposer.org/installer | php -- --install-dir=external
@php external/composer.phar --working-dir=external install
endif
vendorphpunit := $(shell external/vendor/bin/phpunit --version 2>/dev/null)
phpunit:
@echo "Running phpunit tests..."
ifdef vendorphpunit
@external/vendor/bin/phpunit --log-junit logs/tests/phpunit-results.xml htdocs/
else
@phpunit --log-junit logs/tests/phpunit-results.xml htdocs/
endif
revision := $(shell git rev-parse --verify HEAD 2>/dev/null)
whitelist := $(shell grep / test/WHITELIST | xargs -I entry find entry -type f | xargs -I file echo '! -path ' file 2>/dev/null)
minaccept: minaccept:
@echo "Running minimum acceptance test..." @echo "Running minimum acceptance test..."
@find htdocs/ -type f -name "*.php" | xargs -n 1 -P 2 php -l > /dev/null && echo All good! ifdef revision
@find htdocs -type f -name "*.php" -print0 | xargs -0 -n 1 -P 2 php -l > /dev/null && echo All good!
@php test/versioncheck.php @php test/versioncheck.php
@find htdocs/ -type f -name "install.xml" -path "*/db/install.xml" | xargs -n 1 -P 2 xmllint --schema htdocs/lib/xmldb/xmldb.xsd --noout @find htdocs -type f -name "install.xml" -path "*/db/install.xml" -print0 | xargs -0 -n 1 -P 2 xmllint --schema htdocs/lib/xmldb/xmldb.xsd --noout
@if git rev-parse --verify HEAD 2>/dev/null; then git show HEAD ; fi | test/coding-standard-check.pl @git diff-tree --diff-filter=ACMR --no-commit-id --name-only -r $(revision) | xargs -I {} find {} $(whitelist) | xargs -I list git show $(revision) list | test/coding-standard-check.pl
else
@echo "No revision found!"
endif
jenkinsaccept: minaccept jenkinsaccept: minaccept
@find ./ ! -path './.git/*' -type f | xargs clamscan > /dev/null && echo All good! @find ./ ! -path './.git/*' -type f -print0 | xargs -0 clamscan > /dev/null && echo All good!
sshargs := $(shell git config --get remote.gerrit.url | sed -re 's~^ssh://([^@]*)@([^:]*):([0-9]*)/mahara~-p \3 -l \1 \2~') sshargs := $(shell git config --get remote.gerrit.url | sed -re 's~^ssh://([^@]*)@([^:]*):([0-9]*)/mahara~-p \3 -l \1 \2~')
mergebase := $(shell git fetch gerrit >/dev/null 2>&1 && git merge-base HEAD gerrit/master) mergebase := $(shell git fetch gerrit >/dev/null 2>&1 && git merge-base HEAD gerrit/master)
......
...@@ -12,6 +12,10 @@ ...@@ -12,6 +12,10 @@
"fabpot/goutte": "~1.0" "fabpot/goutte": "~1.0"
}, },
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"minimum-stability": "dev" "minimum-stability": "dev"
} }
...@@ -53,8 +53,10 @@ define('DEVMODE_UNPACKEDJS', 8); ...@@ -53,8 +53,10 @@ define('DEVMODE_UNPACKEDJS', 8);
// later in init.php after we've loaded config.php // later in init.php after we've loaded config.php
$errorlevel = E_ALL & ~E_STRICT; $errorlevel = E_ALL & ~E_STRICT;
error_reporting($errorlevel); error_reporting($errorlevel);
set_error_handler('error', $errorlevel); if (!defined('TESTSRUNNING')) {
set_exception_handler('exception'); set_error_handler('error', $errorlevel);
set_exception_handler('exception');
}
// Logging functions // Logging functions
......
This requires phpunit 3.3 or later. This requires phpunit 3.7 or later which is installed by running:
It needs to be run like: make initcomposer
php -c /etc/php5/apache2/php.ini /usr/bin/phpunit . Unit tests can be run by:
Or if you've configured your cli php.ini properly, you can just do
phpunit .
make phpunit
From the main directory of mahara (eg, outside htdocs) From the main directory of mahara (eg, outside htdocs)
...@@ -16,13 +16,17 @@ define('PUBLIC', 1); ...@@ -16,13 +16,17 @@ define('PUBLIC', 1);
// necessary since we're running in a limited scope // necessary since we're running in a limited scope
global $CFG, $db, $SESSION, $USER, $THEME; global $CFG, $db, $SESSION, $USER, $THEME;
ob_start();
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php'); require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
ob_end_clean();
require_once(get_config('libroot') . 'ddl.php'); require_once(get_config('libroot') . 'ddl.php');
require_once(get_config('libroot') . 'upgrade.php'); require_once(get_config('libroot') . 'upgrade.php');
require_once(get_config('libroot') . 'phpunit.php'); require_once(get_config('libroot') . 'phpunit.php');
error_reporting(E_ALL & ~E_STRICT);
ini_set('display_errors', '1');
ini_set('log_errors', '1');
restore_error_handler();
restore_exception_handler();
$bootstrap = new UnitTestBootstrap(); $bootstrap = new UnitTestBootstrap();
$bootstrap->jimmy_config(); $bootstrap->jimmy_config();
......
<?php
/**
* Test the different web service protocols.
*
* @package mahara
* @subpackage auth-webservice
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
# require_once 'PHPUnit/Framework.php';
/**
* phpunit test class that loads all the tests in the current directory
* and then runs them.
*/
class RunAllTests {
/**
* Load all tests
*/
public static function suite() {
$suite = new PHPUnit_Framework_TestSuite('Mahara Web Services TestSuite');
$tests = preg_grep('/RunAllTests|TestBase/', glob(dirname(__FILE__) . '/*.php'), PREG_GREP_INVERT);
foreach ($tests as $test) {
error_log('adding test: ' . $test);
$test = basename($test);
$parts = explode('.', $test);
error_log("Setting up: $parts[0]\n");
require_once($test);
$suite->addTestSuite($parts[0]);
}
return $suite;
}
}
#!/bin/sh
# Test the different web service protocols.
#
# @package mahara
# @subpackage auth-webservice
# @author Catalyst IT Ltd
# @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
# @copyright For copyright information on Mahara, please see the README file distributed with this software.
#
# Example of running ALL the phpunit tests
# protect against execution
if [ -n "$GATEWAY_INTERFACE" ]; then
echo "Content-type: text/html"
echo ""
echo "<html><head><title>ERROR</title></head><body><h1>ERROR</h1></body></html>"
exit 0
fi
sudo -u www-data phpunit "RunAllTests"
PHPUnit Tests, and example clients for Mahara Web Services Example clients for Mahara Web Services
========================================================== ==========================================================
Tests
=====
The test directory contains PHP Unit tests for Web Services.
To run all of these you need to change the the tests/ directory,
switch to the web server user and run:
phpunit RunAllTests
An easy way to do this with Linux is:
sudo -u www-data phpunit "RunAllTests"
Example Clients
===============
The example clients are a to demonstrate the usage of the User, Group, and Institution The example clients are a to demonstrate the usage of the User, Group, and Institution
APIs. APIs.
......
...@@ -195,7 +195,8 @@ while (1) { ...@@ -195,7 +195,8 @@ while (1) {
try { try {
$result = $client->call($function, $functions[$function]); $result = $client->call($function, $functions[$function]);
print "Results are: " . var_export($result, true) . "\n"; print "Results are: " . var_export($result, true) . "\n";
} catch (Exception $e) { }
catch (Exception $e) {
print "exception: " . var_export($e, true) . "\n"; print "exception: " . var_export($e, true) . "\n";
} }
} }
......
...@@ -11,12 +11,12 @@ ...@@ -11,12 +11,12 @@
* *
*/ */
require_once 'TestBase.class.php'; require_once('WebServiceTestBase.class.php');
/** /**
* functional tests for Groups * functional tests for Groups
*/ */
class WebServiceGroupTest extends TestBase { class WebServiceGroupTest extends WebServiceTestBase {
/** /**
* local setup - outline the test functions in the framework * local setup - outline the test functions in the framework
......
...@@ -11,12 +11,12 @@ ...@@ -11,12 +11,12 @@
* *
*/ */
require_once 'TestBase.class.php'; require_once('WebServiceTestBase.class.php');
/** /**
* functional tests for institutions * functional tests for institutions
*/ */
class WebServiceInstitutionTest extends TestBase { class WebServiceInstitutionTest extends WebServiceTestBase {
/** /**
* local setup - outline the test functions in the framework * local setup - outline the test functions in the framework
......
...@@ -10,27 +10,12 @@ ...@@ -10,27 +10,12 @@
* *
*/ */
// disable the WSDL cache
ini_set("soap.wsdl_cache_enabled", "0");
// must be run from the command line
if (isset($_SERVER['REMOTE_ADDR']) || isset($_SERVER['GATEWAY_INTERFACE'])) {
die('Direct access to this script is forbidden.');
}
// disable the WSDL cache // disable the WSDL cache
ini_set("soap.wsdl_cache_enabled", "0"); ini_set("soap.wsdl_cache_enabled", "0");
define('TESTSRUNNING', 1);
define('INTERNAL', 1);
define('PUBLIC', 1);
// necessary since we're running in a limited scope
global $CFG, $db, $SESSION, $USER, $THEME;
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
require_once(get_config('libroot') . 'ddl.php'); require_once(get_config('libroot') . 'ddl.php');
require_once(get_config('libroot') . 'upgrade.php'); require_once(get_config('libroot') . 'upgrade.php');
require_once(get_config('libroot') . 'phpunit.php');
$path = get_config('docroot') . 'lib/zend'; $path = get_config('docroot') . 'lib/zend';
set_include_path($path . PATH_SEPARATOR . get_include_path()); set_include_path($path . PATH_SEPARATOR . get_include_path());
...@@ -66,7 +51,7 @@ require_once('group.php'); ...@@ -66,7 +51,7 @@ require_once('group.php');
* Also, the testRun framework that runs the list of tests in each specific class * Also, the testRun framework that runs the list of tests in each specific class
* against all the protocols and auth methods * against all the protocols and auth methods
*/ */
class TestBase extends PHPUnit_Framework_TestCase { class WebServiceTestBase extends MaharaUnitTest {
public $testtoken; public $testtoken;
public $testrest; public $testrest;
......
...@@ -11,12 +11,12 @@ ...@@ -11,12 +11,12 @@
* *
*/ */
require_once 'TestBase.class.php'; require_once('WebServiceTestBase.class.php');
/** /**
* funcitonal tests for user API * funcitonal tests for user API
*/ */
class WebServiceUserTest extends TestBase { class WebServiceUserTest extends WebServiceTestBase {
/** /**
* local setup - outline the test functions in the framework * local setup - outline the test functions in the framework
......
<phpunit bootstrap="htdocs/lib/tests/phpunit/bootstrap.php" <phpunit
colors="true" bootstrap="htdocs/lib/tests/phpunit/bootstrap.php"
convertErrorsToExceptions="true" colors="true"
convertNoticesToExceptions="true" convertErrorsToExceptions="true"
convertWarningsToExceptions="true" convertNoticesToExceptions="true"
stopOnFailure="true"> convertWarningsToExceptions="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>
<filter> <filter>
<whitelist addUncoveredFilesFromWhitelist="true"> <!-- code coverage --> <whitelist addUncoveredFilesFromWhitelist="true"> <!-- code coverage -->
<directory suffix=".php">htdocs</directory> <directory suffix=".php">htdocs</directory>
......
htdocs/lib/mobile_detect/Mobile_Detect.php
htdocs/lib/pear
htdocs/lib/dwoo/dwoo
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment