Skip to content

Commit

Permalink
Merge pull request #16 from ho-nl/feature/test-modules2
Browse files Browse the repository at this point in the history
Feature/test modules2
  • Loading branch information
paales authored Aug 23, 2020
2 parents 0ff72f9 + a023aee commit 204c89c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ php bin/magento dev:tests:run integration -c $(pwd)/dev/tests/quick-integration/
### TestModule

Automatically installs test modules that are available in the following path:
`vendor/*/*/TestModule*` so for example
`vendor/reach-digital/magento2-order-source-reservations/TestModuleInventoryStateCache`.
`vendor/*/*/TestModule/*/*` so for example
`vendor/reach-digital/magento2-order-source-reservations/TestModule/Magento/TestModuleInventoryStateCache`.

## Goals

Expand Down
22 changes: 0 additions & 22 deletions framework/ReachDigital/TestFramework/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

namespace ReachDigital\TestFramework;

use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\TestFramework\Isolation\DeploymentConfig;

class Application extends \Magento\TestFramework\Application
{
Expand All @@ -37,28 +34,9 @@ public function install($cleanup)
copy($file, $targetFile);
}

$configData = include $file;
$configData['modules'] = array_merge($configData['modules'], $this->getTestModules());

file_put_contents($targetFile, (new PhpFormatter())->format($configData));

parent::install($cleanup);
}

private function getTestModules()
{
$modules = (new ComponentRegistrar())->getPaths(ComponentRegistrar::MODULE);

$filteredModules = array_flip(
array_filter(array_keys($modules), function ($item) {
return strpos($item, 'TestModule') !== false;
})
);
return array_map(function () {
return 1;
}, $filteredModules);
}

/**
* @inheritDoc
*/
Expand Down
124 changes: 111 additions & 13 deletions framework/deployVendorTestModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,122 @@
* See COPYING.txt for license details.
*/

use Magento\Framework\Component\ComponentRegistrar;
use Magento\TestFramework\Bootstrap\Settings;

/**
* phpcs:disable PSR1.Files.SideEffects
* phpcs:disable Squiz.Functions.GlobalFunction
* @var string $testFrameworkDir - Must be defined in parent script.
* @var \Magento\TestFramework\Bootstrap\Settings $settings - Must be defined in parent script.
* @var Settings $settings - Must be defined in parent script.
*/

/** Copy test modules to app/code/Magento to make them visible for Magento instance */
$vendorPath = realpath($testFrameworkDir . '/../../../../vendor');
class DeployVendorTestModules
{
/** @var string */
private $vendorPath;

// Register the modules under '_files/'
$pathPattern = $vendorPath . '/*/*/TestModule*/registration.php';
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$files = glob($pathPattern, GLOB_NOSORT);
if ($files === false) {
throw new \RuntimeException('glob() returned error while searching in \'' . $pathPattern . '\'');
}
foreach ($files as $file) {
// phpcs:ignore Magento2.Security.IncludeFile
include $file;
/** @var ComponentRegistrar */
private $registrar;

/** @var string */
private $targetPath;

public function __construct(string $testFrameworkDir)
{
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$this->vendorPath = realpath($testFrameworkDir . '/../../../../vendor');

// phpcs:ignore Magento2.Functions.DiscouragedFunction
$this->targetPath = realpath($testFrameworkDir . '/../../../../app/code');
$this->registrar = new ComponentRegistrar();
}

private function getModulePaths()
{
return new GlobIterator(
$this->vendorPath . '/*/*/TestModule/*/*/*',
FilesystemIterator::KEY_AS_PATHNAME |
FilesystemIterator::CURRENT_AS_FILEINFO |
FilesystemIterator::FOLLOW_SYMLINKS
);
}

public function install()
{
foreach ($this->getModulePaths() as $testModule) {
$modulePath = $testModule->getPath() . '/';

$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($modulePath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS)
);

foreach ($iterator as $file) {
if (!$file->isDir()) {
$source = $file->getPathname();

$parts = explode(DIRECTORY_SEPARATOR, substr($source, strlen($this->vendorPath)));
$relativePath = implode(DIRECTORY_SEPARATOR, array_slice($parts, 6));

$ns = $parts[4];
$module = $parts[5];

$destination =
$this->targetPath .
DIRECTORY_SEPARATOR .
$ns .
DIRECTORY_SEPARATOR .
$module .
DIRECTORY_SEPARATOR .
$relativePath;

// phpcs:ignore Magento2.Functions.DiscouragedFunction
$targetDir = dirname($destination);

// phpcs:ignore Magento2.Functions.DiscouragedFunction
if (!is_dir($targetDir)) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
mkdir($targetDir, 0755, true);
}
// phpcs:ignore Magento2.Functions.DiscouragedFunction
copy($source, $destination);
}
}
include $modulePath . '/registration.php';
}
}

public function uninstall()
{
$filesystem = new \Symfony\Component\Filesystem\Filesystem();

foreach ($this->getModulePaths() as $testModule) {
$modulePath = $testModule->getPath() . '/';

$parts = explode(DIRECTORY_SEPARATOR, substr($modulePath, strlen($this->vendorPath)));
$relativePath = implode(DIRECTORY_SEPARATOR, array_slice($parts, 6));

$ns = $parts[4];
$module = $parts[5];

$destination =
$this->targetPath .
DIRECTORY_SEPARATOR .
$ns .
DIRECTORY_SEPARATOR .
$module .
DIRECTORY_SEPARATOR .
$relativePath;

$filesystem->remove($destination);
}
}
}

$deploy = new DeployVendorTestModules($testFrameworkDir);
$deploy->install();

// phpcs:ignore Magento2.Functions.DiscouragedFunction
register_shutdown_function(function (DeployVendorTestModules $deploy) {
$deploy->uninstall();
}, $deploy);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@reach-digital/magento2-inventory-source-reservations",
"name": "@reach-digital/magento2-reach-digital-test-framework",
"private": true,
"repository": {
"type": "git",
"url": "[email protected]:ho-nl/magento2-ReachDigital_InventoryOrderSourceReservations.git"
"url": "[email protected]:ho-nl/magento2-ReachDigital_TestFramework.git"
},
"author": "Paul Hachmang <[email protected]>",
"devDependencies": {
Expand Down

0 comments on commit 204c89c

Please sign in to comment.