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: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189:
<?php
namespace Nethgui\Test\Unit\Nethgui\Adapter;
class MultipleAdapterTest extends \PHPUnit_Framework_TestCase
{
protected $object;
private $serializers;
protected function setUp()
{
$this->serializers = array(
$this->getMockBuilder('\Nethgui\Serializer\PropSerializer')
->disableOriginalConstructor()
->getMock(),
$this->getMockBuilder('\Nethgui\Serializer\PropSerializer')
->disableOriginalConstructor()
->getMock(),
);
$this->object = new \Nethgui\Adapter\MultipleAdapter(
array($this, 'readerCallback'),
array($this, 'writerCallback'),
$this->serializers
);
}
public function readerCallback($check, $v)
{
if (empty($check) || $check == 'disabled') {
return 'disabled';
}
return $v;
}
public function writerCallback($v)
{
if ($v === NULL ||
$v == 'disabled') {
return array('disabled', NULL);
}
return array('enabled', $v);
}
private function returnValues($v1, $v2)
{
$this->serializers[0]->expects($this->once())
->method('read')
->will($this->returnValue($v1));
$this->serializers[1]->expects($this->once())
->method('read')
->will($this->returnValue($v2));
}
private function expectWrites($v1, $v2)
{
$this->serializers[0]->expects($this->once())
->method('write')
->with($v1);
$this->serializers[1]->expects($this->once())
->method('write')
->with($v2);
}
public function testGet1()
{
$this->returnValues('enabled', '99');
$this->assertEquals('99', $this->object->get());
}
public function testGet2()
{
$this->returnValues('disabled', '99');
$this->assertEquals('disabled', $this->object->get());
}
public function testSetUnchanged()
{
$this->returnValues('enabled', '99');
$this->object->set('99');
$this->assertFalse($this->object->isModified());
}
public function testSetChanged()
{
$this->returnValues('disabled', '');
$this->object->set('100');
$this->assertTrue($this->object->isModified());
}
public function testDelete()
{
$this->returnValues('enabled', '99');
$this->object->delete();
$this->assertTrue($this->object->isModified());
}
public function testSaveChanged()
{
$this->returnValues('disabled', '');
$this->object->set('100');
$this->expectWrites('enabled', '100');
$this->assertTrue($this->object->save());
}
public function testSaveUnchanged()
{
$this->returnValues('enabled', '99');
$this->object->set('99');
$this->serializers[0]->expects($this->never())
->method('write');
$this->serializers[1]->expects($this->never())
->method('write');
$this->assertFalse($this->object->save());
}
public function testSaveNull()
{
$this->returnValues('enabled', '99');
$this->object->delete();
$this->expectWrites('disabled', NULL);
$this->assertTrue($this->object->save());
}
public function testNoReader()
{
new \Nethgui\Adapter\MultipleAdapter(FALSE);
}
public function testInvalidSerializers()
{
new \Nethgui\Adapter\MultipleAdapter(array($this, 'readerCallback'),
array($this, 'writerCallback'),
array(1, 2, 3)
);
}
public function testNoWriter()
{
$object = new \Nethgui\Adapter\MultipleAdapter(function () {
return '88';
});
$this->assertFalse($object->save());
$object->set('99');
$this->assertTrue($object->save());
}
public function testNoSerializer()
{
$object = new \Nethgui\Adapter\MultipleAdapter(function () {
return '88';
}, function ($value) {
return TRUE;
}, array());
$this->assertFalse($object->save());
$object->set('99');
$this->assertTrue($object->save());
}
}