* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class RecordTest extends TestCase { public function testGetProperty() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record->getProperty('id')); $this->assertEquals('bar', $record->getProperty('title')); } public function testSetProperty() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record->getProperty('id')); $record->setProperty('id', 2); $this->assertEquals(2, $record->getProperty('id')); } public function testRemoveProperty() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertTrue($record->hasProperty('id')); $record->removeProperty('id'); $this->assertFalse($record->hasProperty('id')); } public function testHasProperty() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertTrue($record->hasProperty('id')); } public function testIsEmpty() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertTrue($record->hasProperty('id')); } public function testMerge() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $record->merge(['foo' => 'bar']); $record->merge(Record::fromArray(['bar' => 'bar'])); $this->assertTrue($record->hasProperty('id')); } public function testOffsetSet() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record['id']); $record['id'] = 2; $this->assertEquals(2, $record['id']); } public function testOffsetExists() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertTrue(isset($record['id'])); } public function testOffsetUnset() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertTrue(isset($record['id'])); unset($record['id']); $this->assertFalse(isset($record['id'])); } public function testGetMagicGetter() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record->id); $this->assertEquals('bar', $record->title); } public function testOffsetGet() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record['id']); $this->assertEquals('bar', $record['title']); } public function testSetMagicSetter() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record->id); $this->assertEquals('bar', $record->title); $record->id = 2; $record->title = 'foo'; $this->assertEquals(2, $record->id); $this->assertEquals('foo', $record->title); } public function testGetProperties() { $fields = array( 'id' => 1, 'title' => 'bar', ); $record = new Record($fields); $this->assertEquals($fields, $record->getProperties()); } public function testFilter() { $fields = array( 'id' => 1, 'title' => 'bar', ); $record = new Record($fields); $record->filter(function($value){ return $value !== 1; }); $this->assertEquals(['title' => 'bar'], $record->getProperties()); } public function testMap() { $fields = array( 'id' => 1, 'title' => 'bar', ); $record = new Record($fields); $record->map(function($value){ return strtoupper($value); }); $this->assertEquals(['id' => 1, 'title' => 'BAR'], $record->getProperties()); } public function testSerialize() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $this->assertEquals(1, $record->id); $this->assertEquals('bar', $record->title); $record = unserialize(serialize($record)); $this->assertEquals(1, $record->id); $this->assertEquals('bar', $record->title); } public function testJsonEncode() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $expect = '{"id": 1, "title": "bar"}'; $this->assertJsonStringEqualsJsonString($expect, json_encode($record)); } public function testJsonEncodeEmpty() { $record = new Record([]); $expect = '{}'; $this->assertJsonStringEqualsJsonString($expect, json_encode($record)); } public function testBadProperty() { $record = new Record(array( 'id' => 1, 'title' => 'bar', )); $result = $record->foo; $this->assertNull($result); } public function testFromArray() { $record = Record::fromArray([ 'id' => 1, 'title' => 'bar', ]); $this->assertInstanceOf(RecordInterface::class, $record); $this->assertEquals(1, $record->id); $this->assertEquals('bar', $record->title); } public function testFromStdClass() { $record = Record::fromStdClass((object) [ 'id' => 1, 'title' => 'bar', ]); $this->assertInstanceOf(RecordInterface::class, $record); $this->assertEquals(1, $record->id); $this->assertEquals('bar', $record->title); } public function testFrom() { $record = Record::from(['foo' => 'bar']); $this->assertEquals(['foo' => 'bar'], Record::from(['foo' => 'bar'])->getProperties()); $this->assertEquals(['foo' => 'bar'], Record::from((object) ['foo' => 'bar'])->getProperties()); $this->assertEquals(['foo' => 'bar'], Record::from($record)->getProperties()); } public function testFromInvalid() { $this->expectException(\InvalidArgumentException::class); Record::from('foo'); } public function testIterator() { $record = Record::fromArray(['id' => 1, 'foo' => 'bar']); $i = 0; foreach ($record as $key => $value) { if ($i === 0) { $this->assertEquals('id', $key); $this->assertEquals(1, $value); } elseif ($i === 1) { $this->assertEquals('foo', $key); $this->assertEquals('bar', $value); } $i++; } $this->assertEquals(2, $i); $actual = iterator_to_array($record->getIterator()); $expect = ['id' => 1, 'foo' => 'bar']; $this->assertEquals($expect, $actual); } public function testIsset() { $record = Record::fromArray(['id' => 1, 'foo' => 'bar']); $this->assertTrue(isset($record->id)); $this->assertFalse(isset($record->bar)); } public function testUnset() { $record = Record::fromArray(['id' => 1, 'foo' => 'bar']); $this->assertTrue(isset($record->id)); unset($record->id); $this->assertFalse(isset($record->id)); } public function testSetState() { $oldRecord = new Record(['id' => 1, 'foo' => 'bar']); $newRecord = eval('return ' . var_export($oldRecord, true) . ';'); $this->assertInstanceOf(RecordInterface::class, $newRecord); $this->assertEquals(['id' => 1, 'foo' => 'bar'], $newRecord->getProperties()); } } __halt_compiler();----SIGNATURE:----uespYyS9a+7Di/VXO1H1l0NDjW7gmIVyDrwJyOhi24M9sZpyHdx52pIQp9fxAprWkmuqADjrR63qxHRjV8uL/RtB5BOJ+g9Algmpm6qYTDr+TEJ3PnHax83B6NfLWMZVCkonz3HljxvFfRmmFVyfl0Le7I56mdTkWcYjtogHsAjoCFRLrjADEuYFEbbRQm4MP/pDAjsOyHVYTVgobTAugi+SJovdTi4PeU2N6SsR7SOMvxi3csujzVRAU9gdKhJHj49uxNURjbhamBcBYBKOyHXnn3mWGNiyeJ7hC63AYsAhs4OiLsDjFwpyqv9WYjPn9/NwnsrfAVTYmhtWSZFSND7CWxGmeNJ/zjlroYF3eFs46Aii3mWbkQ/hqNo1AUbwdrDhvrpehu84Te2JuCWkrm9TRB1kEaNnq18nOXp6z0slMeAtkTmZx/4zWtvXlr2g/dGOEFMPCqtkyG3DCfC+xSpgDSqxQboyLWf+bQS4qqXaZpQJjrodsp98w+Tv17GpK4G+4c1O8cZ6magZ7xD+JyLnfHwk0JvpaMEKZg+pOuh2LrmrOhMYr6OJxQkXzRuFwYZRi1CRgSIgiHv3IHUWlFq/d5m3AbITjiqtBdRhsqs5oWAOIWmEyvrPRaNxcTf0YJpVLEEbqVe11jFS+QPO3oUslEs+saBYa/y0h/hVQ64=----ATTACHMENT:----NzU4MzgzMzk5NzE4Mjg2NCA2MTQzOTU0MjE3NTgyNTc0IDg0ODY1OTA4OTgwNjE1NjI=