Skip to content

Commit db4cd0d

Browse files
committed
MQE-351:Single test run for running tests in parallel mode
- add Test Manifest Class - invoke new class during TestGenerator runs - change hardcoded slash to PHP constant - add directory clear between generate calls
1 parent 1f81574 commit db4cd0d

File tree

2 files changed

+111
-9
lines changed

2 files changed

+111
-9
lines changed

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66

77
namespace Magento\FunctionalTestingFramework\Util;
88

9+
use FilesystemIterator;
910
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
1011
use Magento\FunctionalTestingFramework\Test\Handlers\CestObjectHandler;
1112
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1213
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
14+
use Magento\FunctionalTestingFramework\Test\Objects\CestObject;
15+
use RecursiveDirectoryIterator;
1316

1417
class TestGenerator
1518
{
19+
20+
/**
21+
* Path to the export dir.
22+
*
23+
* @var string
24+
*/
25+
private $exportDirectory;
26+
1627
/**
1728
* Test generator.
1829
*
@@ -23,9 +34,47 @@ class TestGenerator
2334
/**
2435
* TestGenerator constructor.
2536
*/
26-
private function __construct()
37+
private function __construct($exportDir)
2738
{
2839
// private constructor for singleton
40+
$this->exportDirectory = $exportDir;
41+
}
42+
43+
/**
44+
* Method used to clean export dir if needed and create new empty export dir.
45+
*
46+
* @return void
47+
*/
48+
private function setupExportDir()
49+
{
50+
if (file_exists($this->exportDirectory)) {
51+
$this->rmDirRecursive($this->exportDirectory);
52+
}
53+
54+
mkdir($this->exportDirectory, 0777, true);
55+
}
56+
57+
/**
58+
* Takes a directory path and recursively deletes all files and folders.
59+
*
60+
* @param string $directory
61+
*/
62+
private function rmdirRecursive($directory)
63+
{
64+
$it = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
65+
66+
while($it->valid()) {
67+
$path = $directory . DIRECTORY_SEPARATOR . $it->getFilename();
68+
if ($it->isDir()) {
69+
$this->rmDirRecursive($path);
70+
} else {
71+
unlink($path);
72+
}
73+
74+
$it->next();
75+
}
76+
77+
rmdir($directory);
2978
}
3079

3180
/**
@@ -36,7 +85,7 @@ private function __construct()
3685
public static function getInstance()
3786
{
3887
if (!self::$testGenerator) {
39-
self::$testGenerator = new TestGenerator();
88+
self::$testGenerator = new TestGenerator(TESTS_MODULE_PATH . DIRECTORY_SEPARATOR . "_generated");
4089
}
4190

4291
return self::$testGenerator;
@@ -63,13 +112,7 @@ private function loadAllCestObjects()
63112
*/
64113
private function createCestFile($cestPhp, $filename)
65114
{
66-
$exportDirectory = TESTS_MODULE_PATH . "/_generated";
67-
$exportFilePath = sprintf("%s/%s.php", $exportDirectory, $filename);
68-
69-
if (!is_dir($exportDirectory)) {
70-
mkdir($exportDirectory, 0777, true);
71-
}
72-
115+
$exportFilePath = $this->exportDirectory . DIRECTORY_SEPARATOR . $filename . ".php";
73116
$file = fopen($exportFilePath, 'w');
74117

75118
if (!$file) {
@@ -88,6 +131,7 @@ private function createCestFile($cestPhp, $filename)
88131
*/
89132
public function createAllCestFiles()
90133
{
134+
$this->setupExportDir();
91135
$cestPhpArray = $this->assembleAllCestPhp();
92136

93137
foreach ($cestPhpArray as $cestPhpFile) {
@@ -134,11 +178,17 @@ private function assembleAllCestPhp()
134178
$cestObjects = $this->loadAllCestObjects();
135179
$cestPhpArray = [];
136180

181+
// create our manifest file here
182+
$testManifest = new TestManifest($this->exportDirectory);
183+
137184
foreach ($cestObjects as $cest) {
138185
$name = $cest->getName();
139186
$name = $string = str_replace(' ', '', $name);
140187
$php = $this->assembleCestPhp($cest);
141188
$cestPhpArray[] = [$name, $php];
189+
190+
//write to manifest here
191+
$testManifest->recordCest($cest->getName(), $cest->getTests());
142192
}
143193

144194
return $cestPhpArray;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\FunctionalTestingFramework\Util;
8+
9+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
10+
11+
class TestManifest
12+
{
13+
/**
14+
* Test Manifest file path.
15+
*
16+
* @var string
17+
*/
18+
private $filePath;
19+
20+
/**
21+
* TestManifest constructor.
22+
*
23+
* @param string $path
24+
*/
25+
public function __construct($path)
26+
{
27+
$filePath = $path . DIRECTORY_SEPARATOR . 'testManifest.txt';
28+
$this->filePath = $filePath;
29+
$fileResource = fopen($filePath, 'w');
30+
fclose($fileResource);
31+
}
32+
33+
/**
34+
* Takes a cest name and set of tests, records the names in a file for codeception to consume.
35+
*
36+
* @param string $cestName
37+
* @param TestObject $tests
38+
* @return void
39+
*/
40+
public function recordCest($cestName, $tests)
41+
{
42+
$fileResource = fopen($this->filePath, 'a');
43+
44+
foreach ($tests as $test)
45+
{
46+
$line = $cestName . ':' . $test->getName();
47+
fwrite($fileResource, $line ."\n");
48+
}
49+
50+
fclose($fileResource);
51+
}
52+
}

0 commit comments

Comments
 (0)