/** * Tests that arrays of nested objects can be appended to and will be updated using the proper * MongoDB operators. */ public function testAppendingNestedObjectArray() { $model = $this->_model; $model::schema(false); $model::schema(array('accounts' => array('type' => 'object', 'array' => true), 'accounts.name' => array('type' => 'string'))); $doc = new Document(compact('model')); $this->assertEqual(array(), $doc->accounts->data()); $doc->sync(); $data = array('name' => 'New account'); $doc->accounts[] = new Document(compact('data')); $result = Exporter::get('update', $doc->export()); $expected = array('update' => array('accounts.0' => $data)); $this->assertEqual($expected, $result); $result = Exporter::toCommand($result); $expected = array('$set' => array('accounts.0' => $data)); $this->assertEqual($expected, $result); }
/** * Tests that when an existing object is attached as a value of another existing object, the * whole sub-object is re-written to the new value. */ public function testAppendExistingObjects() { $doc = new Document(array('exists' => true, 'data' => array( 'deeply' => new Document(array( 'pathKey' => 'deeply', 'exists' => true, 'data' => array('nested' => 'object') )), 'foo' => 'bar' ))); $append = new Document(array('exists' => true, 'data' => array('foo' => 'bar'))); $doc->deeply = $append; $result = Exporter::get('update', $doc->export()); $expected = array('update' => array('deeply' => array('foo' => 'bar'))); $this->assertEqual($expected, $result); $expected = array('$set' => array('deeply' => array('foo' => 'bar'))); $this->assertEqual($expected, Exporter::toCommand($result)); $doc->sync(); $doc->append2 = new Document(array('exists' => false, 'data' => array('foo' => 'bar'))); $expected = array('update' => array('append2' => array('foo' => 'bar'))); $this->assertEqual($expected, Exporter::get('update', $doc->export())); $doc->sync(); $this->assertFalse(Exporter::get('update', $doc->export())); $doc->append2->foo = 'baz'; $doc->append2->bar = 'dib'; $doc->deeply->nested = true; $expected = array('update' => array( 'append2.foo' => 'baz', 'append2.bar' => 'dib', 'deeply.nested' => true )); $this->assertEqual($expected, Exporter::get('update', $doc->export())); }