/** * Get Constraint object * * @param string $class * @return ConstraintInterface * @throws \UnexpectedValueException */ public function get($class) { $object = $this->objectManager->get($class); if (!$object instanceof ConstraintInterface) { throw new \UnexpectedValueException(sprintf('Constraint class "%s" has to implement \\Mtf\\Constraint\\ConstraintInterface interface.', $class)); } return $object; }
/** * @param string $class * @param array $arguments * @return BlockInterface * @throws \UnexpectedValueException */ public function create($class, array $arguments = []) { $object = $this->objectManager->create($class, $arguments); if (!$object instanceof BlockInterface) { throw new \UnexpectedValueException(sprintf('Block class "%s" has to implement \\Mtf\\Block\\BlockInterface interface.', $class)); } return $object; }
/** * Get test suite * * @param string $class * @return InjectableTestCase|RegularTestCase */ public function get($class) { $refClass = new \ReflectionClass($class); if ($refClass->isSubclassOf('Mtf\\TestCase\\Injectable')) { $object = $this->objectManager->get('Mtf\\TestSuite\\InjectableTestCase'); } else { $object = $this->objectManager->create('Mtf\\TestSuite\\RegularTestCase'); } return $object; }
/** * Returns instance of observer * * @param string $class * @return ObserverInterface * @throws \InvalidArgumentException */ public function getObserver($class) { if (empty($this->observerPool[$class])) { $instance = $this->objectManager->create($class); if (!$instance instanceof ObserverInterface) { throw new \InvalidArgumentException(sprintf('Observer class %s should implement ObserverInterface.', $class)); } $this->observerPool[$class] = $instance; } return $this->observerPool[$class]; }
/** * Run a Mtf application * * @param $applicationName * @param array $arguments * @return mixed * @throws \DomainException */ public function run($applicationName, array $arguments = array()) { try { if (!$this->_locator) { $locatorFactory = new \Mtf\ObjectManagerFactory(); $this->_locator = $locatorFactory->create(); } return $this->_locator->create($applicationName, $arguments)->launch(); } catch (\Exception $exception) { $message = "Error happened during application run.\n"; $message .= $exception->getMessage(); throw new \DomainException($message); } }
/** * Initialize module filters and related data structures * * @return void */ protected function init() { $moduleFilter = getenv(self::MODULE_FILTER); if (empty($moduleFilter)) { $this->moduleFilters = []; return; } $this->moduleFilters = array_map('trim', explode(',', $moduleFilter)); if (empty($this->moduleFilters)) { return; } // $this->affectedModules = array_flip($this->moduleFilters); /** @var $constraintCrossReference \Mtf\Util\CrossModuleReference\Constraint */ $constraintCrossReference = $this->objectManager->get('\\Mtf\\Util\\CrossModuleReference\\Constraint'); /** @var $testStepCrossReference \Mtf\Util\CrossModuleReference\TestStep */ $testStepCrossReference = $this->objectManager->get('\\Mtf\\Util\\CrossModuleReference\\TestStep'); /** @var $pageCrossReference \Mtf\Util\CrossModuleReference\Page */ $pageCrossReference = $this->objectManager->create('\\Mtf\\Util\\CrossModuleReference\\Page', ['constraintChecker' => $constraintCrossReference, 'modules' => $this->moduleFilters]); $crossModuleReferenceCheckers = [$constraintCrossReference, $testStepCrossReference, $pageCrossReference]; $this->affectedTestCases = []; foreach ($crossModuleReferenceCheckers as $crossModuleReferenceChecker) { foreach ($this->moduleFilters as $module) { $affectedTestCases = $crossModuleReferenceChecker->getCrossModuleReference($module); $this->affectedTestCases = array_merge($this->affectedTestCases, $affectedTestCases); } } }
/** * Evaluates the constraint for test case * * @param string $testCaseName * @return bool */ protected function matches($testCaseName) { if ($this->result === null) { $this->testCaseName = $testCaseName; $this->result = true; $this->objectManager->invoke($this, 'processAssert', $this->arguments); } return $this->result; }
/** * Prepare data * * @param array $viewsReport * @return array */ protected function prepareData(array $viewsReport) { foreach ($viewsReport as $name => $reportFilter) { if ($name === 'period_type' || $name === 'show_empty_rows') { continue; } $date = ObjectManager::getInstance()->create('\\Magento\\Backend\\Test\\Fixture\\Date', ['params' => [], 'data' => ['pattern' => $reportFilter]]); $viewsReport[$name] = $date->getData(); } return $viewsReport; }
/** * Run code generator if necessary * * @return void */ protected function generateCode() { if (self::$codeGenerationFlag) { return; } self::$codeGenerationFlag = true; /** @var $generate \Mtf\Util\Generate\Page */ $generator = $this->objectManager->get('Mtf\\Util\\Generate\\Page'); $generator->generateClasses(); return; }
/** * Prepare data * * @param array $customersReport * @return array */ protected function prepareData(array $customersReport) { foreach ($customersReport as $name => $reportFilter) { if ($name === 'report_period') { continue; } $date = ObjectManager::getInstance()->create('\\Magento\\Backend\\Test\\Fixture\\Date', ['params' => [], 'data' => ['pattern' => $reportFilter]]); $customersReport[$name] = $date->getData(); } return $customersReport; }
/** * Get list of variations table * * @return array */ protected function getVariations() { self::$count = 0; $variations = []; /** @var $source \Mtf\Util\Iterator\Variation */ $source = $this->objectManager->create('Mtf\\Util\\Iterator\\Variation', ['testCase' => $this->testCase]); if (count($source)) { while ($variation = $source->current()) { if (!$source->valid()) { break; } ++self::$count; $variations[self::$count] = $variation; $source->next(); } } else { $variations['Default'] = []; } return $variations; }
/** * Prepare data * * @param array $viewsReport * @return array */ protected function prepareData(array $viewsReport) { foreach ($viewsReport as $name => $reportFilter) { if ($reportFilter == '-') { unset($viewsReport[$name]); } if ($name === 'from' || $name === 'to') { $date = ObjectManager::getInstance()->create('\\Magento\\Backend\\Test\\Fixture\\Date', ['params' => [], 'data' => ['pattern' => $reportFilter]]); $viewsReport[$name] = $date->getData(); } } return $viewsReport; }
/** * Custom implementation of native PHPUnit_Framework_TestSuite::createTest method * - Test Case class should be instantiated with MTF Object Manager * - Data Provider Test Suite should be instantiated with MTF Object Manager * * @param \ReflectionClass $theClass * @param string $name * @param array $arguments * @return \PHPUnit_Framework_Test * @throws \PHPUnit_Framework_Exception */ public static function createTest(\ReflectionClass $theClass, $name, array $arguments = []) { $objectManager = \Mtf\ObjectManager::getInstance(); $class = $theClass->getName(); $arguments['name'] = $name; $backupSettings = \PHPUnit_Util_Test::getBackupSettings($class, $name); $preserveGlobalState = \PHPUnit_Util_Test::getPreserveGlobalStateSettings($class, $name); $runTestInSeparateProcess = \PHPUnit_Util_Test::getProcessIsolationSettings($class, $name); try { $data = \PHPUnit_Util_Test::getProvidedData($class, $name); } catch (\Exception $e) { /** @var EventManager $eventManager */ $eventManager = $objectManager->get('Mtf\\System\\Event\\Event'); $eventManager->dispatchEvent(['exception'], [$e]); $message = sprintf('The data provider specified for %s::%s is invalid.', $class, $name); $_message = $e->getMessage(); if (!empty($_message)) { $message .= "\n" . $_message; } $data = new \PHPUnit_Framework_Warning($message); } // Test method with @dataProvider. if (isset($data)) { $test = $objectManager->create('Mtf\\TestSuite\\InjectableDataProvider', ['class' => $class . '::' . $name]); if (empty($data)) { $data = new \PHPUnit_Framework_Warning(sprintf('No tests found in suite "%s".', $test->getName())); } $groups = \PHPUnit_Util_Test::getGroups($class, $name); if ($data instanceof \PHPUnit_Framework_Warning) { $test->addTest($data, $groups); } else { foreach ($data as $_dataName => $_data) { $_arguments = $arguments; $_arguments['data'] = $_data; $_arguments['dataName'] = $_dataName; $_test = $objectManager->create($class, $_arguments); if ($runTestInSeparateProcess) { $_test->setRunTestInSeparateProcess(true); if ($preserveGlobalState !== null) { $_test->setPreserveGlobalState($preserveGlobalState); } } if ($backupSettings['backupGlobals'] !== null) { $_test->setBackupGlobals($backupSettings['backupGlobals']); } if ($backupSettings['backupStaticAttributes'] !== null) { $_test->setBackupStaticAttributes($backupSettings['backupStaticAttributes']); } $test->addTest($_test, $groups); } } } else { $test = $objectManager->create($class, $arguments); } if (!isset($test)) { throw new \PHPUnit_Framework_Exception('No valid test provided.'); } if ($test instanceof \PHPUnit_Framework_TestCase) { $test->setName($name); if ($runTestInSeparateProcess) { $test->setRunTestInSeparateProcess(true); if ($preserveGlobalState !== null) { $test->setPreserveGlobalState($preserveGlobalState); } } if ($backupSettings['backupGlobals'] !== null) { $test->setBackupGlobals($backupSettings['backupGlobals']); } if ($backupSettings['backupStaticAttributes'] !== null) { $test->setBackupStaticAttributes($backupSettings['backupStaticAttributes']); } } return $test; }
/** * Return Wrapped Elements. * If element was not created before: * 1. Context is defined. If context was not passed to constructor - test case (all page) is taken as context * 2. Attempt to get selenium elements is performed in loop * that is terminated if elements is found or after timeout set in configuration * * @param bool $waitForElementPresent * @throws \PHPUnit_Extensions_Selenium2TestCase_Exception|\PHPUnit_Extensions_Selenium2TestCase_WebDriverException * @return \PHPUnit_Extensions_Selenium2TestCase_Element[] */ protected function _getWrappedElements($waitForElementPresent = true) { if (!$this->_wrappedElements) { $context = $this->getContext($waitForElementPresent); $criteria = new \PHPUnit_Extensions_Selenium2TestCase_ElementCriteria($this->_locator['using']); $criteria->value($this->_locator['value']); if ($waitForElementPresent) { $wrappedElements = $this->_driver->waitUntil(function () use($context, $criteria) { return $context->elements($criteria); }); } else { $this->waitPageToLoad(); $wrappedElements = $context->elements($criteria); } foreach ($wrappedElements as $wrappedElement) { $element = \Mtf\ObjectManager::getInstance()->create(get_class($this), ['locator' => $this->_locator, 'driver' => $this->_driver, 'context' => $this->_context]); $element->_wrappedElement = $wrappedElement; $this->_wrappedElements[] = $element; } } return $this->_wrappedElements; }
/** * Get MTF Object Manager instance * * @return ObjectManager */ public static function getObjectManager() { if (!($objectManager = ObjectManager::getInstance())) { $objectManagerFactory = new self(); $objectManager = $objectManagerFactory->create(); } return $objectManager; }
/** * Prepare test suite and apply application state * * @return \Mtf\TestSuite\AppState */ public function prepareSuite() { $this->init(); return $this->objectManager->create('Mtf\\TestSuite\\AppState'); }
/** * Get class * * @param string $class * @return mixed */ public function get($class) { $object = $this->objectManager->get($class); $this->checkInterface($object, $class); return $object; }
/** * Init Object Manager * * @return void */ public static function initObjectManager() { self::$_objectManager = \Mtf\ObjectManager::getInstance(); }
/** * Create Event class object * * @param array $tags * @param array $subjects * @return \Mtf\System\Event\Event */ public function create(array $tags, array $subjects) { return $this->objectManager->create('Mtf\\System\\Event\\Event', ['tags' => $tags, 'subjects' => $subjects]); }
/** * Initialize ObjectManager * @return void */ protected function initObjectManager() { $this->objectManager = \Mtf\ObjectManager::getInstance(); }
/** * Delete Store after test * * @return void */ public static function tearDownAfterClass() { $filter['store_title'] = self::$storeName; $storeIndex = ObjectManager::getInstance()->create('Magento\\Backend\\Test\\Page\\Adminhtml\\StoreIndex'); $storeIndex->open(); $storeIndex->getStoreGrid()->searchAndOpen($filter); $storeNew = ObjectManager::getInstance()->create('Magento\\Backend\\Test\\Page\\Adminhtml\\StoreNew'); $storeNew->getFormPageActions()->delete(); $storeDelete = ObjectManager::getInstance()->create('Magento\\Backend\\Test\\Page\\Adminhtml\\StoreDelete'); $storeDelete->getStoreForm()->fillForm(['create_backup' => 'No']); $storeDelete->getFormPageActions()->delete(); }
/** * Fill single condition * * @param array $rules * @param Element $element * @return void * @throws \Exception */ protected function fillCondition(array $rules, Element $element) { $this->resetKeyParam(); foreach ($rules as $rule) { $param = $this->findNextParam($element); $param->find('a')->click(); if (preg_match('`%(.*?)%`', $rule, $chooserGrid)) { $chooserConfig = explode('#', $chooserGrid[1]); $param->find($this->chooserLocator)->click(); $rule = preg_replace('`%(.*?)%`', '', $rule); $grid = ObjectManager::getInstance()->create(str_replace('/', '\\', $chooserConfig[0]), ['element' => $this->find($this->chooserGridLocator)]); $grid->searchAndSelect([$chooserConfig[1] => $rule]); continue; } $value = $param->find('select', Locator::SELECTOR_CSS, 'select'); if ($value->isVisible()) { $value->setValue($rule); continue; } $value = $param->find('input'); if ($value->isVisible()) { $value->setValue($rule); $apply = $param->find('.//*[@class="rule-param-apply"]', Locator::SELECTOR_XPATH); if ($apply->isVisible()) { $apply->click(); } continue; } throw new \Exception('Undefined type of value '); } }