/
DTOTransactionMosaicTransferTest.php
206 lines (170 loc) · 6.65 KB
/
DTOTransactionMosaicTransferTest.php
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
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Part of the evias/nem-php package.
*
* NOTICE OF LICENSE
*
* Licensed under MIT License.
*
* This source file is subject to the MIT License that is
* bundled with this package in the LICENSE file.
*
* @package evias/nem-php
* @version 1.0.0
* @author Grégory Saive <greg@evias.be>
* @license MIT License
* @copyright (c) 2017-2018, Grégory Saive <greg@evias.be>
* @link http://github.com/evias/nem-php
*/
namespace NEM\Tests\SDK\NIS\DTO;
use NEM\Tests\SDK\NIS\NISComplianceTestCase;
use NEM\Models\Transaction;
use NEM\Models\Fee;
use NEM\Models\Amount;
use NEM\Models\Mosaic;
use NEM\Models\MosaicAttachment;
use NEM\Models\MosaicAttachments;
use NEM\Models\TransactionType;
use NEM\Models\Transaction\MosaicTransfer;
use NEM\Models\Transaction\Transfer;
class DTOTransactionMosaicTransferTest
extends NISComplianceTestCase
{
/**
* Unit test for *empty mosaics field in MosaicTransfer object*.
*
* @return void
*/
public function testDefaultValuesForMosaicTransfer()
{
$transaction = new MosaicTransfer();
$transactionNIS = $transaction->toDTO();
$meta = $transactionNIS["meta"];
$content = $transactionNIS["transaction"];
// expected *default* values
$expectType = TransactionType::TRANSFER;
$expectVersion = Transaction::VERSION_2_TEST; // MOSAIC TRANSFER
// base transaction details test
$this->assertEquals($expectType, $content["type"]);
$this->assertEquals($expectVersion, $content["version"]);
// mosaics field should always be returned
$this->assertArrayHasKey("mosaics", $content);
$this->assertEmpty($content["mosaics"]);
}
/**
* Unit test for *DTO structure of MosaicTransfer instances*
*
* @depends testDefaultValuesForMosaicTransfer
* @return void
*/
public function testDTOStructure()
{
// test obligatory fields
$attachDim = new MosaicAttachment([
"mosaicId" => (new Mosaic([
"namespaceId" => "dim",
"name" => "coin"
]))->toDTO(),
"quantity" => 100,
]);
$txData = [
"mosaics" => (new MosaicAttachments([$attachDim]))->toDTO(),
];
$transaction = new MosaicTransfer($txData);
$transactionNIS = $transaction->toDTO();
$meta = $transactionNIS["meta"];
$content = $transactionNIS["transaction"];
// test attachment structure
$attachmentNIS = array_shift($content["mosaics"]);
$this->assertArrayHasKey("mosaicId", $attachmentNIS);
$this->assertArrayHasKey("quantity", $attachmentNIS);
$this->assertArrayHasKey("namespaceId", $attachmentNIS["mosaicId"]);
$this->assertArrayHasKey("name", $attachmentNIS["mosaicId"]);
$mosaicNIS = $attachmentNIS["mosaicId"];
// expected *specialized* values (attachment content)
$expectNamespace = "dim";
$expectMosaic = "coin";
$expectQuantity = 100;
$this->assertEquals($expectNamespace, $mosaicNIS["namespaceId"]);
$this->assertEquals($expectMosaic, $mosaicNIS["name"]);
$this->assertEquals($expectQuantity, $attachmentNIS["quantity"]);
}
/**
* Unit test for *attachMosaic function should attach a mosaic
* to the attachments collection*.
*
* @depends testDTOStructure
* @return void
*/
public function testAttachMosaicPushesToAttachments()
{
// test obligatory fields
$attachDim = new MosaicAttachment([
"mosaicId" => (new Mosaic([
"namespaceId" => "dim",
"name" => "coin"
]))->toDTO(),
"quantity" => 100,
]);
// intentionally exclude "mosaics" field data.
// use attachMosaic to test behaviour
$transaction = new MosaicTransfer();
$transaction->attachMosaic($attachDim);
$this->assertEquals(1, $transaction->mosaics()->count());
$attachmentsNIS = $transaction->mosaics()->toDTO();
// test internal attachment storage
$this->assertCount(1, $attachmentsNIS);
$this->assertArrayHasKey("mosaicId", $attachmentsNIS[0]);
$this->assertArrayHasKey("quantity", $attachmentsNIS[0]);
// test content
$this->assertEquals(100, $attachmentsNIS[0]["quantity"]);
$mosaic = $attachmentsNIS[0]["mosaicId"];
$this->assertArrayHasKey("namespaceId", $mosaic);
$this->assertArrayHasKey("name", $mosaic);
$this->assertEquals("dim", $mosaic["namespaceId"]);
$this->assertEquals("coin", $mosaic["name"]);
}
/**
* Unit test for *fee change corresponding to the Transaction
* specialization*, here MosaicTransfer.
*
* @return void
*/
public function testMosaicTransferFees()
{
$normalTransfer = new Transfer();
$mosaicTransfer = new MosaicTransfer();
$normalNIS = $normalTransfer->toDTO();
$mosaicNIS = $mosaicTransfer->toDTO();
// if no data is set, the fee will be the same
$this->assertEquals($normalNIS["transaction"]["fee"], $mosaicNIS["transaction"]["fee"]);
// add attachments
$mosaicTransfer->attachMosaic("dim:coin", 29);
$mosaicTransfer->attachMosaic("nem:xem", 8);
// now should contain mosaics
$mosaicNIS = $mosaicTransfer->toDTO();
$metaTx = $mosaicNIS["meta"];
$contentTx = $mosaicNIS["transaction"];
// NIS fee is `count_attachments * fee_factor`
$expectFee = 2 * Fee::FEE_FACTOR; // 0.10 XEM
$this->assertArrayHasKey("mosaics", $contentTx);
$this->assertNotEmpty($contentTx["mosaics"]);
$this->assertNotEquals($normalNIS["transaction"]["fee"], $contentTx["fee"]);
$this->assertEquals($expectFee, $contentTx["fee"]);
// one more mosaic to be sure nothing else triggers the fee change
$mosaicTransfer_2 = new MosaicTransfer();
$mosaicTransfer_2->attachMosaic("dim:coin", 29);
$mosaicTransfer_2->attachMosaic("nem:xem", 8);
$mosaicTransfer_2->attachMosaic("dim:token", 10);
// NIS fee is `count_attachments * fee_factor`
$expectFee_2 = 3 * Fee::FEE_FACTOR; // 0.15 XEM
// get content transcribed
$mosaicNIS_2 = $mosaicTransfer_2->toDTO();
$metaTx_2 = $mosaicNIS_2["meta"];
$contentTx_2 = $mosaicNIS_2["transaction"];
$this->assertArrayHasKey("mosaics", $contentTx_2);
$this->assertNotEmpty($contentTx_2["mosaics"]);
$this->assertNotEquals($contentTx["fee"], $contentTx_2["fee"]);
$this->assertEquals($expectFee_2, $contentTx_2["fee"]);
}
}