ConsoleFormatterTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\ConsoleFormatter;
  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. /**
  27. * ConsoleFormatterTest
  28. */
  29. class ConsoleFormatterTest extends TestCase
  30. {
  31. /**
  32. * Test dumping a graph that contains all possible nodes.
  33. */
  34. public function testDump(): void
  35. {
  36. $node = new ClassNode('MyObject', 1);
  37. $node->addProperty(new PropertyNode('stringProp', 'public', new ScalarNode('string', 'value')));
  38. $node->addProperty(new PropertyNode('intProp', 'protected', new ScalarNode('int', 1)));
  39. $node->addProperty(new PropertyNode('floatProp', 'protected', new ScalarNode('float', 1.1)));
  40. $node->addProperty(new PropertyNode('boolProp', 'protected', new ScalarNode('bool', true)));
  41. $node->addProperty(new PropertyNode('nullProp', 'private', new ScalarNode('null', null)));
  42. $arrayNode = new ArrayNode([
  43. new ArrayItemNode(new ScalarNode('string', ''), new SpecialNode('too much')),
  44. new ArrayItemNode(new ScalarNode('int', 1), new ReferenceNode('MyObject', 1)),
  45. ]);
  46. $node->addProperty(new PropertyNode('arrayProp', 'public', $arrayNode));
  47. $formatter = new ConsoleFormatter();
  48. $result = $formatter->dump($node);
  49. $this->assertStringContainsString("\033[1;33m", $result, 'Should contain yellow code');
  50. $this->assertStringContainsString("\033[0;32m", $result, 'Should contain green code');
  51. $this->assertStringContainsString("\033[1;34m", $result, 'Should contain blue code');
  52. $this->assertStringContainsString("\033[0;36m", $result, 'Should contain cyan code');
  53. $expected = <<<TEXT
  54. object(MyObject) id:1 {
  55. stringProp => 'value'
  56. protected intProp => (int) 1
  57. protected floatProp => (float) 1.1
  58. protected boolProp => true
  59. private nullProp => null
  60. arrayProp => [
  61. '' => too much,
  62. (int) 1 => object(MyObject) id:1 {}
  63. ]
  64. }
  65. TEXT;
  66. $noescape = preg_replace('/\\033\[\d\;\d+\;m([^\\\\]+)\\033\[0m/', '$1', $expected);
  67. $this->assertSame($expected, $noescape);
  68. }
  69. }