PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/PHPUnit/Listener/AbstractTemplatablePrinter.php

https://github.com/andreaswolf/Menta
PHP | 141 lines | 57 code | 17 blank | 67 comment | 9 complexity | 938072b09029fd6e0926798ecc7c2b54 MD5 | raw file
  1. <?php
  2. require_once 'PHPUnit/Framework/TestListener.php';
  3. /**
  4. * Templatable printer
  5. *
  6. * @author Fabrizio Branca
  7. */
  8. abstract class Menta_PHPUnit_Listener_AbstractTemplatablePrinter extends PHPUnit_Util_Printer {
  9. /**
  10. * @var string
  11. */
  12. protected $templateFile;
  13. /**
  14. * @var array
  15. */
  16. protected $additionalFiles = array();
  17. /**
  18. * @var string
  19. */
  20. protected $targetFile;
  21. /**
  22. * @var string
  23. */
  24. protected $viewClass = 'Menta_Util_View';
  25. /**
  26. * Constructor
  27. *
  28. * Parameter can be set in the phpunit xml configuration file:
  29. * <code>
  30. * <listeners>
  31. * <listener class="..." file="...">
  32. * <arguments>
  33. * <string>...</string><!-- targetFile -->
  34. *
  35. * <string>...</string><!-- directory -->
  36. *
  37. * <array>
  38. * <element key="js/handle.gif">
  39. * <string>.../Resources/Templates/img/handle.gif</string>
  40. * <element>
  41. * <element key="js/jquery.beforeafter-1.4.min.js">
  42. * <string>###MENTA_ROOTDIR###/PHPUnit/Listener/Resources/img/jquery.beforeafter-1.4.min.js</string>
  43. * <element>
  44. * <!-- ... -->
  45. * </array>
  46. * </arguments>
  47. * </listener>
  48. * </listeners>
  49. * </code>
  50. *
  51. * @param string $targetFile
  52. * @param string $templateFile
  53. * @param array $additionalFiles
  54. * @throws Exception
  55. * @author Fabrizio Branca
  56. */
  57. public function __construct($targetFile=NULL, $templateFile=NULL, array $additionalFiles=NULL) {
  58. if (!is_null($targetFile)) {
  59. $this->targetFile = $targetFile;
  60. }
  61. $dir = dirname($this->targetFile);
  62. if (!is_dir($dir)) {
  63. throw new Exception("Target dir '$dir' does not exist");
  64. }
  65. if (!is_null($templateFile)) {
  66. $this->templateFile = $templateFile;
  67. }
  68. $this->templateFile = str_replace('###MENTA_ROOTDIR###', MENTA_ROOTDIR, $this->templateFile);
  69. if (empty($this->templateFile)) {
  70. throw new Exception('No template file defined');
  71. }
  72. if (!is_null($additionalFiles)) {
  73. $this->additionalFiles = $additionalFiles;
  74. }
  75. parent::__construct($this->targetFile);
  76. }
  77. /**
  78. * Flush: Copy images and additional files to folder and generate index file using a template
  79. *
  80. * This method is called once after all tests have been processed.
  81. * HINT: The flush method is only called if the TestListener inherits from PHPUnit_Util_Printer
  82. *
  83. * @param array $templateVars
  84. * @return void
  85. * @author Fabrizio Branca
  86. */
  87. public function flush(array $templateVars=array()) {
  88. $this->copyAdditionalFiles();
  89. $className = $this->viewClass;
  90. $view = new $className($this->templateFile); /* @var $view Menta_Util_View */
  91. if (!$view instanceof Menta_Util_View) {
  92. throw new Exception('View must inherit from Menta_Util_View.');
  93. }
  94. foreach ($templateVars as $key => $value) {
  95. $view->assign($key, $value);
  96. }
  97. $this->write($view->render());
  98. return parent::flush();
  99. }
  100. /**
  101. * Copy additional files
  102. *
  103. * @return void
  104. * @author Fabrizio Branca
  105. * @throws Exception
  106. */
  107. protected function copyAdditionalFiles() {
  108. $dir = dirname($this->targetFile);
  109. foreach ($this->additionalFiles as $target => $source) {
  110. $target = str_replace('###MENTA_ROOTDIR###', MENTA_ROOTDIR, $target);
  111. $source = str_replace('###MENTA_ROOTDIR###', MENTA_ROOTDIR, $source);
  112. $targetPath = $dir . DIRECTORY_SEPARATOR . $target;
  113. // TODO: filter ".."
  114. $pathinfo = pathinfo($targetPath);
  115. if (!is_dir($pathinfo['dirname'])) {
  116. mkdir($pathinfo['dirname'], 0777, true);
  117. }
  118. $res = copy($source, $targetPath);
  119. if ($res === false) {
  120. throw new Exception("Error while copying file $source to $target");
  121. }
  122. }
  123. }
  124. }