Ejemplo n.º 1
0
// test
$m->testMethod(1, 'foobar')->callback(array('TestCallbackClass', 'callback'));
$m->replay();
$value = $m->testMethod(1, 'foobar');
// assertions
$t->is(TestCallbackClass::$arguments, array(1, 'foobar'), 'The arguments have been passed to the callback');
$t->is($value, 'elvis is alive', 'The return value of the callback has been passed through');
// @Test: If a callback AND a return value is configured, the return value of the callback is ignored
// test
$m->testMethod()->callback(array('TestCallbackClass', 'callback'))->returns('elvis sure is dead');
$m->replay();
$value = $m->testMethod();
// assertions
$t->is($value, 'elvis sure is dead', 'The return value of the callback was ignored');
// @Test: The return value of the callback is ignored even if the configured return value is NULL
// test
$m->testMethod()->callback(array('TestCallbackClass', 'callback'))->returns(null);
$m->replay();
$value = $m->testMethod();
// assertions
$t->same($value, null, 'The return value of the callback was ignored');
// @Test: Parameters are passed to the callback correctly, if any parameters are expected
// fixtures
TestCallbackClass::$arguments = null;
// test
$m->method('testMethod')->callback(array('TestCallbackClass', 'callback'));
$m->replay();
$value = $m->testMethod(1, 'foobar');
// assertions
$t->is(TestCallbackClass::$arguments, array(1, 'foobar'), 'The arguments have been passed to the callback');
$t->is($value, 'elvis is alive', 'The return value of the callback has been passed through');
$t->is($paramHolder->getAll(), $params, 'All parameters were added correctly');
$paramHolder->add(null);
$t->isnt($paramHolder->getAll(), null, 'Nothing is added if NULL was given');
$paramHolder->add(array());
$t->isntSame($paramHolder->getAll(), array(), 'Nothing is added if an empty array was given');
$paramHolder->add('this fails');
$t->isnt($paramHolder->getAll(), 'this fails', 'Nothing is added if parameter isn\'t an array');
// @Test: ->get()
$foo = $paramHolder->get('foo');
$t->is($foo, 42, 'The value is retrieved correctly');
$rex = $paramHolder->get('rex');
$t->is($rex, null, 'NULL is returned if the parameter could not be found');
$rex = $paramHolder->get('rex', 'fido');
$t->is($rex, 'fido', 'If a default value is given for a non-existing parameter, the default value is returned');
// @Test: ->getNames()
$t->same($paramHolder->getNames(), array('foo', 'bar', 'baz'), 'All parameter names are retrieved correctly');
// @Test: ->has()
$t->is($paramHolder->has('foo'), true, 'TRUE is returned if the parameter exists');
$t->is($paramHolder->has('rex'), false, 'FALSE is returned if the parameter does not exist');
// @Test: ->remove()
$ret = $paramHolder->remove('bar');
$t->is($paramHolder->has('bar'), false, 'The parameter was removed correctly');
$t->is($ret, 4711, 'When removing a paramter, its value is returned correctly');
$ret = $paramHolder->remove('rex');
$t->is($ret, null, 'NULL is returned for non-existing parameters');
$ret = $paramHolder->remove('rex', 'fido');
$t->is($ret, 'fido', 'If a default value is given for a non-existing parameter, the default value is returned');
// @Test: ->set()
$paramHolder->set('rex', 'fido');
$t->is($paramHolder->get('rex'), 'fido', 'New parameters are set correctly');
// @Test: ->setByRef()
Ejemplo n.º 3
0
$t->is($safe->doSomething(), 'ok', '->__call() invokes the embedded method');
// ->__isset() ->__unset()
$t->diag('->__isset() ->__unset()');
class TestClass3
{
    public $boolValue = true, $nullValue = null;
}
$safe = new SafeDecorator(new TestClass3());
$t->is(isset($safe->boolValue), true, '->__isset() returns true if the property is not null');
$t->is(isset($safe->nullValue), false, '->__isset() returns false if the property is null');
$t->is(isset($safe->undefinedValue), false, '->__isset() returns false if the property does not exist');
unset($safe->boolValue);
$t->is(isset($safe->boolValue), false, '->__unset() unsets the embedded property');
// Iterator
$t->diag('Iterator');
$input = array('one' => 1, 'two' => 2, 'three' => 3, 'children' => array(1, 2, 3));
$output = array();
$safe = new SafeDecorator($input);
foreach ($safe as $key => $value) {
    $output[$key] = $value;
}
$t->same($output, $input, '"Iterator" implementation imitates an array');
// ArrayAccess
$t->diag('ArrayAccess');
$safe = new SafeDecorator(array('foo' => 'bar'));
$t->is($safe['foo'], 'bar', '"ArrayAccess" implementation returns a value from the embedded array');
$safe['foo'] = 'baz';
$t->is($safe['foo'], 'baz', '"ArrayAccess" implementation sets a value on the embedded array');
$t->is(isset($safe['foo']), true, '"ArrayAccess" checks if a value is set on the embedded array');
unset($safe['foo']);
$t->is(isset($safe['foo']), false, '"ArrayAccess" unsets a value on the embedded array');