Skip to content

Commit

Permalink
UNITTEST : Add unit testing layer. Creation of composer.json to manag…
Browse files Browse the repository at this point in the history
…e dependancy atoum (same has GLPI). New test for class PluginModeticketProfile
  • Loading branch information
mate-infotel committed Feb 20, 2019
1 parent 2b0c19a commit 90f1843
Show file tree
Hide file tree
Showing 9 changed files with 1,019 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .atoum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$tests_dir = __DIR__ . '/tests/';
$coverage_dir = $tests_dir . 'code-coverage/';

if (!file_exists($coverage_dir)) {
mkdir($coverage_dir);
}
$coverageField = new atoum\report\fields\runner\coverage\html(
'GLPI',
$coverage_dir
);
$coverageField->setRootUrl('file://' . realpath($coverage_dir));
$script
->addDefaultReport()
->addField($coverageField);

$runner->addTestsFromDirectory(__DIR__ . '/inc');
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "infotel/moreticket",
"type": "project",
"license": "LGPL2",
"authors": [
{
"name": "Mathieu Templier",
"email": "[email protected]"
}
],
"require": {
"atoum/atoum": "^3.3"
}
}
101 changes: 101 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions scripts/cliinstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2018 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/

//define('GLPI_ROOT', dirname(__DIR__));

define('GLPI_ROOT', __DIR__ . "/../../../");

chdir(GLPI_ROOT);
if (in_array('--tests', $_SERVER['argv'])) { // Uggly, but must be before any other GLPI include, so not from Getopt
define("GLPI_CONFIG_DIR", GLPI_ROOT . "/tests");
@mkdir(GLPI_CONFIG_DIR . '/files/_log', 0775, true);
}
include_once (GLPI_ROOT . "/inc/based_config.php");
include_once (GLPI_ROOT . "/inc/db.function.php");
$GLPI = new GLPI();
$GLPI->initLogger();
Config::detectRootDoc();
try {
$opts = new \Zend\Console\Getopt([
'help' => 'Display usage',
'host|h=s' => 'Machine hosting the database',
'db|d=s' => 'Database name (required)',
'user|u=s' => 'Database user (required)',
'pass|p-s' => 'Database password (default: no password) without value will be prompt',
'lang|l=s' => 'Locale (default: en_GB)',
'tests' => 'Test configuration',
'force|f' => 'Override existing configuration',
]);
$opts->parse();
} catch (Zend\Console\Exception\RuntimeException $e) {
echo $e->getUsageMessage();
exit;
}
$args = $opts->getArguments();
if (!isset($args['host'])) {
$args['host'] = 'localhost';
}
if (!isset($args['pass'])) {
$args['pass'] = '';
}
if (isset($args['help']) || !(isset($args['db']) && isset($args['user']))) {
echo $opts->getUsageMessage();
exit;
}
if (isset($args['lang']) && !isset($CFG_GLPI['languages'][$args['lang']])) {
$kl = implode(', ', array_keys($CFG_GLPI['languages']));
echo "Unkown locale (use one of: $kl)\n";
die(1);
}
if (file_exists(GLPI_CONFIG_DIR . '/config_db.php') && !isset($args['force'])) {
echo "Already installed (see --force option)\n";
die(1);
}
$_SESSION = ['glpilanguage' => (isset($args['lang']) ? $args['lang'] : 'en_GB')];
Toolbox::setDebugMode(Session::DEBUG_MODE, 0, 0, 1);
if ($args['pass'] === true) {
$args['pass'] = \Zend\Console\Prompt\Password::prompt('Password:');
}
echo "Connect to the DB...\n";
//Check if the port is in url
$hostport = explode(':', $args['host']);
if (count($hostport) < 2) {
$link = new mysqli($hostport[0], $args['user'], $args['pass']);
} else {
$link = new mysqli($hostport[0], $args['user'], $args['pass'], '', $hostport[1]);
}
if (!$link || mysqli_connect_error()) {
echo "DB connection failed\n";
die(1);
}
$args['db'] = $link->real_escape_string($args['db']);
$DB_ver = $link->query("SELECT version()");
$row = $DB_ver->fetch_array();
$checkdb = Config::displayCheckDbEngine(true, $row[0]);
if ($checkdb > 0) {
return;
}
echo "Create the DB...\n";
if (!$link->query("CREATE DATABASE IF NOT EXISTS `" . $args['db'] ."`")) {
echo "Can't create the DB\n";
die(1);
}
if (!$link->select_db($args['db'])) {
echo "Can't select the DB\n";
die(1);
}
echo "Save configuration file...\n";
if (!DBConnection::createMainConfig($args['host'], $args['user'], $args['pass'], $args['db'])) {
echo "Can't write configuration file\n";
die(1);
}
echo "Load default schema...\n";
Toolbox::createSchema($_SESSION['glpilanguage']);
echo "Done\n";
97 changes: 97 additions & 0 deletions tests/DbTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2018 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/

// Generic test classe, to be extended for CommonDBTM Object

class DbTestCase extends \GLPITestCase {

public function beforeTestMethod($method) {
global $DB;
$DB->beginTransaction();
parent::beforeTestMethod($method);
}

public function afterTestMethod($method) {
global $DB;
$DB->rollback();
parent::afterTestMethod($method);
}


/**
* Connect (using the test user per default)
*
* @param string $user_name User name (defaults to TU_USER)
* @param string $user_pass user password (defaults to TU_PASS)
*
* @return voidd
*/
protected function login($user_name = TU_USER, $user_pass = TU_PASS) {

$auth = new Auth();
$this->boolean($auth->login($user_name, $user_pass, true))->isTrue();
}

/**
* change current entity
*
* @param string $entityname Name of the entity
* @param boolean $subtree Recursive load
*
* @return void
*/
protected function setEntity($entityname, $subtree) {
$res = Session::changeActiveEntities(getItemByTypeName('Entity', $entityname, true), $subtree);
$this->boolean($res)->isTrue();
}

/**
* Generic method to test if an added object is corretly inserted
*
* @param Object $object The object to test
* @param int $id The id of added object
* @param array $input the input used for add object (optionnal)
*
* @return nothing (do tests)
*/
protected function checkInput(CommonDBTM $object, $id = 0, $input = []) {
$this->integer((int)$id)->isGreaterThan(0);
$this->boolean($object->getFromDB($id))->isTrue();
$this->variable($object->getField('id'))->isEqualTo($id);

if (count($input)) {
foreach ($input as $k => $v) {
$this->variable($object->getField($k))->isEqualTo($v);
}
}
}
}
Loading

0 comments on commit 90f1843

Please sign in to comment.