-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFactoryTest.php
174 lines (144 loc) · 5.28 KB
/
FactoryTest.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
<?php
/*
* This file is part of the RichModelFormsBundle package.
*
* (c) Christian Flothmann <[email protected]>
* (c) Christopher Hertel <[email protected]>
* (c) QOSSMIC GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace Qossmic\RichModelForms\Tests\Integration;
use Qossmic\RichModelForms\Tests\Fixtures\Form\GrossPriceType;
use Qossmic\RichModelForms\Tests\Fixtures\Form\PriceType;
use Qossmic\RichModelForms\Tests\Fixtures\Model\GrossPrice;
use Qossmic\RichModelForms\Tests\Fixtures\Model\Price;
class FactoryTest extends AbstractDataMapperTest
{
public function testFactoryReturnsNull(): void
{
$form = $this->createForm(GrossPriceType::class, null, [
'factory' => function () {
return null;
},
]);
$form->submit([
'amount' => '500',
'taxRate' => '7',
]);
$this->assertNull($form->getData());
}
public function testInitializeNonCompoundRootFormWithEmptyData(): void
{
$form = $this->createForm(PriceType::class, null, [
'factory' => Price::class,
'property_path' => 'amount',
]);
$this->assertSame('', $form->getViewData());
}
public function testInitializeCompoundRootFormWithEmptyData(): void
{
$form = $this->createForm(GrossPriceType::class, null, [
'factory' => GrossPrice::class,
]);
$this->assertSame('', $form->get('amount')->getViewData());
$this->assertSame('', $form->get('taxRate')->getViewData());
}
public function testInitializeEmptyDataWithConstructorFactoryFromNonCompoundForm(): void
{
$form = $this->createForm(PriceType::class, null, [
'factory' => Price::class,
'property_path' => 'amount',
]);
$form->submit('500');
$price = $form->getData();
$this->assertInstanceOf(Price::class, $price);
$this->assertSame(500, $price->amount());
}
public function testInitializeEmptyDataWithConstructorFactoryFromCompoundForm(): void
{
$form = $this->createForm(GrossPriceType::class, null, [
'factory' => GrossPrice::class,
]);
$form->submit([
'amount' => '500',
'taxRate' => '7',
]);
$grossPrice = $form->getData();
$this->assertInstanceOf(GrossPrice::class, $grossPrice);
$this->assertSame(500, $grossPrice->amount());
$this->assertSame(7, $grossPrice->taxRate());
}
public function testInitializeEmptyDataWithFactoryMethodFromNonCompoundForm(): void
{
$form = $this->createForm(PriceType::class, null, [
'factory' => [Price::class, 'fromAmount'],
'property_path' => 'amount',
]);
$form->submit('500');
$price = $form->getData();
$this->assertInstanceOf(Price::class, $price);
$this->assertSame(500, $price->amount());
}
public function testInitializeEmptyDataWithFactoryMethodFromCompoundForm(): void
{
$form = $this->createForm(GrossPriceType::class, null, [
'factory' => [GrossPrice::class, 'withAmountAndTaxRate'],
]);
$form->submit([
'amount' => '500',
'taxRate' => '7',
]);
$grossPrice = $form->getData();
$this->assertInstanceOf(GrossPrice::class, $grossPrice);
$this->assertSame(500, $grossPrice->amount());
$this->assertSame(7, $grossPrice->taxRate());
}
public function testInitializeEmptyDataWithClosureFactoryFromNonCompoundForm(): void
{
$form = $this->createForm(PriceType::class, null, [
'factory' => function ($viewData): Price {
return new Price($viewData);
},
'property_path' => 'amount',
]);
$form->submit('500');
$price = $form->getData();
$this->assertInstanceOf(Price::class, $price);
$this->assertSame(500, $price->amount());
}
public function testInitializeEmptyDataWithClosureFactoryFromCompoundForm(): void
{
$form = $this->createForm(GrossPriceType::class, null, [
'factory' => function (int $amount, int $taxRate): GrossPrice {
return new GrossPrice($amount, $taxRate);
},
]);
$form->submit([
'amount' => '500',
'taxRate' => '7',
]);
$grossPrice = $form->getData();
$this->assertInstanceOf(GrossPrice::class, $grossPrice);
$this->assertSame(500, $grossPrice->amount());
$this->assertSame(7, $grossPrice->taxRate());
}
public function testInitializeCompoundFormWithPropertyPath(): void
{
$form = $this->createForm(GrossPriceType::class, null, [
'factory' => GrossPrice::class,
'data_class' => GrossPrice::class,
'tax_rate_field_name' => 'tax',
'tax_rate_field_options' => [
'factory_argument' => 'taxRate',
],
]);
$form->submit([
'amount' => '500',
'tax' => '7',
]);
$this->assertEquals(new GrossPrice(500, 7), $form->getData());
}
}