diff --git a/lib/CharacterData.php b/lib/CharacterData.php index 130db49..26dd191 100644 --- a/lib/CharacterData.php +++ b/lib/CharacterData.php @@ -27,13 +27,12 @@ abstract class CharacterData extends Node { public function appendData(string $data) { - // PHP's DOM does this correctly already. - return $this->innerNode->appendData($data); + $this->innerNode->data .= $data; } public function deleteData(int $offset, int $count) { // PHP's DOM does this correctly already. - return $this->innerNode->deleteData($data); + return $this->innerNode->deleteData($offset, $count); } public function insertData(int $offset, string $data) { diff --git a/tests/cases/TestCharacterData.php b/tests/cases/TestCharacterData.php new file mode 100644 index 0000000..a5ac977 --- /dev/null +++ b/tests/cases/TestCharacterData.php @@ -0,0 +1,220 @@ +createTextNode('ookeek'); + $this->assertEquals(6, $t->length); + $t->appendData('💩'); + $this->assertEquals(7, $t->length); + + $d = new XMLDocument(); + $t = $d->createCDATASection('ookeek'); + $this->assertEquals(6, $t->length); + $t->appendData('💩'); + $this->assertEquals(7, $t->length); + } + + + /** + * @covers \MensBeam\HTML\DOM\CharacterData::deleteData + * + * @covers \MensBeam\HTML\DOM\CharacterData::__get_data + * @covers \MensBeam\HTML\DOM\CharacterData::__set_data + * @covers \MensBeam\HTML\DOM\Document::__construct + * @covers \MensBeam\HTML\DOM\Document::createTextNode + * @covers \MensBeam\HTML\DOM\DOMImplementation::__construct + * @covers \MensBeam\HTML\DOM\Node::__construct + * @covers \MensBeam\HTML\DOM\Text::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::get + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::has + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::key + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::set + * @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor + */ + public function testMethod_deleteData(): void { + $d = new Document(); + $t = $d->createTextNode('ook eek'); + $t->deleteData(3, 1); + $this->assertSame('ookeek', $t->data); + $t->data = 'ook💩eek'; + $t->deleteData(3, 1); + $this->assertSame('ookeek', $t->data); + } + + + /** + * @covers \MensBeam\HTML\DOM\CharacterData::insertData + * + * @covers \MensBeam\HTML\DOM\CharacterData::__get_data + * @covers \MensBeam\HTML\DOM\Document::__construct + * @covers \MensBeam\HTML\DOM\Document::createTextNode + * @covers \MensBeam\HTML\DOM\DOMImplementation::__construct + * @covers \MensBeam\HTML\DOM\Node::__construct + * @covers \MensBeam\HTML\DOM\Text::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::get + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::has + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::key + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::set + * @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor + */ + public function testMethod_insertData(): void { + $d = new Document(); + $t = $d->createTextNode('ookeek'); + $t->insertData(3, '💩'); + $this->assertSame('ook💩eek', $t->data); + $t->insertData(3, '💩'); + $this->assertSame('ook💩💩eek', $t->data); + } + + + /** + * @covers \MensBeam\HTML\DOM\CharacterData::replaceData + * + * @covers \MensBeam\HTML\DOM\CharacterData::__get_data + * @covers \MensBeam\HTML\DOM\Document::__construct + * @covers \MensBeam\HTML\DOM\Document::createTextNode + * @covers \MensBeam\HTML\DOM\DOMImplementation::__construct + * @covers \MensBeam\HTML\DOM\Node::__construct + * @covers \MensBeam\HTML\DOM\Text::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::get + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::has + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::key + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::set + * @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor + */ + public function testMethod_replaceData(): void { + $d = new Document(); + $t = $d->createTextNode('ook💩💩eek'); + $t->replaceData(3, 2, ' '); + $this->assertSame('ook eek', $t->data); + } + + + /** + * @covers \MensBeam\HTML\DOM\CharacterData::substringData + * + * @covers \MensBeam\HTML\DOM\Document::__construct + * @covers \MensBeam\HTML\DOM\Document::createTextNode + * @covers \MensBeam\HTML\DOM\DOMImplementation::__construct + * @covers \MensBeam\HTML\DOM\Node::__construct + * @covers \MensBeam\HTML\DOM\Text::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::get + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::has + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::key + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::set + * @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor + */ + public function testMethod_substringData(): void { + $d = new Document(); + $t = $d->createTextNode('ook💩💩eek'); + $this->assertSame('💩💩', $t->substringData(3, 2)); + } + + + /** + * @covers \MensBeam\HTML\DOM\CharacterData::__get_data + * @covers \MensBeam\HTML\DOM\CharacterData::__set_data + * + * @covers \MensBeam\HTML\DOM\Document::__construct + * @covers \MensBeam\HTML\DOM\Document::createCDATASection + * @covers \MensBeam\HTML\DOM\Document::createTextNode + * @covers \MensBeam\HTML\DOM\DOMImplementation::__construct + * @covers \MensBeam\HTML\DOM\Node::__construct + * @covers \MensBeam\HTML\DOM\Text::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::get + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::has + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::key + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::set + * @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor + */ + public function testProperty_data(): void { + $d = new Document(); + $t = $d->createTextNode('ook'); + // Getting is thoroughly tested, setting isn't. + $t->data = 'eek'; + $this->assertSame('eek', $t->data); + + $d = new XMLDocument(); + $t = $d->createCDATASection('ook'); + $t->data = 'eek'; + $this->assertSame('eek', $t->data); + } + + + /** + * @covers \MensBeam\HTML\DOM\CharacterData::__get_length + * + * @covers \MensBeam\HTML\DOM\CharacterData::__get_data + * @covers \MensBeam\HTML\DOM\CharacterData::__set_data + * @covers \MensBeam\HTML\DOM\Document::__construct + * @covers \MensBeam\HTML\DOM\Document::createCDATASection + * @covers \MensBeam\HTML\DOM\Document::createTextNode + * @covers \MensBeam\HTML\DOM\DOMImplementation::__construct + * @covers \MensBeam\HTML\DOM\Node::__construct + * @covers \MensBeam\HTML\DOM\Text::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::__construct + * @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::get + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::has + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::key + * @covers \MensBeam\HTML\DOM\Inner\NodeCache::set + * @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor + */ + public function testProperty_length(): void { + $d = new Document(); + $t = $d->createTextNode('ookeek'); + $this->assertEquals(6, $t->length); + $t->data .= '💩'; + $this->assertEquals(7, $t->length); + + $d = new XMLDocument(); + $t = $d->createCDATASection('ookeek'); + $this->assertEquals(6, $t->length); + $t->data .= '💩'; + $this->assertEquals(7, $t->length); + } +} \ No newline at end of file diff --git a/tests/phpunit.dist.xml b/tests/phpunit.dist.xml index b25ef5e..7d6d314 100644 --- a/tests/phpunit.dist.xml +++ b/tests/phpunit.dist.xml @@ -17,6 +17,7 @@ cases/TestAttr.php + cases/TestCharacterData.php cases/TestDocument.php cases/TestDocumentOrElement.php cases/TestDOMImplementation.php