27-02-2012, 03:50 PM
Voilà un extrait d'un test d'une classe simple. C'est pas très propre mais ça fonctionne bien. La classe est triviale aussi
<?php
require_once dirname(__FILE__) . '/../src/behaviours/gen_state.php';
//require_once dirname(__FILE__).'/../behaviours/gen_state.php';
class Test_gen_state extends UnitTestCase {
function __construct() {
parent::__construct();
debug_printer::get_printer()->title(__CLASS__, '-');
}
function test_wrap() {
$this->assertTrue(gen_state::wrap(array()) instanceof gen_state);
$this->assertTrue(count(gen_state::wrap(array())->export()) == 0);
$gs = gen_state::wrap(array('a' => 'b'));
$this->assertTrue(count($gs->export()) == 1);
$this->assertTrue($gs->get('a') == 'b');
}
function test_exception() {
$this->expectException();
$gs = gen_state::wrap(array('a' => 'b'));
$gs->get('b');
}
function test_default() {
$gs = gen_state::wrap(array());
$this->assertTrue($gs->get_default('b', 'default value') == 'default value');
}
function test_set() {
$gs = gen_state::wrap(array());
$self = $gs->set('a', 42);
$this->assertFalse($gs->export() == array('a' => 43));
$this->assertTrue($gs->export() == array('a' => 42));
$this->assertTrue($self === $gs);
}
function test_set_new() {
$gs = gen_state::wrap(array());
$this->assertTrue($gs->set_new('newkey', 'newvalue'));
$this->expectException();
$this->assertTrue($gs->set_new('newkey', 'newvalue'));
}
function test_replace() {
$gs = gen_state::wrap(array('a' => 'wrong value'));
$this->assertTrue($gs->replace('a', 'correct value'));
$gs2 = gen_state::wrap(array());
$this->expectException();
$gs2->replace('a', 'correct value');
}
function test_keys() {
$state = array(
'A', 'b', 'CCC', 'dé'
);
$expected_keys = array_keys($state);
$gs = gen_state::wrap($expected_keys);
$this->assertTrue($gs->keys() == $expected_keys);
}
function test_get_callback() {
$gs = gen_state::wrap(array('a' => 'exists'));
$callback = 'max';
$callback_args = array(
array(1, 2, 7, 6, 5) // max is 7
);
$this->assertTrue($gs->get_callback('a', $callback, $callback_args) == 'exists');
$this->assertTrue($gs->get_callback('b', $callback, $callback_args) == 7);
$gs2 = gen_state::wrap(array());
$this->expectException();
$gs2->replace('a', 'correct value');
}
function test_merge_new() {
$gs = gen_state::wrap(array(
'a' => 'exists_a',
'b' => 'exists_b',
'c' => 'exists_c'
));
$gs->merge_new(array(
'a' => 'replaced_a',
'd' => 'replaced_d'
));
$this->assertTrue($gs->get('a') == 'exists_a');
$this->assertTrue($gs->get('d') == 'replaced_d');
}
function test_null_value() {
$a = gen_state::wrap(array('a' => null));
$this->assertTrue(is_null($a->get('a')));
$b = gen_state::wrap(array());
$b->set('b', null);
$this->assertTrue(is_null($b->get('b')));
}
function test_has() {
$gs = gen_state::wrap(array('a' => null, 'b' => 123));
$this->assertTrue($gs->has('a'));
$this->assertTrue($gs->has('b'));
$this->assertFalse($gs->has('c'));
}
function test_append_to() {
$gs = gen_state::wrap(array(
'a' => array(1, 2, 3),
'b' => 'some string'
));
$gs->append_to('a', 4);
$this->assertEqual($gs->get('a'), array(1, 2, 3, 4));
$gs->append_to('b', '-other string');
$this->assertEqual($gs->get('b'), 'some string-other string');
$gs->append_to('a', 'val', 'key');
$this->assertEqual($gs->get('a'), array(1, 2, 3, 4, 'key' => 'val'));
}
}