‪TYPO3CMS  ‪main
ParserTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪ParserTest extends UnitTestCase
27 {
28  private const ‪TYPES = [
29  ‪SimpleNode::TYPE_ELEMENT => 'ELEMENT',
30  ‪SimpleNode::TYPE_TEXT => 'TEXT',
31  ‪SimpleNode::TYPE_CDATA => 'CDATA',
32  ‪SimpleNode::TYPE_COMMENT => 'COMMENT',
33  ];
34 
38  public static function ‪nodesAreResolvedDataProvider(): array
39  {
40  return [
41  [
42  'text',
43  ['[TEXT]: text'],
44  ],
45  [
46  '<element>',
47  ['[ELEMENT]: <element>'],
48  ],
49  [
50  '</element>',
51  ['[ELEMENT]: </element>'],
52  ],
53  [
54  '<!-- comment -->',
55  ['[COMMENT]: <!-- comment -->'],
56  ],
57  [
58  '<![CDATA[ cdata ]]>',
59  ['[CDATA]: <![CDATA[ cdata ]]>'],
60  ],
61  [
62  '<![CDATA[ <!-- comment --> ]]>',
63  ['[CDATA]: <![CDATA[ <!-- comment --> ]]>'],
64  ],
65  [
66  '<!-- <![CDATA[ cdata ]]> -->',
67  ['[COMMENT]: <!-- <![CDATA[ cdata ]]> -->'],
68  ],
69  [
70  '<text',
71  [], // invalid element start, therefore ignored
72  ],
73  [
74  '< text',
75  ['[TEXT]: < text'],
76  ],
77  [
78  'x < y',
79  ['[TEXT]: x < y'],
80  ],
81  [
82  'text>',
83  ['[TEXT]: text>'],
84  ],
85  [
86  'text >',
87  ['[TEXT]: text >'],
88  ],
89  [
90  'x > y',
91  ['[TEXT]: x > y'],
92  ],
93  [
94  'x < y > z',
95  ['[TEXT]: x < y > z'],
96  ],
97  [
98  'x <= y >= z',
99  ['[TEXT]: x <= y >= z'],
100  ],
101  [
102  'x =<y>= z',
103  ['[TEXT]: x =', '[ELEMENT]: <y>', '[TEXT]: = z'],
104  ],
105  ];
106  }
107 
111  #[DataProvider('nodesAreResolvedDataProvider')]
112  #[Test]
113  public function ‪nodesAreResolved(string $html, array $expectation): void
114  {
116  $nodes = array_map(
117  static function (‪SimpleNode $node) {
118  return sprintf('[%s]: %s', self::TYPES[$node->‪getType()], $node);
119  },
120  ‪$parser->getNodes()
121  );
122  self::assertSame($expectation, $nodes);
123  }
124 }
‪TYPO3\CMS\Core\Html\SimpleParser
Definition: SimpleParser.php:32
‪TYPO3\CMS\Core\Html\SimpleNode\getType
‪getType()
Definition: SimpleNode.php:60
‪TYPO3\CMS\Core\Tests\Unit\Html\Parser\ParserTest\nodesAreResolved
‪nodesAreResolved(string $html, array $expectation)
Definition: ParserTest.php:113
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Core\Html\SimpleNode\TYPE_ELEMENT
‪const TYPE_ELEMENT
Definition: SimpleNode.php:26
‪TYPO3\CMS\Core\Html\SimpleNode\TYPE_TEXT
‪const TYPE_TEXT
Definition: SimpleNode.php:27
‪TYPO3\CMS\Core\Tests\Unit\Html\Parser
Definition: ParserTest.php:18
‪TYPO3\CMS\Core\Html\SimpleNode\TYPE_COMMENT
‪const TYPE_COMMENT
Definition: SimpleNode.php:29
‪TYPO3\CMS\Core\Html\SimpleNode\TYPE_CDATA
‪const TYPE_CDATA
Definition: SimpleNode.php:28
‪TYPO3\CMS\Core\Html\SimpleParser\fromString
‪static fromString(string $string)
Definition: SimpleParser.php:49
‪TYPO3\CMS\Core\Tests\Unit\Html\Parser\ParserTest\TYPES
‪const TYPES
Definition: ParserTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Html\Parser\ParserTest\nodesAreResolvedDataProvider
‪static string[] nodesAreResolvedDataProvider()
Definition: ParserTest.php:38
‪TYPO3\CMS\Core\Html\SimpleNode
Definition: SimpleNode.php:24
‪TYPO3\CMS\Core\Tests\Unit\Html\Parser\ParserTest
Definition: ParserTest.php:27