PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/LimePrinterTest.php

http://github.com/bschussek/lime
PHP | 197 lines | 69 code | 39 blank | 89 comment | 0 complexity | c0a126b5fb7586939c3905f808d6a18a MD5 | raw file
Possible License(s): ISC
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. LimeAnnotationSupport::enable();
  12. $t = new LimeTest();
  13. // @Before
  14. $colorizer = $t->stub('LimeColorizer');
  15. $printer = new LimePrinter($colorizer);
  16. // @After
  17. $colorizer = null;
  18. $printer = null;
  19. // @Test: printText() prints text using the given style
  20. // fixtures
  21. $colorizer->colorize('My text', 'RED')->returns('<RED>My text</RED>');
  22. $colorizer->replay();
  23. // test
  24. ob_start();
  25. $printer->printText('My text', 'RED');
  26. $result = ob_get_clean();
  27. // assertions
  28. $t->is($result, '<RED>My text</RED>', 'The result was colorized and printed');
  29. // @Test: printLine() prints text followed by a newline
  30. // fixtures
  31. $colorizer->colorize('My text', 'RED')->returns('<RED>My text</RED>');
  32. $colorizer->replay();
  33. // test
  34. ob_start();
  35. $printer->printLine('My text', 'RED');
  36. $result = ob_get_clean();
  37. // assertions
  38. $t->is($result, "<RED>My text</RED>\n", 'The result was colorized and printed');
  39. // @Test: printBox() prints text in a box with a width of 80 characters
  40. // fixtures
  41. $paddedText = str_pad('My text', 80, ' ');
  42. $colorizer->colorize($paddedText, 'RED')->returns('<RED>'.$paddedText.'</RED>');
  43. $colorizer->replay();
  44. // test
  45. ob_start();
  46. $printer->printBox('My text', 'RED');
  47. $result = ob_get_clean();
  48. // assertions
  49. $t->is($result, '<RED>'.$paddedText."</RED>\n", 'The result was colorized and printed');
  50. // @Test: printLargeBox() prints text in a large box with a width of 80 characters or more
  51. // fixtures
  52. $paddedText = str_pad(' My text', 80, ' ');
  53. $paddedSpace = str_repeat(' ', 80);
  54. $colorizer->colorize($paddedText, 'RED')->returns('<RED>'.$paddedText.'</RED>');
  55. $colorizer->colorize($paddedSpace, 'RED')->returns('<RED>'.$paddedSpace.'</RED>');
  56. $colorizer->replay();
  57. // test
  58. ob_start();
  59. $printer->printLargeBox('My text', 'RED');
  60. $result = ob_get_clean();
  61. // assertions
  62. $t->is($result, "\n<RED>".$paddedSpace."</RED>\n<RED>".$paddedText."</RED>\n<RED>".$paddedSpace."</RED>\n\n", 'The result was colorized and printed');
  63. // @Test: The printer does also work without colorizer
  64. // fixtures
  65. $printer = new LimePrinter();
  66. // test
  67. ob_start();
  68. $printer->printText('My text');
  69. $result = ob_get_clean();
  70. // assertions
  71. $t->is($result, 'My text', 'The result was printed');
  72. // @Test: Strings in unformatted text are automatically formatted
  73. // fixtures
  74. $colorizer->colorize('"Test string"', LimePrinter::STRING)->returns('<BLUE>"Test string"</BLUE>');
  75. $colorizer->replay();
  76. // test
  77. ob_start();
  78. $printer->printText('My text with a "Test string"');
  79. $result = ob_get_clean();
  80. // assertions
  81. $t->is($result, 'My text with a <BLUE>"Test string"</BLUE>', 'The result was colorized and printed');
  82. // @Test: Integers in unformatted text are automatically formatted
  83. // fixtures
  84. $colorizer->colorize('123', LimePrinter::NUMBER)->returns('<BLUE>123</BLUE>');
  85. $colorizer->replay();
  86. // test
  87. ob_start();
  88. $printer->printText('My text with an integer: 123');
  89. $result = ob_get_clean();
  90. // assertions
  91. $t->is($result, 'My text with an integer: <BLUE>123</BLUE>', 'The result was colorized and printed');
  92. // @Test: Integers within words are not formatted
  93. // test
  94. ob_start();
  95. $printer->printText('My text with an inte123ger');
  96. $result = ob_get_clean();
  97. // assertions
  98. $t->is($result, 'My text with an inte123ger', 'The result was not colorized and printed');
  99. // @Test: Floats in unformatted text are automatically formatted
  100. // fixtures
  101. $colorizer->colorize('1.23', LimePrinter::NUMBER)->returns('<BLUE>1.23</BLUE>');
  102. $colorizer->replay();
  103. // test
  104. ob_start();
  105. $printer->printText('My text with a float: 1.23');
  106. $result = ob_get_clean();
  107. // assertions
  108. $t->is($result, 'My text with a float: <BLUE>1.23</BLUE>', 'The result was colorized and printed');
  109. // @Test: Booleans in unformatted text are automatically formatted
  110. // fixtures
  111. $colorizer->colorize('true', LimePrinter::BOOLEAN)->returns('<BLUE>true</BLUE>');
  112. $colorizer->colorize('false', LimePrinter::BOOLEAN)->returns('<BLUE>false</BLUE>');
  113. $colorizer->replay();
  114. // test
  115. ob_start();
  116. $printer->printText('My text with true and false');
  117. $result = ob_get_clean();
  118. // assertions
  119. $t->is($result, 'My text with <BLUE>true</BLUE> and <BLUE>false</BLUE>', 'The result was colorized and printed');
  120. /*
  121. // @Test: functions in unformatted text are automatically formatted
  122. // @Test: Case 1 - Function without prefix
  123. // fixtures
  124. $colorizer->colorize('function(1, 2)', LimePrinter::METHOD)->returns('<BLUE>function(1, 2)</BLUE>');
  125. $colorizer->replay();
  126. // test
  127. ob_start();
  128. $printer->printText('My text with a function(1, 2)');
  129. $result = ob_get_clean();
  130. // assertions
  131. $t->is($result, 'My text with a <BLUE>function(1, 2)</BLUE>', 'The result was colorized and printed');
  132. // @Test: Case 2 - Function with "->" prefix
  133. // fixtures
  134. $colorizer->colorize('->function()', LimePrinter::METHOD)->returns('<BLUE>->function()</BLUE>');
  135. $colorizer->replay();
  136. // test
  137. ob_start();
  138. $printer->printText('My text with a ->function()');
  139. $result = ob_get_clean();
  140. // assertions
  141. $t->is($result, 'My text with a <BLUE>->function()</BLUE>', 'The result was colorized and printed');
  142. // @Test: Case 3 - Function with "::" prefix
  143. // fixtures
  144. $colorizer->colorize('::function(1, 2)', LimePrinter::METHOD)->returns('<BLUE>::function(1, 2)</BLUE>');
  145. $colorizer->replay();
  146. // test
  147. ob_start();
  148. $printer->printText('My text with a ::function(1, 2)');
  149. $result = ob_get_clean();
  150. // assertions
  151. $t->is($result, 'My text with a <BLUE>::function(1, 2)</BLUE>', 'The result was colorized and printed');
  152. */