|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 |
| - |
7 |
| -// @codingStandardsIgnoreFile |
8 |
| - |
9 | 6 | namespace Magento\Framework\Interception\Test\Unit\Code\Generator;
|
10 | 7 |
|
| 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 | + |
11 | 13 | class InterceptorTest extends \PHPUnit_Framework_TestCase
|
12 | 14 | {
|
13 | 15 | /**
|
14 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 16 | + * @var Io|MockObject |
15 | 17 | */
|
16 |
| - protected $ioObjectMock; |
| 18 | + private $ioGenerator; |
17 | 19 |
|
18 | 20 | /**
|
19 |
| - * @var \PHPUnit_Framework_MockObject_MockObject |
| 21 | + * @inheritdoc |
20 | 22 | */
|
21 |
| - protected $classGeneratorMock; |
22 |
| - |
23 | 23 | protected function setUp()
|
24 | 24 | {
|
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' |
32 | 33 | );
|
| 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.'); |
33 | 71 | }
|
34 | 72 |
|
35 |
| - public function testGetDefaultResultClassName() |
| 73 | + /** |
| 74 | + * Gets list of interceptor samples. |
| 75 | + * |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + public function interceptorDataProvider() |
36 | 79 | {
|
37 |
| - // resultClassName should be stdClass_Interceptor |
38 |
| - $model = $this->getMock( |
39 |
| - \Magento\Framework\Interception\Code\Generator\Interceptor::class, |
40 |
| - ['_validateData'], |
| 80 | + return [ |
41 | 81 | [
|
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' |
46 | 85 | ]
|
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 | + ]; |
62 | 87 | }
|
63 | 88 | }
|
0 commit comments