1 | <?php |
||
24 | class FormIntegrationTest extends TestCase |
||
25 | { |
||
26 | /** |
||
27 | * @return MoneyFieldset |
||
28 | */ |
||
29 | private function getMoneyFieldset() |
||
36 | |||
37 | public function testElementDirectlyInTheForm() |
||
38 | { |
||
39 | $element = $this->getMoneyFieldset(); |
||
40 | $element->init(); |
||
41 | |||
42 | $form = new Form(); |
||
43 | $form->setHydrator(new ObjectProperty()); |
||
|
|||
44 | $form->setObject(new StdClass()); |
||
45 | $form->add($element, ['name' => 'money']); |
||
46 | |||
47 | $this->assertFalse($form->setData([])->isValid()); |
||
48 | $this->assertFalse($form->setData(['money' => ['amount' => '123', 'currency' => '']])->isValid()); |
||
49 | $this->assertFalse($form->setData(['money' => ['amount' => '', 'currency' => 'BRL']])->isValid()); |
||
50 | |||
51 | $data = [ |
||
52 | 'money' => [ |
||
53 | 'amount' => '500.20', |
||
54 | 'currency' => 'BRL', |
||
55 | ], |
||
56 | ]; |
||
57 | |||
58 | $form->setData($data); |
||
59 | |||
60 | $this->assertTrue($form->isValid()); |
||
61 | |||
62 | $amountValue = $form->get('money')->get('amount')->getValue(); |
||
63 | $currencyValue = $form->get('money')->get('currency')->getValue(); |
||
64 | $object = $form->getData()->money; |
||
65 | |||
66 | $this->assertSame('500.20', $amountValue); |
||
67 | $this->assertSame('BRL', $currencyValue); |
||
68 | $this->assertInstanceOf(Money::class, $object); |
||
69 | $this->assertSame(50020, $object->getAmount()); |
||
70 | $this->assertSame('BRL', $object->getCurrency()->getName()); |
||
71 | } |
||
72 | |||
73 | public function testElementInAFieldsetForSomeModel() |
||
74 | { |
||
75 | $element = $this->getMoneyFieldset(); |
||
76 | $element->init(); |
||
77 | |||
78 | $fieldset = new Fieldset('hasMoneyElementFieldset'); |
||
79 | $fieldset->add($element, ['name' => 'price']); |
||
80 | $fieldset->setHydrator(new ClassMethods()); |
||
81 | $fieldset->setUseAsBaseFieldset(true); |
||
82 | |||
83 | $form = new Form(); |
||
84 | $form->add($fieldset); |
||
85 | |||
86 | // todo: can't load this |
||
87 | $form->bind(new HasMoneyPropertyModel()); |
||
88 | |||
89 | $data = [ |
||
90 | 'hasMoneyElementFieldset' => [ |
||
91 | 'price' => [ |
||
92 | 'amount' => '500.25', |
||
93 | 'currency' => 'BRL', |
||
94 | ], |
||
95 | ], |
||
96 | ]; |
||
97 | |||
98 | $form->setData($data); |
||
99 | $this->assertTrue($form->isValid()); |
||
100 | |||
101 | $amountValue = $form->get('hasMoneyElementFieldset')->get('price')->get('amount')->getValue(); |
||
102 | $currencyValue = $form->get('hasMoneyElementFieldset')->get('price')->get('currency')->getValue(); |
||
103 | $object = $form->getData(); |
||
104 | |||
105 | $this->assertSame('500.25', $amountValue); |
||
106 | $this->assertSame('BRL', $currencyValue); |
||
107 | $this->assertInstanceOf(Money::class, $object->getPrice()); |
||
108 | $this->assertSame(50025, $object->getPrice()->getAmount()); |
||
109 | $this->assertSame('BRL', $object->getPrice()->getCurrency()->getName()); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @expectedException InvalidArgumentException |
||
114 | * @expectedExceptionMessage The value could not be parsed as money |
||
115 | */ |
||
116 | public function testValueCouldNotBeParsedAsMoney() |
||
126 | } |
||
127 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.