/** * @return array */ public function allBlocksDataProvider() { $blockClass = ''; try { /** @var $website \Magento\Store\Model\Website */ \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Store\\Model\\StoreManagerInterface')->getStore()->setWebsiteId(0); $enabledModules = $this->_getEnabledModules(); $skipBlocks = $this->_getBlocksToSkip(); $templateBlocks = array(); $blockMods = \Magento\TestFramework\Utility\Classes::collectModuleClasses('Block'); foreach ($blockMods as $blockClass => $module) { if (!isset($enabledModules[$module]) || isset($skipBlocks[$blockClass])) { continue; } $class = new \ReflectionClass($blockClass); if ($class->isAbstract() || !$class->isSubclassOf('Magento\\Framework\\View\\Element\\Template')) { continue; } $templateBlocks = $this->_addBlock($module, $blockClass, $class, $templateBlocks); } return $templateBlocks; } catch (\Exception $e) { trigger_error("Corrupted data provider. Last known block instantiation attempt: '{$blockClass}'." . " Exception: {$e}", E_USER_ERROR); } }
public function testGetChildHtml() { $invoker = new \Magento\TestFramework\Utility\AggregateInvoker($this); $invoker(function ($file) { $result = \Magento\TestFramework\Utility\Classes::getAllMatches(file_get_contents($file), "/(->getChildHtml\\([^,()]+, ?[^,()]+,)/i"); $this->assertEmpty($result, "3rd parameter is not needed anymore for getChildHtml() in '{$file}': " . print_r($result, true)); $result = \Magento\TestFramework\Utility\Classes::getAllMatches(file_get_contents($file), "/(->getChildChildHtml\\([^,()]+, ?[^,()]+, ?[^,()]+,)/i"); $this->assertEmpty($result, "4th parameter is not needed anymore for getChildChildHtml() in '{$file}': " . print_r($result, true)); }, \Magento\TestFramework\Utility\Files::init()->getPhpFiles()); }
/** * @return array */ public function getChildBlockDataProvider() { $result = array(); foreach (\Magento\TestFramework\Utility\Files::init()->getPhpFiles(true, false, true, false) as $file) { $aliases = \Magento\TestFramework\Utility\Classes::getAllMatches(file_get_contents($file), '/\\->getChildBlock\\(\'([^\']+)\'\\)/x'); foreach ($aliases as $alias) { $result[$file] = array($alias, $file); } } return $result; }
public function testLayouts() { $invoker = new \Magento\TestFramework\Utility\AggregateInvoker($this); $invoker(function ($path) { $xml = simplexml_load_file($path); $classes = \Magento\TestFramework\Utility\Classes::collectLayoutClasses($xml); foreach (\Magento\TestFramework\Utility\Classes::getXmlAttributeValues($xml, '/layout//@helper', 'helper') as $class) { $classes[] = \Magento\TestFramework\Utility\Classes::getCallbackClass($class); } $classes = array_merge($classes, \Magento\TestFramework\Utility\Classes::getXmlAttributeValues($xml, '/layout//@module', 'module')); $this->_assertNonFactoryName(array_unique($classes), $path); $tabs = \Magento\TestFramework\Utility\Classes::getXmlNodeValues($xml, '/layout//action[@method="addTab"]/block'); $this->_assertNonFactoryName(array_unique($tabs), $path, true); }, \Magento\TestFramework\Utility\Files::init()->getLayoutFiles()); }
/** * @return array */ public function allTemplatesDataProvider() { $blockClass = ''; try { /** @var $website \Magento\Store\Model\Website */ \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Store\\Model\\StoreManagerInterface')->getStore()->setWebsiteId(0); $templates = array(); $skippedBlocks = $this->_getBlocksToSkip(); foreach (\Magento\TestFramework\Utility\Classes::collectModuleClasses('Block') as $blockClass => $module) { if (!in_array($module, $this->_getEnabledModules()) || in_array($blockClass, $skippedBlocks)) { continue; } $class = new \ReflectionClass($blockClass); if ($class->isAbstract() || !$class->isSubclassOf('Magento\\Framework\\View\\Element\\Template')) { continue; } $area = 'frontend'; if ($module == 'Magento_Install') { $area = 'install'; } elseif ($module == 'Magento_Adminhtml' || strpos($blockClass, '\\Adminhtml\\') || strpos($blockClass, '\\Backend\\') || $class->isSubclassOf('Magento\\Backend\\Block\\Template')) { $area = 'adminhtml'; } \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\App\\AreaList')->getArea($area)->load(\Magento\Framework\App\Area::PART_CONFIG); \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\Config\\ScopeInterface')->setCurrentScope($area); \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\App\\State')->setAreaCode($area); $context = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\App\\Http\\Context'); $context->setValue(\Magento\Customer\Helper\Data::CONTEXT_AUTH, false, false); $context->setValue(\Magento\Customer\Helper\Data::CONTEXT_GROUP, \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID, \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID); $block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($blockClass); $template = $block->getTemplate(); if ($template) { $templates[$module . ', ' . $template . ', ' . $blockClass . ', ' . $area] = array($module, $template, $blockClass, $area); } } return $templates; } catch (\Exception $e) { trigger_error("Corrupted data provider. Last known block instantiation attempt: '{$blockClass}'." . " Exception: {$e}", E_USER_ERROR); } }
/** * Get application plugins * * @return array */ protected function pluginDataProvider() { $files = \Magento\TestFramework\Utility\Files::init()->getDiConfigs(); $plugins = array(); foreach ($files as $file) { $dom = new \DOMDocument(); $dom->load($file); $xpath = new \DOMXPath($dom); $pluginList = $xpath->query('//config/type/plugin'); foreach ($pluginList as $node) { /** @var $node \DOMNode */ $type = $node->parentNode->attributes->getNamedItem('name')->nodeValue; $type = \Magento\TestFramework\Utility\Classes::resolveVirtualType($type); if ($node->attributes->getNamedItem('type')) { $plugin = $node->attributes->getNamedItem('type')->nodeValue; $plugin = \Magento\TestFramework\Utility\Classes::resolveVirtualType($plugin); $plugins[] = array('plugin' => $plugin, 'intercepted type' => $type); } } } return $plugins; }
$factoryNames = array_filter($classes, 'isFactoryName'); if (!$factoryNames) { continue; } $search = array(); $replace = array(); foreach ($factoryNames as $factoryName) { list($module, $name) = getModuleName($factoryName); addReplace($factoryName, $module, $name, 'type="%s"', '_Block_', $search, $replace); } replaceAndOutput($file, $search, $replace, $factoryNames); } // modules in configuration and layouts $configs = \Magento\TestFramework\Utility\Files::init()->getConfigFiles('*.xml', array('wsdl.xml', 'wsdl2.xml', 'wsi.xml'), false); foreach (array_merge($layouts, $configs) as $file) { $modules = array_unique(\Magento\TestFramework\Utility\Classes::getXmlAttributeValues(simplexml_load_file($file), '//@module', 'module')); $factoryNames = array_filter($modules, 'isFactoryName'); if (!$factoryNames) { continue; } $search = array(); $replace = array(); foreach ($factoryNames as $factoryName) { list($module, $name) = getModuleName($factoryName); if ($module) { $search[] = 'module="' . $factoryName . '"'; $replace[] = 'module="' . implode('\\', array_map('ucfirst', explode('_', $module))) . '"'; } else { $search[] = 'module="' . $factoryName . '"'; $replace[] = 'module="' . implode('\\', array_map('ucfirst', explode('_', $name))) . '"'; }
if (isPatternExist($content, $pattern, $factoryName)) { if (!isset($map[$classType])) { $map[$classType] = array(); } $map[$classType][$factoryName] = getClassName($module, $classType, $name); } } } } } // layouts $classType = 'Block'; $layouts = $utilityFiles->getLayoutFiles(array(), false); foreach ($layouts as $file) { $xml = simplexml_load_file($file); $classes = \Magento\TestFramework\Utility\Classes::collectLayoutClasses($xml); $factoryNames = array_filter($classes, 'isFactoryName'); if (!$factoryNames) { continue; } foreach ($factoryNames as $factoryName) { list($module, $name) = getModuleName($factoryName, $compositeModules); $map[$classType][$factoryName] = getClassName($module, $classType, $name); } } echo Zend_Json::prettyPrint(Zend_Json::encode($map)); /** * Get combined array from similar files by pattern * * @param string $dirPath * @param string $filePattern
/** * Check whether specified classes correspond to a file according PSR-0 standard * * Cyclomatic complexity is because of temporary marking test as incomplete * Suppressing "unused variable" because of the "catch" block * * @param array $classes * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function _assertClassesExist($classes, $path) { if (!$classes) { return; } $badClasses = array(); $badUsages = array(); foreach ($classes as $class) { try { if (strrchr($class, '\\') == false) { $badUsages[] = $class; continue; } else { $this->assertTrue(isset(self::$_existingClasses[$class]) || \Magento\TestFramework\Utility\Files::init()->classFileExists($class) || \Magento\TestFramework\Utility\Classes::isVirtual($class) || \Magento\TestFramework\Utility\Classes::isAutogenerated($class)); } self::$_existingClasses[$class] = 1; } catch (\PHPUnit_Framework_AssertionFailedError $e) { $badClasses[] = $class; } } if ($badClasses) { $this->fail("Files not found for following usages in {$path}:\n" . implode("\n", $badClasses)); } if ($badUsages) { $this->fail("Bad usages of classes in {$path}: \n" . implode("\n", $badUsages)); } }