|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Unit\Model; |
| 8 | + |
| 9 | +use Magento\Catalog\Model\ProductLink\CollectionProvider; |
| 10 | +use Magento\Catalog\Model\ProductLink\CollectionProviderInterface; |
| 11 | +use Magento\Catalog\Model\ProductLink\Converter\ConverterInterface; |
| 12 | +use Magento\Catalog\Model\ProductLink\Converter\ConverterPool; |
| 13 | +use Magento\Catalog\Model\Product; |
| 14 | + |
| 15 | +class CollectionProviderTest extends \PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var CollectionProvider |
| 19 | + */ |
| 20 | + private $model; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + private $converterPoolMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $providerMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $productMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + private $converterMock; |
| 41 | + |
| 42 | + protected function setUp() |
| 43 | + { |
| 44 | + $this->productMock = $this->getMock(Product::class, [], [], '', false, false); |
| 45 | + $this->converterPoolMock = $this->getMock(ConverterPool::class, [], [], '', false, false); |
| 46 | + $this->providerMock = $this->getMock(CollectionProviderInterface::class); |
| 47 | + $this->converterMock = $this->getMock(ConverterInterface::class); |
| 48 | + |
| 49 | + $this->model = new CollectionProvider($this->converterPoolMock, ['crosssell' => $this->providerMock]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test sort order of linked products based on configured item position. |
| 54 | + */ |
| 55 | + public function testGetCollection() |
| 56 | + { |
| 57 | + $linkedProductOneMock = $this->getMock(Product::class, [], [], '', false, false); |
| 58 | + $linkedProductTwoMock = $this->getMock(Product::class, [], [], '', false, false); |
| 59 | + $linkedProductThreeMock = $this->getMock(Product::class, [], [], '', false, false); |
| 60 | + |
| 61 | + $linkedProductOneMock->expects($this->once())->method('getId')->willReturn(1); |
| 62 | + $linkedProductTwoMock->expects($this->once())->method('getId')->willReturn(2); |
| 63 | + $linkedProductThreeMock->expects($this->once())->method('getId')->willReturn(3); |
| 64 | + |
| 65 | + $this->converterPoolMock->expects($this->once()) |
| 66 | + ->method('getConverter') |
| 67 | + ->with('crosssell') |
| 68 | + ->willReturn($this->converterMock); |
| 69 | + |
| 70 | + $map = [ |
| 71 | + [$linkedProductOneMock, ['name' => 'Product One', 'position' => 10]], |
| 72 | + [$linkedProductTwoMock, ['name' => 'Product Two', 'position' => 2]], |
| 73 | + [$linkedProductThreeMock, ['name' => 'Product Three', 'position' => 2]], |
| 74 | + ]; |
| 75 | + |
| 76 | + $this->converterMock->expects($this->exactly(3))->method('convert')->willReturnMap($map); |
| 77 | + |
| 78 | + $this->providerMock->expects($this->once()) |
| 79 | + ->method('getLinkedProducts') |
| 80 | + ->with($this->productMock) |
| 81 | + ->willReturn( |
| 82 | + [ |
| 83 | + $linkedProductOneMock, |
| 84 | + $linkedProductTwoMock, |
| 85 | + $linkedProductThreeMock |
| 86 | + ] |
| 87 | + ); |
| 88 | + |
| 89 | + $expectedResult = [ |
| 90 | + 2 => ['name' => 'Product Two', 'position' => 2], |
| 91 | + 3 => ['name' => 'Product Three', 'position' => 2], |
| 92 | + 10 => ['name' => 'Product One', 'position' => 10], |
| 93 | + ]; |
| 94 | + |
| 95 | + $actualResult = $this->model->getCollection($this->productMock, 'crosssell'); |
| 96 | + |
| 97 | + $this->assertEquals($expectedResult, $actualResult, 'Sort order of linked products in incorrect'); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test exception when collection provider is not configured for product link type. |
| 102 | + * |
| 103 | + * @expectedException \Magento\Framework\Exception\NoSuchEntityException |
| 104 | + * @expectedExceptionMessage Collection provider is not registered |
| 105 | + */ |
| 106 | + public function testGetCollectionWithMissingProviders() |
| 107 | + { |
| 108 | + $this->model->getCollection($this->productMock, 'upsell'); |
| 109 | + } |
| 110 | +} |
0 commit comments