Skip to content

Commit de25cec

Browse files
committed
MC-37307: Code generation failed for *ExtensionInterfaceFactory
1 parent 50404cc commit de25cec

File tree

7 files changed

+339
-22
lines changed

7 files changed

+339
-22
lines changed

dev/tests/integration/testsuite/Magento/Framework/Code/GeneratorTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespace.php';
1717
require_once __DIR__ . '/GeneratorTest/ParentClassWithNamespace.php';
1818
require_once __DIR__ . '/GeneratorTest/SourceClassWithNamespaceExtension.php';
19+
require_once __DIR__ . '/GeneratorTest/NestedNamespace/SourceClassWithNestedNamespace.php';
20+
require_once __DIR__ . '/GeneratorTest/NestedNamespace/SourceClassWithNestedNamespaceExtension.php';
1921

2022
/**
2123
* @magentoAppIsolation enabled
@@ -24,6 +26,7 @@
2426
class GeneratorTest extends TestCase
2527
{
2628
const CLASS_NAME_WITH_NAMESPACE = GeneratorTest\SourceClassWithNamespace::class;
29+
const CLASS_NAME_WITH_NESTED_NAMESPACE = GeneratorTest\NestedNamespace\SourceClassWithNestedNamespace::class;
2730

2831
/**
2932
* @var Generator
@@ -116,6 +119,45 @@ public function testGenerateClassFactoryWithNamespace()
116119
$this->assertEquals($expectedContent, $content);
117120
}
118121

122+
/**
123+
* Generates a new file with Factory class and compares with the sample from the
124+
* SourceClassWithNestedNamespaceFactory.php.sample file.
125+
*/
126+
public function testGenerateClassFactoryWithNestedNamespace()
127+
{
128+
$factoryClassName = self::CLASS_NAME_WITH_NESTED_NAMESPACE . 'Factory';
129+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
130+
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
131+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NESTED_NAMESPACE, $factory->create());
132+
$content = $this->_clearDocBlock(
133+
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
134+
);
135+
$expectedContent = $this->_clearDocBlock(
136+
file_get_contents(__DIR__ . '/_expected/SourceClassWithNestedNamespaceFactory.php.sample')
137+
);
138+
$this->assertEquals($expectedContent, $content);
139+
}
140+
141+
/**
142+
* Generates a new file with ExtensionInterfaceFactory class and compares with the sample from the
143+
* SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample file.
144+
*/
145+
public function testGenerateClassExtensionAttributesInterfaceFactoryWithNestedNamespace()
146+
{
147+
$factoryClassName = self::CLASS_NAME_WITH_NESTED_NAMESPACE . 'ExtensionInterfaceFactory';
148+
$this->generatedDirectory->create($this->testRelativePath);
149+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
150+
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
151+
$this->assertInstanceOf(self::CLASS_NAME_WITH_NESTED_NAMESPACE . 'Extension', $factory->create());
152+
$content = $this->_clearDocBlock(
153+
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
154+
);
155+
$expectedContent = $this->_clearDocBlock(
156+
file_get_contents(__DIR__ . '/_expected/SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample')
157+
);
158+
$this->assertEquals($expectedContent, $content);
159+
}
160+
119161
/**
120162
* Generates a new file with Proxy class and compares with the sample from the
121163
* SourceClassWithNamespaceProxy.php.sample file.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Code\GeneratorTest\NestedNamespace;
7+
8+
use Laminas\Code\Generator\ClassGenerator;
9+
10+
/**
11+
* phpcs:ignoreFile
12+
*/
13+
class SourceClassWithNestedNamespace extends \Magento\Framework\Code\GeneratorTest\ParentClassWithNamespace
14+
{
15+
/**
16+
* Public child constructor
17+
*
18+
* @param string $param1
19+
* @param string $param2
20+
* @param string $param3
21+
*
22+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
23+
*/
24+
public function __construct($param1 = '', $param2 = '\\', $param3 = '\'')
25+
{
26+
}
27+
28+
/**
29+
* Public child method
30+
*
31+
* @param \Laminas\Code\Generator\ClassGenerator $classGenerator
32+
* @param string $param1
33+
* @param string $param2
34+
* @param string $param3
35+
* @param array $array
36+
* @return void
37+
*
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
*/
40+
public function publicChildMethod(
41+
ClassGenerator $classGenerator,
42+
$param1 = '',
43+
$param2 = '\\',
44+
$param3 = '\'',
45+
array $array = []
46+
) {
47+
}
48+
49+
/**
50+
* Public child method with reference
51+
*
52+
* @param \Laminas\Code\Generator\ClassGenerator $classGenerator
53+
* @param string $param1
54+
* @param array $array
55+
*
56+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
57+
*/
58+
public function publicMethodWithReference(ClassGenerator &$classGenerator, &$param1, array &$array)
59+
{
60+
}
61+
62+
/**
63+
* Protected child method
64+
*
65+
* @param \Laminas\Code\Generator\ClassGenerator $classGenerator
66+
* @param string $param1
67+
* @param string $param2
68+
* @param string $param3
69+
*
70+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
71+
*/
72+
protected function _protectedChildMethod(
73+
ClassGenerator $classGenerator,
74+
$param1 = '',
75+
$param2 = '\\',
76+
$param3 = '\''
77+
) {
78+
}
79+
80+
/**
81+
* Private child method
82+
*
83+
* @param \Laminas\Code\Generator\ClassGenerator $classGenerator
84+
* @param string $param1
85+
* @param string $param2
86+
* @param string $param3
87+
* @param array $array
88+
* @return void
89+
*
90+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
91+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
92+
*/
93+
private function _privateChildMethod(
94+
ClassGenerator $classGenerator,
95+
$param1 = '',
96+
$param2 = '\\',
97+
$param3 = '\'',
98+
array $array = []
99+
) {
100+
}
101+
102+
/**
103+
* Test method
104+
*/
105+
public function publicChildWithoutParameters()
106+
{
107+
}
108+
109+
/**
110+
* Test method
111+
*/
112+
public static function publicChildStatic()
113+
{
114+
}
115+
116+
/**
117+
* Test method
118+
*/
119+
final public function publicChildFinal()
120+
{
121+
}
122+
123+
/**
124+
* Test method
125+
*
126+
* @param mixed $arg1
127+
* @param string $arg2
128+
* @param int|null $arg3
129+
* @param int|null $arg4
130+
*
131+
* @return void
132+
*
133+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
134+
*/
135+
public function public71(
136+
$arg1,
137+
string $arg2,
138+
?int $arg3,
139+
?int $arg4 = null
140+
): void {
141+
}
142+
143+
/**
144+
* Test method
145+
*
146+
* @param \DateTime|null $arg1
147+
* @param mixed $arg2
148+
*
149+
* @return null|string
150+
*
151+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
152+
*/
153+
public function public71Another(?\DateTime $arg1, $arg2 = false): ?string
154+
{
155+
// phpstan:ignore
156+
}
157+
158+
/**
159+
* Test method
160+
*
161+
* @param bool $arg
162+
* @return SourceClassWithNamespace
163+
*
164+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
165+
*/
166+
public function publicWithSelf($arg = false): self
167+
{
168+
// phpstan:ignore
169+
}
170+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Code\GeneratorTest\NestedNamespace;
8+
9+
/**
10+
* Source class for ExtensionInterfaceFactory generator.
11+
*/
12+
class SourceClassWithNestedNamespaceExtension extends \Magento\Framework\Code\GeneratorTest\ParentClassWithNamespace
13+
{
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Magento\Framework\Code\GeneratorTest\NestedNamespace;
3+
4+
/**
5+
* Factory class for @see \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespaceExtension
6+
*
7+
* Copyright © Magento, Inc. All rights reserved.
8+
* See COPYING.txt for license details.
9+
*/
10+
class SourceClassWithNestedNamespaceExtensionInterfaceFactory
11+
{
12+
/**
13+
* Object Manager instance
14+
*
15+
* @var \Magento\Framework\ObjectManagerInterface
16+
*/
17+
protected $_objectManager = null;
18+
19+
/**
20+
* Instance name to create
21+
*
22+
* @var string
23+
*/
24+
protected $_instanceName = null;
25+
26+
/**
27+
* ExtensionInterfaceFactory constructor
28+
*
29+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
30+
* @param string $instanceName
31+
*/
32+
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Framework\\Code\\GeneratorTest\\NestedNamespace\\SourceClassWithNestedNamespaceExtension')
33+
{
34+
$this->_objectManager = $objectManager;
35+
$this->_instanceName = $instanceName;
36+
}
37+
38+
/**
39+
* Create class instance with specified parameters
40+
*
41+
* @param array $data
42+
* @return \Magento\Framework\Code\GeneratorTest\NestedNamespace\SourceClassWithNestedNamespaceExtension
43+
*/
44+
public function create(array $data = [])
45+
{
46+
return $this->_objectManager->create($this->_instanceName, $data);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Magento\Framework\Code\GeneratorTest\NestedNamespace;
3+
4+
/**
5+
* Factory class for @see \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
6+
*
7+
* Copyright © Magento, Inc. All rights reserved.
8+
* See COPYING.txt for license details.
9+
*/
10+
class SourceClassWithNestedNamespaceFactory
11+
{
12+
/**
13+
* Object Manager instance
14+
*
15+
* @var \Magento\Framework\ObjectManagerInterface
16+
*/
17+
protected $_objectManager = null;
18+
19+
/**
20+
* Instance name to create
21+
*
22+
* @var string
23+
*/
24+
protected $_instanceName = null;
25+
26+
/**
27+
* Factory constructor
28+
*
29+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
30+
* @param string $instanceName
31+
*/
32+
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Framework\\Code\\GeneratorTest\\NestedNamespace\\SourceClassWithNestedNamespace')
33+
{
34+
$this->_objectManager = $objectManager;
35+
$this->_instanceName = $instanceName;
36+
}
37+
38+
/**
39+
* Create class instance with specified parameters
40+
*
41+
* @param array $data
42+
* @return \Magento\Framework\Code\GeneratorTest\NestedNamespace\SourceClassWithNestedNamespace
43+
*/
44+
public function create(array $data = [])
45+
{
46+
return $this->_objectManager->create($this->_instanceName, $data);
47+
}
48+
}

lib/internal/Magento/Framework/Api/Code/Generator/ExtensionAttributesInterfaceFactoryGenerator.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
namespace Magento\Framework\Api\Code\Generator;
88

9-
use Magento\Framework\ObjectManager\Code\Generator\Factory;
9+
use Magento\Framework\Code\Generator\CodeGeneratorInterface;
1010
use Magento\Framework\Code\Generator\DefinedClasses;
1111
use Magento\Framework\Code\Generator\Io;
12-
use Magento\Framework\Code\Generator\CodeGeneratorInterface;
12+
use Magento\Framework\ObjectManager\Code\Generator\Factory;
1313

1414
class ExtensionAttributesInterfaceFactoryGenerator extends Factory
1515
{
@@ -18,11 +18,6 @@ class ExtensionAttributesInterfaceFactoryGenerator extends Factory
1818
*/
1919
const ENTITY_TYPE = 'extensionInterfaceFactory';
2020

21-
/**
22-
* @var string
23-
*/
24-
private static $suffix = 'InterfaceFactory';
25-
2621
/**
2722
* Initialize dependencies.
2823
*
@@ -52,19 +47,8 @@ public function __construct(
5247
/**
5348
* {@inheritdoc}
5449
*/
55-
protected function _validateData()
50+
protected function getResultClassSuffix()
5651
{
57-
$result = true;
58-
$sourceClassName = $this->getSourceClassName();
59-
$resultClassName = $this->_getResultClassName();
60-
61-
if ($resultClassName !== $sourceClassName . self::$suffix) {
62-
$this->_addError(
63-
'Invalid Factory class name [' . $resultClassName . ']. Use ' . $sourceClassName . self::$suffix
64-
);
65-
$result = false;
66-
}
67-
68-
return $result;
52+
return 'InterfaceFactory';
6953
}
7054
}

0 commit comments

Comments
 (0)