root()->appendElement('x');
$child = $moved->appendElement('y');
$moved->moveTo($target->root());
$source->close();
$this->assertTrue($moved->document() === $target);
$this->assertTrue($child->document() === $target);
$this->assertSame('y', $child->name());
$this->assertSame('', $target->toXml());
}
/**
* Корень документа переносить нельзя — HIERARCHY_VIOLATION, дерево не меняется.
* The document root cannot be moved — HIERARCHY_VIOLATION, the tree does not change.
*/
public function testRootCannotBeMoved(): void
{
$source = Document::create('a');
$target = Document::create('b');
$error = $this->assertThrows(BerestaException::class, fn() => $source->root()->moveTo($target->root()));
$this->assertSame(Status::HierarchyViolation, $error->status());
$this->assertSame('', $source->toXml());
}
/**
* copyTo — независимая копия в другом документе.
* copyTo makes an independent copy in another document.
*/
public function testCopyIsIndependent(): void
{
$source = Document::create('a');
$original = $source->root()->appendElement('x');
$target = Document::create('b');
$copy = $original->copyTo($target->root());
$copy->setText('копия');
$this->assertTrue($copy->document() === $target);
$this->assertSame('', $original->text());
$this->assertSame('копия', $target->toXml());
}
/**
* Один узел — один объект: разные пути к узлу дают тот же объект.
* One node — one object: different paths to the node give the same object.
*/
public function testOneObjectPerNode(): void
{
$document = Document::create('a');
$first = $document->root()->appendElement('x');
$document->root()->appendElement('y');
$this->assertTrue($document->root()->firstChild() === $first);
$this->assertTrue($document->root()->child(1)->previousSibling() === $first);
$this->assertTrue($first->parent() === $document->root());
$this->assertTrue($document->root()->parent() === $document);
}
/**
* remove() убирает узел с потомками. / remove() takes the node away with its descendants.
*/
public function testRemove(): void
{
$document = Document::create('a');
$document->root()->appendElement('x')->appendText('t');
$document->root()->appendComment('c');
$document->root()->firstChild()->remove();
$this->assertSame(1, count($document->root()));
$this->assertSame('', $document->toXml());
}
}