public function testSaveUpdateMerge()
 {
     $synonymGroupModel = $this->getMock('Magento\\Search\\Model\\SynonymGroup', [], [], '', false);
     $synonymGroupModel->expects($this->once())->method('load')->with(1);
     $synonymGroupModel->expects($this->exactly(2))->method('getSynonymGroup')->willReturn('a,b,c');
     $synonymGroupModel->expects($this->once())->method('getGroupId')->willReturn(1);
     $existingSynonymGroupModel = $this->getMock('Magento\\Search\\Model\\SynonymGroup', [], [], '', false);
     $existingSynonymGroupModel->expects($this->once())->method('load')->with(2);
     $existingSynonymGroupModel->expects($this->once())->method('delete');
     $existingSynonymGroupModel->expects($this->once())->method('getSynonymGroup')->willReturn('d,e,f');
     $synonymGroupModel->expects($this->once())->method('setStoreId');
     $synonymGroupModel->expects($this->once())->method('setWebsiteId');
     // merged result
     $synonymGroupModel->expects($this->once())->method('setSynonymGroup')->with('d,e,f,a,z');
     $this->factory->expects($this->at(0))->method('create')->willReturn($synonymGroupModel);
     $this->factory->expects($this->at(1))->method('create')->willReturn($existingSynonymGroupModel);
     $this->resourceModel->expects($this->once())->method('getByScope')->willReturn([['group_id' => 1, 'synonyms' => 'a,b,c'], ['group_id' => 2, 'synonyms' => 'd,e,f']]);
     $this->resourceModel->expects($this->once())->method('save')->with($synonymGroupModel);
     $data = $this->getMockForAbstractClass('Magento\\Search\\Api\\Data\\SynonymGroupInterface', [], '', false);
     $data->expects($this->once())->method('getGroupId')->willReturn(1);
     $data->expects($this->exactly(2))->method('getStoreId');
     $data->expects($this->exactly(2))->method('getWebsiteId');
     $data->expects($this->exactly(3))->method('getSynonymGroup')->willReturn('a,d,z');
     $this->object->save($data);
 }
 /**
  * Gets all other matching synonym groups in the same scope
  *
  * @param SynonymGroupInterface $synonymGroup
  * @return string[]
  */
 private function getMatchingSynonymGroups(SynonymGroupInterface $synonymGroup)
 {
     $synonymGroupsInScope = $this->resourceModel->getByScope($synonymGroup->getWebsiteId(), $synonymGroup->getStoreId());
     $matchingSynonymGroups = [];
     foreach ($synonymGroupsInScope as $synonymGroupInScope) {
         if (array_intersect(explode(',', $synonymGroup->getSynonymGroup()), explode(',', $synonymGroupInScope['synonyms']))) {
             $matchingSynonymGroups[$synonymGroupInScope['group_id']] = $synonymGroupInScope['synonyms'];
         }
     }
     return $matchingSynonymGroups;
 }