Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
StockRepositoryTest.php
Go to the documentation of this file.
1 <?php
7 
11 
17 class StockRepositoryTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $model;
23 
27  protected $stockMock;
28 
32  protected $stockResourceMock;
33 
37  protected $stockFactoryMock;
38 
43 
48 
52  protected $mapperMock;
53 
58 
59  protected function setUp()
60  {
61  $this->stockMock = $this->getMockBuilder(\Magento\CatalogInventory\Model\Stock::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64 
65  $this->stockResourceMock = $this->getMockBuilder(\Magento\CatalogInventory\Model\ResourceModel\Stock::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->stockFactoryMock = $this->getMockBuilder(
69  \Magento\CatalogInventory\Model\StockFactory::class
70  )
71  ->setMethods(['create'])
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->stockCollectionMock = $this->getMockBuilder(
75  \Magento\CatalogInventory\Api\Data\StockCollectionInterfaceFactory::class
76  )
77  ->setMethods(['create'])
78  ->disableOriginalConstructor()
79  ->getMock();
80 
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  StockRepository::class,
94  [
95  'resource' => $this->stockResourceMock,
96  'stockFactory' => $this->stockFactoryMock,
97  'collectionFactory' => $this->stockCollectionMock,
98  'queryBuilderFactory' => $this->queryBuilderFactoryMock,
99  'mapperFactory' => $this->mapperMock,
100  'stockRegistryStorage' => $this->stockRegistryStorage,
101  ]
102  );
103  }
104 
105  public function testSave()
106  {
107  $this->stockResourceMock->expects($this->once())
108  ->method('save')
109  ->with($this->stockMock)
110  ->willReturnSelf();
111 
112  $this->assertEquals($this->stockMock, $this->model->save($this->stockMock));
113  }
114 
118  public function testSaveException()
119  {
120  $this->stockResourceMock->expects($this->once())
121  ->method('save')
122  ->with($this->stockMock)
123  ->willThrowException(new \Exception());
124 
125  $this->model->save($this->stockMock);
126  }
127 
128  public function testGetList()
129  {
130  $criteriaMock = $this->getMockBuilder(\Magento\CatalogInventory\Api\StockCriteriaInterface::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(\Magento\CatalogInventory\Api\Data\StockCollectionInterface::class)
139  ->getMock();
140 
141  $this->queryBuilderFactoryMock->expects($this->once())->method('create')->willReturn($queryBuilderMock);
142  $queryBuilderMock->expects($this->once())->method('setCriteria')->with($criteriaMock)->willReturnSelf();
143  $queryBuilderMock->expects($this->once())
144  ->method('setResource')
145  ->with($this->stockResourceMock)
146  ->willReturnSelf();
147  $queryBuilderMock->expects($this->once())->method('create')->willReturn($queryMock);
148  $this->stockCollectionMock->expects($this->once())->method('create')->willReturn($queryCollectionMock);
149 
150  $this->assertEquals($queryCollectionMock, $this->model->getList($criteriaMock));
151  }
152 
153  public function testDelete()
154  {
155  $this->stockRegistryStorage->expects($this->once())->method('removeStock');
156 
157  $this->stockResourceMock->expects($this->once())
158  ->method('delete')
159  ->with($this->stockMock)
160  ->willReturnSelf();
161 
162  $this->assertTrue($this->model->delete($this->stockMock));
163  }
164 
168  public function testDeleteException()
169  {
170  $this->stockResourceMock->expects($this->once())
171  ->method('delete')
172  ->with($this->stockMock)
173  ->willThrowException(new \Exception());
174 
175  $this->model->delete($this->stockMock);
176  }
177 
178  public function testDeleteById()
179  {
180  $id = 1;
181 
182  $this->stockFactoryMock->expects($this->once())->method('create')->willReturn($this->stockMock);
183  $this->stockResourceMock->expects($this->once())->method('load')->with($this->stockMock, $id);
184  $this->stockMock->expects($this->once())->method('getId')->willReturn($id);
185 
186  $this->assertTrue($this->model->deleteById($id));
187  }
188 
193  public function testDeleteByIdException()
194  {
195  $id = 1;
196 
197  $this->stockFactoryMock->expects($this->once())->method('create')->willReturn($this->stockMock);
198  $this->stockResourceMock->expects($this->once())->method('load')->with($this->stockMock, $id);
199  $this->stockMock->expects($this->once())->method('getId')->willReturn(null);
200 
201  $this->assertTrue($this->model->deleteById($id));
202  }
203 }
$id
Definition: fieldset.phtml:14