/** * Get array of class names * * @param array $files * @return array */ public function collectEntities(array $files) { $repositoryClassNames = []; foreach ($files as $fileName) { $dom = new \DOMDocument(); $dom->loadXML(file_get_contents($fileName)); $xpath = new \DOMXPath($dom); /** @var $node \DOMNode */ foreach ($xpath->query('//preference') as $node) { $forType = $node->attributes->getNamedItem('for'); $replacementType = $node->attributes->getNamedItem('type'); if ($forType !== null && $replacementType !== null && substr($forType->nodeValue, -19) == 'RepositoryInterface') { if (!class_exists($replacementType->nodeValue, false) && !AutoloaderRegistry::getAutoloader()->loadClass($replacementType->nodeValue)) { $persistor = str_replace('\\Repository', 'InterfacePersistor', $replacementType->nodeValue); $factory = str_replace('\\Repository', 'InterfaceFactory', $replacementType->nodeValue); $searchResultFactory = str_replace('\\Repository', 'SearchResultInterfaceFactory', $replacementType->nodeValue); $repositoryClassNames[$persistor] = $persistor; $repositoryClassNames[$factory] = $factory; $repositoryClassNames[$searchResultFactory] = $searchResultFactory; $repositoryClassNames[$replacementType->nodeValue] = $replacementType->nodeValue; } } } } return $repositoryClassNames; }
protected function setUp() { $autoloadWrapper = \Magento\Framework\Autoload\AutoloaderRegistry::getAutoloader(); $autoloadWrapper->addPsr4('Magento\\Wonderland\\', realpath(__DIR__ . '/_files/Magento/Wonderland')); $this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->_objectManager->configure(['preferences' => ['Magento\\Wonderland\\Api\\Data\\FakeAddressInterface' => 'Magento\\Wonderland\\Model\\FakeAddress', 'Magento\\Wonderland\\Api\\Data\\FakeRegionInterface' => 'Magento\\Wonderland\\Model\\FakeRegion']]); }
protected function setUp() { $autoloadWrapper = \Magento\Framework\Autoload\AutoloaderRegistry::getAutoloader(); $autoloadWrapper->addPsr4('Magento\\Wonderland\\', realpath(__DIR__ . '/_files/Magento/Wonderland')); /** @var \Magento\Framework\ObjectManagerInterface */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $this->factory = $objectManager->create('Magento\\Framework\\Api\\ExtensionAttributesFactory', ['objectManager' => $objectManager]); }
/** * Determine if a class exists on disc * * @param string $className * @return bool */ public function isClassLoadableFromDisc($className) { try { return (bool) AutoloaderRegistry::getAutoloader()->findFile($className); } catch (\Exception $e) { // Couldn't get access to the autoloader so we need to allow class_exists to call autoloader chain return class_exists($className) || interface_exists($className); } }
/** * Performs autoload for given class name * * @param string $className * @return bool */ protected function performAutoload($className) { try { return AutoloaderRegistry::getAutoloader()->loadClass($className); } catch (\Exception $e) { // Couldn't get access to the autoloader so we need to allow class_exists to call autoloader chain return class_exists($className) || interface_exists($className); } }
public function testCopyFieldsetWithAbstractSimpleObject() { $autoloadWrapper = \Magento\Framework\Autoload\AutoloaderRegistry::getAutoloader(); $autoloadWrapper->addPsr4('Magento\\Wonderland\\', realpath(__DIR__ . '/_files/Magento/Wonderland')); $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $fieldset = 'sales_copy_order'; $aspect = 'to_edit'; $fieldsetConfigMock = $this->getMockBuilder('\\Magento\\Framework\\DataObject\\Copy\\Config')->disableOriginalConstructor()->setMethods(['getFieldSet'])->getMock(); $service = $objectManager->create('Magento\\Framework\\DataObject\\Copy', ['fieldsetConfig' => $fieldsetConfigMock]); $data = ['store_label' => ['to_edit' => '*'], 'frontend_label' => ['to_edit' => '*'], 'attribute_code' => ['to_edit' => '*'], 'note' => ['to_edit' => '*']]; $fieldsetConfigMock->expects($this->any())->method('getFieldSet')->willReturn($data); $source = $objectManager->get('Magento\\Wonderland\\Model\\Data\\FakeAttributeMetadata'); $source->setStoreLabel('storeLabel'); $source->setFrontendLabel('frontendLabel'); $source->setAttributeCode('attributeCode'); $source->setNote('note'); $target = $objectManager->get('Magento\\Wonderland\\Model\\Data\\FakeAttributeMetadata'); $expectedTarget = $source; $this->assertEquals($target, $service->copyFieldsetToTarget('invalid_fieldset', $aspect, $source, $target)); $this->assertEquals($expectedTarget, $service->copyFieldsetToTarget($fieldset, $aspect, $source, $target)); }
protected function setUp() { $this->model = new DefinedClasses(); self::$definedClassesTestActive = true; $this->initAutoloader = AutoloaderRegistry::getAutoloader(); }
<?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ require_once __DIR__ . '/../../../../app/autoload.php'; $testsBaseDir = dirname(__DIR__); $integrationTestsDir = realpath("{$testsBaseDir}/../integration/"); $autoloadWrapper = \Magento\Framework\Autoload\AutoloaderRegistry::getAutoloader(); $autoloadWrapper->addPsr4('Magento\\TestFramework\\', "{$testsBaseDir}/framework/Magento/TestFramework/"); $autoloadWrapper->addPsr4('Magento\\TestFramework\\', "{$integrationTestsDir}/framework/Magento/TestFramework/"); $autoloadWrapper->addPsr4('Magento\\', "{$testsBaseDir}/testsuite/Magento/"); // registration of classes under '../_files' $autoloadWrapper->addPsr4('Magento\\', "{$testsBaseDir}/_files/");
$logWriter->setFormatter(new \Zend_Log_Formatter_Simple('%message%' . PHP_EOL)); $shell = new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer(), new \Zend_Log($logWriter)); } else { $shell = new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer()); } $installConfigFile = $settings->getAsConfigFile('TESTS_INSTALL_CONFIG_FILE'); if (!file_exists($installConfigFile)) { $installConfigFile .= '.dist'; } $globalConfigFile = $settings->getAsConfigFile('TESTS_GLOBAL_CONFIG_FILE'); if (!file_exists($globalConfigFile)) { $globalConfigFile .= '.dist'; } $sandboxUniqueId = md5(sha1_file($installConfigFile)); $installDir = TESTS_TEMP_DIR . "/sandbox-{$settings->get('TESTS_PARALLEL_THREAD', 0)}-{$sandboxUniqueId}"; $application = new \Magento\TestFramework\Application($shell, $installDir, $installConfigFile, $globalConfigFile, $settings->get('TESTS_GLOBAL_CONFIG_DIR'), $settings->get('TESTS_MAGENTO_MODE'), AutoloaderRegistry::getAutoloader(), true); $bootstrap = new \Magento\TestFramework\Bootstrap($settings, new \Magento\TestFramework\Bootstrap\Environment(), new \Magento\TestFramework\Bootstrap\DocBlock("{$testsBaseDir}/testsuite"), new \Magento\TestFramework\Bootstrap\Profiler(new \Magento\Framework\Profiler\Driver\Standard()), $shell, $application, new \Magento\TestFramework\Bootstrap\MemoryFactory($shell)); $bootstrap->runBootstrap(); if ($settings->getAsBoolean('TESTS_CLEANUP')) { $application->cleanup(); } if (!$application->isInstalled()) { $application->install(); } $application->initialize([]); \Magento\TestFramework\Helper\Bootstrap::setInstance(new \Magento\TestFramework\Helper\Bootstrap($bootstrap)); \Magento\Framework\App\Utility\Files::setInstance(new Magento\Framework\App\Utility\Files($magentoBaseDir)); /* Unset declared global variables to release the PHPUnit from maintaining their values between tests */ unset($testsBaseDir, $magentoBaseDir, $logWriter, $settings, $shell, $application, $bootstrap); } catch (\Exception $e) { echo $e . PHP_EOL;
/** * Populates autoloader with mapping info * * @param string $rootDir * @param array $initParams * @return void */ public static function populateAutoloader($rootDir, $initParams) { $dirList = self::createFilesystemDirectoryList($rootDir, $initParams); $autoloadWrapper = AutoloaderRegistry::getAutoloader(); Populator::populateMappings($autoloadWrapper, $dirList); }
try { $opt = new Zend_Console_Getopt(['serializer=w' => 'serializer function that should be used (serialize|igbinary) default: serialize', 'verbose|v' => 'output report after tool run', 'extra-classes-file=s' => 'path to file with extra proxies and factories to generate', 'generation=s' => 'absolute path to generated classes, <magento_root>/var/generation by default', 'di=s' => 'absolute path to DI definitions directory, <magento_root>/var/di by default', 'exclude-pattern=s' => 'allows to exclude Paths from compilation (default is #[\\\\/]m1[\\\\/]#i)']); $opt->parse(); $generationDir = $opt->getOption('generation') ? $opt->getOption('generation') : $rootDir . '/var/generation'; $diDir = $opt->getOption('di') ? $opt->getOption('di') : $rootDir . '/var/di'; $testExcludePatterns = ["#^{$rootDir}/app/code/[\\w]+/[\\w]+/Test#", "#^{$rootDir}/lib/internal/[\\w]+/[\\w]+/([\\w]+/)?Test#", "#^{$rootDir}/setup/src/Magento/Setup/Test#", "#^{$rootDir}/dev/tools/Magento/Tools/[\\w]+/Test#"]; $fileExcludePatterns = $opt->getOption('exclude-pattern') ? [$opt->getOption('exclude-pattern')] : ['#[\\\\/]M1[\\\\/]#i']; $fileExcludePatterns = array_merge($fileExcludePatterns, $testExcludePatterns); $relationsFile = $diDir . '/relations.ser'; $pluginDefFile = $diDir . '/plugins.ser'; $compilationDirs = [$rootDir . '/app/code', $rootDir . '/lib/internal/Magento', $rootDir . '/dev/tools/Magento/Tools']; /** @var Writer\WriterInterface $logWriter Writer model for success messages */ $logWriter = $opt->getOption('v') ? new Writer\Console() : new Writer\Quiet(); $log = new Log($logWriter, new Writer\Console()); $serializer = $opt->getOption('serializer') == Igbinary::NAME ? new Igbinary() : new Standard(); AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', $generationDir . '/Magento/'); // 1 Code generation // 1.1 Code scan $filePatterns = ['php' => '/.*\\.php$/', 'di' => '/\\/etc\\/([a-zA-Z_]*\\/di|di)\\.xml$/']; $codeScanDir = realpath($rootDir . '/app'); $directoryScanner = new Scanner\DirectoryScanner(); $files = $directoryScanner->scan($codeScanDir, $filePatterns, $fileExcludePatterns); $files['additional'] = [$opt->getOption('extra-classes-file')]; $entities = []; $repositoryScanner = new Scanner\RepositoryScanner(); $repositories = $repositoryScanner->collectEntities($files['di']); $scanner = new Scanner\CompositeScanner(); $scanner->addChild(new Scanner\PhpScanner($log), 'php'); $scanner->addChild(new Scanner\XmlScanner($log), 'di'); $scanner->addChild(new Scanner\ArrayScanner(), 'additional'); $entities = $scanner->collectEntities($files);
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $errors = $this->validate($input); if ($errors) { $output->writeln($errors); return; } $generationDir = $input->getOption(self::INPUT_KEY_GENERATION) ? $input->getOption(self::INPUT_KEY_GENERATION) : $this->directoryList->getPath(DirectoryList::GENERATION); $modulesExcludePatterns = $this->getModuleExcludePatterns(); $testExcludePatterns = ["#^" . $this->directoryList->getPath(DirectoryList::SETUP) . "/[\\w]+/[\\w]+/Test#", "#^" . $this->directoryList->getRoot() . "/dev/tools/Magento/Tools/[\\w]+/Test#"]; $librariesExcludePatterns = $this->getLibraryExcludePatterns(); $testExcludePatterns = array_merge($testExcludePatterns, $modulesExcludePatterns, $librariesExcludePatterns); $fileExcludePatterns = $input->getOption('exclude-pattern') ? [$input->getOption(self::INPUT_KEY_EXCLUDE_PATTERN)] : ['#[\\\\/]M1[\\\\/]#i']; $fileExcludePatterns = array_merge($fileExcludePatterns, $testExcludePatterns); /** @var Writer\Console logWriter Writer model for success messages */ $logWriter = new Writer\Console($output); $this->log = new Log($logWriter, $logWriter); AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', $generationDir . '/Magento/'); // 1 Code generation $this->generateCode($generationDir, $fileExcludePatterns, $input); // 2. Compilation $this->compileCode($generationDir, $fileExcludePatterns, $input); //Reporter $this->log->report(); if (!$this->log->hasError()) { $output->writeln('<info>On *nix systems, verify the Magento application has permissions to modify files ' . 'created by the compiler in the "var" directory. For instance, if you run the Magento application ' . 'using Apache, the owner of the files in the "var" directory should be the Apache user (example ' . 'command: "chown -R www-data:www-data <MAGENTO_ROOT>/var" where MAGENTO_ROOT is the Magento ' . 'root directory).</info>'); } }
/** * A script for deploying static view files for Magento system "production mode" * * The resulting files will be recorded into pub/static directory. * They can be used not only by the server where Magento instance is, * but also can be copied to a CDN, and the Magento instance may be configured to generate base URL to the CDN. * * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ use Magento\Framework\Autoload\AutoloaderRegistry; $baseName = basename(__FILE__); $options = getopt('', ['langs::', 'dry-run', 'verbose::', 'help']); define('USAGE', "USAGE:\n\tphp -f {$baseName} -- [--langs=en_US,de_DE,...] [--verbose=0|1] [--dry-run] [--help]\n"); require __DIR__ . '/../../../../../app/bootstrap.php'; AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', [BP . '/tools/Magento/']); // parse all options if (isset($options['help'])) { echo USAGE; exit(0); } $langs = ['en_US']; if (isset($options['langs'])) { $langs = explode(',', $options['langs']); foreach ($langs as $lang) { if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $lang)) { echo USAGE; exit(1); } } }
mkdir($targetDir, 0755, true); } copy($source, $destination); } } unset($iterator, $file); /* Bootstrap the application */ $settings = new \Magento\TestFramework\Bootstrap\Settings($testsBaseDir, get_defined_constants()); $shell = new \Magento\Framework\Shell(new \Magento\Framework\Shell\CommandRenderer(), $logger); $installConfigFile = $settings->getAsConfigFile('TESTS_INSTALL_CONFIG_FILE'); if (!file_exists($installConfigFile)) { $installConfigFile = $installConfigFile . '.dist'; } $globalConfigFile = $settings->getAsConfigFile('TESTS_GLOBAL_CONFIG_FILE'); if (!file_exists($installConfigFile)) { $installConfigFile = $installConfigFile . '.dist'; } $dirList = new \Magento\Framework\App\Filesystem\DirectoryList(BP); $application = new \Magento\TestFramework\WebApiApplication($shell, $dirList->getPath(DirectoryList::VAR_DIR), $installConfigFile, $globalConfigFile, BP . '/app/etc/', $settings->get('TESTS_MAGENTO_MODE'), AutoloaderRegistry::getAutoloader()); if (defined('TESTS_MAGENTO_INSTALLATION') && TESTS_MAGENTO_INSTALLATION === 'enabled') { if (defined('TESTS_CLEANUP') && TESTS_CLEANUP === 'enabled') { $application->cleanup(); } $application->install(); } $bootstrap = new \Magento\TestFramework\Bootstrap($settings, new \Magento\TestFramework\Bootstrap\Environment(), new \Magento\TestFramework\Bootstrap\WebapiDocBlock("{$integrationTestsDir}/testsuite"), new \Magento\TestFramework\Bootstrap\Profiler(new \Magento\Framework\Profiler\Driver\Standard()), $shell, $application, new \Magento\TestFramework\Bootstrap\MemoryFactory($shell)); $bootstrap->runBootstrap(); $application->initialize(); \Magento\TestFramework\Helper\Bootstrap::setInstance(new \Magento\TestFramework\Helper\Bootstrap($bootstrap)); \Magento\Framework\App\Utility\Files::setInstance(new \Magento\Framework\App\Utility\Files(BP)); unset($bootstrap, $application, $settings, $shell);
$installConfigFile = $settings->getAsConfigFile('TESTS_INSTALL_CONFIG_FILE'); if (!file_exists($installConfigFile)) { $installConfigFile .= '.dist'; } // Additional Magento configuration for the testing context. // @TODO What configuration can be set this way? $globalConfigFile = $settings->getAsConfigFile('TESTS_GLOBAL_CONFIG_FILE'); if (!file_exists($globalConfigFile)) { $globalConfigFile .= '.dist'; } // Create a sandbox directory for configuration, generated, cache, session // and related files created for the test context. $sandboxUniqueId = md5(sha1_file($installConfigFile)); $installDir = "{$testsTmpDir}/sandbox-{$settings->get('TESTS_PARALLEL_THREAD', 0)}-{$sandboxUniqueId}"; // Create the application - M2 application context for the test run. $application = new Application($shell, $installDir, $installConfigFile, $globalConfigFile, $settings->get('TESTS_GLOBAL_CONFIG_DIR'), $settings->get('TESTS_MAGENTO_MODE'), AutoloaderRegistry::getAutoloader()); // Create a bootstrapper which will do most of the work of setting up the // M2 environment for the application to run. $bootstrap = new Bootstrap($settings, new Environment(), new BehatDocBlock("{$testsBaseDir}/testsuite"), new Profiler(new StandardProfilerDriver()), $shell, $application, new MemoryFactory($shell)); // Setup for environment HTTP and session emulation, profiling, memory // limits, and event registration (through a somewhat dubious side-effect // of DocBlock annotation registration). $bootstrap->runBootstrap(); // cleanup and install ensure the M2 environment is clean before running // tests - uninstalls M2 and then reinstalls it using the test configuration. // @TODO This may need to be extended to also include any initial data needed // for the tests. if ($settings->getAsBoolean('TESTS_CLEANUP')) { $application->cleanup(); } if (!$application->isInstalled()) {
/** * A script for deploying static view files for Magento system "production mode" * * The resulting files will be recorded into pub/static directory. * They can be used not only by the server where Magento instance is, * but also can be copied to a CDN, and the Magento instance may be configured to generate base URL to the CDN. * * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ use Magento\Framework\Autoload\AutoloaderRegistry; $baseName = basename(__FILE__); $options = getopt('', ['langs::', 'dry-run', 'verbose::', 'help']); define('USAGE', "USAGE:\n\tphp -f {$baseName} -- [--langs=en_US,de_DE,...] [--verbose=0|1] [--dry-run] [--help]\n"); require __DIR__ . '/../../../../../app/bootstrap.php'; AutoloaderRegistry::getAutoloader()->addPsr4('Magento\\', [BP . '/dev/tests/static/framework/Magento/', realpath(__DIR__ . '/../../../Magento/')]); // parse all options if (isset($options['help'])) { echo USAGE; exit(0); } $langs = ['en_US']; if (isset($options['langs'])) { $langs = explode(',', $options['langs']); foreach ($langs as $lang) { if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $lang)) { echo USAGE; exit(1); } } }