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:
<?php
namespace Nethgui\Test\Unit\Nethgui\Adapter\ParameterSet;
class WithAdaptersTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = new \Nethgui\Adapter\ParameterSet;
$this->arrayAdapter = $this->getMockBuilder('\Nethgui\Adapter\ArrayAdapter')
->disableOriginalConstructor()
->getMock();
$this->scalarAdapter = $this->getMockBuilder('\Nethgui\Adapter\ScalarAdapter')
->disableOriginalConstructor()
->getMock();
$this->object->addAdapter($this->arrayAdapter, 'arrayAdapter');
$this->object->addAdapter($this->scalarAdapter, 'scalarAdapter');
$this->object['inner'] = $this->getMock('\Nethgui\Adapter\ParameterSet');
$this->object['pi'] = 3.14;
}
public function testCount()
{
$this->assertEquals(4, $this->object->count());
}
public function testUserScalarAdapter()
{
$this->scalarAdapter->expects($this->once())
->method('set')
->with('VALUE');
$this->object['scalarAdapter'] = 'VALUE';
}
public function testUseArrayAdapter()
{
$this->arrayAdapter->expects($this->once())
->method('offsetSet')
->with(1, 'NEW');
$this->arrayAdapter->expects($this->exactly(2))
->method('get')
->will($this->returnValue($this->arrayAdapter));
$this->arrayAdapter->expects($this->once())
->method('offsetGet')
->with(1)
->will($this->returnValue('VALUE'));
$this->assertEquals('VALUE', $this->object['arrayAdapter'][1]);
$this->object['arrayAdapter'][1] = 'NEW';
}
public function testSaveClean()
{
$this->arrayAdapter->expects($this->once())
->method('save');
$this->scalarAdapter->expects($this->once())
->method('save');
$this->object['inner']->expects($this->once())
->method('save');
$this->assertEquals(FALSE, $this->object->save());
}
public function testSaveDirty()
{
$this->arrayAdapter->expects($this->once())
->method('save')
->will($this->returnValue(1))
;
$this->scalarAdapter->expects($this->once())
->method('save')
->will($this->returnValue(1))
;
$this->object['inner']->expects($this->once())
->method('save')
->will($this->returnValue(3))
;
$this->assertEquals(TRUE, $this->object->save());
}
public function testGetKeys()
{
$this->assertEquals(array('arrayAdapter', 'scalarAdapter', 'inner', 'pi'), $this->object->getKeys());
}
public function testGetKeys1()
{
$this->object['pi'] = 6.28;
$this->assertEquals(array(), $this->object->getModifiedKeys());
}
public function testGetKeys2()
{
$mockModifiable = $this->getMock('Nethgui\Adapter\ModifiableInterface');
$mockModifiable->expects($this->once())
->method('isModified')
->will($this->returnValue('TRUE'));
$this->object['modifiable'] = $mockModifiable;
$this->assertEquals(array('modifiable'), $this->object->getModifiedKeys());
}
public function testGetAdapter()
{
$this->assertNull($this->object->getAdapter('pi'));
$this->assertInstanceOf('Nethgui\Adapter\AdapterInterface', $this->object->getAdapter('scalarAdapter'));
}
public function testOffsetUnset()
{
$this->object->offsetUnset('scalarAdapter');
$this->assertFalse($this->object->offsetExists('scalarAdapter'));
}
public function testIsModified()
{
$this->object['pi'] = 6.28;
$this->assertFalse($this->object->isModified());
$mockModifiable = $this->getMock('Nethgui\Adapter\ModifiableInterface');
$mockModifiable->expects($this->once())
->method('isModified')
->will($this->returnValue('TRUE'));
$this->object['modifiable'] = $mockModifiable;
$this->assertTrue($this->object->isModified());
}
public function testIteratorAndArrayInterface()
{
$object = new \Nethgui\Adapter\ParameterSet();
$object['a'] = 1;
$object['b'] = 2;
$object['c'] = array();
$this->assertEquals(array('a' => 1, 'b' => 2, 'c' => array()), iterator_to_array($object));
$this->assertEquals(array('a' => 1, 'b' => 2, 'c' => array()), $object->getArrayCopy());
}
}