Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RelationPersisterTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Model\ProductLink\LinkFactory;
14 
15 class RelationPersisterTest extends \PHPUnit\Framework\TestCase
16 {
18  private $object;
19 
21  private $link;
22 
24  private $relationProcessor;
25 
29  private $objectManager;
30 
34  private $linkFactory;
35 
39  private $subject;
40 
44  protected function setUp()
45  {
46  $this->objectManager = new ObjectManager($this);
47 
48  $this->linkFactory = $this->getMockBuilder(LinkFactory::class)
49  ->setMethods(['create'])
50  ->disableOriginalConstructor()
51  ->getMock();
52 
53  $this->relationProcessor = $this->getMockBuilder(Relation::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56 
57  $this->link = $this->getMockBuilder(Link::class)
58  ->setMethods(['getLinkTypeId', 'getProductId', 'getLinkedProductId'])
59  ->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->linkFactory->expects($this->any())->method('create')->willReturn($this->link);
63 
64  $this->subject = $this->getMockBuilder(LinkResourceModel::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67 
68  $this->object = $this->objectManager->getObject(
69  RelationPersister::class,
70  [
71  'relationProcessor' => $this->relationProcessor,
72  'linkFactory' => $this->linkFactory
73  ]
74  );
75  }
76 
77  public function testAfterSaveProductLinks()
78  {
79  $this->relationProcessor->expects($this->once())->method('addRelation')->with(2, 10);
80  $this->assertEquals($this->subject, $this->object->afterSaveProductLinks(
81  $this->subject,
82  $this->subject,
83  2,
84  [['product_id' => 10]],
85  3
86  ));
87  }
88 
89  public function testAroundDeleteProductLink()
90  {
91  $subject = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Link::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94  $subject->expects($this->any())->method('getIdFieldName')->willReturn('id');
95  $subject->expects($this->once())->method('load')->with($this->link, 155, 'id');
96 
97  $this->link->expects($this->any())
98  ->method('getLinkTypeId')
99  ->willReturn(\Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED);
100  $this->link->expects($this->any())
101  ->method('getProductId')
102  ->willReturn(12);
103  $this->link->expects($this->any())
104  ->method('getLinkedProductId')
105  ->willReturn(13);
106 
107  $this->relationProcessor->expects($this->once())->method('removeRelations')->with(12, 13);
108  $this->assertEquals(
109  $subject,
110  $this->object->aroundDeleteProductLink(
111  $subject,
112  function () use ($subject) {
113  return $subject;
114  },
115  155
116  )
117  );
118  }
119 }
else function()