Inheritance: extends lithium\data\Collection
Ejemplo n.º 1
0
 public function testData()
 {
     $collection = new DocumentSet();
     $data = array('Lorem Ipsum', 'value', 'bar');
     $collection->set($data);
     $this->assertEqual($data, $collection->data());
 }
Ejemplo n.º 2
0
 public function testAccessorMethods()
 {
     $model = $this->_model;
     $model::config(array('connection' => false));
     $collection = new DocumentSet(compact('model'));
     $this->assertEqual($model, $collection->model());
     $this->assertEqual(compact('model'), $collection->meta());
 }
Ejemplo n.º 3
0
 public function testMappingToNewDocumentSet()
 {
     $result = new MockResult();
     $model = $this->_model;
     $doc = new DocumentSet(compact('model', 'result'));
     $mapped = $doc->map(function ($data) {
         return $data;
     });
     $this->assertEqual($doc->data(), $mapped->data());
     $this->assertEqual($model, $doc->model());
     $this->assertEqual($model, $mapped->model());
 }
Ejemplo n.º 4
0
 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());
 }
 /**
  * Lazy-loads a document from a query using a reference to a database adapter and a query
  * result resource.
  *
  * @param array $data
  * @param mixed $key
  * @return array
  */
 protected function _populate($data = null, $key = null)
 {
     $item = parent::_populate($data, $key);
     $params = compact('data', 'key', 'item');
     return $this->_filter(__METHOD__, $params, function ($self, $params) {
         return $params['item'];
     });
 }
Ejemplo n.º 6
0
 public function testRewindData()
 {
     $doc = new DocumentSet(array('model' => $this->_model, 'data' => array(array('id' => 1, 'name' => 'One'), array('id' => 2, 'name' => 'Two'), array('id' => 3, 'name' => 'Three'))));
     $expected = array('id' => 1, 'name' => 'One');
     $result = $doc->rewind()->data();
     $this->assertEqual($expected, $result);
 }
Ejemplo n.º 7
0
 public function testHandlers()
 {
     $model = $this->_model;
     $schema = new DocumentSchema(array('fields' => array('_id' => array('type' => 'id'), 'date' => array('type' => 'date')), 'types' => array('date' => 'date'), 'handlers' => array('date' => function ($v) {
         return (object) $v;
     })));
     $handlers = array('stdClass' => function ($value) {
         return date('d/m/Y H:i', strtotime($value->scalar));
     });
     $array = new DocumentSet(compact('model', 'schema', 'handlers') + array('data' => array(array('_id' => '2', 'date' => '2013-06-06 13:00:00'), array('_id' => '3', 'date' => '2013-06-06 12:00:00'), array('_id' => '4', 'date' => '2013-06-06 11:00:00'))));
     $expected = array(array('_id' => '2', 'date' => '06/06/2013 13:00'), array('_id' => '3', 'date' => '06/06/2013 12:00'), array('_id' => '4', 'date' => '06/06/2013 11:00'));
     $this->assertIdentical($expected, $array->to('array', array('indexed' => false)));
 }
Ejemplo n.º 8
0
 /**
  * Tests `Collection::assignTo`.
  */
 public function testAssignTo()
 {
     $parent = new stdClass();
     $config = array('valid' => false, 'model' => $this->_model);
     $collection = new DocumentSet();
     $collection->assignTo($parent, $config);
     $this->assertEqual($this->_model, $collection->model());
     $this->assertEqual($parent, $collection->parent());
 }
 public function testTo()
 {
     Collection::formats('lithium\\net\\http\\Media');
     $resource = new MockResult();
     $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
     $expected = array('4c8f86167675abfabdbf0300' => array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), '5c8f86167675abfabdbf0301' => array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), '6c8f86167675abfabdbf0302' => array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'));
     $this->assertEqual($expected, $doc->to('array'));
     $expected = array(array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'));
     $this->assertEqual($expected, $doc->to('array', array('indexed' => false)));
 }
 public function testIndexesOnExportingDocumentSet()
 {
     $schema = new Schema(array('fields' => array('_id' => array('type' => 'id'), 'accounts' => array('type' => 'object', 'array' => true), 'accounts._id' => array('type' => 'id'), 'accounts.name' => array('type' => 'string'))));
     $data = array(array('_id' => '4c8f86167675abfabd970300', 'accounts' => array(array('_id' => "4fb6e2dd3e91581fe6e75736", 'name' => 'Foo1'), array('_id' => "4fb6e2df3e91581fe6e75737", 'name' => 'Bar1'))), array('_id' => '4c8f86167675abfabd970301', 'accounts' => array(array('_id' => "4fb6e2dd3e91581fe6e75738", 'name' => 'Foo2'), array('_id' => "4fb6e2df3e91581fe6e75739", 'name' => 'Bar2'))));
     $model = $this->_model;
     $array = new DocumentSet(compact('model', 'schema', 'data'));
     $this->assertTrue($array['4c8f86167675abfabd970300']->accounts instanceof DocumentSet);
     $this->assertTrue($array['4c8f86167675abfabd970301']->accounts instanceof DocumentSet);
     $result = Exporter::get('create', $array->export());
     $this->assertTrue(isset($result['create'][0]));
     $this->assertTrue(isset($result['create'][1]));
     $this->assertFalse(isset($result['create']['4c8f86167675abfabd970300']));
     $this->assertFalse(isset($result['create']['4c8f86167675abfabd970301']));
     $this->assertTrue(isset($result['create'][0]['accounts'][0]));
     $this->assertTrue(isset($result['create'][0]['accounts'][1]));
     $this->assertTrue(isset($result['create'][1]['accounts'][0]));
     $this->assertTrue(isset($result['create'][1]['accounts'][1]));
     $result = Exporter::get('update', $array->export());
     $this->assertTrue(isset($result['update'][0]));
     $this->assertTrue(isset($result['update'][1]));
     $this->assertFalse(isset($result['update']['4c8f86167675abfabd970300']));
     $this->assertFalse(isset($result['update']['4c8f86167675abfabd970301']));
     $this->assertTrue(isset($result['update'][0]['accounts'][0]));
     $this->assertTrue(isset($result['update'][0]['accounts'][1]));
     $this->assertTrue(isset($result['update'][1]['accounts'][0]));
     $this->assertTrue(isset($result['update'][1]['accounts'][1]));
 }
Ejemplo n.º 11
0
 /**
  * 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;
 }
Ejemplo n.º 12
0
 public function testAccessorMethods()
 {
     $collection = new DocumentSet(array('model' => 'Foo'));
     $this->assertEqual('Foo', $collection->model());
     $this->assertEqual(array('model' => 'Foo'), $collection->meta());
 }
Ejemplo n.º 13
0
	public function testSort() {
		$collection = new DocumentSet();
		$collection->set(array(
			array('id' => 1, 'name' => 'Annie'),
			array('id' => 2, 'name' => 'Zilean'),
			array('id' => 3, 'name' => 'Trynamere'),
			array('id' => 4, 'name' => 'Katarina'),
			array('id' => 5, 'name' => 'Nunu')
		));

		$collection->sort('name');

		$idsSorted = $collection->map(function ($v) { return $v['id']; })->to('array');
		$this->assertEqual($idsSorted, array(1,4,5,3,2));
	}