Skip to content

Commit ab0c300

Browse files
author
Dmytro Yushkin
committed
MAGETWO-67260: [Github] Incompatible generated interceptor for methods which return references #9167
1 parent 45dce69 commit ab0c300

File tree

4 files changed

+124
-40
lines changed

4 files changed

+124
-40
lines changed

lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function _getMethodInfo(\ReflectionMethod $method)
110110
}
111111

112112
$methodInfo = [
113-
'name' => $method->getName(),
113+
'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(),
114114
'parameters' => $parameters,
115115
'body' => "\$pluginInfo = \$this->pluginList->getNext(\$this->subjectType, '{$method->getName()}');\n" .
116116
"if (!\$pluginInfo) {\n" .

lib/internal/Magento/Framework/Interception/Test/Unit/Code/Generator/InterceptorTest.php

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,86 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
// @codingStandardsIgnoreFile
8-
96
namespace Magento\Framework\Interception\Test\Unit\Code\Generator;
107

8+
use Composer\Autoload\ClassLoader;
9+
use Magento\Framework\Code\Generator\Io;
10+
use Magento\Framework\Interception\Code\Generator\Interceptor;
11+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
12+
1113
class InterceptorTest extends \PHPUnit_Framework_TestCase
1214
{
1315
/**
14-
* @var \PHPUnit_Framework_MockObject_MockObject
16+
* @var Io|MockObject
1517
*/
16-
protected $ioObjectMock;
18+
private $ioGenerator;
1719

1820
/**
19-
* @var \PHPUnit_Framework_MockObject_MockObject
21+
* @inheritdoc
2022
*/
21-
protected $classGeneratorMock;
22-
2323
protected function setUp()
2424
{
25-
$this->ioObjectMock = $this->getMock(\Magento\Framework\Code\Generator\Io::class, [], [], '', false);
26-
$this->classGeneratorMock = $this->getMock(
27-
\Magento\Framework\Code\Generator\CodeGeneratorInterface::class,
28-
[],
29-
[],
30-
'',
31-
false
25+
$this->ioGenerator = $this->getMockBuilder(Io::class)
26+
->disableOriginalConstructor()
27+
->getMock();
28+
29+
$loader = new ClassLoader();
30+
$loader->addPsr4(
31+
'Magento\\Framework\\Interception\\Code\\Generator\\',
32+
__DIR__ . '/_files'
3233
);
34+
$loader->register();
35+
}
36+
37+
/**
38+
* Checks a test case when interceptor generates code for the specified class.
39+
*
40+
* @param string $className
41+
* @param string $resultClassName
42+
* @param string $fileName
43+
* @dataProvider interceptorDataProvider
44+
*/
45+
public function testGenerate($className, $resultClassName, $fileName)
46+
{
47+
/** @var Interceptor|MockObject $interceptor */
48+
$interceptor = $this->getMockBuilder(Interceptor::class)
49+
->setMethods(['_validateData'])
50+
->setConstructorArgs([
51+
$className,
52+
$resultClassName,
53+
$this->ioGenerator,
54+
])
55+
->getMock();
56+
57+
$this->ioGenerator
58+
->method('generateResultFileName')
59+
->with('\\' . $resultClassName)
60+
->willReturn($fileName . '.php');
61+
62+
$code = file_get_contents(__DIR__ . '/_files/' . $fileName . '.txt');
63+
$this->ioGenerator->method('writeResultFile')
64+
->with($fileName . '.php', $code);
65+
66+
$interceptor->method('_validateData')
67+
->willReturn(true);
68+
69+
$generated = $interceptor->generate();
70+
$this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.');
3371
}
3472

35-
public function testGetDefaultResultClassName()
73+
/**
74+
* Gets list of interceptor samples.
75+
*
76+
* @return array
77+
*/
78+
public function interceptorDataProvider()
3679
{
37-
// resultClassName should be stdClass_Interceptor
38-
$model = $this->getMock(
39-
\Magento\Framework\Interception\Code\Generator\Interceptor::class,
40-
['_validateData'],
80+
return [
4181
[
42-
'Exception',
43-
null,
44-
$this->ioObjectMock,
45-
$this->classGeneratorMock,
82+
\Magento\Framework\Interception\Code\Generator\Sample::class,
83+
\Magento\Framework\Interception\Code\Generator\Sample\Interceptor::class,
84+
'Interceptor'
4685
]
47-
);
48-
49-
$this->classGeneratorMock->expects($this->once())->method('setName')
50-
->willReturnSelf();
51-
$this->classGeneratorMock->expects($this->once())->method('addProperties')
52-
->willReturnSelf();
53-
$this->classGeneratorMock->expects($this->once())->method('addMethods')
54-
->willReturnSelf();
55-
$this->classGeneratorMock->expects($this->once())->method('setClassDocBlock')
56-
->willReturnSelf();
57-
$this->classGeneratorMock->expects($this->once())->method('generate')
58-
->will($this->returnValue('source code example'));
59-
$model->expects($this->once())->method('_validateData')->will($this->returnValue(true));
60-
$this->ioObjectMock->expects($this->any())->method('generateResultFileName')->with('Exception_Interceptor');
61-
$this->assertEquals('', $model->generate());
86+
];
6287
}
6388
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Magento\Framework\Interception\Code\Generator\Sample;
2+
3+
/**
4+
* Interceptor class for @see \Magento\Framework\Interception\Code\Generator\Sample
5+
*/
6+
class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample implements \Magento\Framework\Interception\InterceptorInterface
7+
{
8+
use \Magento\Framework\Interception\Interceptor;
9+
10+
public function __construct()
11+
{
12+
$this->___init();
13+
}
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getValue()
19+
{
20+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getValue');
21+
if (!$pluginInfo) {
22+
return parent::getValue();
23+
} else {
24+
return $this->___callPlugins('getValue', func_get_args(), $pluginInfo);
25+
}
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function & getReference()
32+
{
33+
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getReference');
34+
if (!$pluginInfo) {
35+
return parent::getReference();
36+
} else {
37+
return $this->___callPlugins('getReference', func_get_args(), $pluginInfo);
38+
}
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Interception\Code\Generator;
7+
8+
class Sample
9+
{
10+
public function getValue()
11+
{
12+
13+
}
14+
15+
public function & getReference()
16+
{
17+
18+
}
19+
}

0 commit comments

Comments
 (0)