-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathDocumentReferenceOrderTest.php
More file actions
194 lines (166 loc) · 6.5 KB
/
Copy pathDocumentReferenceOrderTest.php
File metadata and controls
194 lines (166 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace NumNum\UBL\Tests\Write;
use PHPUnit\Framework\TestCase;
/**
* UBL 2.1 declares the document reference elements in an xsd:sequence, and the
* required order differs per document type. Writing them in any other order
* produces a document that fails schema validation.
*/
class DocumentReferenceOrderTest extends TestCase
{
/**
* Every document reference element this library can write, applied to a
* document that is otherwise minimal but complete.
*
* @param \NumNum\UBL\Invoice $document
* @return \NumNum\UBL\Invoice
*/
private function withAllDocumentReferences($document)
{
$country = (new \NumNum\UBL\Country())
->setIdentificationCode('BE');
$address = (new \NumNum\UBL\Address())
->setStreetName('Korenmarkt')
->setCityName('Gent')
->setPostalZone('9000')
->setCountry($country);
$supplierCompany = (new \NumNum\UBL\Party())
->setName('Supplier Company Name')
->setPostalAddress($address);
$clientCompany = (new \NumNum\UBL\Party())
->setName('Client Company Name')
->setPostalAddress($address);
$legalMonetaryTotal = (new \NumNum\UBL\LegalMonetaryTotal())
->setPayableAmount(12.1)
->setTaxExclusiveAmount(10);
$price = (new \NumNum\UBL\Price())
->setPriceAmount(10)
->setUnitCode(\NumNum\UBL\UnitCode::UNIT);
$item = (new \NumNum\UBL\Item())
->setName('Product Name');
$invoiceLine = (new \NumNum\UBL\InvoiceLine())
->setId(1)
->setItem($item)
->setPrice($price)
->setLineExtensionAmount(10)
->setInvoicedQuantity(1);
return $document
->setId(1234)
->setIssueDate(new \DateTime())
->setAccountingSupplierParty(
(new \NumNum\UBL\AccountingParty())->setParty($supplierCompany)
)
->setAccountingCustomerParty(
(new \NumNum\UBL\AccountingParty())->setParty($clientCompany)
)
->setInvoiceLines([$invoiceLine])
->setLegalMonetaryTotal($legalMonetaryTotal)
->setOrderReference((new \NumNum\UBL\OrderReference())->setId('ORD-1'))
->setBillingReference(
(new \NumNum\UBL\BillingReference())->setInvoiceDocumentReference(
(new \NumNum\UBL\InvoiceDocumentReference())->setOriginalInvoiceId('PREC-1')
)
)
->setDespatchDocumentReference(
(new \NumNum\UBL\DespatchDocumentReference())->setId('DESP-1')
)
->setReceiptDocumentReference(
(new \NumNum\UBL\ReceiptDocumentReference())->setId('RCPT-1')
)
->setOriginatorDocumentReference(
(new \NumNum\UBL\OriginatorDocumentReference())->setId('ORIG-1')
)
->setContractDocumentReference(
(new \NumNum\UBL\ContractDocumentReference())->setId('CTR-1')
)
->setAdditionalDocumentReferences([
(new \NumNum\UBL\AdditionalDocumentReference())->setId('ADD-1'),
]);
}
/**
* The names of the direct child elements of the document that carry a
* reference, in the order they were written.
*
* @param string $xml
* @return string[]
*/
private function referenceElementsIn(string $xml): array
{
$dom = new \DOMDocument();
$dom->loadXML($xml);
$elements = [];
foreach ($dom->documentElement->childNodes as $node) {
if (!$node instanceof \DOMElement) {
continue;
}
if (substr($node->localName, -9) === 'Reference') {
$elements[] = $node->localName;
}
}
return $elements;
}
/** @test */
public function testInvoiceWritesDocumentReferencesInSchemaOrder()
{
$invoice = $this->withAllDocumentReferences(new \NumNum\UBL\Invoice());
$invoice->setProjectReference((new \NumNum\UBL\ProjectReference())->setId('PRJ-1'));
$xml = (new \NumNum\UBL\Generator())->invoice($invoice);
$this->assertEquals([
'OrderReference',
'BillingReference',
'DespatchDocumentReference',
'ReceiptDocumentReference',
'OriginatorDocumentReference',
'ContractDocumentReference',
'AdditionalDocumentReference',
'ProjectReference',
], $this->referenceElementsIn($xml));
}
/** @test */
public function testCreditNoteWritesDocumentReferencesInSchemaOrder()
{
$creditNote = $this->withAllDocumentReferences(new \NumNum\UBL\CreditNote());
$xml = (new \NumNum\UBL\Generator())->creditNote($creditNote);
// A CreditNote puts ContractDocumentReference before, and
// OriginatorDocumentReference after, AdditionalDocumentReference.
$this->assertEquals([
'OrderReference',
'BillingReference',
'DespatchDocumentReference',
'ReceiptDocumentReference',
'ContractDocumentReference',
'AdditionalDocumentReference',
'OriginatorDocumentReference',
], $this->referenceElementsIn($xml));
}
/** @test */
public function testDebitNoteWritesDocumentReferencesInSchemaOrder()
{
$debitNote = $this->withAllDocumentReferences(new \NumNum\UBL\DebitNote());
$xml = (new \NumNum\UBL\Generator())->debitNote($debitNote);
$this->assertEquals([
'OrderReference',
'BillingReference',
'DespatchDocumentReference',
'ReceiptDocumentReference',
'ContractDocumentReference',
'AdditionalDocumentReference',
'OriginatorDocumentReference',
], $this->referenceElementsIn($xml));
}
/** @test */
public function testOmittedDocumentReferencesAreNotWritten()
{
$invoice = $this->withAllDocumentReferences(new \NumNum\UBL\Invoice())
->setDespatchDocumentReference(null)
->setOriginatorDocumentReference(null)
->setAdditionalDocumentReferences([]);
$xml = (new \NumNum\UBL\Generator())->invoice($invoice);
$this->assertEquals([
'OrderReference',
'BillingReference',
'ReceiptDocumentReference',
'ContractDocumentReference',
], $this->referenceElementsIn($xml));
}
}