/** * @expectedException \LogicException * @expectedExceptionMessage Class "\Some\Class" does not exist. Please note that namespace must be specified. */ public function testResolveObjectTypeWithConfiguredAttributeAndNonExistedClass() { $code = 'some_code'; $value = new \stdClass(); $context = '\\Some\\Class'; $config = ['Some\\Class' => ['some_code' => ['type' => '\\Some\\Class']]]; $this->typeProcessor->expects($this->once())->method('getArrayItemType')->with('\\Some\\Class')->willReturn('\\Some\\Class'); $this->configMock->expects($this->once())->method('get')->willReturn($config); $this->model->resolveObjectType($code, $value, $context); }
/** * {@inheritdoc} */ public function resolveObjectType($attributeCode, $value, $context) { if (!is_object($value)) { throw new \InvalidArgumentException('Provided value is not object type'); } $data = $this->config->get(); $context = trim($context, '\\'); $config = isset($data[$context]) ? $data[$context] : []; $output = get_class($value); if (isset($config[$attributeCode])) { $type = $config[$attributeCode]['type']; $output = $this->typeProcessor->getArrayItemType($type); if (!(class_exists($output) || interface_exists($output))) { throw new \LogicException(sprintf('Class "%s" does not exist. Please note that namespace must be specified.', $type)); } } return $output; }
/** * @param bool $isPermissionAllowed * @param array $expectedValue * @dataProvider buildOutputDataArrayWithPermissionProvider */ public function testBuildOutputDataArrayWithPermission($isPermissionAllowed, $expectedValue) { $dataObject = new \Magento\Framework\Reflection\Test\Unit\ExtensionAttributesObject(); $dataObjectType = 'Magento\\Framework\\Reflection\\Test\\Unit\\ExtensionAttributesObject'; $methodName = 'getAttrName'; $attributeName = 'attr_name'; $attributeValue = 'attrName'; $this->methodsMapProcessorMock->expects($this->once())->method('getMethodsMap')->with($dataObjectType)->will($this->returnValue([$methodName => []])); $this->methodsMapProcessorMock->expects($this->once())->method('isMethodValidForDataField')->with($dataObjectType, $methodName)->will($this->returnValue(true)); $this->fieldNamerMock->expects($this->once())->method('getFieldNameForMethodName')->with($methodName)->will($this->returnValue($attributeName)); $permissionName = 'Magento_Permission'; $this->configMock->expects($this->once())->method('get')->will($this->returnValue([$dataObjectType => [$attributeName => [Converter::RESOURCE_PERMISSIONS => [$permissionName]]]])); $this->authorizationMock->expects($this->once())->method('isAllowed')->with($permissionName)->will($this->returnValue($isPermissionAllowed)); if ($isPermissionAllowed) { $this->methodsMapProcessorMock->expects($this->once())->method('getMethodReturnType')->with($dataObjectType, $methodName)->will($this->returnValue('string')); $this->typeCasterMock->expects($this->once())->method('castValueToType')->with($attributeValue, 'string')->will($this->returnValue($attributeValue)); } $value = $this->model->buildOutputDataArray($dataObject, $dataObjectType); $this->assertEquals($value, $expectedValue); }
/** * @param string $typeName * @param string $attributeCode * @return string[] A list of permissions */ private function getPermissionsForTypeAndMethod($typeName, $attributeCode) { $attributes = $this->config->get(); if (isset($attributes[$typeName]) && isset($attributes[$typeName][$attributeCode])) { $attributeMetadata = $attributes[$typeName][$attributeCode]; $permissions = []; foreach ($attributeMetadata[Converter::RESOURCE_PERMISSIONS] as $permission) { $permissions[] = $permission; } return $permissions; } return []; }
/** * Returns the internal join directive config for a given type. * * Array returned has all of the \Magento\Framework\Api\ExtensionAttribute\Config\Converter JOIN* fields set. * * @param string $extensibleEntityClass * @return array */ private function getJoinDirectivesForType($extensibleEntityClass) { $extensibleInterfaceName = $this->extensionAttributesFactory->getExtensibleInterfaceName($extensibleEntityClass); $extensibleInterfaceName = ltrim($extensibleInterfaceName, '\\'); $config = $this->config->get(); if (!isset($config[$extensibleInterfaceName])) { return []; } $typeAttributesConfig = $config[$extensibleInterfaceName]; $joinDirectives = []; foreach ($typeAttributesConfig as $attributeCode => $attributeConfig) { if (isset($attributeConfig[Converter::JOIN_DIRECTIVE])) { $joinDirectives[$attributeCode] = $attributeConfig[Converter::JOIN_DIRECTIVE]; $joinDirectives[$attributeCode][Converter::DATA_TYPE] = $attributeConfig[Converter::DATA_TYPE]; } } return $joinDirectives; }
/** * Retrieve a list of attributes associated with current source class. * * @return array */ protected function getCustomAttributes() { if (!isset($this->allCustomAttributes)) { $this->allCustomAttributes = $this->config->get(); } $dataInterface = ltrim($this->getSourceClassName(), '\\'); if (isset($this->allCustomAttributes[$dataInterface])) { foreach ($this->allCustomAttributes[$dataInterface] as $attributeName => $attributeMetadata) { $attributeType = $attributeMetadata[Converter::DATA_TYPE]; if (strpos($attributeType, '\\') !== false) { /** Add preceding slash to class names, while leaving primitive types as is */ $attributeType = $this->_getFullyQualifiedClassName($attributeType); $this->allCustomAttributes[$dataInterface][$attributeName][Converter::DATA_TYPE] = $this->_getFullyQualifiedClassName($attributeType); } } return $this->allCustomAttributes[$dataInterface]; } else { return []; } }
/** * Returns config data values * * @return array|mixed|null */ public function getConfigData() { return $this->config->get(); }
public function testGenerateEmptyExtension() { $this->configMock->expects($this->any())->method('get')->willReturn(['Magento\\Catalog\\Api\\Data\\Product' => ['should_not_include' => 'string']]); $expectedResult = file_get_contents(__DIR__ . '/_files/SampleEmptyExtension.txt'); $this->validateGeneratedCode($expectedResult); }