$parent->addChild($child);
$t->is($parent->getChildren(), array('child' => $child), '->addChild() adds a child');
$parent->addChild($child);
$t->is($parent->getChildren(), array('child' => $child), '->addChild() does not add a child twice');
try {
    $msg = '->addChild() rejects circular children';
    $child->addChild($parent);
    $t->fail($msg);
} catch (Exception $e) {
    $t->pass($msg);
}
$grandparent = new xfDocument('grandparent');
$parent = new xfDocument('parent');
$child = new xfDocument('child');
$grandchild = new xfDocument('grandchild');
$grandparent->addChild($parent);
$parent->addChild($child);
try {
    $msg = '->addChild() accepts long linear children';
    $child->addChild($grandchild);
    $t->pass($msg);
} catch (Exception $e) {
    $t->fail($msg);
}
try {
    $msg = '->addChild() rejects long circular children';
    $grandchild->addChild($grandparent);
    $t->fail($msg);
} catch (Exception $e) {
    $t->pass($msg);
}
    $doc->addField($field);
    $doc->addField(Zend_Search_Lucene_Field::UnIndexed('__boosts', serialize(array('test' => 1))));
    $response = $engine->unwriteDocument($doc)->getField('test')->getField()->getType();
    $t->is($response, $type, '->unwriteDocument() can exclusively handle "' . $property . '"');
}
$t->diag('->add()');
$doc = new xfDocument('guid');
$doc->addField(new xfFieldValue(new xfField('name', xfField::KEYWORD), 'carl'));
$doc->addField(new xfFieldValue(new xfField('age', xfField::STORED), 18));
$engine->add($doc);
$engine->commit();
$t->is($engine->count(), 1, '->add() adds a document');
$parent = new xfDocument('parent');
$child = new xfDocument('child');
$pet = new xfDocument('pet');
$parent->addChild($child);
$child->addChild($pet);
$engine->add($parent);
$engine->commit();
$t->is($engine->count(), 4, '->add() adds a document and every subdocument');
$t->diag('->findGuid()');
$doc = $engine->findGuid('guid');
$t->isa_ok($doc, 'xfDocument', '->findGuid() returns an xfDocument');
$t->is($doc->getGuid(), 'guid', '->findGuid() returns the correct document');
$doc = $engine->findGuid('parent');
$children = $doc->getChildren();
$t->is(count($children), 1, '->findGuid() rebuilds subdocuments correctly');
$t->is($children['child']->getGuid(), 'child', '->findGuid() rebuilds subdocuments in correct order');
$children = $children['child']->getChildren();
$t->is($children['pet']->getGuid(), 'pet', '->findGuid() rebuilds subdocuments recursively');
try {
 /**
  * Unrewrites a Zend_Search_Lucene document into a xfDocument
  *
  * @param Zend_Search_Lucene_Document $zdoc
  * @returns xfDocument
  */
 public function unwriteDocument(Zend_Search_Lucene_Document $zdoc)
 {
     $doc = new xfDocument($zdoc->getFieldValue('__guid'));
     $boosts = unserialize($zdoc->getFieldValue('__boosts'));
     foreach ($zdoc->getFieldNames() as $name) {
         // ignore internal fields
         if (substr($name, 0, 2) != '__') {
             $zfield = $zdoc->getField($name);
             $type = 0;
             if ($zfield->isStored) {
                 $type |= xfField::STORED;
             }
             if ($zfield->isIndexed) {
                 $type |= xfField::INDEXED;
             }
             if ($zfield->isTokenized) {
                 $type |= xfField::TOKENIZED;
             }
             if ($zfield->isBinary) {
                 $type |= xfField::BINARY;
             }
             $field = new xfField($name, $type);
             $field->setBoost($boosts[$name]);
             $value = new xfFieldValue($field, $zfield->value);
             $doc->addField($value);
         }
     }
     foreach (unserialize($zdoc->getFieldValue('__sub_documents')) as $guid) {
         $doc->addChild($this->findGuid($guid));
     }
     return $doc;
 }