/** * Tests `Collection::rewind` and `Collection::current`. */ public function testNextRewindCurrent() { $collection = new DocumentSet(array('data' => array('title' => 'Lorem Ipsum', 'value' => 42, 'foo' => 'bar'))); $this->assertEqual('Lorem Ipsum', $collection->current()); $this->assertEqual(42, $collection->next()); $this->assertEqual('bar', $collection->next()); $this->assertEqual('Lorem Ipsum', $collection->rewind()); $this->assertEqual(42, $collection->next()); }
public function testPopulateResourceClose() { $resource = new MockResult(); $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource)); $model = $this->_model; $result = $doc->rewind(); $this->assertTrue($result instanceof Document); $this->assertTrue(is_object($result['_id'])); $expected = array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'); $this->assertEqual($expected, $result->data()); $expected = array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'); $this->assertEqual($expected, $doc->next()->data()); $expected = array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'); $result = $doc->next()->data(); $this->assertEqual($expected, $result); $this->assertNull($doc->next()); }
public function testSetMultiple() { $doc = new DocumentSet(array('model' => $this->_model)); $doc->set(array(array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one'), array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two'), array('id' => 3, 'name' => 'Three', 'content' => 'Lorem ipsum three'))); $expected = array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one'); return; $result = $doc->current()->data(); $this->assertEqual($expected, $result); $expected = array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two'); $result = $doc->next()->data(); $this->assertEqual($expected, $result); }
public function testPopulateResourceClose() { $result = new MockResult(array('data' => array(array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib')))); $doc = new DocumentSet(array('model' => $this->_model, 'result' => $result)); $result = $doc->rewind(); $this->assertInstanceOf('lithium\\data\\entity\\Document', $result); $this->assertInternalType('string', $result['_id']); $expected = array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'); $this->assertEqual($expected, $result->data()); $expected = array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'); $this->assertEqual($expected, $doc->next()->data()); $expected = array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'); $result = $doc->next()->data(); $this->assertEqual($expected, $result); $this->assertFalse($doc->next()); }
/** * Tests that `Document`s with embedded objects are cast to arrays so as not to cause fatal * errors when traversing via array interfaces. * * @return void */ public function testObjectIteration() { $doc = new DocumentSet(array('data' => array((object) array('foo' => 'bar'), (object) array('bar' => 'foo')))); $result = $doc->first()->foo; $expected = 'bar'; $this->assertEqual($expected, $result); $result = $doc->next()->bar; $expected = 'foo'; $this->assertEqual($expected, $result); $doc = new Document(array('data' => (object) array('first' => array('foo' => 'bar'), 'second' => array('bar' => 'foo')))); $result = $doc->first->foo; }