Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExtensionAttributesProcessorTest.php
Go to the documentation of this file.
1 <?php
8 
16 
20 class ExtensionAttributesProcessorTest extends \PHPUnit\Framework\TestCase
21 {
25  private $model;
26 
30  private $dataObjectProcessorMock;
31 
35  private $methodsMapProcessorMock;
36 
40  private $fieldNamerMock;
41 
45  private $typeCasterMock;
46 
50  private $configMock;
51 
55  private $authorizationMock;
56 
60  protected function setUp()
61  {
62  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
63 
64  $this->dataObjectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->methodsMapProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\MethodsMap::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->typeCasterMock = $this->getMockBuilder(\Magento\Framework\Reflection\TypeCaster::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $this->fieldNamerMock = $this->getMockBuilder(\Magento\Framework\Reflection\FieldNamer::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->configMock = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttribute\Config::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\AuthorizationInterface::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82 
83  $this->model = $objectManager->getObject(
84  \Magento\Framework\Reflection\ExtensionAttributesProcessor::class,
85  [
86  'dataObjectProcessor' => $this->dataObjectProcessorMock,
87  'methodsMapProcessor' => $this->methodsMapProcessorMock,
88  'typeCaster' => $this->typeCasterMock,
89  'fieldNamer' => $this->fieldNamerMock,
90  'authorization' => $this->authorizationMock,
91  'config' => $this->configMock,
92  'isPermissionChecked' => true,
93  ]
94  );
95  }
96 
102  public function testBuildOutputDataArrayWithPermission($isPermissionAllowed, $expectedValue)
103  {
104  $dataObject = new \Magento\Framework\Reflection\Test\Unit\ExtensionAttributesObject();
105  $dataObjectType = \Magento\Framework\Reflection\Test\Unit\ExtensionAttributesObject::class;
106  $methodName = 'getAttrName';
107  $attributeName = 'attr_name';
108  $attributeValue = 'attrName';
109 
110  $this->methodsMapProcessorMock->expects($this->once())
111  ->method('getMethodsMap')
112  ->with($dataObjectType)
113  ->will($this->returnValue([$methodName => []]));
114  $this->methodsMapProcessorMock->expects($this->once())
115  ->method('isMethodValidForDataField')
116  ->with($dataObjectType, $methodName)
117  ->will($this->returnValue(true));
118  $this->fieldNamerMock->expects($this->once())
119  ->method('getFieldNameForMethodName')
120  ->with($methodName)
121  ->will($this->returnValue($attributeName));
122  $permissionName = 'Magento_Permission';
123  $this->configMock->expects($this->once())
124  ->method('get')
125  ->will($this->returnValue([
126  $dataObjectType => [
127  $attributeName => [ Converter::RESOURCE_PERMISSIONS => [ $permissionName ] ]
128  ]
129  ]));
130  $this->authorizationMock->expects($this->once())
131  ->method('isAllowed')
132  ->with($permissionName)
133  ->will($this->returnValue($isPermissionAllowed));
134 
135  if ($isPermissionAllowed) {
136  $this->methodsMapProcessorMock->expects($this->once())
137  ->method('getMethodReturnType')
138  ->with($dataObjectType, $methodName)
139  ->will($this->returnValue('string'));
140  $this->typeCasterMock->expects($this->once())
141  ->method('castValueToType')
142  ->with($attributeValue, 'string')
143  ->will($this->returnValue($attributeValue));
144  }
145 
146  $value = $this->model->buildOutputDataArray(
147  $dataObject,
148  $dataObjectType
149  );
150 
151  $this->assertEquals(
152  $value,
153  $expectedValue
154  );
155  }
156 
161  {
162  return [
163  'permission allowed' => [
164  true,
165  [
166  'attr_name' => 'attrName',
167  ],
168  ],
169  'permission not allowed' => [
170  false,
171  [],
172  ],
173  ];
174  }
175 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16