LimeAnnotationSupport.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /**
  12. * Extends lime_test to support annotations in test files.
  13. *
  14. * With this extension of lime_test, you can write very simple test files that
  15. * support more features than regular lime, such as code executed before
  16. * or after each test, code executed before or after the whole test suite
  17. * or expected exceptions.
  18. *
  19. * A test file can be written like this with LimeTest:
  20. *
  21. * <code>
  22. * <?php
  23. *
  24. * include dirname(__FILE__).'/../bootstrap/unit.php';
  25. *
  26. * $t = new LimeTest(2, new lime_output_color());
  27. *
  28. * // @Before
  29. * $r = new Record();
  30. *
  31. * // @Test
  32. * $r->setValue('Bumblebee');
  33. * $t->is($r->getValue(), 'Bumblebee', 'The setter works');
  34. *
  35. * // @Test
  36. * $t->is($r->getValue(), 'Foobar', 'The value is "Foobar" by default');
  37. * </code>
  38. *
  39. * The available annotations are:
  40. *
  41. * * @BeforeAll Executed before the whole test suite
  42. * * @Before Executed before each test
  43. * * @After Executed after each test
  44. * * @AfterAll Executed after the whole test suite
  45. * * @Test A test case
  46. *
  47. * You can add comments to the annotations that will be printed in the console:
  48. *
  49. * <code>
  50. * // @Test: The record supports setValue()
  51. * $r->setValue('Bumblebee')
  52. * // etc.
  53. * </code>
  54. *
  55. * You can also automatically test that certain exceptions are thrown from
  56. * within a test. To do that, you must call the method ->expect() on the
  57. * LimeTest object '''before''' executing the test that should throw
  58. * an exception.
  59. *
  60. * <code>
  61. * // @Test
  62. * $r->expect('RuntimeException');
  63. * throw new RuntimeException();
  64. *
  65. * // results in a passed test
  66. * </code>
  67. *
  68. * @package lime
  69. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  70. * @version SVN: $Id: LimeAnnotationSupport.php 23701 2009-11-08 21:23:40Z bschussek $
  71. */
  72. class LimeAnnotationSupport
  73. {
  74. protected static
  75. $enabled = false;
  76. protected
  77. $originalPath = null,
  78. $path = null,
  79. $test = null,
  80. $lexer = null;
  81. /**
  82. * Enables annotation support in a script file.
  83. */
  84. public static function enable()
  85. {
  86. // make sure that annotations are not replaced twice at the same time
  87. if (!self::$enabled)
  88. {
  89. self::$enabled = true;
  90. $support = new LimeAnnotationSupport(self::getScriptPath());
  91. $support->execute();
  92. exit;
  93. }
  94. }
  95. /**
  96. * Returns the file path of the executed test script
  97. *
  98. * @return string The file path
  99. */
  100. protected static function getScriptPath()
  101. {
  102. $traces = debug_backtrace();
  103. $file = $traces[count($traces)-1]['file'];
  104. if (!is_file($file))
  105. {
  106. throw new RuntimeException('The script name from the traces is not valid: '.$file);
  107. }
  108. return $file;
  109. }
  110. /**
  111. * Constructor.
  112. *
  113. * Creates a backup of the given file with the extension .bak.
  114. */
  115. protected function __construct($path)
  116. {
  117. $this->originalPath = $path;
  118. $this->path = dirname($path).'/@'.basename($path);
  119. register_shutdown_function(array($this, 'cleanup'));
  120. }
  121. /**
  122. * Removes the transformed script file.
  123. */
  124. public function cleanup()
  125. {
  126. if (file_exists($this->path))
  127. {
  128. unlink($this->path);
  129. }
  130. }
  131. /**
  132. * Transforms the annotations in the script file and executes the resulting
  133. * script.
  134. */
  135. protected function execute()
  136. {
  137. if (file_exists($this->path))
  138. {
  139. unlink($this->path);
  140. }
  141. $this->lexer = new LimeLexerTransformAnnotations($this->path);
  142. $callbacks = $this->lexer->parse($this->originalPath);
  143. $this->includeTestFile();
  144. $testRunner = new LimeTestRunner($this->test ? $this->test->getOutput() : null);
  145. foreach ($callbacks as $annotation => $callbacks)
  146. {
  147. $addMethod = 'add'.$annotation;
  148. foreach ($callbacks as $list)
  149. {
  150. list ($callback, $comment) = $list;
  151. $testRunner->$addMethod($callback, $comment);
  152. }
  153. }
  154. if ($this->test instanceof LimeTest)
  155. {
  156. $testRunner->addExceptionHandler(array($this->test, 'handleException'));
  157. $testRunner->addAfter(array($this->test, 'verifyException'));
  158. }
  159. $testRunner->run();
  160. }
  161. /**
  162. * Includes the test file in a separate scope.
  163. *
  164. * @param string $testVariable
  165. */
  166. protected function includeTestFile()
  167. {
  168. // var_dump(file_get_contents($this->path));
  169. include $this->path;
  170. if (!is_null($this->lexer->getTestVariable()))
  171. {
  172. eval(sprintf('$this->test = %s;', $this->lexer->getTestVariable()));
  173. }
  174. }
  175. }