Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EntityChildTestAbstract.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\Code\Generator\Io;
10 
14 abstract class EntityChildTestAbstract extends \PHPUnit\Framework\TestCase
15 {
19  protected $ioObjectMock;
20 
24  protected $generator;
25 
29  protected $classGenerator;
30 
33 
37  abstract protected function getSourceClassName();
38 
42  abstract protected function getResultClassName();
43 
47  abstract protected function getGeneratorClassName();
48 
52  abstract protected function getOutputFileName();
53 
54  protected function setUp()
55  {
56  require_once __DIR__ . '/Sample.php';
57 
58  $this->ioObjectMock = $this->createMock(\Magento\Framework\Code\Generator\Io::class);
59  $this->classGenerator = $this->createMock(\Magento\Framework\Code\Generator\ClassGenerator::class);
60  $this->definedClassesMock = $this->getMockBuilder(\Magento\Framework\Code\Generator\DefinedClasses::class)
61  ->disableOriginalConstructor()->getMock();
62 
63  $objectManager = new ObjectManager($this);
64  $this->generator = $objectManager->getObject(
65  $this->getGeneratorClassName(),
66  [
67  'sourceClassName' => $this->getSourceClassName(),
68  'resultClassName' => $this->getResultClassName(),
69  'ioObject' => $this->ioObjectMock,
70  'classGenerator' => $this->classGenerator,
71  'definedClasses' => $this->definedClassesMock,
72  ]
73  );
74  }
75 
79  public function testGenerate()
80  {
81  $generatedCode = 'Generated code';
82  $resultFileName = $this->getOutputFileName();
83 
84  //Mocking _validateData call
85  $this->mockDefinedClassesCall();
86 
87  $this->ioObjectMock->expects($this->once())
88  ->method('makeResultFileDirectory')
89  ->with($this->getResultClassName())
90  ->willReturn(true);
91 
92  //Mocking _generateCode call
93  $this->classGenerator->expects($this->once())
94  ->method('setName')
95  ->with($this->getResultClassName())
96  ->willReturnSelf();
97  $this->classGenerator->expects($this->once())
98  ->method('addProperties')
99  ->willReturnSelf();
100  $this->classGenerator->expects($this->once())
101  ->method('addMethods')
102  ->willReturnSelf();
103  $this->classGenerator->expects($this->once())
104  ->method('setClassDocBlock')
105  ->willReturnSelf();
106  $this->classGenerator->expects($this->once())
107  ->method('generate')
108  ->willReturn($generatedCode);
109 
110  //Mocking generation
111  $this->ioObjectMock->expects($this->any())
112  ->method('generateResultFileName')
113  ->with($this->getResultClassName())
114  ->willReturn($resultFileName);
115  $this->ioObjectMock->expects($this->once())
116  ->method('writeResultFile')
117  ->with($resultFileName, $generatedCode);
118 
119  $this->assertEquals(
120  $resultFileName,
121  $this->generator->generate(),
122  implode("\n", $this->generator->getErrors())
123  );
124  }
125 
126  protected function mockDefinedClassesCall()
127  {
128  $this->definedClassesMock->expects($this->at(0))
129  ->method('isClassLoadable')
130  ->with($this->getSourceClassName())
131  ->willReturn(true);
132  }
133 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$generatedCode
Definition: autoload.php:24