<?php
class ExtendedArrayObject extends ArrayObject {
function offsetGet($offset) {
echo "Called", PHP_EOL;
return parent::offsetGet($offset);
}
}
class ExtendedArrayAccess implements ArrayAccess {
private $data = array();
function __construct(array $array = array()) {
$this->data = $array;
}
function offsetExists($offset) {
return array_key_exists($offset, $this->data);
}
function offsetGet($offset) {
return $this->data[$offset];
}
function offsetSet($offset, $value) {
$this->data[$offset] = $value;
}
function offsetUnset($offset) {
unset($this->data[$offset]);
}
}
function createInputs() {
return array(
array(),
new ExtendedArrayObject(),
new ExtendedArrayAccess(),
);
}
class Tests {
function isset_existing_key_with_not_empty_value($array) {
$array['foo'] = 1;
$this->expect(isset($array['foo']) === true, __FUNCTION__, $array);
}
function empty_existing_key_with_not_empty_value($array) {
$array['foo'] = 1;
$this->expect(empty($array['foo']) === false, __FUNCTION__, $array);
}
function isset_existing_key_with_empty_value($array) {
$array['foo'] = 0;
$this->expect(isset($array['foo']) === true, __FUNCTION__, $array);
}
function empty_existing_key_with_empty_value($array) {
$array['foo'] = 0;
$this->expect(empty($array['foo']) === true, __FUNCTION__, $array);
}
function isset_non_existent_key($array) {
$this->expect(isset($array['foo']) === false, __FUNCTION__, $array);
}
function empty_non_existent_key($array) {
$this->expect(empty($array['foo']) === true, __FUNCTION__, $array);
}
function isset_existing_key_with_null_value($array) {
$array['foo'] = null;
$this->expect(isset($array['foo']) === false, __FUNCTION__, $array);
}
function empty_existing_key_with_null_value($array) {
$array['foo'] = null;
$this->expect(empty($array['foo']) === true, __FUNCTION__, $array);
}
private function getType($v) {
return is_array($v) ? 'array' : get_class($v);
}
private function expect($condition, $function, $array) {
if (!$condition) {
echo $this->getType($array), ' failed ', $function, PHP_EOL;
}
}
}
$tests = new Tests;
foreach (get_class_methods($tests) as $test) {
foreach (createInputs() as $structure) {
$tests->$test($structure);
}
}
preferences:
48.09 ms | 402 KiB | 5 Q