TestDataArrayBuilder.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Test\Util\ActionObjectExtractor;
  8. use Magento\FunctionalTestingFramework\Test\Util\AnnotationExtractor;
  9. use Magento\FunctionalTestingFramework\Test\Util\TestObjectExtractor;
  10. class TestDataArrayBuilder
  11. {
  12. /**
  13. * Mock test name
  14. *
  15. * @var string
  16. */
  17. public $testName = 'testTest';
  18. /**
  19. * Mock file name
  20. *
  21. * @var string
  22. */
  23. public $filename = null;
  24. /**
  25. * Mock before action name
  26. *
  27. * @var string
  28. */
  29. public $testActionBeforeName = 'testActionBefore';
  30. /**
  31. * Mock after action name
  32. *
  33. * @var string
  34. */
  35. public $testActionAfterName = 'testActionAfter';
  36. /**
  37. * Mock failed action name
  38. *
  39. * @var string
  40. */
  41. public $testActionFailedName = 'testActionFailed';
  42. /**
  43. * Mock test action in test name
  44. *
  45. * @var string
  46. */
  47. public $testTestActionName = 'testActionInTest';
  48. /**
  49. * Mock test action type
  50. *
  51. * @var string
  52. */
  53. public $testActionType = 'testAction';
  54. /**
  55. * @var array
  56. */
  57. private $annotations = [];
  58. /**
  59. * @var array
  60. */
  61. private $beforeHook = [];
  62. /**
  63. * @var array
  64. */
  65. private $afterHook = [];
  66. /**
  67. * @var array
  68. */
  69. private $failedHook = [];
  70. /**
  71. * @var array
  72. */
  73. private $testActions = [];
  74. /**
  75. * @var array
  76. */
  77. private $testReference = null;
  78. /**
  79. * @param string $name
  80. * @return $this
  81. */
  82. public function withName($name)
  83. {
  84. $this->testName = $name;
  85. return $this;
  86. }
  87. /**
  88. * Add annotations passed in by arg (or default if no arg)
  89. *
  90. * @param array $annotations
  91. * @return $this
  92. */
  93. public function withAnnotations($annotations = null)
  94. {
  95. if ($annotations == null) {
  96. $this->annotations = ['group' => [['value' => 'test']]];
  97. } else {
  98. $this->annotations = $annotations;
  99. }
  100. return $this;
  101. }
  102. /**
  103. * Add a before hook passed in by arg (or default if no arg)
  104. *
  105. * @param null $beforeHook
  106. * @return $this
  107. */
  108. public function withBeforeHook($beforeHook = null)
  109. {
  110. if ($beforeHook == null) {
  111. $this->beforeHook = [$this->testActionBeforeName => [
  112. ActionObjectExtractor::NODE_NAME => $this->testActionType,
  113. ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionBeforeName
  114. ]];
  115. } else {
  116. $this->beforeHook = $beforeHook;
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Add an after hook passed in by arg (or default if no arg)
  122. *
  123. * @param null $afterHook
  124. * @return $this
  125. */
  126. public function withAfterHook($afterHook = null)
  127. {
  128. if ($afterHook == null) {
  129. $this->afterHook = [$this->testActionAfterName => [
  130. ActionObjectExtractor::NODE_NAME => $this->testActionType,
  131. ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionAfterName
  132. ]];
  133. } else {
  134. $this->afterHook = $afterHook;
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Add a failed hook passed in by arg (or default if no arg)
  140. *
  141. * @param null $failedHook
  142. * @return $this
  143. */
  144. public function withFailedHook($failedHook = null)
  145. {
  146. if ($failedHook == null) {
  147. $this->failedHook = [$this->testActionFailedName => [
  148. ActionObjectExtractor::NODE_NAME => $this->testActionType,
  149. ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testActionFailedName
  150. ]];
  151. } else {
  152. $this->failedHook = $failedHook;
  153. }
  154. return $this;
  155. }
  156. /**
  157. * Add test actions passed in by arg (or default if no arg)
  158. *
  159. * @param array $actions
  160. * @return $this
  161. */
  162. public function withTestActions($actions = null)
  163. {
  164. if ($actions == null) {
  165. $this->testActions = [$this->testTestActionName => [
  166. ActionObjectExtractor::NODE_NAME => $this->testActionType,
  167. ActionObjectExtractor::TEST_STEP_MERGE_KEY => $this->testTestActionName
  168. ]];
  169. } else {
  170. $this->testActions = $actions;
  171. }
  172. return $this;
  173. }
  174. /**
  175. * Add file name passe in by arg (or default if no arg)
  176. * @param string $filename
  177. * @return $this
  178. */
  179. public function withFileName($filename = null)
  180. {
  181. if ($filename == null) {
  182. $this->filename =
  183. "/magento2-functional-testing-framework/dev/tests/verification/TestModule/Test/BasicFunctionalTest.xml";
  184. } else {
  185. $this->filename = $filename;
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Add test reference passed in by arg (or default if no arg)
  191. *
  192. * @param string $reference
  193. * @return $this
  194. */
  195. public function withTestReference($reference = null)
  196. {
  197. if ($reference != null) {
  198. $this->testReference = $reference;
  199. }
  200. return $this;
  201. }
  202. /**
  203. * Output the resulting test data array based on parameters set in the object
  204. *
  205. * @return array
  206. */
  207. public function build()
  208. {
  209. // return a static data array representing a single test
  210. return [$this->testName => array_merge(
  211. [
  212. TestObjectExtractor::NAME => $this->testName,
  213. TestObjectExtractor::TEST_ANNOTATIONS => $this->annotations,
  214. TestObjectExtractor::TEST_BEFORE_HOOK => $this->beforeHook,
  215. TestObjectExtractor::TEST_AFTER_HOOK => $this->afterHook,
  216. TestObjectExtractor::TEST_FAILED_HOOK => $this->failedHook,
  217. "filename" => $this->filename,
  218. "extends" => $this->testReference
  219. ],
  220. $this->testActions
  221. )];
  222. }
  223. }