/** * @param string $entityType * @param object $entity * @param array $data * @return object */ public function execute($entityType, $entity, $data = []) { $hydrator = $this->metadataPool->getHydrator($entityType); $entityData = array_merge($hydrator->extract($entity), $data); $this->updateEntityRow->execute($entityType, $entityData); $hydrator->hydrate($entity, $entityData); return $entity; }
public function testExecute() { $entityType = 'Test_Entity'; $entityTable = 'test_table_1'; $linkField = 'test_table_2'; $describeTable = [['DEFAULT' => 'CURRENT_TIMESTAMP'], ['DEFAULT' => 'NOT_CURRENT_TIMESTAMP', 'IDENTITY' => false, 'COLUMN_NAME' => 'test_column_name']]; $data = [$linkField => $linkField, 'test_column_name' => 'test_column_name']; $output['test_column_name'] = 'test_column_name'; $expectedResult = true; $entityMetadataMock = $this->getMockBuilder('Magento\\Framework\\EntityManager\\EntityMetadata')->disableOriginalConstructor()->getMock(); $connectionMock = $this->getMockBuilder('Magento\\Framework\\DB\\Adapter\\AdapterInterface')->disableOriginalConstructor()->getMock(); $connectionMock->expects($this->once())->method('describeTable')->willReturn($describeTable); $connectionMock->expects($this->once())->method('update')->with($entityTable, $output, [$linkField . ' = ?' => $data[$linkField]])->willReturn($expectedResult); $entityMetadataMock->expects($this->any())->method('getEntityConnection')->willReturn($connectionMock); $entityMetadataMock->expects($this->any())->method('getEntityTable')->willReturn($entityTable); $entityMetadataMock->expects($this->any())->method('getLinkField')->willReturn($linkField); $this->metadataPoolMock->expects($this->once())->method('getMetadata')->with($entityType)->willReturn($entityMetadataMock); $result = $this->model->execute($entityType, $data); $this->assertEquals($expectedResult, $result); }