Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvoiceServiceTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class InvoiceServiceTest extends \PHPUnit\Framework\TestCase
14 {
20  protected $repositoryMock;
21 
28 
35 
41  protected $filterBuilderMock;
42 
49 
53  protected $invoiceService;
54 
58  protected function setUp()
59  {
60  $objectManager = new ObjectManagerHelper($this);
61 
62  $this->repositoryMock = $this->getMockForAbstractClass(
63  \Magento\Sales\Api\InvoiceRepositoryInterface::class,
64  ['get'],
65  '',
66  false
67  );
68  $this->commentRepositoryMock = $this->getMockForAbstractClass(
69  \Magento\Sales\Api\InvoiceCommentRepositoryInterface::class,
70  ['getList'],
71  '',
72  false
73  );
74  $this->searchCriteriaBuilderMock = $this->createPartialMock(
75  \Magento\Framework\Api\SearchCriteriaBuilder::class,
76  ['create', 'addFilters']
77  );
78  $this->filterBuilderMock = $this->createPartialMock(
79  \Magento\Framework\Api\FilterBuilder::class,
80  ['setField', 'setValue', 'setConditionType', 'create']
81  );
82  $this->invoiceNotifierMock = $this->createPartialMock(
83  \Magento\Sales\Model\Order\InvoiceNotifier::class,
84  ['notify']
85  );
86 
87  $this->invoiceService = $objectManager->getObject(
88  \Magento\Sales\Model\Service\InvoiceService::class,
89  [
90  'repository' => $this->repositoryMock,
91  'commentRepository' => $this->commentRepositoryMock,
92  'criteriaBuilder' => $this->searchCriteriaBuilderMock,
93  'filterBuilder' => $this->filterBuilderMock,
94  'notifier' => $this->invoiceNotifierMock
95  ]
96  );
97  }
98 
102  public function testSetCapture()
103  {
104  $id = 145;
105  $returnValue = true;
106 
107  $invoiceMock = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, ['capture']);
108 
109  $this->repositoryMock->expects($this->once())
110  ->method('get')
111  ->with($id)
112  ->will($this->returnValue($invoiceMock));
113  $invoiceMock->expects($this->once())
114  ->method('capture')
115  ->will($this->returnValue($returnValue));
116 
117  $this->assertTrue($this->invoiceService->setCapture($id));
118  }
119 
123  public function testGetCommentsList()
124  {
125  $id = 25;
126  $returnValue = 'return-value';
127 
128  $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
129  $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
130 
131  $this->filterBuilderMock->expects($this->once())
132  ->method('setField')
133  ->with('parent_id')
134  ->will($this->returnSelf());
135  $this->filterBuilderMock->expects($this->once())
136  ->method('setValue')
137  ->with($id)
138  ->will($this->returnSelf());
139  $this->filterBuilderMock->expects($this->once())
140  ->method('setConditionType')
141  ->with('eq')
142  ->will($this->returnSelf());
143  $this->filterBuilderMock->expects($this->once())
144  ->method('create')
145  ->will($this->returnValue($filterMock));
146  $this->searchCriteriaBuilderMock->expects($this->once())
147  ->method('addFilters')
148  ->with([$filterMock]);
149  $this->searchCriteriaBuilderMock->expects($this->once())
150  ->method('create')
151  ->will($this->returnValue($searchCriteriaMock));
152  $this->commentRepositoryMock->expects($this->once())
153  ->method('getList')
154  ->with($searchCriteriaMock)
155  ->will($this->returnValue($returnValue));
156 
157  $this->assertEquals($returnValue, $this->invoiceService->getCommentsList($id));
158  }
159 
163  public function testNotify()
164  {
165  $id = 123;
166  $returnValue = 'return-value';
167 
168  $modelMock = $this->getMockForAbstractClass(
169  \Magento\Sales\Model\AbstractModel::class,
170  [],
171  '',
172  false
173  );
174 
175  $this->repositoryMock->expects($this->once())
176  ->method('get')
177  ->with($id)
178  ->will($this->returnValue($modelMock));
179  $this->invoiceNotifierMock->expects($this->once())
180  ->method('notify')
181  ->with($modelMock)
182  ->will($this->returnValue($returnValue));
183 
184  $this->assertEquals($returnValue, $this->invoiceService->notify($id));
185  }
186 
190  public function testSetVoid()
191  {
192  $id = 145;
193  $returnValue = true;
194 
195  $invoiceMock = $this->createPartialMock(\Magento\Sales\Model\Order\Invoice::class, ['void']);
196 
197  $this->repositoryMock->expects($this->once())
198  ->method('get')
199  ->with($id)
200  ->will($this->returnValue($invoiceMock));
201  $invoiceMock->expects($this->once())
202  ->method('void')
203  ->will($this->returnValue($returnValue));
204 
205  $this->assertTrue($this->invoiceService->setVoid($id));
206  }
207 }
$objectManager
Definition: bootstrap.php:17
$id
Definition: fieldset.phtml:14