Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockStatusRepositoryTest.php
Go to the documentation of this file.
1 <?php
7 
12 
18 class StockStatusRepositoryTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $model;
24 
28  protected $stockStatusMock;
29 
34 
39 
44 
49 
53  protected $mapperMock;
54 
59 
60  protected function setUp()
61  {
62  $this->stockStatusMock = $this->getMockBuilder(\Magento\CatalogInventory\Model\Stock\Status::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->stockStatusResourceMock =
66  $this->getMockBuilder(\Magento\CatalogInventory\Model\ResourceModel\Stock\Status::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->stockStatusFactoryMock = $this->getMockBuilder(
70  \Magento\CatalogInventory\Model\Stock\StatusFactory::class
71  )
72  ->setMethods(['create'])
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->stockStatusCollectionMock = $this->getMockBuilder(
76  \Magento\CatalogInventory\Api\Data\StockStatusCollectionInterfaceFactory::class
77  )
78  ->setMethods(['create'])
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->queryBuilderFactoryMock = $this->getMockBuilder(\Magento\Framework\DB\QueryBuilderFactory::class)
82  ->setMethods(['create'])
83  ->disableOriginalConstructor()
84  ->getMock();
85  $this->mapperMock = $this->getMockBuilder(\Magento\Framework\DB\MapperFactory::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88  $this->stockRegistryStorage = $this->getMockBuilder(StockRegistryStorage::class)
89  ->disableOriginalConstructor()
90  ->getMock();
91 
92  $this->model = (new ObjectManager($this))->getObject(
93  StockStatusRepository::class,
94  [
95  'resource' => $this->stockStatusResourceMock,
96  'stockStatusFactory' => $this->stockStatusFactoryMock,
97  'collectionFactory' => $this->stockStatusCollectionMock,
98  'queryBuilderFactory' => $this->queryBuilderFactoryMock,
99  'mapperFactory' => $this->mapperMock,
100  'stockRegistryStorage' => $this->stockRegistryStorage,
101  ]
102  );
103  }
104 
105  public function testSave()
106  {
107  $this->stockStatusResourceMock->expects($this->once())
108  ->method('save')
109  ->with($this->stockStatusMock)
110  ->willReturnSelf();
111 
112  $this->assertEquals($this->stockStatusMock, $this->model->save($this->stockStatusMock));
113  }
114 
118  public function testSaveException()
119  {
120  $this->stockStatusResourceMock->expects($this->once())
121  ->method('save')
122  ->with($this->stockStatusMock)
123  ->willThrowException(new \Exception());
124 
125  $this->model->save($this->stockStatusMock);
126  }
127 
128  public function testGetList()
129  {
130  $criteriaMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockStatusCriteriaInterface::class)
131  ->getMock();
132  $queryBuilderMock = $this->getMockBuilder(\Magento\Framework\DB\QueryBuilder::class)
133  ->disableOriginalConstructor()
134  ->setMethods(['setCriteria', 'setResource', 'create'])
135  ->getMock();
136  $queryMock = $this->getMockBuilder(\Magento\Framework\DB\QueryInterface::class)
137  ->getMock();
138  $queryCollectionMock = $this->getMockBuilder(
139  \Magento\CatalogInventory\Api\Data\StockStatusCollectionInterface::class
140  )->getMock();
141 
142  $this->queryBuilderFactoryMock->expects($this->once())->method('create')->willReturn($queryBuilderMock);
143  $queryBuilderMock->expects($this->once())->method('setCriteria')->with($criteriaMock)->willReturnSelf();
144  $queryBuilderMock->expects($this->once())
145  ->method('setResource')
146  ->with($this->stockStatusResourceMock)
147  ->willReturnSelf();
148  $queryBuilderMock->expects($this->once())->method('create')->willReturn($queryMock);
149  $this->stockStatusCollectionMock->expects($this->once())->method('create')->willReturn($queryCollectionMock);
150 
151  $this->assertEquals($queryCollectionMock, $this->model->getList($criteriaMock));
152  }
153 
154  public function testDelete()
155  {
156  $productId = 1;
157  $this->stockStatusMock->expects($this->atLeastOnce())->method('getProductId')->willReturn($productId);
158  $this->stockRegistryStorage->expects($this->once())->method('removeStockStatus')->with($productId);
159 
160  $this->stockStatusResourceMock->expects($this->once())
161  ->method('delete')
162  ->with($this->stockStatusMock)
163  ->willReturnSelf();
164 
165  $this->assertTrue($this->model->delete($this->stockStatusMock));
166  }
167 
171  public function testDeleteException()
172  {
173  $this->stockStatusResourceMock->expects($this->once())
174  ->method('delete')
175  ->with($this->stockStatusMock)
176  ->willThrowException(new \Exception());
177 
178  $this->model->delete($this->stockStatusMock);
179  }
180 
181  public function testDeleteById()
182  {
183  $id = 1;
184 
185  $this->stockStatusFactoryMock->expects($this->once())->method('create')->willReturn($this->stockStatusMock);
186  $this->stockStatusResourceMock->expects($this->once())->method('load')->with($this->stockStatusMock, $id);
187 
188  $this->assertTrue($this->model->deleteById($id));
189  }
190 
194  public function testDeleteByIdException()
195  {
196  $id = 1;
197 
198  $this->stockStatusFactoryMock->expects($this->once())->method('create')->willReturn($this->stockStatusMock);
199  $this->stockStatusResourceMock->expects($this->once())->method('load')->with($this->stockStatusMock, $id);
200  $this->stockStatusResourceMock->expects($this->once())
201  ->method('delete')
202  ->with($this->stockStatusMock)
203  ->willThrowException(new \Exception());
204 
205  $this->assertTrue($this->model->deleteById($id));
206  }
207 }
$id
Definition: fieldset.phtml:14