PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Codeception/Module.php

https://github.com/pmcjury/Codeception
PHP | 194 lines | 131 code | 33 blank | 30 comment | 6 complexity | 7d154d2ba8079e6ae6f1e9c0535af464 MD5 | raw file
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Util\Debug;
  4. use Codeception\Util\Shared\Asserts;
  5. abstract class Module
  6. {
  7. use Asserts;
  8. /**
  9. * By setting it to false module wan't inherit methods of parent class.
  10. *
  11. * @var bool
  12. */
  13. public static $includeInheritedActions = true;
  14. /**
  15. * Allows to explicitly set what methods have this class.
  16. *
  17. * @var array
  18. */
  19. public static $onlyActions = array();
  20. /**
  21. * Allows to explicitly exclude actions from module.
  22. *
  23. * @var array
  24. */
  25. public static $excludeActions = array();
  26. /**
  27. * Allows to rename actions
  28. *
  29. * @var array
  30. */
  31. public static $aliases = array();
  32. protected $storage = array();
  33. protected $config = array();
  34. protected $backupConfig = array();
  35. protected $requiredFields = array();
  36. public function __construct($config = null)
  37. {
  38. $this->backupConfig = $this->config;
  39. if (is_array($config)) {
  40. $this->_setConfig($config);
  41. }
  42. }
  43. public function _setConfig($config)
  44. {
  45. $this->config = $this->backupConfig = array_merge($this->config, $config);
  46. $this->validateConfig();
  47. }
  48. public function _reconfigure($config)
  49. {
  50. $this->config = array_merge($this->backupConfig, $config);
  51. $this->onReconfigure();
  52. $this->validateConfig();
  53. }
  54. protected function onReconfigure()
  55. {
  56. // update client on reconfigurations
  57. }
  58. public function _resetConfig()
  59. {
  60. $this->config = $this->backupConfig;
  61. }
  62. protected function validateConfig()
  63. {
  64. $fields = array_keys($this->config);
  65. if (array_intersect($this->requiredFields, $fields) != $this->requiredFields) {
  66. throw new Exception\ModuleConfig(
  67. get_class($this),
  68. "\nOptions: " . implode(', ', $this->requiredFields) . " are required\n
  69. Please, update the configuration and set all the required fields\n\n"
  70. );
  71. }
  72. }
  73. public function _getName()
  74. {
  75. $module = get_class($this);
  76. if (preg_match('@\\\\([\w]+)$@', $module, $matches)) {
  77. $module = $matches[1];
  78. }
  79. return $module;
  80. }
  81. public function _hasRequiredFields()
  82. {
  83. return !empty($this->requiredFields);
  84. }
  85. // HOOK: used after configuration is loaded
  86. public function _initialize()
  87. {
  88. }
  89. // HOOK: on every Guy class initialization
  90. public function _cleanup()
  91. {
  92. }
  93. // HOOK: before each suite
  94. public function _beforeSuite($settings = array())
  95. {
  96. }
  97. // HOOK: after suite
  98. public function _afterSuite()
  99. {
  100. }
  101. // HOOK: before every step
  102. public function _beforeStep(Step $step)
  103. {
  104. }
  105. // HOOK: after every step
  106. public function _afterStep(Step $step)
  107. {
  108. }
  109. // HOOK: before scenario
  110. public function _before(TestCase $test)
  111. {
  112. }
  113. // HOOK: after scenario
  114. public function _after(TestCase $test)
  115. {
  116. }
  117. // HOOK: on fail
  118. public function _failed(TestCase $test, $fail)
  119. {
  120. }
  121. protected function debug($message)
  122. {
  123. Debug::debug($message);
  124. }
  125. protected function debugSection($title, $message)
  126. {
  127. if (is_array($message) or is_object($message)) {
  128. $message = stripslashes(json_encode($message));
  129. }
  130. $this->debug("[$title] $message");
  131. }
  132. protected function hasModule($name)
  133. {
  134. return SuiteManager::hasModule($name);
  135. }
  136. protected function getModules()
  137. {
  138. return SuiteManager::$modules;
  139. }
  140. protected function getModule($name)
  141. {
  142. if (!$this->hasModule($name)) {
  143. throw new Exception\Module(__CLASS__, "Module $name couldn't be connected");
  144. }
  145. return SuiteManager::$modules[$name];
  146. }
  147. protected function scalarizeArray($array)
  148. {
  149. foreach ($array as $k => $v) {
  150. if (!is_scalar($v)) {
  151. $array[$k] = (is_array($v) || $v instanceof \ArrayAccess)
  152. ? $this->scalarizeArray($v)
  153. : (string)$v;
  154. }
  155. }
  156. return $array;
  157. }
  158. }