/** * @param $className * @return bool * @throws Magento_Exception */ public function generateClass($className) { // check if source class a generated entity $entity = null; $entityName = null; foreach ($this->_generatedEntities as $entityType) { $entitySuffix = ucfirst($entityType); // if $className string ends on $entitySuffix substring if (strrpos($className, $entitySuffix) === strlen($className) - strlen($entitySuffix)) { $entity = $entityType; $entityName = rtrim(substr($className, 0, -1 * strlen($entitySuffix)), '_'); break; } } if (!$entity || !$entityName) { return false; } // check if file already exists $autoloader = $this->_autoloader; if ($autoloader::getFile($className)) { return false; } // generate class file $this->_initGenerator($entity, $entityName, $className); if (!$this->_generator->generate()) { $errors = $this->_generator->getErrors(); throw new Magento_Exception(implode(' ', $errors)); } // remove generator $this->_generator = null; return true; }
/** * @expectedException Magento_Exception */ public function testGenerateClassWithError() { $this->_autoloader->staticExpects($this->once())->method('getFile')->will($this->returnValue(false)); $this->_generator->expects($this->once())->method('generate')->will($this->returnValue(false)); $this->_model = new Magento_Di_Generator($this->_generator, $this->_autoloader); $expectedEntities = array_values($this->_expectedEntities); $resultClassName = self::SOURCE_CLASS . ucfirst(array_shift($expectedEntities)); $this->_model->generateClass($resultClassName); }
/** * @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_Di_Generator_EntityAbstract::generate * @covers Magento_Di_Generator_EntityAbstract::getErrors * @covers Magento_Di_Generator_EntityAbstract::_getSourceClassName * @covers Magento_Di_Generator_EntityAbstract::_getResultClassName * @covers Magento_Di_Generator_EntityAbstract::_getDefaultResultClassName * @covers Magento_Di_Generator_EntityAbstract::_generateCode * @covers Magento_Di_Generator_EntityAbstract::_addError * @covers Magento_Di_Generator_EntityAbstract::_validateData * @covers Magento_Di_Generator_EntityAbstract::_getClassDocBlock * @covers Magento_Di_Generator_EntityAbstract::_getGeneratedCode * @covers Magento_Di_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_Di_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()); } }
/** * @return string */ protected function _generateCode() { $this->_classGenerator->setExtendedClass($this->_getFullyQualifiedClassName($this->_getSourceClassName())); return parent::_generateCode(); }
/** * @return string */ protected function _generateCode() { $this->_classGenerator->setImplementedInterfaces(array(self::FACTORY_INTERFACE)); return parent::_generateCode(); }