|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\GroupedProduct\Test\Unit\Model\ResourceModel\Product\Link; |
| 8 | + |
| 9 | +use Magento\GroupedProduct\Model\ResourceModel\Product\Link\RelationPersister; |
| 10 | +use Magento\Catalog\Model\ProductLink\LinkFactory; |
| 11 | +use Magento\Catalog\Model\Product\Link; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\Relation; |
| 13 | + |
| 14 | +class RelationPersisterTest extends \PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + /** @var RelationPersister|PHPUnit_Framework_MockObject_MockObject */ |
| 17 | + private $object; |
| 18 | + |
| 19 | + /** @var Link */ |
| 20 | + private $link; |
| 21 | + |
| 22 | + /** @var Relation */ |
| 23 | + private $relationProcessor; |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | + protected function setUp() |
| 29 | + { |
| 30 | + $linkFactory = $this->getMockBuilder(LinkFactory::class) |
| 31 | + ->setMethods(['create']) |
| 32 | + ->disableOriginalConstructor() |
| 33 | + ->getMock(); |
| 34 | + |
| 35 | + $this->relationProcessor = $this->getMockBuilder(Relation::class) |
| 36 | + ->disableOriginalConstructor() |
| 37 | + ->getMock(); |
| 38 | + |
| 39 | + $this->link = $this->getMockBuilder(Link::class) |
| 40 | + ->setMethods(['getLinkTypeId', 'getProductId', 'getLinkedProductId']) |
| 41 | + ->disableOriginalConstructor() |
| 42 | + ->getMock(); |
| 43 | + |
| 44 | + $linkFactory->expects($this->any())->method('create')->willReturn($this->link); |
| 45 | + |
| 46 | + $this->object = new RelationPersister( |
| 47 | + $this->relationProcessor, |
| 48 | + $linkFactory |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + public function testAroundSaveProductLinks() |
| 53 | + { |
| 54 | + $subject = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Link::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->getMock(); |
| 57 | + $this->relationProcessor->expects($this->once())->method('addRelation')->with(2, 10); |
| 58 | + $this->assertEquals($subject, $this->object->aroundSaveProductLinks( |
| 59 | + $subject, |
| 60 | + function() use ($subject) { return $subject; }, |
| 61 | + 2, |
| 62 | + [['product_id' => 10]], |
| 63 | + 3 |
| 64 | + )); |
| 65 | + } |
| 66 | + |
| 67 | + public function testAroundDeleteProductLink() |
| 68 | + { |
| 69 | + $subject = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Link::class) |
| 70 | + ->disableOriginalConstructor() |
| 71 | + ->getMock(); |
| 72 | + $subject->expects($this->any())->method('getIdFieldName')->willReturn('id'); |
| 73 | + $subject->expects($this->once())->method('load')->with($this->link, 155, 'id'); |
| 74 | + |
| 75 | + $this->link->expects($this->any()) |
| 76 | + ->method('getLinkTypeId') |
| 77 | + ->willReturn(\Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED); |
| 78 | + $this->link->expects($this->any()) |
| 79 | + ->method('getProductId') |
| 80 | + ->willReturn(12); |
| 81 | + $this->link->expects($this->any()) |
| 82 | + ->method('getLinkedProductId') |
| 83 | + ->willReturn(13); |
| 84 | + |
| 85 | + $this->relationProcessor->expects($this->once())->method('removeRelations')->with(12, 13); |
| 86 | + $this->assertEquals( |
| 87 | + $subject, |
| 88 | + $this->object->aroundDeleteProductLink( |
| 89 | + $subject, |
| 90 | + function() use ($subject) { return $subject; }, |
| 91 | + 155 |
| 92 | + ) |
| 93 | + ); |
| 94 | + |
| 95 | + } |
| 96 | +} |
0 commit comments