Skip to content

Commit a8cb888

Browse files
committed
Merge branch '2.4-develop' into PR_L3_06_04_2022
2 parents ade6b51 + 97eb9ae commit a8cb888

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

dev/tests/integration/framework/Magento/TestFramework/Fixture/LegacyDataFixture.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ private function execute(string $filePath): void
6666
require $filePath;
6767
} catch (\Throwable $e) {
6868
throw new \Exception(
69-
'Error in fixture ' . $filePath . PHP_EOL . $e->getTraceAsString(),
69+
'Error in fixture: ' . $filePath
70+
. PHP_EOL . $e->getMessage()
71+
. PHP_EOL . $e->getTraceAsString(),
7072
0,
7173
$e
7274
);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
9+
namespace Magento\Test\Integrity;
10+
11+
use Magento\Framework\Component\ComponentRegistrar;
12+
use Magento\TestFramework\Fixture\DataFixtureInterface;
13+
use Magento\TestFramework\Utility\AddedFiles;
14+
use Magento\TestFramework\Utility\ClassNameExtractor;
15+
use PHPUnit\Framework\TestCase;
16+
use ReflectionClass;
17+
use ReflectionException;
18+
19+
/**
20+
* Static test for legacy data fixtures
21+
*/
22+
class ParameterizedFixtureTest extends TestCase
23+
{
24+
/**
25+
* Validates parameterized data fixtures location
26+
*
27+
* @return void
28+
*/
29+
public function testLocation(): void
30+
{
31+
$classNameExtractor = new ClassNameExtractor();
32+
$files = AddedFiles::getAddedFilesList(__DIR__ . '/..');
33+
$errors = [];
34+
foreach ($files as $file) {
35+
if (pathinfo($file, PATHINFO_EXTENSION) !== 'php' || !file_exists($file)) {
36+
continue;
37+
}
38+
$path = str_replace(BP . '/', '', $file);
39+
$errorMessage = "Parameterized data fixture $path MUST be placed in {{ModuleAppDir}}/Test/Fixture folder";
40+
$class = $classNameExtractor->getNameWithNamespace(file_get_contents($file));
41+
if ($class) {
42+
try {
43+
$classReflection = new ReflectionClass($class);
44+
if (!$classReflection->isSubclassOf(DataFixtureInterface::class)) {
45+
continue;
46+
}
47+
} catch (ReflectionException $exception) {
48+
$errors[] = $errorMessage;
49+
continue;
50+
}
51+
52+
if (!$this->isFileLocatedInModuleDirectory($file)) {
53+
$errors[] = $errorMessage;
54+
}
55+
}
56+
}
57+
if (!empty($errors)) {
58+
$this->fail(implode(PHP_EOL, $errors));
59+
}
60+
}
61+
62+
/**
63+
* @param string $file
64+
* @return bool
65+
*/
66+
private function isFileLocatedInModuleDirectory(string $file): bool
67+
{
68+
$componentRegistrar = new ComponentRegistrar();
69+
$found = false;
70+
foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
71+
if ($file === $moduleDir . '/Test/Fixture/' . basename($file)) {
72+
$found = true;
73+
break;
74+
}
75+
}
76+
return $found;
77+
}
78+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Test\Legacy;
9+
10+
use Magento\TestFramework\Utility\AddedFiles;
11+
use PHPUnit\Framework\TestCase;
12+
13+
/**
14+
* Static test for parameterized data fixtures
15+
*/
16+
class LegacyFixtureTest extends TestCase
17+
{
18+
/**
19+
* Prevent creating new fixture files
20+
*
21+
* @return void
22+
*/
23+
public function testNew(): void
24+
{
25+
$docUrl = 'https://devdocs.magento.com/guides/v2.4/test/integration/parameterized_data_fixture.html';
26+
$files = AddedFiles::getAddedFilesList(__DIR__ . '/..');
27+
$legacyFixtureFiles = [];
28+
foreach ($files as $file) {
29+
if (pathinfo($file, PATHINFO_EXTENSION) === 'php'
30+
&& preg_match('/(Test|integration\/testsuite|api-functional\/testsuite).*\/(_files|Fixtures)/', $file)
31+
) {
32+
$legacyFixtureFiles[] = str_replace(BP . '/', '', $file);
33+
}
34+
}
35+
36+
$this->assertCount(
37+
0,
38+
$legacyFixtureFiles,
39+
"The format used for creating fixtures is deprecated. Please use parameterized fixture format.\n"
40+
. "For details please look at $docUrl.\r\n"
41+
. "The following fixture files were added:\r\n"
42+
. implode(PHP_EOL, $legacyFixtureFiles)
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)