Sortie du snippet PHP "Changer la valeur des nœuds d'un document XML"

Environement :

  • Strangebuzz Snippet VM : 2.1.0
  • PHP : 7.4

Sortie du code exécuté :

<?xml version="1.0" encoding="iso-8859-1"?>
<data>
  <Id>123456</Id>
  <Email>example@example.com</Email>
  <Login>MrExample</Login>
  <Birthday>1979-01-01</Birthday>
</data>

<?xml version="1.0" encoding="iso-8859-1"?>
<data>
  <Id>123456</Id>
  <Email>example@example.com</Email>
  <Login></Login>
  <Birthday></Birthday>
</data>

 Retour au code  ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  Plus sur Stackoverflow  Snippet aléatoire

<?php declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
 * @covers Snippet32Trait
 */
final class Snippet32Test extends KernelTestCase
{
    /**
     * @covers Snippet32Trait::snippet32
     */
    public function testSnippet32(): void
    {
        $xml = <<<EOD
<?xml version="1.0" encoding="iso-8859-1"?>
<data>
  <Id>12345689</Id>
  <Email>example@example.com</Email>
  <Login>MrExample</Login>
  <Birthday>1979-01-01</Birthday>
</data>
EOD;

        $nodesPath = '//Login|//Birthday';
        $dom = new \DOMDocument();
        $dom->preserveWhiteSpace = false;
        $dom->loadXML($xml);

        $nodes = (new \DOMXPath($dom))->query($nodesPath);
        foreach ($nodes ?: [] as $node) {
            $this->assertNotEmpty($node->nodeValue);
        }

        foreach ($nodes ?: [] as $node) {
            $node->nodeValue = '';
        }

        foreach ($nodes ?: [] as $node) {
            $this->assertEmpty($node->nodeValue);
        }
    }
}