/** @test */ public function itShouldAppendCdataSections() { $xml = new SimpleXMLElement('<node></node>'); $xml->addCDATASection('string'); $this->assertXmlStringEqualsXmlString('<node><![CDATA[string]]></node>', $xml->asXML()); if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.7', '<')) { $this->markTestIncomplete('HHVM segmentation fault.'); } $xml = new SimpleXMLElement('<node></node>'); $xml->addCDATASection(new SimpleXML('<data>string</data>')); $this->assertXmlStringEqualsXmlString('<node><![CDATA[<data>string</data>]]></node>', $xml->asXML()); $xml = new SimpleXMLElement('<node></node>'); $dom = new DOM(); $dom->loadXML('<data>string</data>'); $xml->addCDATASection($dom); $this->assertXmlStringEqualsXmlString('<node><![CDATA[<data>string</data>]]></node>', $xml->asXML()); $xml = new SimpleXMLElement('<node></node>'); $dom = new DOMDocument(); $dom->loadXML('<root><data>string</data></root>'); $xml->addCDATASection($dom->xpath('data')->item(0)); $this->assertXmlStringEqualsXmlString('<node><![CDATA[<data>string</data>]]></node>', $xml->asXML()); $xml = new SimpleXMLElement('<node></node>'); try { $xml->addCDATASection([]); } catch (\InvalidArgumentException $e) { return $this->assertTrue(true); } }
/** @test */ public function itShouldAppendElementToItsFirstChildIfNoReferenceIsGiven() { $dom = new DOMDocument(); $firstChild = $dom->createElement('data'); $nextChild = $dom->createElement('foo', 'bar'); $dom->appendChild($firstChild); $dom->appendDomElement($nextChild); $list = $dom->xpath('foo'); $this->assertSame($list->item(0), $nextChild); }
/** * setElementValue * * @param DOMDocument $dom * @param DOMElement $node * @param mixed $value * * @return null|bool returns false if no value was set, else null; */ private function setElementValue(DOMDocument $dom, DOMElement $node, $value = null) { if ($value instanceof \SimpleXMLElement) { return $node->appendChild($dom->importNode(dom_import_simplexml($value), true)); } if ($value instanceof \DOMDocument) { return $node->appendDomElement($value->firstChild); } if ($value instanceof \DOMElement) { return $dom->appendDomElement($value, $node); } if (is_array($value) || $value instanceof \Traversable) { return $this->buildXML($dom, $node, $value); } if (is_int($value) || is_float($value)) { return $this->createText($dom, $node, (string) $value); } if (is_bool($value)) { return $this->createText($dom, $node, $value ? 'true' : 'false'); } if (is_string($value) || null === $value) { return $this->appendTextNode($dom, $node, (string) $value); } return false; }
/** @test */ public function staticHelperTest() { $dom = new DOMDocument(); $elementParent = $dom->createElement('data'); $elementChild = $dom->createElement('foo', 'bar'); $elementParent->appendChild($elementChild); $this->assertEquals(['foo' => 'bar'], Parser::getPhpValue($elementParent)); $this->assertNull(Parser::getPhpValue('')); $this->assertSame(10, Parser::getPhpValue('10')); $this->assertSame(1.2, Parser::getPhpValue('1.2')); $this->assertTrue(Parser::getPhpValue('true')); $this->assertFalse(Parser::getPhpValue('false')); $this->assertSame(3840, Parser::getPhpValue('0xF00')); $this->assertSame('0xNaN', Parser::getPhpValue('0xNaN')); }