CollectionProviderTest.php 4.3 KB

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