InjectableTests.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\TestSuite;
  7. use Magento\Mtf\ObjectManager;
  8. use Magento\Mtf\ObjectManagerFactory;
  9. /**
  10. * Class InjectableTests
  11. *
  12. */
  13. class InjectableTests extends \PHPUnit\Framework\TestSuite
  14. {
  15. /**
  16. * @var ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var \PHPUnit\Framework\TestSuite
  21. */
  22. protected $suite;
  23. /**
  24. * @var \PHPUnit\Framework\TestResult
  25. */
  26. protected $result;
  27. /**
  28. * Run collected tests
  29. *
  30. * @param \PHPUnit\Framework\TestResult $result
  31. * @return \PHPUnit\Framework\TestResult|void
  32. *
  33. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  34. */
  35. public function run(\PHPUnit\Framework\TestResult $result = null)
  36. {
  37. if ($result === null) {
  38. $this->result = $this->createResult();
  39. }
  40. }
  41. /**
  42. * Prepare test suite
  43. *
  44. * @return mixed
  45. */
  46. public static function suite()
  47. {
  48. $suite = new self();
  49. return $suite->prepareSuite();
  50. }
  51. /**
  52. * Prepare test suite and apply application state
  53. *
  54. * @return \Magento\Mtf\TestSuite\AppState
  55. */
  56. public function prepareSuite()
  57. {
  58. $this->init();
  59. return $this->objectManager->create(\Magento\Mtf\TestSuite\AppState::class);
  60. }
  61. /**
  62. * Call the initialization of ObjectManager
  63. */
  64. public function init()
  65. {
  66. $this->initObjectManager();
  67. }
  68. /**
  69. * Initialize ObjectManager
  70. */
  71. private function initObjectManager()
  72. {
  73. if (!isset($this->objectManager)) {
  74. $objectManagerFactory = new ObjectManagerFactory();
  75. $configFileName = isset($_ENV['testsuite_rule']) ? $_ENV['testsuite_rule'] : 'basic';
  76. $configFilePath = realpath(MTF_BP . '/testsuites/' . $_ENV['testsuite_rule_path']);
  77. /** @var \Magento\Mtf\Config\DataInterface $configData */
  78. $configData = $objectManagerFactory->getObjectManager()->create(\Magento\Mtf\Config\TestRunner::class);
  79. $filter = getopt('', ['filter:']);
  80. if (!isset($filter['filter'])) {
  81. $configData->setFileName($configFileName . '.xml')->load($configFilePath);
  82. } else {
  83. $isValid = preg_match('`variation::(.*?)$`', $filter['filter'], $variation);
  84. if ($isValid === 1) {
  85. $configData->setFileName($configFileName . '.xml')->load($configFilePath);
  86. $data['rule']['variation']['allow'][0]['name'][0]['value'] = $variation[1];
  87. $configData->merge($data);
  88. }
  89. }
  90. $this->objectManager = $objectManagerFactory->create(
  91. [\Magento\Mtf\Config\TestRunner::class => $configData]
  92. );
  93. }
  94. }
  95. }