1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
<?php
namespace Nethgui\Test\Unit\Nethgui\Serializer;
class ArrayAccessSerializerTest extends \PHPUnit_Framework_TestCase
{
protected $tests = array();
protected $data;
protected function setUp()
{
$a = array(
'A' => array('f0' => 'a0', 'f1' => 'a1', 'f2' => 'a2'),
'B' => array('f0' => 'b0', 'f1' => 'b1', 'f2' => 'b2'),
'C' => array('f0' => 'c0', 'f1' => 'c1', 'f2' => 'c2'),
);
$this->data = new \ArrayObject($a);
foreach ($a as $rowKey => $row) {
foreach ($row as $fieldKey => $value) {
$this->tests[] = array(
$rowKey,
$fieldKey,
$value,
new \Nethgui\Serializer\ArrayAccessSerializer($this->data, $rowKey, $fieldKey)
);
}
}
}
public function testRead()
{
foreach ($this->tests as $args) {
list($rowKey, $fieldKey, $value, $object) = $args;
$this->assertEquals($value, $object->read());
}
}
public function testReadNonExistingRow()
{
$object = new \Nethgui\Serializer\ArrayAccessSerializer($this->data, 'X', 'f0');
$this->assertNull($object->read());
}
public function testReadNonExistingField()
{
$object = new \Nethgui\Serializer\ArrayAccessSerializer($this->data, 'A', 'x0');
$this->assertNull($object->read());
}
public function testReadInvalidData()
{
$data = array(
'A' => 'x0',
'B' => array('1', '2'),
);
$object = new \Nethgui\Serializer\ArrayAccessSerializer(new \ArrayObject($data), 'A', 'x0');
$object->read();
}
public function testWriteUpdate()
{
foreach ($this->tests as $args) {
list($rowKey, $fieldKey, $value, $object) = $args;
$this->assertEquals($value, $this->data[$rowKey][$fieldKey]);
$object->write('UU');
$this->assertEquals('UU', $this->data[$rowKey][$fieldKey]);
}
}
public function testWriteAppend()
{
$object = new \Nethgui\Serializer\ArrayAccessSerializer($this->data, 'C', 'f2');
$object->write('c2');
$this->assertEquals('c2', $this->data['C']['f2']);
}
public function testWriteUnchanded()
{
$object = new \Nethgui\Serializer\ArrayAccessSerializer($this->data, 'D', 'f0');
$object->write('AA');
$this->assertEquals('AA', $this->data['D']['f0']);
$this->assertEquals(array('f0' => 'AA'), $this->data['D']);
}
public function testInvalidSubscript1()
{
new \Nethgui\Serializer\ArrayAccessSerializer($this->data, array(), 1.12);
}
public function testEmptySubscript1()
{
new \Nethgui\Serializer\ArrayAccessSerializer($this->data);
}
public function testNullSubscript()
{
$data = array(
'X' => '123',
'Y' => '456'
);
$o = new \Nethgui\Serializer\ArrayAccessSerializer(new \ArrayObject($data), 'X', NULL);
$this->assertEquals('123', $o->read());
}
}