Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LowestPriceOptionsProviderTest.php
Go to the documentation of this file.
1 <?php
8 
15 
16 class LowestPriceOptionsProviderTest extends \PHPUnit\Framework\TestCase
17 {
21  private $model;
22 
26  private $resourceConnection;
27 
31  protected $connection;
32 
36  private $linkedProductSelectBuilder;
37 
41  private $collectionFactory;
42 
46  private $productCollection;
47 
51  private $storeManagerMock;
52 
56  private $storeMock;
57 
58  protected function setUp()
59  {
60  $this->connection = $this
61  ->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
62  ->getMock();
63  $this->resourceConnection = $this
64  ->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
65  ->disableOriginalConstructor()
66  ->setMethods(['getConnection'])
67  ->getMock();
68  $this->resourceConnection->expects($this->once())->method('getConnection')->willReturn($this->connection);
69  $this->linkedProductSelectBuilder = $this
70  ->getMockBuilder(LinkedProductSelectBuilderInterface::class)
71  ->setMethods(['build'])
72  ->getMock();
73  $this->productCollection = $this
74  ->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['addAttributeToSelect', 'addIdFilter', 'getItems'])
77  ->getMock();
78  $this->collectionFactory = $this
79  ->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['create'])
82  ->getMock();
83  $this->collectionFactory->expects($this->once())->method('create')->willReturn($this->productCollection);
84  $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
85  ->getMockForAbstractClass();
86  $this->storeMock = $this->getMockBuilder(StoreInterface::class)
87  ->setMethods(['getId'])
88  ->getMockForAbstractClass();
89 
90  $objectManager = new ObjectManager($this);
91  $this->model = $objectManager->getObject(
92  \Magento\ConfigurableProduct\Pricing\Price\LowestPriceOptionsProvider::class,
93  [
94  'resourceConnection' => $this->resourceConnection,
95  'linkedProductSelectBuilder' => $this->linkedProductSelectBuilder,
96  'collectionFactory' => $this->collectionFactory,
97  'storeManager' => $this->storeManagerMock,
98  ]
99  );
100  }
101 
102  public function testGetProducts()
103  {
104  $productId = 1;
105  $linkedProducts = ['some', 'linked', 'products', 'dataobjects'];
106  $product = $this->getMockBuilder(ProductInterface::class)->disableOriginalConstructor()->getMock();
107  $product->expects($this->any())->method('getId')->willReturn($productId);
108  $this->linkedProductSelectBuilder->expects($this->any())->method('build')->with($productId)->willReturn([]);
109  $this->productCollection
110  ->expects($this->once())
111  ->method('addAttributeToSelect')
112  ->with(['price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id'])
113  ->willReturnSelf();
114  $this->productCollection->expects($this->once())->method('addIdFilter')->willReturnSelf();
115  $this->productCollection->expects($this->once())->method('getItems')->willReturn($linkedProducts);
116  $this->storeManagerMock->expects($this->any())
117  ->method('getStore')
119  ->willReturn($this->storeMock);
120  $this->storeMock->expects($this->any())
121  ->method('getId')
122  ->willReturn(Store::DEFAULT_STORE_ID);
123 
124  $this->assertEquals($linkedProducts, $this->model->getProducts($product));
125  }
126 }
$objectManager
Definition: bootstrap.php:17