Module.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Exception\ModuleException;
  4. use Codeception\Lib\Interfaces\RequiresPackage;
  5. use Codeception\Lib\ModuleContainer;
  6. use Codeception\Util\Shared\Asserts;
  7. /**
  8. * Basic class for Modules and Helpers.
  9. * You must extend from it while implementing own helpers.
  10. *
  11. * Public methods of this class start with `_` prefix in order to ignore them in actor classes.
  12. * Module contains **HOOKS** which allow to handle test execution routine.
  13. *
  14. */
  15. abstract class Module
  16. {
  17. use Asserts;
  18. /**
  19. * @var ModuleContainer
  20. */
  21. protected $moduleContainer;
  22. /**
  23. * By setting it to false module wan't inherit methods of parent class.
  24. *
  25. * @var bool
  26. */
  27. public static $includeInheritedActions = true;
  28. /**
  29. * Allows to explicitly set what methods have this class.
  30. *
  31. * @var array
  32. */
  33. public static $onlyActions = [];
  34. /**
  35. * Allows to explicitly exclude actions from module.
  36. *
  37. * @var array
  38. */
  39. public static $excludeActions = [];
  40. /**
  41. * Allows to rename actions
  42. *
  43. * @var array
  44. */
  45. public static $aliases = [];
  46. protected $storage = [];
  47. protected $config = [];
  48. protected $backupConfig = [];
  49. protected $requiredFields = [];
  50. /**
  51. * Module constructor.
  52. *
  53. * Requires module container (to provide access between modules of suite) and config.
  54. *
  55. * @param ModuleContainer $moduleContainer
  56. * @param null $config
  57. */
  58. public function __construct(ModuleContainer $moduleContainer, $config = null)
  59. {
  60. $this->moduleContainer = $moduleContainer;
  61. $this->backupConfig = $this->config;
  62. if (is_array($config)) {
  63. $this->_setConfig($config);
  64. }
  65. }
  66. /**
  67. * Allows to define initial module config.
  68. * Can be used in `_beforeSuite` hook of Helpers or Extensions
  69. *
  70. * ```php
  71. * <?php
  72. * public function _beforeSuite($settings = []) {
  73. * $this->getModule('otherModule')->_setConfig($this->myOtherConfig);
  74. * }
  75. * ```
  76. *
  77. * @param $config
  78. * @throws Exception\ModuleConfigException
  79. * @throws ModuleException
  80. */
  81. public function _setConfig($config)
  82. {
  83. $this->config = $this->backupConfig = array_merge($this->config, $config);
  84. $this->validateConfig();
  85. }
  86. /**
  87. * Allows to redefine config for a specific test.
  88. * Config is restored at the end of a test.
  89. *
  90. * ```php
  91. * <?php
  92. * // cleanup DB only for specific group of tests
  93. * public function _before(Test $test) {
  94. * if (in_array('cleanup', $test->getMetadata()->getGroups()) {
  95. * $this->getModule('Db')->_reconfigure(['cleanup' => true]);
  96. * }
  97. * }
  98. * ```
  99. *
  100. * @param $config
  101. * @throws Exception\ModuleConfigException
  102. * @throws ModuleException
  103. */
  104. public function _reconfigure($config)
  105. {
  106. $this->config = array_merge($this->backupConfig, $config);
  107. $this->onReconfigure();
  108. $this->validateConfig();
  109. }
  110. /**
  111. * HOOK to be executed when config changes with `_reconfigure`.
  112. */
  113. protected function onReconfigure()
  114. {
  115. // update client on reconfigurations
  116. }
  117. /**
  118. * Reverts config changed by `_reconfigure`
  119. */
  120. public function _resetConfig()
  121. {
  122. $this->config = $this->backupConfig;
  123. }
  124. /**
  125. * Validates current config for required fields and required packages.
  126. *
  127. * @throws Exception\ModuleConfigException
  128. * @throws ModuleException
  129. */
  130. protected function validateConfig()
  131. {
  132. $fields = array_keys($this->config);
  133. if (array_intersect($this->requiredFields, $fields) != $this->requiredFields) {
  134. throw new Exception\ModuleConfigException(
  135. get_class($this),
  136. "\nOptions: " . implode(', ', $this->requiredFields) . " are required\n" .
  137. "Please, update the configuration and set all the required fields\n\n"
  138. );
  139. }
  140. if ($this instanceof RequiresPackage) {
  141. $errorMessage = '';
  142. foreach ($this->_requires() as $className => $package) {
  143. if (class_exists($className)) {
  144. continue;
  145. }
  146. $errorMessage .= "Class $className can't be loaded, please add $package to composer.json\n";
  147. }
  148. if ($errorMessage) {
  149. throw new ModuleException($this, $errorMessage);
  150. }
  151. }
  152. }
  153. /**
  154. * Returns a module name for a Module, a class name for Helper
  155. *
  156. * @return string
  157. */
  158. public function _getName()
  159. {
  160. $moduleName = '\\'.get_class($this);
  161. if (strpos($moduleName, ModuleContainer::MODULE_NAMESPACE) === 0) {
  162. return substr($moduleName, strlen(ModuleContainer::MODULE_NAMESPACE));
  163. }
  164. return $moduleName;
  165. }
  166. /**
  167. * Checks if a module has required fields
  168. *
  169. * @return bool
  170. */
  171. public function _hasRequiredFields()
  172. {
  173. return !empty($this->requiredFields);
  174. }
  175. /**
  176. * **HOOK** triggered after module is created and configuration is loaded
  177. */
  178. public function _initialize()
  179. {
  180. }
  181. /**
  182. * **HOOK** executed before suite
  183. *
  184. * @param array $settings
  185. */
  186. public function _beforeSuite($settings = [])
  187. {
  188. }
  189. /**
  190. * **HOOK** executed after suite
  191. */
  192. public function _afterSuite()
  193. {
  194. }
  195. /**
  196. * **HOOK** executed before step
  197. *
  198. * @param Step $step
  199. */
  200. public function _beforeStep(Step $step)
  201. {
  202. }
  203. /**
  204. * **HOOK** executed after step
  205. *
  206. * @param Step $step
  207. */
  208. public function _afterStep(Step $step)
  209. {
  210. }
  211. /**
  212. * **HOOK** executed before test
  213. *
  214. * @param TestInterface $test
  215. */
  216. public function _before(TestInterface $test)
  217. {
  218. }
  219. /**
  220. * **HOOK** executed after test
  221. *
  222. * @param TestInterface $test
  223. */
  224. public function _after(TestInterface $test)
  225. {
  226. }
  227. /**
  228. * **HOOK** executed when test fails but before `_after`
  229. *
  230. * @param TestInterface $test
  231. * @param \Exception $fail
  232. */
  233. public function _failed(TestInterface $test, $fail)
  234. {
  235. }
  236. /**
  237. * Print debug message to the screen.
  238. *
  239. * @param $message
  240. */
  241. protected function debug($message)
  242. {
  243. codecept_debug($message);
  244. }
  245. /**
  246. * Print debug message with a title
  247. *
  248. * @param $title
  249. * @param $message
  250. */
  251. protected function debugSection($title, $message)
  252. {
  253. if (is_array($message) or is_object($message)) {
  254. $message = stripslashes(json_encode($message));
  255. }
  256. $this->debug("[$title] $message");
  257. }
  258. /**
  259. * Checks that module is enabled.
  260. *
  261. * @param $name
  262. * @return bool
  263. */
  264. protected function hasModule($name)
  265. {
  266. return $this->moduleContainer->hasModule($name);
  267. }
  268. /**
  269. * Get all enabled modules
  270. *
  271. * @return array
  272. */
  273. protected function getModules()
  274. {
  275. return $this->moduleContainer->all();
  276. }
  277. /**
  278. * Get another module by its name:
  279. *
  280. * ```php
  281. * <?php
  282. * $this->getModule('WebDriver')->_findElements('.items');
  283. * ```
  284. *
  285. * @param $name
  286. * @return Module
  287. * @throws ModuleException
  288. */
  289. protected function getModule($name)
  290. {
  291. if (!$this->hasModule($name)) {
  292. throw new Exception\ModuleException(__CLASS__, "Module $name couldn't be connected");
  293. }
  294. return $this->moduleContainer->getModule($name);
  295. }
  296. /**
  297. * Get config values or specific config item.
  298. *
  299. * @param null $key
  300. * @return array|mixed|null
  301. */
  302. public function _getConfig($key = null)
  303. {
  304. if (!$key) {
  305. return $this->config;
  306. }
  307. if (isset($this->config[$key])) {
  308. return $this->config[$key];
  309. }
  310. return null;
  311. }
  312. protected function scalarizeArray($array)
  313. {
  314. foreach ($array as $k => $v) {
  315. if (!is_null($v) && !is_scalar($v)) {
  316. $array[$k] = (is_array($v) || $v instanceof \ArrayAccess)
  317. ? $this->scalarizeArray($v)
  318. : (string)$v;
  319. }
  320. }
  321. return $array;
  322. }
  323. }