private function entityFactoryClosure() { return function ($sourceClass, $originalClass) { $generator = $this->entityGenerator->getWrappedObject(); $generator->init($sourceClass, $originalClass); return $generator; }; }
/** * generate repository name */ public function testGenerate() { $generatedCode = 'Generated code'; $resultFileName = $this->getOutputFileName(); //Mocking _validateData call $this->mockDefinedClassesCall(); $this->ioObjectMock->expects($this->once())->method('makeResultFileDirectory')->with($this->getResultClassName())->willReturn(true); //Mocking _generateCode call $this->classGenerator->expects($this->once())->method('setName')->with($this->getResultClassName())->willReturnSelf(); $this->classGenerator->expects($this->once())->method('addProperties')->willReturnSelf(); $this->classGenerator->expects($this->once())->method('addMethods')->willReturnSelf(); $this->classGenerator->expects($this->once())->method('setClassDocBlock')->willReturnSelf(); $this->classGenerator->expects($this->once())->method('generate')->willReturn($generatedCode); //Mocking generation $this->ioObjectMock->expects($this->any())->method('getResultFileName')->with($this->getResultClassName())->willReturn($resultFileName); $this->ioObjectMock->expects($this->once())->method('writeResultFile')->with($resultFileName, $generatedCode); $this->assertEquals($resultFileName, $this->generator->generate(), implode("\n", $this->generator->getErrors())); }
public function testGenerate() { $generatedCode = 'Generated code'; $resultFileName = 'SampleConverter.php'; //Mocking _validateData call $this->definedClassesMock->expects($this->at(0))->method('isClassLoadable')->will($this->returnValue(true)); $this->ioObjectMock->expects($this->once())->method('makeResultFileDirectory')->with(self::RESULT_CLASS_NAME)->will($this->returnValue(true)); //Mocking _generateCode call $this->classGenerator->expects($this->once())->method('setName')->with(self::RESULT_CLASS_NAME)->willReturnSelf(); $this->classGenerator->expects($this->once())->method('addProperties')->willReturnSelf(); $this->classGenerator->expects($this->once())->method('addMethods')->willReturnSelf(); $this->classGenerator->expects($this->once())->method('setClassDocBlock')->willReturnSelf(); $this->classGenerator->expects($this->once())->method('generate')->will($this->returnValue($generatedCode)); //Mocking generation $this->ioObjectMock->expects($this->any())->method('generateResultFileName')->with(self::RESULT_CLASS_NAME)->will($this->returnValue($resultFileName)); $this->ioObjectMock->expects($this->once())->method('writeResultFile')->with($resultFileName, $generatedCode); $this->assertEquals($resultFileName, $this->generator->generate()); }
/** * generate repository name */ public function testGenerate() { $generatedCode = 'Generated code'; $sourceFileName = 'Sample.php'; $resultFileName = self::OUTPUT_FILE_NAME; //Mocking _validateData call $this->autoloaderMock->expects($this->at(0))->method('getFile')->with(self::SOURCE_CLASS_NAME)->will($this->returnValue($sourceFileName)); $this->autoloaderMock->expects($this->at(1))->method('getFile')->with(self::RESULT_CLASS_NAME)->will($this->returnValue(false)); $this->ioObjectMock->expects($this->once())->method('makeGenerationDirectory')->will($this->returnValue(true)); $this->ioObjectMock->expects($this->once())->method('makeResultFileDirectory')->with(self::RESULT_CLASS_NAME)->will($this->returnValue(true)); $this->ioObjectMock->expects($this->once())->method('fileExists')->with($resultFileName)->will($this->returnValue(false)); //Mocking _generateCode call $this->classGenerator->expects($this->once())->method('setName')->with(self::RESULT_CLASS_NAME)->will($this->returnSelf()); $this->classGenerator->expects($this->once())->method('setExtendedClass')->with(SearchResults::SEARCH_RESULT)->will($this->returnSelf()); $this->classGenerator->expects($this->once())->method('addMethods')->will($this->returnSelf()); $this->classGenerator->expects($this->once())->method('generate')->will($this->returnValue($generatedCode)); //Mocking generation $this->ioObjectMock->expects($this->any())->method('getResultFileName')->with(self::RESULT_CLASS_NAME)->will($this->returnValue($resultFileName)); $this->ioObjectMock->expects($this->once())->method('writeResultFile')->with($resultFileName, $generatedCode); $this->assertTrue($this->generator->generate()); }
/** * {@inheritdoc} */ protected function _validateData() { $result = parent::_validateData(); if ($result) { $sourceClassName = $this->getSourceClassName(); $resultClassName = $this->_getResultClassName(); if ($resultClassName !== $sourceClassName . 'Mapper') { $this->_addError('Invalid Mapper class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Mapper'); $result = false; } } return $result; }
/** * {@inheritdoc} */ protected function _validateData() { if (!parent::_validateData()) { return false; } $sourceClassName = $this->getSourceClassName(); $resultClassName = $this->_getResultClassName(); if ($resultClassName !== $sourceClassName . 'Converter') { $this->_addError('Invalid Converter class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Converter'); return false; } return true; }
/** * @param array $errors * @param bool $isGeneration * @param bool $classExistsFirst * @param bool $classExistsSecond * @param bool $makeGeneration * @param bool $makeResultFile * @param bool $fileExists * @param bool $isValid * * @dataProvider generateDataProvider * @covers \Magento\Framework\Code\Generator\EntityAbstract::generate * @covers \Magento\Framework\Code\Generator\EntityAbstract::getErrors * @covers \Magento\Framework\Code\Generator\EntityAbstract::_getSourceClassName * @covers \Magento\Framework\Code\Generator\EntityAbstract::_getResultClassName * @covers \Magento\Framework\Code\Generator\EntityAbstract::_getDefaultResultClassName * @covers \Magento\Framework\Code\Generator\EntityAbstract::_generateCode * @covers \Magento\Framework\Code\Generator\EntityAbstract::_addError * @covers \Magento\Framework\Code\Generator\EntityAbstract::_validateData * @covers \Magento\Framework\Code\Generator\EntityAbstract::_getClassDocBlock * @covers \Magento\Framework\Code\Generator\EntityAbstract::_getGeneratedCode * @covers \Magento\Framework\Code\Generator\EntityAbstract::_fixCodeStyle */ public function testGenerate($errors = array(), $isGeneration = true, $classExistsFirst = true, $classExistsSecond = false, $makeGeneration = true, $makeResultFile = true, $fileExists = false, $isValid = true) { if ($isGeneration) { $arguments = $this->_prepareMocksForGenerateCode($isValid); } else { $arguments = $this->_prepareMocksForValidateData($classExistsFirst, $classExistsSecond, $makeGeneration, $makeResultFile, $fileExists); } $abstractGetters = array('_getClassProperties', '_getClassMethods'); $this->_model = $this->getMockForAbstractClass('Magento\\Framework\\Code\\Generator\\EntityAbstract', $arguments, '', true, true, true, $abstractGetters); // we need to mock abstract methods to set correct return value type foreach ($abstractGetters as $methodName) { $this->_model->expects($this->any())->method($methodName)->will($this->returnValue(array())); } $result = $this->_model->generate(); if ($errors) { $this->assertFalse($result); $this->assertEquals($errors, $this->_model->getErrors()); } else { $this->assertTrue($result); $this->assertEmpty($this->_model->getErrors()); } }
/** * {@inheritdoc} */ protected function _generateCode() { $this->_classGenerator->setImplementedInterfaces([$this->_getResultClassName() . 'Interface']); $this->_classGenerator->setExtendedClass($this->getExtendedClass()); return parent::_generateCode(); }
/** * Try to load/generate source class to check if it is valid or not. * * @param string $className * @param \Magento\Framework\Code\Generator\EntityAbstract $generator * @return void * @throws \RuntimeException */ protected function tryToLoadSourceClass($className, $generator) { $sourceClassName = $generator->getSourceClassName(); if (!$this->definedClasses->isClassLoadable($sourceClassName)) { if ($this->generateClass($sourceClassName) !== self::GENERATION_SUCCESS) { $phrase = new \Magento\Framework\Phrase('Source class "%1" for "%2" generation does not exist.', [$sourceClassName, $className]); throw new \RuntimeException($phrase->__toString()); } } }
/** * Try to load/generate source class to check if it is valid or not. * * @param string $className * @param \Magento\Framework\Code\Generator\EntityAbstract $generator * @return void * @throws \Magento\Framework\Exception */ protected function tryToLoadSourceClass($className, $generator) { $sourceClassName = $generator->getSourceClassName(); if (!$this->definedClasses->classLoadable($sourceClassName)) { if ($this->generateClass($sourceClassName) !== self::GENERATION_SUCCESS) { throw new \Magento\Framework\Exception(sprintf('Source class "%s" for "%s" generation does not exist.', $sourceClassName, $className)); } } }
/** * Initialize dependencies. * * @param string|null $sourceClassName * @param string|null $resultClassName * @param Io|null $ioObject * @param CodeGenerator\CodeGeneratorInterface|null $classGenerator * @param \Magento\Framework\Code\Generator\DefinedClasses|null $definedClasses */ public function __construct($sourceClassName = null, $resultClassName = null, Io $ioObject = null, CodeGenerator\CodeGeneratorInterface $classGenerator = null, \Magento\Framework\Code\Generator\DefinedClasses $definedClasses = null) { $this->typeProcessor = new \Magento\Framework\Reflection\TypeProcessor(); parent::__construct($sourceClassName, $resultClassName, $ioObject, $classGenerator, $definedClasses); }
/** * Get source class name * * @return string */ public function getSourceClassName() { return parent::getSourceClassName() . 'Interface'; }