ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilServicesXmlTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  public function testHeader(): void
27  {
28  $writer = new ilXmlWriter();
29  $writer->xmlHeader();
30 
31  $this->assertEquals(
32  $this->brutallyTrim("<?xml version=\"1.0\" encoding=\"utf-8\"?><!--Generated by ILIAS XmlWriter-->"),
33  $this->brutallyTrim($writer->xmlDumpMem())
34  );
35  }
36 
37  public function testStartEndTag(): void
38  {
39  $writer = new ilXmlWriter();
40  $writer->xmlStartTag('lorem');
41  $writer->xmlEndTag('lorem');
42  $this->assertEquals(
43  $this->brutallyTrim("<lorem></lorem>"),
44  $this->brutallyTrim($writer->xmlDumpMem())
45  );
46  }
47 
48  public function testElements(): void
49  {
50  $writer = new ilXmlWriter();
51  $writer->xmlStartTag('lorem');
52  $writer->xmlElement('ipsum', ['attr1' => 1], 'data1');
53  $writer->xmlElement('dolor', ['attr2' => 2, 'attr3' => 3], 'data2');
54  $writer->xmlEndTag('lorem');
55  $this->assertEquals(
56  $this->brutallyTrim(
57  "<lorem><ipsum attr1=\"1\"> data1</ipsum><dolor attr2=\"2\" attr3=\"3\"> data2</dolor></lorem>"
58  ),
59  $this->brutallyTrim($writer->xmlDumpMem())
60  );
61  }
62 
63  protected function brutallyTrim(string $string): string
64  {
65  $string = str_replace(["\n", "\r", "\t"], "", $string);
66  $string = preg_replace('# {2,}#', " ", $string);
67  $string = preg_replace("/>(\s+)</", "><", $string);
68  $string = str_replace(" >", ">", $string);
69  $string = str_replace(" <", "<", $string);
70  return trim($string);
71  }
72 }
brutallyTrim(string $string)