Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2395

Přidáno uživatelem Ondřej Fibich před téměř 10 roky(ů)

Novinky:
- prostredi pro tvorbu integracnich testu (testy, ktere umi pracovat s vlasni testovaci DB a automaticky spravovat DB schema) - integracni test musi rozsirovat AbstractItTest
- samostatny konfiguracni soubor pro testy (obsahuje konfiguraci pripojeni k testovaci DB, URL testovaciho serveru)

Zobrazit rozdíly:

freenetis/branches/1.2/tests/AbstractItCase.php
<?php
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*/
/**
* Abstract integration test case that allows to test parts of FreenetIS with
* up-to-date database schema and provides access to database connection
* and services.
*
* @author Ondřej Fibich <fibich@freenetis.org>
* @since 1.2
*/
abstract class AbstractItCase extends PHPUnit_Framework_TestCase
{
/**
* @var Database
*/
protected static $connection;
/**
* @var ServiceFactory
*/
protected static $services;
/**
* Reset URL setting to given values.
*
* @param string $domain installed domain name
* @param string $path installed directory sub-path
*/
private static function reset_url_settings($domain, $path)
{
// set base domain
Settings::set('domain', $domain);
// set subdirectory
Settings::set('suffix', $path);
}
/**
* Reset URL setting to values in test configuration.
*/
protected static function reset_url_settings_to_current()
{
$domain = TestConfig::get('url.domain', 'localhost');
$path = TestConfig::get('url.path', '/freenetis/');
self::reset_url_settings($domain, $path);
}
/**
* Overridden setup before class in order to init/update databse schema
* and setup provided service factory and DB connection.
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
// service factory
self::$services = new ServiceFactory();
// init DB schema if not already
$lck_file = server::base_dir() . '/upload/mutex';
self::$services->injectCoreDatabaseInit()->make($lck_file, function ()
{
self::reset_url_settings_to_current();
});
// get DB connection
self::$connection = Database::instance();
}
}
freenetis/branches/1.2/tests/BootstrapPHPUnit.php
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Kohana process control file, loaded by the front controller.
*
*
* $Id: Bootstrap.php 1968 2008-02-06 21:41:29Z Shadowhand $
*
* @package Core
......
// Load core files
require SYSPATH.'core/utf8'.EXT;
require SYSPATH.'core/Config'.EXT;
require __DIR__.'/TestConfig'.EXT;
TestConfig::init(); // set DB configuration
require SYSPATH.'core/Log'.EXT;
require SYSPATH.'core/Event'.EXT;
require SYSPATH.'core/Kohana'.EXT;
// test classes
require __DIR__.'/AbstractItCase'.EXT;
// End: kohana_loading
Benchmark::stop(SYSTEM_BENCHMARK.'_kohana_loading');
......
//Benchmark::stop(SYSTEM_BENCHMARK.'_system_initialization');
//Event::run('system.execute');
Event::run('system.shutdown');
Event::run('system.shutdown');
freenetis/branches/1.2/tests/TestConfig.php
<?php
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*/
/**
* TestConfig provides access to test configuration options that are located
* in configuration file that is automatically loaded within the class.
*
* @author Ondřej Fibich <ondrej.fibich@gmail.com>
* @since 1.2
*/
final class TestConfig
{
/**
* Test config file name.
*/
const CONFIG_FILE = 'tests/config.ini';
/**
* @var array Configuration options array
*/
private static $config = NULL;
/**
* Initilize test configuration from INI file.
*
* @param string $file INI configuration file relative path [optional]
*/
public static function init($file = self::CONFIG_FILE)
{
if (self::$config == NULL)
{
self::load($file);
}
}
/**
* Get test configuration option that is located in configuration file that
* is loaded lazily on first call of this method. Configuration can be
* nested than the name may be defined using dots, e.g. "db.password".
*
* @param string $name option name that may be nested using dots
* @param mixed $default_value default option value if not found[optional]
* @return mixed option value
*/
public static function get($name, $default_value = NULL)
{
self::init();
// sub properties (nested)
$name_parts = explode('.', $name);
$current_config = self::$config;
for ($i = 0; $i < count($name_parts) - 1; $i++)
{
if (array_key_exists($name_parts[$i], $current_config) &&
is_array($current_config[$name_parts[$i]]))
{
$current_config = $current_config[$name_parts[$i]];
}
else
{
return $default_value; // not found
}
}
// return value
$p_name = $name_parts[count($name_parts) - 1];
return array_key_exists($p_name, $current_config) ?
$current_config[$p_name] : $default_value;
}
/**
* Loads configuration file from the given path.
*
* @param string $file configuration file path
* @throws Exception on error
*/
private static function load($file)
{
$full_file = DOCROOT . DIRECTORY_SEPARATOR . $file;
// load
try
{
self::$config = self::read_ini_file($full_file);
}
catch (InvalidArgumentException $ex)
{
throw new Exception('Invalid test configuration', NULL, $ex);
}
// DB config apply
if (isset(self::$config['db']))
{
try
{
self::set_db_config(self::$config['db']);
}
catch (InvalidArgumentException $ex)
{
throw new Exception('Invalid DB test configuration', NULL, $ex);
}
}
}
/**
* Loads INI file and return its content.
*
* @param string $file file path
* @return array content of INI file
* @throws InvalidArgumentException on any error during reading
*/
private static function read_ini_file($file)
{
if (!file_exists($file) || !is_readable($file))
{
throw new InvalidArgumentException('conf not readable: ' . $file);
}
$content = parse_ini_file($file, TRUE);
if ($content === FALSE)
{
throw new InvalidArgumentException('conf file cannot be parsed');
}
return $content;
}
/**
* Sets global application configuration options to one defined by passed
* array.
*
* @param array $config configuration array
* @throws InvalidArgumentException on invalid passed array
*/
private static function set_db_config($config)
{
static $required_options = array
(
'host' => 'db_host',
'database' => 'db_name',
'user' => 'db_user',
'pass' => 'db_password'
);
if (empty($config) || !is_array($config))
{
throw new InvalidArgumentException('invalid config array');
}
if (array_key_exists('type', $config))
{
Config::set('db_type', $config['type']);
}
foreach ($required_options as $option_name => $dest_option_name)
{
if (!array_key_exists($option_name, $config))
{
throw new InvalidArgumentException('option ' . $option_name
. 'is missing');
}
Config::set($dest_option_name, $config[$option_name]);
}
}
}
freenetis/branches/1.2/tests/application/controllers/api_endpoints/AbstractEndPointTestCase.php
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
*
* More info about project can be found:
* http://www.freenetis.org/
*/
......
use \Httpful\Request;
/**
* Abstract test case for easy testing of API endpoints.
* Abstract integration test case for easy testing of API endpoints.
*
* @author Ondřej Fibich <fibich@freenetis.org>
*/
abstract class AbstractEndPointTestCase extends PHPUnit_Framework_TestCase
abstract class AbstractEndPointTestCase extends AbstractItCase
{
/**
* API user account username.
*/
const API_USERNAME = 'test_NL';
/**
* API user account password.
*/
const API_PASSWORD = '12345678901234567890123456789012';
/**
* Base path to FreenetIS API.
*
*
* @var string
*/
protected $base_path;
/**
* API account that is used for logging to API.
*
*
* @var Api_account_Model
*/
protected $api_account;
/**
* Defines authentification type that is used for connecting to API.
*
*
* @var string
*/
protected $auth_method;
......
$this->base_path = Settings::get('protocol') . '://'
. Settings::get('domain') . Settings::get('suffix') . 'cs'
. Api_Controller::API_BASE_PATH;
$this->api_account = new Api_account_Model();
$this->api_account->allowed_paths = '/**'; // allow all
$this->api_account->enabled = TRUE;
......
$this->api_account->username = self::API_USERNAME;
$this->api_account->token = self::API_PASSWORD;
$this->api_account->save_throwable();
$this->auth_method = 'authenticateWith'
$this->auth_method = 'authenticateWith'
. ucfirst(Settings::get('api_auth_type'));
}
/**
* Remove API account.
*/
......
{
$this->api_account->delete();
}
/**
* Gets resource on given path, with given parameters, than test the response
* Gets resource on given path, with given parameters, than test the response
* code and return the response.
*
*
* @param string $path path relative to base path
* @params array $params optional request parameters
* @param integer $expected_code optional expected HTTP response code
......
}
$path .= '?' . implode('&', $params_str_array);
}
$rsp = Request::get($this->base_path . $path)
->{$this->auth_method}(self::API_USERNAME, self::API_PASSWORD)
->send();
$this->assertNotNull($rsp);
if ($expected_code > 0)
{
$this->assertEquals($expected_code, $rsp->code, 'GET failed expected');
}
return $rsp;
}
}
freenetis/branches/1.2/tests/config.sample.ini
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Sample test configuration options, please fill proper values and create
; file config.ini in same directory according to this file.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; URL configuration
[url]
domain = localhost
path = /freenetis/
; Database configuration
[db]
type = mysql
user = root
pass =
host = localhost
database = test

Také k dispozici: Unified diff