1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ZFBrasil\Test\DoctrineMoneyModule\Form; |
4
|
|
|
|
5
|
|
|
use StdClass; |
6
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
7
|
|
|
use Money\Money; |
8
|
|
|
use Money\InvalidArgumentException; |
9
|
|
|
use Zend\Form\Form; |
10
|
|
|
use Zend\Form\Fieldset; |
11
|
|
|
use Zend\Form\FormElementManager; |
12
|
|
|
use Zend\Stdlib\Hydrator\ClassMethods; |
13
|
|
|
use ZFBrasil\DoctrineMoneyModule\Form\Factory\MoneyFieldsetFactory; |
14
|
|
|
use ZFBrasil\DoctrineMoneyModule\Form\MoneyFieldset; |
15
|
|
|
use ZFBrasil\Test\DoctrineMoneyModule\TestAsset\Model\HasMoneyPropertyModel; |
16
|
|
|
use Zend\Stdlib\Hydrator\ObjectProperty; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Test to see if Form returns a valid object on getData. |
20
|
|
|
* |
21
|
|
|
* @author Fábio Carneiro <[email protected]> |
22
|
|
|
* @license MIT |
23
|
|
|
*/ |
24
|
|
|
class FormIntegrationTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @return MoneyFieldset |
28
|
|
|
*/ |
29
|
|
|
private function getMoneyFieldset() |
30
|
|
|
{ |
31
|
|
|
$factory = new MoneyFieldsetFactory(); |
32
|
|
|
$formManager = $this->getMock(FormElementManager::class); |
33
|
|
|
|
34
|
|
|
return $factory($formManager); |
35
|
|
|
} |
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() |
117
|
|
|
{ |
118
|
|
|
$element = $this->getMoneyFieldset(); |
119
|
|
|
$element->init(); |
120
|
|
|
|
121
|
|
|
$form = new Form(); |
122
|
|
|
$form->add($element, ['name' => 'money']); |
123
|
|
|
|
124
|
|
|
$this->assertFalse($form->setData(['money' => ['amount' => 'bad', 'currency' => 'BRL']])->isValid()); |
125
|
|
|
} |
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.