SuiteDataArrayBuilder.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace tests\unit\Util;
  7. use Magento\FunctionalTestingFramework\Suite\Util\SuiteObjectExtractor;
  8. use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor;
  9. use Magento\FunctionalTestingFramework\Test\Util\TestObjectExtractor;
  10. class SuiteDataArrayBuilder
  11. {
  12. /**
  13. * Mock test name
  14. *
  15. * @var string
  16. */
  17. private $name;
  18. /**
  19. * Mock before action name
  20. *
  21. * @var string
  22. */
  23. private $testActionBeforeName = 'testActionBefore';
  24. /**
  25. * Mock after action name
  26. *
  27. * @var string
  28. */
  29. private $testActionAfterName = 'testActionAfter';
  30. /**
  31. * Array containing before hook actions
  32. *
  33. * @var array
  34. */
  35. private $beforeHook = [];
  36. /**
  37. * Arrat containing after hook actions
  38. *
  39. * @var array
  40. */
  41. private $afterHook = [];
  42. /**
  43. * Array which contains tests, groups, modules included as part of suite
  44. *
  45. * @var array
  46. */
  47. private $includes = [];
  48. /**
  49. * Array which contains, tests, groups, module excluded from suite
  50. *
  51. * @var array
  52. */
  53. private $excludes = [];
  54. /**
  55. * Mock test action type
  56. *
  57. * @var string
  58. */
  59. public $testActionType = 'testAction';
  60. /**
  61. * Function which sets the name of the mock suite array
  62. *
  63. * @param string $name
  64. * @return $this
  65. */
  66. public function withName($name)
  67. {
  68. $this->name = $name;
  69. return $this;
  70. }
  71. /**
  72. * Function which takes an array of test names and formats them as included raw suite data
  73. *
  74. * @param array $tests
  75. * @return $this
  76. */
  77. public function includeTests($tests)
  78. {
  79. $this->includes = $this->appendEntriesToSuiteContents($this->includes, 'test', $tests);
  80. return $this;
  81. }
  82. /**
  83. * Function which takes an array of test names and formats them as excluded raw suite data
  84. *
  85. * @param array $tests
  86. * @return $this
  87. */
  88. public function excludeTests($tests)
  89. {
  90. $this->excludes = $this->appendEntriesToSuiteContents($this->excludes, 'test', $tests);
  91. return $this;
  92. }
  93. /**
  94. * Function which takes an array of group names and formats them as included raw suite data
  95. *
  96. * @param array $groups
  97. * @return $this
  98. */
  99. public function includeGroups($groups)
  100. {
  101. $this->includes = $this->appendEntriesToSuiteContents($this->includes, 'group', $groups);
  102. return $this;
  103. }
  104. /**
  105. * Function which takes an array of group names and formats them as excluded raw suite data
  106. *
  107. * @param array $groups
  108. * @return $this
  109. */
  110. public function excludeGroups($groups)
  111. {
  112. $this->excludes = $this->appendEntriesToSuiteContents($this->excludes, 'groups', $groups);
  113. return $this;
  114. }
  115. /**
  116. * Function which takes an array of module names and formats them as included raw suite data
  117. *
  118. * @param array $modules
  119. * @return $this
  120. */
  121. public function includeModules($modules)
  122. {
  123. $this->includes = $this->appendEntriesToSuiteContents($this->includes, 'module', $modules);
  124. return $this;
  125. }
  126. /**
  127. * Function which takes an array of group names and formats them as excluded raw suite data
  128. *
  129. * @param array $modules
  130. * @return $this
  131. */
  132. public function excludeModules($modules)
  133. {
  134. $this->excludes = $this->appendEntriesToSuiteContents($this->excludes, 'module', $modules);
  135. return $this;
  136. }
  137. /**
  138. * Function which takes an array of current include/exclude contents, a type (group, module, or test) and contents
  139. * to be appended to the array and returns a propelry formatted array representative of parsed suite data.
  140. *
  141. * @param array $currentContents
  142. * @param string $type
  143. * @param array $contents
  144. * @return array
  145. */
  146. private function appendEntriesToSuiteContents($currentContents, $type, $contents)
  147. {
  148. $newContents = $currentContents;
  149. foreach ($contents as $entry) {
  150. $newContents[$entry] = [
  151. SuiteObjectExtractor::NODE_NAME => $type,
  152. SuiteObjectExtractor::NAME => $entry
  153. ];
  154. }
  155. return $newContents;
  156. }
  157. /**
  158. * Add an after hook passed in by arg (or default if no arg)
  159. *
  160. * @param null $afterHook
  161. * @return $this
  162. */
  163. public function withAfterHook($afterHook = null)
  164. {
  165. if ($afterHook == null) {
  166. $this->afterHook = [$this->testActionAfterName => [
  167. ActionObjectExtractor::NODE_NAME => $this->testActionType,
  168. ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionAfterName
  169. ]];
  170. } else {
  171. $this->afterHook = $afterHook;
  172. }
  173. return $this;
  174. }
  175. /**
  176. * Add a before hook passed in by arg (or default if no arg)
  177. *
  178. * @param null $beforeHook
  179. * @return $this
  180. */
  181. public function withBeforeHook($beforeHook = null)
  182. {
  183. if ($beforeHook == null) {
  184. $this->beforeHook = [$this->testActionBeforeName => [
  185. ActionObjectExtractor::NODE_NAME => $this->testActionType,
  186. ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionBeforeName
  187. ]];
  188. } else {
  189. $this->beforeHook = $beforeHook;
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Function which takes all class properties set and generates an array representing suite data as parsed from xml.
  195. *
  196. * @return array
  197. */
  198. public function build()
  199. {
  200. return ['suites' => [
  201. $this->name => [
  202. SuiteObjectExtractor::NAME => $this->name,
  203. TestObjectExtractor::TEST_BEFORE_HOOK => $this->beforeHook,
  204. TestObjectExtractor::TEST_AFTER_HOOK => $this->afterHook,
  205. SuiteObjectExtractor::INCLUDE_TAG_NAME => $this->includes,
  206. SuiteObjectExtractor::EXCLUDE_TAG_NAME => $this->excludes
  207. ]
  208. ]];
  209. }
  210. }