1+ <?php
2+
3+ namespace Tests \Unit \Accounting ;
4+
5+ use FireAPI \Accounting \Accounting ;
6+ use FireAPI \Client ;
7+ use FireAPI \Exceptions \ParameterException ;
8+ use GuzzleHttp \Client as GuzzleClient ;
9+ use GuzzleHttp \Handler \MockHandler ;
10+ use GuzzleHttp \HandlerStack ;
11+ use GuzzleHttp \Psr7 \Response ;
12+ use PHPUnit \Framework \TestCase ;
13+
14+ class AccountingTest extends TestCase
15+ {
16+ private Accounting $ accounting ;
17+ private MockHandler $ mockHandler ;
18+ private string $ testToken = 'test-token-123 ' ;
19+
20+ protected function setUp (): void
21+ {
22+ parent ::setUp ();
23+
24+ // Create a mock handler for HTTP responses
25+ $ this ->mockHandler = new MockHandler ();
26+ $ handlerStack = HandlerStack::create ($ this ->mockHandler );
27+
28+ // Create a GuzzleHttp client with the mock handler
29+ $ guzzleClient = new GuzzleClient (['handler ' => $ handlerStack ]);
30+
31+ // Initialize the FireAPI client with the mocked GuzzleHttp client
32+ $ client = new Client ($ this ->testToken , false , $ guzzleClient );
33+
34+ // Create the Accounting instance with the mocked client
35+ $ this ->accounting = new Accounting ($ client );
36+ }
37+
38+ public function testGetInvoices (): void
39+ {
40+ // Mock response for getting invoices
41+ $ expectedResponse = [
42+ 'invoices ' => [
43+ [
44+ 'id ' => 1 ,
45+ 'number ' => 'INV-2024-001 ' ,
46+ 'amount ' => 100.00 ,
47+ 'status ' => 'paid '
48+ ]
49+ ]
50+ ];
51+
52+ $ this ->mockHandler ->append (
53+ new Response (
54+ 200 ,
55+ ['Content-Type ' => 'application/json ' ],
56+ json_encode ($ expectedResponse )
57+ )
58+ );
59+
60+ $ result = $ this ->accounting ->getInvoices ();
61+
62+ $ this ->assertEquals ($ expectedResponse , $ result );
63+ }
64+
65+ public function testGetInvoiceDetails (): void
66+ {
67+ // Mock response for getting a specific invoice
68+ $ invoiceId = 1 ;
69+ $ expectedResponse = [
70+ 'id ' => $ invoiceId ,
71+ 'number ' => 'INV-2024-001 ' ,
72+ 'amount ' => 100.00 ,
73+ 'status ' => 'paid ' ,
74+ 'items ' => [
75+ [
76+ 'description ' => 'Service A ' ,
77+ 'quantity ' => 1 ,
78+ 'price ' => 100.00
79+ ]
80+ ]
81+ ];
82+
83+ $ this ->mockHandler ->append (
84+ new Response (
85+ 200 ,
86+ ['Content-Type ' => 'application/json ' ],
87+ json_encode ($ expectedResponse )
88+ )
89+ );
90+
91+ $ result = $ this ->accounting ->getInvoice ($ invoiceId );
92+
93+ $ this ->assertEquals ($ expectedResponse , $ result );
94+ }
95+
96+ public function testCreateInvoice (): void
97+ {
98+ // Test data for creating an invoice
99+ $ invoiceData = [
100+ 'customer_id ' => 123 ,
101+ 'items ' => [
102+ [
103+ 'description ' => 'New Service ' ,
104+ 'quantity ' => 1 ,
105+ 'price ' => 150.00
106+ ]
107+ ]
108+ ];
109+
110+ // Mock response for invoice creation
111+ $ expectedResponse = [
112+ 'id ' => 2 ,
113+ 'status ' => 'created ' ,
114+ 'number ' => 'INV-2024-002 '
115+ ];
116+
117+ $ this ->mockHandler ->append (
118+ new Response (
119+ 201 ,
120+ ['Content-Type ' => 'application/json ' ],
121+ json_encode ($ expectedResponse )
122+ )
123+ );
124+
125+ $ result = $ this ->accounting ->createInvoice ($ invoiceData );
126+
127+ $ this ->assertEquals ($ expectedResponse , $ result );
128+ }
129+
130+ public function testUpdateInvoice (): void
131+ {
132+ // Test data for updating an invoice
133+ $ invoiceId = 1 ;
134+ $ updateData = [
135+ 'status ' => 'cancelled ' ,
136+ 'notes ' => 'Customer requested cancellation '
137+ ];
138+
139+ // Mock response for invoice update
140+ $ expectedResponse = [
141+ 'id ' => $ invoiceId ,
142+ 'status ' => 'cancelled ' ,
143+ 'notes ' => 'Customer requested cancellation '
144+ ];
145+
146+ $ this ->mockHandler ->append (
147+ new Response (
148+ 200 ,
149+ ['Content-Type ' => 'application/json ' ],
150+ json_encode ($ expectedResponse )
151+ )
152+ );
153+
154+ $ result = $ this ->accounting ->updateInvoice ($ invoiceId , $ updateData );
155+
156+ $ this ->assertEquals ($ expectedResponse , $ result );
157+ }
158+
159+ public function testDeleteInvoice (): void
160+ {
161+ $ invoiceId = 1 ;
162+
163+ // Mock response for invoice deletion
164+ $ this ->mockHandler ->append (
165+ new Response (
166+ 204 ,
167+ ['Content-Type ' => 'application/json ' ]
168+ )
169+ );
170+
171+ $ result = $ this ->accounting ->deleteInvoice ($ invoiceId );
172+
173+ $ this ->assertTrue ($ result );
174+ }
175+
176+ public function testInvalidInvoiceDataThrowsException (): void
177+ {
178+ // Test invalid invoice data handling
179+ $ invalidData = 'not an array ' ;
180+
181+ $ this ->expectException (ParameterException::class);
182+ $ this ->expectExceptionMessage ('Invoice data must be an array ' );
183+
184+ $ this ->accounting ->createInvoice ($ invalidData );
185+ }
186+
187+ public function testGetInvoiceFilters (): void
188+ {
189+ // Test getting invoices with filters
190+ $ filters = [
191+ 'status ' => 'paid ' ,
192+ 'date_from ' => '2024-01-01 ' ,
193+ 'date_to ' => '2024-12-31 '
194+ ];
195+
196+ $ expectedResponse = [
197+ 'invoices ' => [
198+ [
199+ 'id ' => 1 ,
200+ 'status ' => 'paid ' ,
201+ 'date ' => '2024-02-15 '
202+ ]
203+ ]
204+ ];
205+
206+ $ this ->mockHandler ->append (
207+ new Response (
208+ 200 ,
209+ ['Content-Type ' => 'application/json ' ],
210+ json_encode ($ expectedResponse )
211+ )
212+ );
213+
214+ $ result = $ this ->accounting ->getInvoices ($ filters );
215+
216+ $ this ->assertEquals ($ expectedResponse , $ result );
217+ }
218+
219+ public function testGetPayments (): void
220+ {
221+ // Mock response for getting payments
222+ $ expectedResponse = [
223+ 'payments ' => [
224+ [
225+ 'id ' => 1 ,
226+ 'invoice_id ' => 1 ,
227+ 'amount ' => 100.00 ,
228+ 'date ' => '2024-03-01 '
229+ ]
230+ ]
231+ ];
232+
233+ $ this ->mockHandler ->append (
234+ new Response (
235+ 200 ,
236+ ['Content-Type ' => 'application/json ' ],
237+ json_encode ($ expectedResponse )
238+ )
239+ );
240+
241+ $ result = $ this ->accounting ->getPayments ();
242+
243+ $ this ->assertEquals ($ expectedResponse , $ result );
244+ }
245+
246+ protected function tearDown (): void
247+ {
248+ parent ::tearDown ();
249+ $ this ->mockHandler = null ;
250+ $ this ->accounting = null ;
251+ }
252+ }
0 commit comments