From 6be3b350127d38f61ec3b5d0771c792df238a52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schadegg=20Br=C3=B8nniche?= Date: Tue, 20 Feb 2024 14:00:27 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Updated=20and=20added=20invoice=20t?= =?UTF-8?q?ests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Fixtures/Invoices/draft/book-request.json | 5 + .../Invoices/draft/book-response.json | 69 ++++ .../Invoices/draft/create-request.json | 34 ++ .../Invoices/draft/create-response.json | 79 +++++ .../Invoices/draft/get-collection.json | 208 +++++++++++ tests/Fixtures/Invoices/draft/get-single.json | 133 +++++++ tests/Fixtures/Invoices/draft/update.json | 0 tests/Unit/InvoiceTest.php | 334 +++++++++--------- 8 files changed, 689 insertions(+), 173 deletions(-) create mode 100644 tests/Fixtures/Invoices/draft/book-request.json create mode 100644 tests/Fixtures/Invoices/draft/book-response.json create mode 100644 tests/Fixtures/Invoices/draft/create-request.json create mode 100644 tests/Fixtures/Invoices/draft/create-response.json create mode 100644 tests/Fixtures/Invoices/draft/get-collection.json create mode 100644 tests/Fixtures/Invoices/draft/get-single.json create mode 100644 tests/Fixtures/Invoices/draft/update.json diff --git a/tests/Fixtures/Invoices/draft/book-request.json b/tests/Fixtures/Invoices/draft/book-request.json new file mode 100644 index 0000000..700def9 --- /dev/null +++ b/tests/Fixtures/Invoices/draft/book-request.json @@ -0,0 +1,5 @@ +{ + "draftInvoice": { + "draftInvoiceNumber": 424 + } +} diff --git a/tests/Fixtures/Invoices/draft/book-response.json b/tests/Fixtures/Invoices/draft/book-response.json new file mode 100644 index 0000000..016c45a --- /dev/null +++ b/tests/Fixtures/Invoices/draft/book-response.json @@ -0,0 +1,69 @@ +{ + "bookedInvoiceNumber": 300, + "orderNumber": 424, + "date": "2024-02-13", + "currency": "DKK", + "exchangeRate": 100.000000, + "netAmount": 500.00, + "netAmountInBaseCurrency": 500.00, + "grossAmount": 625.00, + "grossAmountInBaseCurrency": 625.00, + "vatAmount": 125.00, + "roundingAmount": 0.00, + "remainder": 625.00, + "remainderInBaseCurrency": 625.00, + "dueDate": "2024-02-21", + "paymentTerms": { + "paymentTermsNumber": 1, + "daysOfCredit": 8, + "name": "Netto 8 dage", + "paymentTermsType": "net", + "self": "https://restapi.e-conomic.com/payment-terms/1" + }, + "customer": { + "customerNumber": 1, + "self": "https://restapi.e-conomic.com/customers/1" + }, + "recipient": { + "name": "John Doe", + "vatZone": { + "name": "Domestic", + "vatZoneNumber": 1, + "enabledForCustomer": true, + "enabledForSupplier": true, + "self": "https://restapi.e-conomic.com/vat-zones/1" + } + }, + "notes": { + "heading": "Heading", + "textLine1": "Text line 1", + "textLine2": "Text line 2" + }, + "layout": { + "layoutNumber": 14, + "self": "https://restapi.e-conomic.com/layouts/14" + }, + "pdf": { + "download": "https://restapi.e-conomic.com/invoices/booked/300/pdf" + }, + "lines": [ + { + "lineNumber": 1, + "sortKey": 1, + "description": "T-shirt - Size L", + "quantity": 1.00, + "unitNetPrice": 500.00, + "discountPercentage": 0.00, + "unitCostPrice": 800.00, + "vatRate": 25.00000000, + "vatAmount": 125.0000, + "totalNetAmount": 500.00, + "product": { + "productNumber": "1", + "self": "https://restapi.e-conomic.com/products/1" + } + } + ], + "sent": "https://restapi.e-conomic.com/invoices/booked/300/sent", + "self": "https://restapi.e-conomic.com/invoices/booked/300" +} diff --git a/tests/Fixtures/Invoices/draft/create-request.json b/tests/Fixtures/Invoices/draft/create-request.json new file mode 100644 index 0000000..a5da1f9 --- /dev/null +++ b/tests/Fixtures/Invoices/draft/create-request.json @@ -0,0 +1,34 @@ +{ + "currency": "DKK", + "customer": { + "customerNumber": 1 + }, + "date": "2024-02-13T12:20:18+00:00", + "layout": { + "layoutNumber": 14 + }, + "paymentTerms": { + "paymentTermsNumber": 1 + }, + "recipient": { + "name": "John Doe", + "vatZone": { + "vatZoneNumber": 1 + } + }, + "lines": [ + { + "description": "T-shirt - Size L", + "product": { + "productNumber": "1" + }, + "quantity": 1, + "unitNetPrice": 500 + } + ], + "notes": { + "heading": "Heading", + "textLine1": "Text line 1", + "textLine2": "Text line 2" + } +} diff --git a/tests/Fixtures/Invoices/draft/create-response.json b/tests/Fixtures/Invoices/draft/create-response.json new file mode 100644 index 0000000..20a4b05 --- /dev/null +++ b/tests/Fixtures/Invoices/draft/create-response.json @@ -0,0 +1,79 @@ +{ + "draftInvoiceNumber": 424, + "soap": { + "currentInvoiceHandle": { + "id": 133 + } + }, + "templates": { + "bookingInstructions": "https://restapi.e-conomic.com/invoices/drafts/424/templates/booking-instructions", + "self": "https://restapi.e-conomic.com/invoices/drafts/424/templates" + }, + "attachment": "https://restapi.e-conomic.com/invoices/drafts/424/attachment", + "lines": [ + { + "lineNumber": 1, + "sortKey": 1, + "description": "T-shirt - Size L", + "product": { + "productNumber": "1", + "self": "https://restapi.e-conomic.com/products/1" + }, + "quantity": 1.00, + "unitNetPrice": 500.00, + "discountPercentage": 0.00, + "unitCostPrice": 800.00, + "totalNetAmount": 500.00, + "marginInBaseCurrency": -300.00, + "marginPercentage": -60.00 + } + ], + "date": "2024-02-13", + "currency": "DKK", + "exchangeRate": 100.000000, + "netAmount": 500.000000, + "netAmountInBaseCurrency": 500.00, + "grossAmount": 625.000000, + "grossAmountInBaseCurrency": 625.00, + "marginInBaseCurrency": -300.0000, + "marginPercentage": -60.00, + "vatAmount": 125.000000, + "roundingAmount": 0.00, + "costPriceInBaseCurrency": 800.0000, + "dueDate": "2024-02-21", + "paymentTerms": { + "paymentTermsNumber": 1, + "daysOfCredit": 8, + "name": "Netto 8 dage", + "paymentTermsType": "net", + "self": "https://restapi.e-conomic.com/payment-terms/1" + }, + "customer": { + "customerNumber": 1, + "self": "https://restapi.e-conomic.com/customers/1" + }, + "recipient": { + "name": "John Doe", + "vatZone": { + "name": "Domestic", + "vatZoneNumber": 1, + "enabledForCustomer": true, + "enabledForSupplier": true, + "self": "https://restapi.e-conomic.com/vat-zones/1" + } + }, + "notes": { + "heading": "Heading", + "textLine1": "Text line 1", + "textLine2": "Text line 2" + }, + "layout": { + "layoutNumber": 14, + "self": "https://restapi.e-conomic.com/layouts/14" + }, + "pdf": { + "download": "https://restapi.e-conomic.com/invoices/drafts/424/pdf" + }, + "lastUpdated": "2024-02-13T12:20:00Z", + "self": "https://restapi.e-conomic.com/invoices/drafts/424" +} diff --git a/tests/Fixtures/Invoices/draft/get-collection.json b/tests/Fixtures/Invoices/draft/get-collection.json new file mode 100644 index 0000000..39d02f4 --- /dev/null +++ b/tests/Fixtures/Invoices/draft/get-collection.json @@ -0,0 +1,208 @@ +{ + "collection": [ + { + "draftInvoiceNumber": 422, + "externalId": "123456", + "soap": { + "currentInvoiceHandle": { + "id": 131 + } + }, + "templates": { + "bookingInstructions": "https://restapi.e-conomic.com/invoices/drafts/422/templates/booking-instructions", + "self": "https://restapi.e-conomic.com/invoices/drafts/422/templates" + }, + "attachment": "https://restapi.e-conomic.com/invoices/drafts/422/attachment", + "lines": [ + { + "lineNumber": 1, + "sortKey": 1, + "description": "Test Vare 1", + "unit": { + "unitNumber": 2, + "name": "Stk.", + "products": "https://restapi.e-conomic.com/units/2/products", + "self": "https://restapi.e-conomic.com/units/2" + }, + "product": { + "productNumber": "1", + "self": "https://restapi.e-conomic.com/products/1" + }, + "quantity": 1.00, + "unitNetPrice": 1000.00, + "discountPercentage": 0.00, + "unitCostPrice": 800.00, + "totalNetAmount": 1000.00, + "marginInBaseCurrency": 200.00, + "marginPercentage": 20.00 + }, + { + "lineNumber": 2, + "sortKey": 2, + "description": "Test Vare 2", + "unit": { + "unitNumber": 2, + "name": "Stk.", + "products": "https://restapi.e-conomic.com/units/2/products", + "self": "https://restapi.e-conomic.com/units/2" + }, + "product": { + "productNumber": "2", + "self": "https://restapi.e-conomic.com/products/2" + }, + "quantity": 2.00, + "unitNetPrice": 850.00, + "discountPercentage": 10.00, + "unitCostPrice": 0.00, + "totalNetAmount": 1530.00, + "marginInBaseCurrency": 1530.00, + "marginPercentage": 100.00 + } + ], + "date": "2023-12-07", + "currency": "DKK", + "exchangeRate": 100.000000, + "netAmount": 2530.000000, + "netAmountInBaseCurrency": 2530.00, + "grossAmount": 3162.500000, + "grossAmountInBaseCurrency": 3162.50, + "marginInBaseCurrency": 1730.0000, + "marginPercentage": 68.38, + "vatAmount": 632.500000, + "roundingAmount": 0.00, + "costPriceInBaseCurrency": 800.0000, + "dueDate": "2023-12-15", + "paymentTerms": { + "paymentTermsNumber": 1, + "daysOfCredit": 8, + "name": "Netto 8 dage", + "paymentTermsType": "net", + "self": "https://restapi.e-conomic.com/payment-terms/1" + }, + "customer": { + "customerNumber": 500000011, + "self": "https://restapi.e-conomic.com/customers/500000011" + }, + "recipient": { + "name": "John Doe", + "address": "Teststreet 1", + "zip": "5000", + "city": "Odense C", + "country": "Denmark", + "vatZone": { + "name": "Domestic", + "vatZoneNumber": 1, + "enabledForCustomer": true, + "enabledForSupplier": true, + "self": "https://restapi.e-conomic.com/vat-zones/1" + }, + "nemHandelType": "corporateIdentificationNumber", + "cvr": "33362749" + }, + "notes": { + "heading": "Test Headline", + "textLine1": "Test Note 1", + "textLine2": "Test Note 2" + }, + "references": { + "customerContact": { + "customerContactNumber": 132, + "customer": { + "customerNumber": 500000011, + "self": "https://restapi.e-conomic.com/customers/500000011" + }, + "self": "https://restapi.e-conomic.com/customers/500000011/contacts/132" + }, + "salesPerson": { + "employeeNumber": 1001, + "self": "https://restapi.e-conomic.com/employees/1001" + }, + "vendorReference": { + "employeeNumber": 100, + "self": "https://restapi.e-conomic.com/employees/100" + }, + "other": "Test Other reference" + }, + "layout": { + "layoutNumber": 14, + "self": "https://restapi.e-conomic.com/layouts/14" + }, + "pdf": { + "download": "https://restapi.e-conomic.com/invoices/drafts/422/pdf" + }, + "lastUpdated": "2024-02-13T10:44:00Z", + "self": "https://restapi.e-conomic.com/invoices/drafts/422" + }, + { + "draftInvoiceNumber": 2, + "soap": { + "currentInvoiceHandle": { + "id": 2 + } + }, + "templates": { + "bookingInstructions": "https://restapi.e-conomic.com/invoices/drafts/2/templates/booking-instructions", + "self": "https://restapi.e-conomic.com/invoices/drafts/2/templates" + }, + "attachment": "https://restapi.e-conomic.com/invoices/drafts/2/attachment", + "date": "2019-12-03", + "currency": "DKK", + "exchangeRate": 100.000000, + "netAmount": 0.000000, + "netAmountInBaseCurrency": 0.00, + "grossAmount": 0.000000, + "grossAmountInBaseCurrency": 0.00, + "marginInBaseCurrency": 0.0000, + "marginPercentage": 0.0, + "vatAmount": 0.000000, + "roundingAmount": 0.00, + "costPriceInBaseCurrency": 0.0000, + "dueDate": "2020-01-30", + "paymentTerms": { + "paymentTermsNumber": 2, + "daysOfCredit": 30, + "name": "Lb. md. 30 dage", + "paymentTermsType": "invoiceMonth", + "self": "https://restapi.e-conomic.com/payment-terms/2" + }, + "customer": { + "customerNumber": 4, + "self": "https://restapi.e-conomic.com/customers/4" + }, + "recipient": { + "name": "James Smith", + "address": "Teststreet 3", + "zip": "5000", + "city": "Odense", + "country": "DK", + "vatZone": { + "name": "EU", + "vatZoneNumber": 2, + "enabledForCustomer": true, + "enabledForSupplier": true, + "self": "https://restapi.e-conomic.com/vat-zones/2" + } + }, + "notes": { + "heading": "Notes", + "textLine1": "Note 1" + }, + "layout": { + "layoutNumber": 14, + "self": "https://restapi.e-conomic.com/layouts/14" + }, + "pdf": { + "download": "https://restapi.e-conomic.com/invoices/drafts/2/pdf" + }, + "lastUpdated": "2021-02-23T11:19:00Z", + "self": "https://restapi.e-conomic.com/invoices/drafts/2" + } + ], + "pagination": { + "skipPages": 0, + "pageSize": 20, + "maxPageSizeAllowed": 1000, + "results": 2 + }, + "self": "https://restapi.e-conomic.com/invoices/drafts?skipPages=0&pageSize=20" +} diff --git a/tests/Fixtures/Invoices/draft/get-single.json b/tests/Fixtures/Invoices/draft/get-single.json new file mode 100644 index 0000000..8eef396 --- /dev/null +++ b/tests/Fixtures/Invoices/draft/get-single.json @@ -0,0 +1,133 @@ +{ + "draftInvoiceNumber": 422, + "externalId": "123456", + "soap": { + "currentInvoiceHandle": { + "id": 131 + } + }, + "templates": { + "bookingInstructions": "https://restapi.e-conomic.com/invoices/drafts/422/templates/booking-instructions", + "self": "https://restapi.e-conomic.com/invoices/drafts/422/templates" + }, + "attachment": "https://restapi.e-conomic.com/invoices/drafts/422/attachment", + "lines": [ + { + "lineNumber": 1, + "sortKey": 1, + "description": "Test Vare 1", + "unit": { + "unitNumber": 2, + "name": "Stk.", + "products": "https://restapi.e-conomic.com/units/2/products", + "self": "https://restapi.e-conomic.com/units/2" + }, + "product": { + "productNumber": "1", + "self": "https://restapi.e-conomic.com/products/1" + }, + "quantity": 1.00, + "unitNetPrice": 1000.00, + "discountPercentage": 0.00, + "unitCostPrice": 800.00, + "totalNetAmount": 1000.00, + "marginInBaseCurrency": 200.00, + "marginPercentage": 20.00 + }, + { + "lineNumber": 2, + "sortKey": 2, + "description": "Test Vare 2", + "unit": { + "unitNumber": 2, + "name": "Stk.", + "products": "https://restapi.e-conomic.com/units/2/products", + "self": "https://restapi.e-conomic.com/units/2" + }, + "product": { + "productNumber": "2", + "self": "https://restapi.e-conomic.com/products/2" + }, + "quantity": 2.00, + "unitNetPrice": 850.00, + "discountPercentage": 10.00, + "unitCostPrice": 0.00, + "totalNetAmount": 1530.00, + "marginInBaseCurrency": 1530.00, + "marginPercentage": 100.00 + } + ], + "date": "2023-12-07", + "currency": "DKK", + "exchangeRate": 100.000000, + "netAmount": 2530.000000, + "netAmountInBaseCurrency": 2530.00, + "grossAmount": 3162.500000, + "grossAmountInBaseCurrency": 3162.50, + "marginInBaseCurrency": 1730.0000, + "marginPercentage": 68.38, + "vatAmount": 632.500000, + "roundingAmount": 0.00, + "costPriceInBaseCurrency": 800.0000, + "dueDate": "2023-12-15", + "paymentTerms": { + "paymentTermsNumber": 1, + "daysOfCredit": 8, + "name": "Netto 8 dage", + "paymentTermsType": "net", + "self": "https://restapi.e-conomic.com/payment-terms/1" + }, + "customer": { + "customerNumber": 500000011, + "self": "https://restapi.e-conomic.com/customers/500000011" + }, + "recipient": { + "name": "John Doe", + "address": "Teststreet 1", + "zip": "5000", + "city": "Odense C", + "country": "Denmark", + "vatZone": { + "name": "Domestic", + "vatZoneNumber": 1, + "enabledForCustomer": true, + "enabledForSupplier": true, + "self": "https://restapi.e-conomic.com/vat-zones/1" + }, + "nemHandelType": "corporateIdentificationNumber", + "cvr": "33362749" + }, + "notes": { + "heading": "Test Headline", + "textLine1": "Test Note 1", + "textLine2": "Test Note 2" + }, + "references": { + "customerContact": { + "customerContactNumber": 132, + "customer": { + "customerNumber": 500000011, + "self": "https://restapi.e-conomic.com/customers/500000011" + }, + "self": "https://restapi.e-conomic.com/customers/500000011/contacts/132" + }, + "salesPerson": { + "employeeNumber": 1001, + "self": "https://restapi.e-conomic.com/employees/1001" + }, + "vendorReference": { + "employeeNumber": 100, + "self": "https://restapi.e-conomic.com/employees/100" + }, + "other": "Test Other reference" + }, + "layout": { + "layoutNumber": 14, + "self": "https://restapi.e-conomic.com/layouts/14" + }, + "pdf": { + "download": "https://restapi.e-conomic.com/invoices/drafts/422/pdf" + }, + "lastUpdated": "2024-02-13T10:44:00Z", + "self": "https://restapi.e-conomic.com/invoices/drafts/422" +} diff --git a/tests/Fixtures/Invoices/draft/update.json b/tests/Fixtures/Invoices/draft/update.json new file mode 100644 index 0000000..e69de29 diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 6c83b83..3487887 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -1,161 +1,206 @@ driver->expects()->post() - ->withArgs(function (string $url, array $body) { - return $url === 'https://restapi.e-conomic.com/invoices/drafts' - && $body === [ - 'customer' => [ - 'customerNumber' => 1, - ], - 'layout' => [ - 'layoutNumber' => 1, - ], - 'currency' => 'DKK', - 'paymentTerms' => [ - 'paymentTermsNumber' => 1, - 'paymentTermsType' => 'paidInCash', - ], - 'date' => '2021-01-01', - 'recipient' => [ - 'name' => 'John Doe', - 'vatZone' => [ - 'vatZoneNumber' => 1, - ], - ], - 'lines' => [ - [ - 'description' => 'T-shirt - Size L', - 'product' => [ - 'productNumber' => '1', - ], - 'quantity' => 1, - 'unitNetPrice' => 500, - ], - ], - 'notes' => [ - 'heading' => 'Heading', - 'textLine1' => 'Text line 1', - 'textLine2' => 'Text line 2', - ], - ]; - }) - ->once() - ->andReturn(new EconomicResponse(201, [ - 'draftInvoiceNumber' => 1, - 'recipient' => [ - 'name' => 'John Doe', - 'vatZone' => [ - 'vatZoneNumber' => 1, - ], - ], - ])); +it('gets a drafted invoice', function() { + $this->driver->expects()->get( + 'https://restapi.e-conomic.com/invoices/drafts/422', + [] + )->andReturn(new EconomicResponse(200, fixture('Invoices/draft/get-single'))); + + $invoice = DraftInvoice::find(422); + + expect($invoice) + ->toBeInstanceOf(DraftInvoice::class) + ->draftInvoiceNumber->toBe(422) + ->externalId->toBe('123456') + ->date->toBeInstanceOf(DateTime::class) + ->currency->toBe('DKK') + ->exchangeRate->toBeFloat() + ->netAmount->toBeFloat() + ->grossAmount->toBeFloat() + ->grossAmountInBaseCurrency->toBeFloat() + ->marginInBaseCurrency->toBeFloat() + ->marginPercentage->toBeFloat() + ->vatAmount->toBeFloat() + ->roundingAmount->toBeFloat() + ->costPriceInBaseCurrency->toBeFloat() + ->dueDate->toBeInstanceOf(DateTime::class) + ->paymentTerms->toBeInstanceOf(PaymentTerm::class) + ->customer->toBeInstanceOf(Customer::class) + ->recipient->toBeInstanceOf(Recipient::class) + ->notes->toBeInstanceOf(Note::class) + ->references->toBeInstanceOf(Reference::class) + ->layout->toBeInstanceOf(Layout::class) + ->paymentTerms->toBeInstanceOf(PaymentTerm::class) + ->lines->toBeInstanceOf(Collection::class) + ->lines->first()->toBeInstanceOf(ProductLine::class); +}); + +it('gets all drafted invoices', function() { + $this->driver->expects()->get( + 'https://restapi.e-conomic.com/invoices/drafts', + [ + 'pageSize' => 20, + 'skipPages' => 0, + ] + )->andReturn(new EconomicResponse(200, fixture('Invoices/draft/get-collection'))); + + $invoices = DraftInvoice::all(); + + expect($invoices)->toBeInstanceOf(EconomicCollection::class); + expect($invoices->first())->toBeInstanceOf(DraftInvoice::class); + expect($invoices->all())->toHaveCount(2); +}); + +it('creates a draft invoice using create', function() { + $this->driver->expects()->post()->with( + 'https://restapi.e-conomic.com/invoices/drafts', + fixture('Invoices/draft/create-request') + )->andReturn(new EconomicResponse(201, fixture('Invoices/draft/create-response'))); + - DraftInvoice::new( + $invoice = DraftInvoice::create( + 'DKK', 1, + new DateTime('2024-02-13T12:20:18+00:00'), + 14, 1, - 'DKK', - PaymentTerm::new( - paymentTermsNumber: 1, - paymentTermsType: PaymentTermsType::PAID_IN_CASH, + Recipient::new( + 'John Doe', + new VatZone(1), ), - DateTime::createFromFormat('Y-m-d', '2021-01-01'), + [ + ProductLine::new( + description: 'T-shirt - Size L', + product: new Product([ + 'productNumber' => 1, + ]), + quantity: 1, + unitNetPrice: 500 + ) + ], + notes: Note::new( + heading: 'Heading', + textLine1: 'Text line 1', + textLine2: 'Text line 2' + ) + ); + + expect($invoice) + ->toBeInstanceOf(DraftInvoice::class) + ->draftInvoiceNumber->toBe(424); +}); + +it('creates a draft invoice using new and save', function() { + $this->driver->expects()->post()->with( + 'https://restapi.e-conomic.com/invoices/drafts', + fixture('Invoices/draft/create-request') + )->andReturn(new EconomicResponse(201, fixture('Invoices/draft/create-response'))); + + $invoice = DraftInvoice::new( + 'DKK', + 1, + new DateTime('2024-02-13T12:20:18+00:00'), + 14, + 1, Recipient::new( 'John Doe', new VatZone(1), ), - Note::new( + notes: Note::new( heading: 'Heading', textLine1: 'Text line 1', textLine2: 'Text line 2' ) - ) - ->addLine(ProductLine::new( + ); + + $invoice->addLine( + ProductLine::new( description: 'T-shirt - Size L', product: new Product([ 'productNumber' => 1, ]), quantity: 1, unitNetPrice: 500 - )) - ->create(); + ) + ); + + $invoice->save(); + + expect($invoice) + ->toBeInstanceOf(DraftInvoice::class) + ->draftInvoiceNumber->toBe(424); }); it('books draft invoice', function () { - $this->driver->expects()->post() - ->withArgs(function (string $url, array $body) { - return $url === 'https://restapi.e-conomic.com/invoices/drafts'; - }) + $this->driver->expects()->post( + 'https://restapi.e-conomic.com/invoices/booked', + fixture('Invoices/draft/book-request') + ) ->once() - ->andReturn(new EconomicResponse(201, [ - 'draftInvoiceNumber' => 1, - ])); + ->andReturn(new EconomicResponse( + 201, + fixture('Invoices/draft/book-response') + )); - $this->driver->expects()->post() - ->withArgs(function (string $url, array $body) { - return $url === 'https://restapi.e-conomic.com/invoices/booked' - && $body === [ - 'draftInvoice' => [ - 'draftInvoiceNumber' => 1, - ], - ]; - }) - ->once() - ->andReturn(new EconomicResponse(201, [])); + $invoice = new DraftInvoice(424); - DraftInvoice::new( - 1, - 1, - 'DKK', - 1, - DateTime::createFromFormat('Y-m-d', '2021-01-01'), - new Recipient( - 'John Doe', - new VatZone(1), - ) - ) - ->create() - ->book(); + $bookedInvoice = $invoice->book(); + + expect($bookedInvoice) + ->toBeInstanceOf(BookedInvoice::class) + ->bookedInvoiceNumber->toBe(300); }); it('can add lines', function () { - $invoice = DraftInvoice::new( + $invoice = DraftInvoice::new('DKK', 1, + new DateTime('2024-02-13T12:20:18+00:00'), + 14, 1, - 'DKK', - 1, - DateTime::createFromFormat('Y-m-d', '2021-01-01'), - new Recipient( + Recipient::new( 'John Doe', new VatZone(1), ) - ) - ->addLine(ProductLine::new( + ); + + $invoice->addLine(ProductLine::new( product: new Product(1), quantity: 1, unitNetPrice: 500 - )) - ->addLine(ProductLine::new( - product: new Product(2), - quantity: 5, - unitNetPrice: 100, + ))->addLines([ + [ + 'product' => new Product(2), + 'quantity' => 2, + 'unitNetPrice' => 100, + 'description' => 'Some description' + ], + ProductLine::new( + product: new Product(3), + quantity: 3, + unitNetPrice: 200, description: 'Some description', - )); + ) + ]); expect($invoice->lines) - ->toBeArray() - ->toHaveCount(2); + ->toBeInstanceOf(Collection::class) + ->toHaveCount(3); expect($invoice->lines[0]) ->toBeInstanceOf(ProductLine::class) @@ -173,78 +218,21 @@ ->marginInBaseCurrency->toBeNull() ->marginPercentage->toBeNull() ->product->toBeInstanceOf(Product::class) - ->quantity->toBe(5.0); -}); - -it('returns expected booked invoice data', function () { - $this->driver->expects()->post() - ->withArgs(function (string $url, array $body) { - return $url === 'https://restapi.e-conomic.com/invoices/booked' - && $body === [ - 'draftInvoice' => [ - 'draftInvoiceNumber' => 1, - ], - ]; - }) - ->once() - ->andReturn(new EconomicResponse(201, [ - 'bookedInvoiceNumber' => 1, - ])); - - $bookedInvoice = BookedInvoice::createFromDraft(1); - - expect($bookedInvoice) - ->bookedInvoiceNumber->toBe(1); -}); - -it('handles populating resource with enum value', function () { - $this->driver->expects()->post() - ->withAnyArgs() - ->once() - ->andReturn(new EconomicResponse(201, [ - 'draftInvoiceNumber' => 1, - 'recipient' => [ - 'name' => 'John Doe', - 'vatZone' => [ - 'vatZoneNumber' => 1, - ], - ], - 'paymentTerms' => [ - 'paymentTermsNumber' => 1, - 'paymentTermsType' => 'net', - ], - ]), new EconomicResponse(201, [ - 'bookedInvoiceNumber' => 1, - ])); - - DraftInvoice::new( - 1, - 1, - 'DKK', - PaymentTerm::new( - paymentTermsNumber: 1, - ), - DateTime::createFromFormat('Y-m-d', '2021-01-01'), - Recipient::new( - 'John Doe', - new VatZone(1), - ) - ) - ->create(); + ->quantity->toBe(2.0); }); it('can add notes', function () { $invoice = DraftInvoice::new( - 1, - 1, 'DKK', 1, - DateTime::createFromFormat('Y-m-d', '2021-01-01'), - new Recipient( + new DateTime('2024-02-13T12:20:18+00:00'), + 14, + 1, + Recipient::new( 'John Doe', new VatZone(1), ), - Note::new( + notes: Note::new( heading: 'Heading', textLine1: 'Text line 1', textLine2: 'Text line 2'