HtmlFormatterTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 4.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Error\Debug;
  17. use Cake\Error\Debug\ArrayItemNode;
  18. use Cake\Error\Debug\ArrayNode;
  19. use Cake\Error\Debug\ClassNode;
  20. use Cake\Error\Debug\HtmlFormatter;
  21. use Cake\Error\Debug\PropertyNode;
  22. use Cake\Error\Debug\ReferenceNode;
  23. use Cake\Error\Debug\ScalarNode;
  24. use Cake\Error\Debug\SpecialNode;
  25. use Cake\TestSuite\TestCase;
  26. use DomDocument;
  27. /**
  28. * HtmlFormatterTest
  29. */
  30. class HtmlFormatterTest extends TestCase
  31. {
  32. /**
  33. * Test dumping a graph that contains all possible nodes.
  34. */
  35. public function testDump(): void
  36. {
  37. $node = new ClassNode('MyObject', 1);
  38. $node->addProperty(new PropertyNode('stringProp', 'public', new ScalarNode('string', 'value')));
  39. $node->addProperty(new PropertyNode('intProp', 'protected', new ScalarNode('int', 1)));
  40. $node->addProperty(new PropertyNode('floatProp', 'protected', new ScalarNode('float', 1.1)));
  41. $node->addProperty(new PropertyNode('boolProp', 'protected', new ScalarNode('bool', true)));
  42. $node->addProperty(new PropertyNode('nullProp', 'private', new ScalarNode('null', null)));
  43. $arrayNode = new ArrayNode([
  44. new ArrayItemNode(new ScalarNode('string', ''), new SpecialNode('too much')),
  45. new ArrayItemNode(new ScalarNode('int', 1), new ReferenceNode('MyObject', 1)),
  46. ]);
  47. $node->addProperty(new PropertyNode('arrayProp', 'public', $arrayNode));
  48. $formatter = new HtmlFormatter();
  49. $result = $formatter->dump($node);
  50. // Check important classnames
  51. $this->assertStringContainsString('class="cake-debug-const"', $result);
  52. $this->assertStringContainsString('class="cake-debug-string"', $result);
  53. $this->assertStringContainsString('class="cake-debug-number"', $result);
  54. $this->assertStringContainsString('class="cake-debug-array-items"', $result);
  55. $this->assertStringContainsString('class="cake-debug-array-item"', $result);
  56. $this->assertStringContainsString('class="cake-debug-array"', $result);
  57. $this->assertStringContainsString('class="cake-debug-object"', $result);
  58. $this->assertStringContainsString('class="cake-debug-object-props"', $result);
  59. $this->assertStringContainsString('class="cake-debug-special"', $result);
  60. $this->assertStringContainsString('class="cake-debug-ref"', $result);
  61. // Check valid HTML
  62. $dom = new DomDocument();
  63. $dom->loadHtml($result);
  64. $this->assertGreaterThan(0, count($dom->childNodes));
  65. $expected = <<<TEXT
  66. object(MyObject) id:1 {
  67. stringProp =&gt; &#039;value&#039;
  68. protected intProp =&gt; (int) 1
  69. protected floatProp =&gt; (float) 1.1
  70. protected boolProp =&gt; true
  71. private nullProp =&gt; null
  72. arrayProp =&gt; [
  73. &#039;&#039; =&gt; too much,
  74. (int) 1 =&gt; object(MyObject) id: 1 {},
  75. ]
  76. }
  77. TEXT;
  78. $this->assertStringContainsString($expected, strip_tags($result));
  79. }
  80. }