Skip to content

Commit 350d8c7

Browse files
committed
Merge remote-tracking branch 'origin/MC-37307' into 2.4-develop-sidecar-pr2
2 parents 204510d + f39fbce commit 350d8c7

File tree

7 files changed

+369
-70
lines changed

7 files changed

+369
-70
lines changed

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

Lines changed: 70 additions & 43 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,10 @@
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;
30+
const EXTENSION_CLASS_NAME_WITH_NAMESPACE = GeneratorTest\SourceClassWithNamespaceExtension::class;
31+
const EXTENSION_CLASS_NAME_WITH_NESTED_NAMESPACE =
32+
GeneratorTest\NestedNamespace\SourceClassWithNestedNamespaceExtension::class;
2733

2834
/**
2935
* @var Generator
@@ -59,6 +65,7 @@ protected function setUp(): void
5965
/** @var Filesystem $filesystem */
6066
$filesystem = $objectManager->get(Filesystem::class);
6167
$this->generatedDirectory = $filesystem->getDirectoryWrite(DirectoryList::GENERATED_CODE);
68+
$this->generatedDirectory->create($this->testRelativePath);
6269
$this->logDirectory = $filesystem->getDirectoryRead(DirectoryList::LOG);
6370
$generatedDirectoryAbsolutePath = $this->generatedDirectory->getAbsolutePath();
6471
$this->_ioObject = new Generator\Io(new Filesystem\Driver\File(), $generatedDirectoryAbsolutePath);
@@ -98,78 +105,99 @@ protected function _clearDocBlock($classBody)
98105
}
99106

100107
/**
101-
* Generates a new file with Factory class and compares with the sample from the
102-
* SourceClassWithNamespaceFactory.php.sample file.
108+
* Generates a new class Factory file and compares with the sample.
109+
*
110+
* @param $className
111+
* @param $generateType
112+
* @param $expectedDataPath
113+
* @dataProvider generateClassFactoryDataProvider
103114
*/
104-
public function testGenerateClassFactoryWithNamespace()
115+
public function testGenerateClassFactory($className, $generateType, $expectedDataPath)
105116
{
106-
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'Factory';
117+
$factoryClassName = $className . $generateType;
107118
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
108119
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
109-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $factory->create());
120+
$this->assertInstanceOf($className, $factory->create());
110121
$content = $this->_clearDocBlock(
111122
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
112123
);
113124
$expectedContent = $this->_clearDocBlock(
114-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceFactory.php.sample')
125+
file_get_contents(__DIR__ . $expectedDataPath)
115126
);
116127
$this->assertEquals($expectedContent, $content);
117128
}
118129

119130
/**
120-
* Generates a new file with Proxy class and compares with the sample from the
121-
* SourceClassWithNamespaceProxy.php.sample file.
131+
* DataProvider for testGenerateClassFactory
132+
*
133+
* @return array
122134
*/
123-
public function testGenerateClassProxyWithNamespace()
135+
public function generateClassFactoryDataProvider()
124136
{
125-
$proxyClassName = self::CLASS_NAME_WITH_NAMESPACE . '\Proxy';
126-
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($proxyClassName));
127-
$proxy = Bootstrap::getObjectManager()->create($proxyClassName);
128-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE, $proxy);
129-
$content = $this->_clearDocBlock(
130-
file_get_contents($this->_ioObject->generateResultFileName($proxyClassName))
131-
);
132-
$expectedContent = $this->_clearDocBlock(
133-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceProxy.php.sample')
134-
);
135-
$this->assertEquals($expectedContent, $content);
137+
return [
138+
'factory_with_namespace' => [
139+
'className' => self::CLASS_NAME_WITH_NAMESPACE,
140+
'generateType' => 'Factory',
141+
'expectedDataPath' => '/_expected/SourceClassWithNamespaceFactory.php.sample'
142+
],
143+
'factory_with_nested_namespace' => [
144+
'classToGenerate' => self::CLASS_NAME_WITH_NESTED_NAMESPACE,
145+
'generateType' => 'Factory',
146+
'expectedDataPath' => '/_expected/SourceClassWithNestedNamespaceFactory.php.sample'
147+
],
148+
'ext_interface_factory_with_namespace' => [
149+
'classToGenerate' => self::EXTENSION_CLASS_NAME_WITH_NAMESPACE,
150+
'generateType' => 'InterfaceFactory',
151+
'expectedDataPath' => '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample'
152+
],
153+
'ext_interface_factory_with_nested_namespace' => [
154+
'classToGenerate' => self::EXTENSION_CLASS_NAME_WITH_NESTED_NAMESPACE,
155+
'generateType' => 'InterfaceFactory',
156+
'expectedDataPath' => '/_expected/SourceClassWithNestedNamespaceExtensionInterfaceFactory.php.sample'
157+
],
158+
];
136159
}
137160

138161
/**
139-
* Generates a new file with Interceptor class and compares with the sample from the
140-
* SourceClassWithNamespaceInterceptor.php.sample file.
162+
* @param $className
163+
* @param $generateType
164+
* @param $expectedDataPath
165+
* @dataProvider generateClassDataProvider
141166
*/
142-
public function testGenerateClassInterceptorWithNamespace()
167+
public function testGenerateClass($className, $generateType, $expectedDataPath)
143168
{
144-
$interceptorClassName = self::CLASS_NAME_WITH_NAMESPACE . '\Interceptor';
145-
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($interceptorClassName));
169+
$generateClassName = $className . $generateType;
170+
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($generateClassName));
171+
$instance = Bootstrap::getObjectManager()->create($generateClassName);
172+
$this->assertInstanceOf($className, $instance);
146173
$content = $this->_clearDocBlock(
147-
file_get_contents($this->_ioObject->generateResultFileName($interceptorClassName))
174+
file_get_contents($this->_ioObject->generateResultFileName($generateClassName))
148175
);
149176
$expectedContent = $this->_clearDocBlock(
150-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceInterceptor.php.sample')
177+
file_get_contents(__DIR__ . $expectedDataPath)
151178
);
152179
$this->assertEquals($expectedContent, $content);
153180
}
154181

155182
/**
156-
* Generates a new file with ExtensionInterfaceFactory class and compares with the sample from the
157-
* SourceClassWithNamespaceExtensionInterfaceFactory.php.sample file.
183+
* DataProvider for testGenerateClass
184+
*
185+
* @return array
158186
*/
159-
public function testGenerateClassExtensionAttributesInterfaceFactoryWithNamespace()
187+
public function generateClassDataProvider()
160188
{
161-
$factoryClassName = self::CLASS_NAME_WITH_NAMESPACE . 'ExtensionInterfaceFactory';
162-
$this->generatedDirectory->create($this->testRelativePath);
163-
$this->assertEquals(Generator::GENERATION_SUCCESS, $this->_generator->generateClass($factoryClassName));
164-
$factory = Bootstrap::getObjectManager()->create($factoryClassName);
165-
$this->assertInstanceOf(self::CLASS_NAME_WITH_NAMESPACE . 'Extension', $factory->create());
166-
$content = $this->_clearDocBlock(
167-
file_get_contents($this->_ioObject->generateResultFileName($factoryClassName))
168-
);
169-
$expectedContent = $this->_clearDocBlock(
170-
file_get_contents(__DIR__ . '/_expected/SourceClassWithNamespaceExtensionInterfaceFactory.php.sample')
171-
);
172-
$this->assertEquals($expectedContent, $content);
189+
return [
190+
'proxy' => [
191+
'className' => self::CLASS_NAME_WITH_NAMESPACE,
192+
'generateType' => '\Proxy',
193+
'expectedDataPath' => '/_expected/SourceClassWithNamespaceProxy.php.sample'
194+
],
195+
'interceptor' => [
196+
'className' => self::CLASS_NAME_WITH_NAMESPACE,
197+
'generateType' => '\Interceptor',
198+
'expectedDataPath' => '/_expected/SourceClassWithNamespaceInterceptor.php.sample'
199+
]
200+
];
173201
}
174202

175203
/**
@@ -183,7 +211,6 @@ public function testGeneratorClassWithErrorSaveClassFile()
183211
$regexpMsgPart = preg_quote($msgPart);
184212
$this->expectException(\RuntimeException::class);
185213
$this->expectExceptionMessageMatches("/.*$regexpMsgPart.*/");
186-
$this->generatedDirectory->create($this->testRelativePath);
187214
$this->generatedDirectory->changePermissionsRecursively($this->testRelativePath, 0555, 0444);
188215
$generatorResult = $this->_generator->generateClass($factoryClassName);
189216
$this->assertFalse($generatorResult);
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+
}

0 commit comments

Comments
 (0)