Commit 901af79e authored by Mindiell's avatar Mindiell

Added test base and some first tests on Main class

parent fee0499c
/tmp
app/config.php
tests/tmp
<?php
/*
* Base class for testing
*
* Sub classes could set their own "set up" / "tear down" methods but should
* always call the parents one by using parent::setup() / parent::teardown()
*
*/
// Framework instance
require_once('../lib/base.php');
class BaseTest {
protected $test;
protected $f3;
function __construct() {
$this->f3 = Base::instance();
$this->f3->config('config.ini');
$this->f3->config('../app/routes.ini');
}
function setup() {
$this->test = new Test;
}
function teardown() {
// Nothing yet
}
function run($output="text") {
// Set Up
$this->setup();
// Adding all tests
foreach (get_class_methods($this) as $method) {
if (substr($method, 0, 4)=='test') {
try {
$this->$method();
} catch (Exception $e) {
//echo $e;
}
}
}
// Tear Down
$this->teardown();
// Getting tests results
$this->results = $this->test->results();
// Show results
switch ($output) {
default:
$this->output_text();
}
}
function sanitize_output($string) {
return str_replace('_', ' ', substr($string,5));
}
// Outputs results in raw text
function output_text() {
$failed = array();
echo "Testing " . $this->sanitize_output(get_class($this)) . "\n";
echo "================================================================\n";
echo "Launching " . count($this->results) . " tests:\n";
foreach ($this->results as $result) {
if ($result['status']) {
echo "P";
} else {
echo "F";
$failed[] = $result;
}
}
echo "\n----------------------------------------------------------------\n";
echo count($failed) . " on " . count($this->results) . " test(s) failed\n";
foreach ($failed as $result) {
echo "On " . $result['source'] . "\n";
echo " " . $result['text'] . "\n";
}
}
}
<?php
/*
* Launching tests for each test file
*/
$directory = opendir('.');
$file = readdir($directory);
while ($file) {
if (substr($file,0,4)=="test") {
include($file);
}
$file = readdir();
}
[globals]
; Folder containing classes
AUTOLOAD=../app/
; Folder containing templates
UI=../templates/
; Debug level
DEBUG=3
; Default language
LANGUAGE='fr'
; Global variables of website
<?php
/*
* Tests for Main class of piphone
*/
include('BaseTest.php');
class Test_Main_Class extends BaseTest {
function setup() {
parent::setup();
$this->f3->set('QUIET',TRUE);
}
function teardown() {
parent::teardown();
$this->f3->set('QUIET',FALSE);
}
function test_main_existence() {
$this->test->expect(
class_exists('Main'),
'Main class is not defined'
);
}
function test_main_get_campaign_return_array() {
$this->f3->mock('GET /');
$this->test->expect(
is_array($this->f3->get('campaign')),
'Do not get a campaign array'
);
$this->f3->clear('ERROR'); // clear any errors
}
function test_main_get_campaign_contains_known_fields() {
$fields = array("title", "description", "start_date", "end_date", "default_language");
$this->f3->mock('GET /');
foreach ($fields as $field) {
$this->test->expect(
array_key_exists($field, $this->f3->get('campaign')),
"Campaign should have a " . $field ." field"
);
}
$this->f3->clear('ERROR'); // clear any errors
}
}
// Launching tests
$tests = new Test_Main_Class();
$tests->run();
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