Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AfterProductLoadTest.php
Go to the documentation of this file.
1 <?php
9 
10 class AfterProductLoadTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $plugin;
16 
20  protected $productMock;
21 
26 
31 
32  protected function setUp()
33  {
34  $stockRegistryMock = $this->createMock(\Magento\CatalogInventory\Api\StockRegistryInterface::class);
35  $this->productExtensionFactoryMock = $this->getMockBuilder(
36  \Magento\Catalog\Api\Data\ProductExtensionFactory::class
37  )
38  ->setMethods(['create'])
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $this->plugin = new \Magento\CatalogInventory\Model\Plugin\AfterProductLoad(
43  $stockRegistryMock,
44  $this->productExtensionFactoryMock
45  );
46 
47  $productId = 5494;
48  $stockItemMock = $this->createMock(\Magento\CatalogInventory\Api\Data\StockItemInterface::class);
49 
50  $stockRegistryMock->expects($this->once())
51  ->method('getStockItem')
52  ->with($productId)
53  ->willReturn($stockItemMock);
54 
55  $this->productExtensionMock = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductExtensionInterface::class)
56  ->setMethods(['setStockItem'])
57  ->getMockForAbstractClass();
58  $this->productExtensionMock->expects($this->once())
59  ->method('setStockItem')
60  ->with($stockItemMock)
61  ->willReturnSelf();
62 
63  $this->productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->productMock->expects($this->once())
67  ->method('setExtensionAttributes')
68  ->with($this->productExtensionMock)
69  ->willReturnSelf();
70  $this->productMock->expects(($this->once()))
71  ->method('getId')
72  ->will($this->returnValue($productId));
73  }
74 
75  public function testAfterLoad()
76  {
77  $this->productMock->expects($this->once())
78  ->method('getExtensionAttributes')
79  ->willReturn($this->productExtensionMock);
80  $this->productExtensionFactoryMock->expects($this->never())
81  ->method('create');
82 
83  $this->assertEquals(
84  $this->productMock,
85  $this->plugin->afterLoad($this->productMock)
86  );
87  }
88 }