Skip to content

Commit de131cb

Browse files
committed
Added test for #35572
1 parent c3d9fc2 commit de131cb

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Code/Generator/ProxyTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class ProxyTest extends TestCase
1717
{
18+
private const MIXED_TYPE_PHP_VERSION = '8.0.0';
19+
1820
/**
1921
* @var MockObject
2022
*/
@@ -52,4 +54,36 @@ public function testGenerate()
5254
$model->expects($this->once())->method('_validateData')->willReturn(true);
5355
$this->assertEquals('sample_file.php', $model->generate());
5456
}
57+
58+
public function testGenerateMixedType()
59+
{
60+
if (version_compare(PHP_VERSION, self::MIXED_TYPE_PHP_VERSION) < 0) {
61+
$this->markTestSkipped('This test requires at least PHP version ' . self::MIXED_TYPE_PHP_VERSION);
62+
}
63+
64+
require_once __DIR__ . '/_files/SampleMixed.php';
65+
$model = $this->getMockBuilder(Proxy::class)
66+
->setMethods(['_validateData'])
67+
->setConstructorArgs(
68+
[
69+
\Magento\Framework\ObjectManager\Code\Generator\SampleMixed::class,
70+
null,
71+
$this->ioObjectMock,
72+
null,
73+
null,
74+
$this->createMock(FileResolver::class)
75+
]
76+
)
77+
->getMock();
78+
$sampleMixedProxyCode = file_get_contents(__DIR__ . '/_files/SampleMixedProxy.txt');
79+
80+
$this->ioObjectMock->expects($this->once())->method('generateResultFileName')
81+
->with('\\' . \Magento\Framework\ObjectManager\Code\Generator\SampleMixed_Proxy::class)
82+
->willReturn('sample_mixed_file.php');
83+
$this->ioObjectMock->expects($this->once())->method('writeResultFile')
84+
->with('sample_mixed_file.php', $sampleMixedProxyCode);
85+
86+
$model->expects($this->once())->method('_validateData')->willReturn(true);
87+
$this->assertEquals('sample_mixed_file.php', $model->generate());
88+
}
5589
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Framework\ObjectManager\Code\Generator;
9+
10+
/**
11+
* Class SampleMixed for Proxy and Factory generation with mixed type
12+
*/
13+
class SampleMixed
14+
{
15+
/**
16+
* @var mixed
17+
*/
18+
protected mixed $mixed = null;
19+
20+
/**
21+
* @param mixed $mixed
22+
* @return void
23+
*/
24+
public function setMixed(mixed $mixed = null): void
25+
{
26+
$this->mixed = $mixed;
27+
}
28+
29+
/**
30+
* @return mixed
31+
*/
32+
public function getMixed(): mixed
33+
{
34+
return $this->mixed;
35+
}
36+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
namespace Magento\Framework\ObjectManager\Code\Generator;
2+
3+
/**
4+
* Proxy class for @see \Magento\Framework\ObjectManager\Code\Generator\SampleMixed
5+
*/
6+
class SampleMixed_Proxy extends SampleMixed implements \Magento\Framework\ObjectManager\NoninterceptableInterface
7+
{
8+
/**
9+
* Object Manager instance
10+
*
11+
* @var \Magento\Framework\ObjectManagerInterface
12+
*/
13+
protected $_objectManager = null;
14+
15+
/**
16+
* Proxied instance name
17+
*
18+
* @var string
19+
*/
20+
protected $_instanceName = null;
21+
22+
/**
23+
* Proxied instance
24+
*
25+
* @var \Magento\Framework\ObjectManager\Code\Generator\SampleMixed
26+
*/
27+
protected $_subject = null;
28+
29+
/**
30+
* Instance shareability flag
31+
*
32+
* @var bool
33+
*/
34+
protected $_isShared = null;
35+
36+
/**
37+
* Proxy constructor
38+
*
39+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
40+
* @param string $instanceName
41+
* @param bool $shared
42+
*/
43+
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Framework\\ObjectManager\\Code\\Generator\\SampleMixed', $shared = true)
44+
{
45+
$this->_objectManager = $objectManager;
46+
$this->_instanceName = $instanceName;
47+
$this->_isShared = $shared;
48+
}
49+
50+
/**
51+
* @return array
52+
*/
53+
public function __sleep()
54+
{
55+
return ['_subject', '_isShared', '_instanceName'];
56+
}
57+
58+
/**
59+
* Retrieve ObjectManager from global scope
60+
*/
61+
public function __wakeup()
62+
{
63+
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
64+
}
65+
66+
/**
67+
* Clone proxied instance
68+
*/
69+
public function __clone()
70+
{
71+
$this->_subject = clone $this->_getSubject();
72+
}
73+
74+
/**
75+
* Get proxied instance
76+
*
77+
* @return \Magento\Framework\ObjectManager\Code\Generator\SampleMixed
78+
*/
79+
protected function _getSubject()
80+
{
81+
if (!$this->_subject) {
82+
$this->_subject = true === $this->_isShared
83+
? $this->_objectManager->get($this->_instanceName)
84+
: $this->_objectManager->create($this->_instanceName);
85+
}
86+
return $this->_subject;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function setMixed(mixed $mixed = null) : void
93+
{
94+
$this->_getSubject()->setMixed($mixed);
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function getMixed() : mixed
101+
{
102+
return $this->_getSubject()->getMixed();
103+
}
104+
}

0 commit comments

Comments
 (0)