/components/test/Components/TestCase.php

https://github.com/ewandor/horde · PHP · 172 lines · 128 code · 17 blank · 27 comment · 3 complexity · 4e0d9350e6b6ad4fc42f5ff643696c28 MD5 · raw file

  1. <?php
  2. /**
  3. * Test base.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Horde
  8. * @package Components
  9. * @subpackage UnitTests
  10. * @author Gunnar Wrobel <wrobel@pardus.de>
  11. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  12. * @link http://pear.horde.org/index.php?package=Components
  13. */
  14. /**
  15. * Test base.
  16. *
  17. * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
  18. *
  19. * See the enclosed file COPYING for license information (LGPL). If you
  20. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  21. *
  22. * @category Horde
  23. * @package Components
  24. * @subpackage UnitTests
  25. * @author Gunnar Wrobel <wrobel@pardus.de>
  26. * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
  27. * @link http://pear.horde.org/index.php?package=Components
  28. */
  29. class Components_TestCase
  30. extends PHPUnit_Framework_TestCase
  31. {
  32. protected function getComponentFactory(
  33. $arguments = array(), $options = array()
  34. )
  35. {
  36. $dependencies = new Components_Dependencies_Injector();
  37. $config = new Components_Stub_Config($arguments, $options);
  38. $dependencies->initConfig($config);
  39. return $dependencies->getComponentFactory();
  40. }
  41. protected function getComponent(
  42. $directory, $arguments = array(), $options = array()
  43. )
  44. {
  45. $dependencies = new Components_Dependencies_Injector();
  46. $config = new Components_Stub_Config($arguments, $options);
  47. $dependencies->initConfig($config);
  48. $factory = $dependencies->getComponentFactory();
  49. return new Components_Component_Source(
  50. $directory, $config, $factory
  51. );
  52. }
  53. protected function getReleaseTask($name, $package)
  54. {
  55. $dependencies = new Components_Dependencies_Injector();
  56. $this->output = new Components_Stub_Output();
  57. $dependencies->setInstance('Components_Output', $this->output);
  58. return $dependencies->getReleaseTasks()->getTask($name, $package);
  59. }
  60. protected function getReleaseTasks()
  61. {
  62. $dependencies = new Components_Dependencies_Injector();
  63. $this->output = new Components_Stub_Output();
  64. $dependencies->setInstance('Components_Output', $this->output);
  65. return $dependencies->getReleaseTasks();
  66. }
  67. protected function getTemporaryDirectory()
  68. {
  69. return Horde_Util::createTempDir();
  70. }
  71. protected function getHelp()
  72. {
  73. $_SERVER['argv'] = array('horde-components', '--help');
  74. return $this->_callStrictComponents();
  75. }
  76. protected function getActionHelp($action)
  77. {
  78. $_SERVER['argv'] = array('horde-components', 'help', $action);
  79. return $this->_callStrictComponents();
  80. }
  81. protected function _callStrictComponents(array $parameters = array())
  82. {
  83. return $this->_callComponents($parameters, array($this, '_callStrict'));
  84. }
  85. protected function _callUnstrictComponents(array $parameters = array())
  86. {
  87. return $this->_callComponents($parameters, array($this, '_callUnstrict'));
  88. }
  89. private function _callComponents(array $parameters, $callback)
  90. {
  91. ob_start();
  92. $parameters['parser']['class'] = 'Horde_Test_Stub_Parser';
  93. $parameters['dependencies'] = new Components_Dependencies_Injector();
  94. $parameters['dependencies']->setInstance(
  95. 'Horde_Cli',
  96. new Horde_Test_Stub_Cli()
  97. );
  98. call_user_func_array($callback, array($parameters));
  99. $output = ob_get_contents();
  100. ob_end_clean();
  101. return $output;
  102. }
  103. private function _callUnstrict(array $parameters)
  104. {
  105. $old_errorreporting = error_reporting(E_ALL & ~(E_STRICT | E_DEPRECATED));
  106. error_reporting(E_ALL & ~(E_STRICT | E_DEPRECATED));
  107. $this->_callStrict($parameters);
  108. error_reporting($old_errorreporting);
  109. }
  110. private function _callStrict(array $parameters)
  111. {
  112. Components::main($parameters);
  113. }
  114. protected function fileRegexpPresent($regex, $dir)
  115. {
  116. $files = array();
  117. $found = false;
  118. foreach (new DirectoryIterator($dir) as $file) {
  119. if (preg_match($regex, $file->getBasename('.tgz'), $matches)) {
  120. $found = true;
  121. }
  122. $files[] = $file->getPath();
  123. }
  124. $this->assertTrue(
  125. $found,
  126. sprintf("File \"%s\" not found in \n\n%s\n", $regex, join("\n", $files))
  127. );
  128. }
  129. protected function setPearGlobals()
  130. {
  131. $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'] = array(
  132. '*' => false,
  133. );
  134. $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] = false;
  135. $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array();
  136. }
  137. protected function changeDirectory($path)
  138. {
  139. $this->cwd = getcwd();
  140. chdir($path);
  141. }
  142. protected function lessStrict()
  143. {
  144. $this->old_errorreporting = error_reporting(E_ALL & ~(E_STRICT | E_DEPRECATED));
  145. }
  146. public function tearDown()
  147. {
  148. if (!empty($this->cwd)) {
  149. chdir($this->cwd);
  150. }
  151. if (!empty($this->old_errorreporting)) {
  152. error_reporting($this->old_errorreporting);
  153. }
  154. }
  155. }