Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CollectionProviderTest.php
Go to the documentation of this file.
1 <?php
8 
14 
15 class CollectionProviderTest extends \PHPUnit\Framework\TestCase
16 {
20  private $model;
21 
25  private $converterPoolMock;
26 
30  private $providerMock;
31 
35  private $productMock;
36 
40  private $converterMock;
41 
42  protected function setUp()
43  {
44  $this->productMock = $this->createMock(Product::class);
45  $this->converterPoolMock = $this->createMock(ConverterPool::class);
46  $this->providerMock = $this->createMock(CollectionProviderInterface::class);
47  $this->converterMock = $this->createMock(ConverterInterface::class);
48 
49  $this->model = new CollectionProvider($this->converterPoolMock, ['crosssell' => $this->providerMock]);
50  }
51 
55  public function testGetCollection()
56  {
57  $linkedProductOneMock = $this->createMock(Product::class);
58  $linkedProductTwoMock = $this->createMock(Product::class);
59  $linkedProductThreeMock = $this->createMock(Product::class);
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 
107  {
108  $this->model->getCollection($this->productMock, 'upsell');
109  }
110 }