Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvoiceRepositoryTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class InvoiceRepositoryTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $invoice;
20 
24  protected $invoiceMetadata;
25 
30 
34  private $collectionProcessorMock;
35 
36  protected function setUp()
37  {
38  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39  $this->invoiceMetadata = $this->createMock(\Magento\Sales\Model\ResourceModel\Metadata::class);
40  $this->searchResultFactory = $this->getMockBuilder(
41  \Magento\Sales\Api\Data\InvoiceSearchResultInterfaceFactory::class
42  )
43  ->disableOriginalConstructor()
44  ->setMethods(['create'])
45  ->getMock();
46  $this->collectionProcessorMock = $this->getMockBuilder(CollectionProcessorInterface::class)
47  ->getMock();
48  $this->invoice = $objectManager->getObject(
49  \Magento\Sales\Model\Order\InvoiceRepository::class,
50  [
51  'invoiceMetadata' => $this->invoiceMetadata,
52  'searchResultFactory' => $this->searchResultFactory,
53  'collectionProcessor' => $this->collectionProcessorMock,
54  ]
55  );
56  $this->type = $this->createPartialMock(\Magento\Eav\Model\Entity\Type::class, ['fetchNewIncrementId']);
57  }
58 
59  public function testGet()
60  {
61  $id = 1;
62 
63  $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $entity->expects($this->once())
67  ->method('load')
68  ->with($id)
69  ->willReturn($entity);
70  $entity->expects($this->once())
71  ->method('getEntityId')
72  ->willReturn($id);
73 
74  $this->invoiceMetadata->expects($this->once())
75  ->method('getNewInstance')
76  ->willReturn($entity);
77 
78  $this->assertEquals($entity, $this->invoice->get($id));
79  }
80 
85  public function testGetNoId()
86  {
87  $this->invoice->get(null);
88  }
89 
94  public function testGetEntityNoId()
95  {
96  $id = 1;
97 
98  $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101  $entity->expects($this->once())
102  ->method('load')
103  ->with($id)
104  ->willReturn($entity);
105  $entity->expects($this->once())
106  ->method('getEntityId')
107  ->willReturn(null);
108 
109  $this->invoiceMetadata->expects($this->once())
110  ->method('getNewInstance')
111  ->willReturn($entity);
112 
113  $this->assertNull($entity, $this->invoice->get($id));
114  }
115 
116  public function testCreate()
117  {
118  $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $this->invoiceMetadata->expects($this->once())
122  ->method('getNewInstance')
123  ->willReturn($entity);
124  $this->assertEquals($entity, $this->invoice->create());
125  }
126 
127  public function testGetList()
128  {
129  $searchCriteria = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteria::class)
130  ->disableOriginalConstructor()
131  ->getMock();
132  $collection = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice\Collection::class)
133  ->disableOriginalConstructor()
134  ->getMock();
135  $this->collectionProcessorMock->expects($this->once())
136  ->method('process')
137  ->with($searchCriteria, $collection);
138  $this->searchResultFactory->expects($this->once())
139  ->method('create')
140  ->willReturn($collection);
141 
142  $this->assertEquals($collection, $this->invoice->getList($searchCriteria));
143  }
144 
145  public function testDelete()
146  {
147  $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
148  ->disableOriginalConstructor()
149  ->getMock();
150  $entity->expects($this->once())
151  ->method('getEntityId')
152  ->willReturn(1);
153 
154  $mapper = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157  $mapper->expects($this->once())
158  ->method('delete')
159  ->with($entity);
160 
161  $this->invoiceMetadata->expects($this->any())
162  ->method('getMapper')
163  ->willReturn($mapper);
164 
165  $this->assertTrue($this->invoice->delete($entity));
166  }
167 
168  public function testDeleteById()
169  {
170  $id = 1;
171 
172  $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
173  ->disableOriginalConstructor()
174  ->getMock();
175  $entity->expects($this->once())
176  ->method('load')
177  ->with($id)
178  ->willReturn($entity);
179  $entity->expects($this->any())
180  ->method('getEntityId')
181  ->willReturn($id);
182 
183  $this->invoiceMetadata->expects($this->once())
184  ->method('getNewInstance')
185  ->willReturn($entity);
186 
187  $mapper = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice::class)
188  ->disableOriginalConstructor()
189  ->getMock();
190  $mapper->expects($this->once())
191  ->method('delete')
192  ->with($entity);
193 
194  $this->invoiceMetadata->expects($this->any())
195  ->method('getMapper')
196  ->willReturn($mapper);
197 
198  $this->assertTrue($this->invoice->deleteById($id));
199  }
200 
201  public function testSave()
202  {
203  $entity = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
204  ->disableOriginalConstructor()
205  ->getMock();
206  $entity->expects($this->any())
207  ->method('getEntityId')
208  ->willReturn(1);
209 
210  $mapper = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Invoice::class)
211  ->disableOriginalConstructor()
212  ->getMock();
213  $mapper->expects($this->once())
214  ->method('save')
215  ->with($entity);
216 
217  $this->invoiceMetadata->expects($this->any())
218  ->method('getMapper')
219  ->willReturn($mapper);
220 
221  $this->assertEquals($entity, $this->invoice->save($entity));
222  }
223 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14
$searchCriteria
$entity
Definition: element.phtml:22