11
11
namespace Magento \Sales \Block \Adminhtml \Order \Create \Form ;
12
12
13
13
use Magento \Backend \Model \Session \Quote as SessionQuote ;
14
+ use Magento \Customer \Api \Data \AttributeMetadataInterface ;
14
15
use Magento \Customer \Api \Data \AttributeMetadataInterfaceFactory ;
16
+ use Magento \Customer \Model \Data \Option ;
15
17
use Magento \Customer \Model \Metadata \Form ;
16
18
use Magento \Customer \Model \Metadata \FormFactory ;
17
19
use Magento \Framework \View \LayoutInterface ;
18
20
use Magento \Quote \Model \Quote ;
19
21
use Magento \TestFramework \Helper \Bootstrap ;
22
+ use Magento \TestFramework \ObjectManager ;
23
+ use PHPUnit \Framework \MockObject \MockObject ;
20
24
21
25
/**
22
26
* @magentoAppArea adminhtml
23
27
*/
24
28
class AccountTest extends \PHPUnit \Framework \TestCase
25
29
{
26
- /** @var Account */
30
+ /**
31
+ * @var Account
32
+ */
27
33
private $ accountBlock ;
28
34
29
35
/**
30
- * @var Bootstrap
36
+ * @var ObjectManager
31
37
*/
32
38
private $ objectManager ;
33
39
40
+ /**
41
+ * @var SessionQuote|MockObject
42
+ */
43
+ private $ session ;
44
+
34
45
/**
35
46
* @magentoDataFixture Magento/Sales/_files/quote.php
36
47
*/
37
48
protected function setUp ()
38
49
{
39
50
$ this ->objectManager = Bootstrap::getObjectManager ();
40
51
$ quote = $ this ->objectManager ->create (Quote::class)->load (1 );
41
- $ sessionQuoteMock = $ this ->getMockBuilder (
42
- SessionQuote::class
43
- )->disableOriginalConstructor ()->setMethods (
44
- ['getCustomerId ' , 'getStore ' , 'getStoreId ' , 'getQuote ' ]
45
- )->getMock ();
46
- $ sessionQuoteMock ->expects ($ this ->any ())->method ('getCustomerId ' )->will ($ this ->returnValue (1 ));
47
- $ sessionQuoteMock ->expects ($ this ->any ())->method ('getQuote ' )->will ($ this ->returnValue ($ quote ));
52
+
53
+ $ this ->session = $ this ->getMockBuilder (SessionQuote::class)
54
+ ->disableOriginalConstructor ()
55
+ ->setMethods (['getCustomerId ' , 'getStore ' , 'getStoreId ' , 'getQuote ' , 'getQuoteId ' ])
56
+ ->getMock ();
57
+ $ this ->session ->method ('getCustomerId ' )
58
+ ->willReturn (1 );
59
+ $ this ->session ->method ('getQuote ' )
60
+ ->willReturn ($ quote );
61
+ $ this ->session ->method ('getQuoteId ' )
62
+ ->willReturn ($ quote ->getId ());
48
63
/** @var LayoutInterface $layout */
49
64
$ layout = $ this ->objectManager ->get (LayoutInterface::class);
50
65
$ this ->accountBlock = $ layout ->createBlock (
51
66
Account::class,
52
67
'address_block ' . rand (),
53
- ['sessionQuote ' => $ sessionQuoteMock ]
68
+ ['sessionQuote ' => $ this -> session ]
54
69
);
55
70
parent ::setUp ();
56
71
}
@@ -62,13 +77,13 @@ public function testGetForm()
62
77
{
63
78
$ expectedFields = ['group_id ' , 'email ' ];
64
79
$ form = $ this ->accountBlock ->getForm ();
65
- $ this -> assertEquals (1 , $ form ->getElements ()->count (), "Form has invalid number of fieldsets " );
80
+ self :: assertEquals (1 , $ form ->getElements ()->count (), "Form has invalid number of fieldsets " );
66
81
$ fieldset = $ form ->getElements ()[0 ];
67
82
68
- $ this -> assertEquals (count ($ expectedFields ), $ fieldset ->getElements ()->count ());
83
+ self :: assertEquals (count ($ expectedFields ), $ fieldset ->getElements ()->count ());
69
84
70
85
foreach ($ fieldset ->getElements () as $ element ) {
71
- $ this -> assertTrue (
86
+ self :: assertTrue (
72
87
in_array ($ element ->getId (), $ expectedFields ),
73
88
sprintf ('Unexpected field "%s" in form. ' , $ element ->getId ())
74
89
);
@@ -79,6 +94,7 @@ public function testGetForm()
79
94
* Tests a case when user defined custom attribute has default value.
80
95
*
81
96
* @magentoDataFixture Magento/Customer/_files/customer.php
97
+ * @magentoConfigFixture current_store customer/create_account/default_group 3
82
98
*/
83
99
public function testGetFormWithUserDefinedAttribute ()
84
100
{
@@ -91,18 +107,27 @@ public function testGetFormWithUserDefinedAttribute()
91
107
92
108
$ form = $ accountBlock ->getForm ();
93
109
$ form ->setUseContainer (true );
110
+ $ content = $ form ->toHtml ();
94
111
95
- $ this -> assertContains (
112
+ self :: assertContains (
96
113
'<option value="1" selected="selected">Yes</option> ' ,
97
- $ form ->toHtml (),
98
- 'Default value for user defined custom attribute should be selected '
114
+ $ content ,
115
+ 'Default value for user defined custom attribute should be selected. '
116
+ );
117
+
118
+ self ::assertContains (
119
+ '<option value="3" selected="selected">Customer Group 1</option> ' ,
120
+ $ content ,
121
+ 'The Customer Group specified for the chosen store should be selected. '
99
122
);
100
123
}
101
124
102
125
/**
103
- * @return \PHPUnit_Framework_MockObject_MockObject
126
+ * Creates a mock for Form object.
127
+ *
128
+ * @return MockObject
104
129
*/
105
- private function getFormFactoryMock (): \ PHPUnit_Framework_MockObject_MockObject
130
+ private function getFormFactoryMock (): MockObject
106
131
{
107
132
/** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
108
133
$ attributeMetadataFactory = $ this ->objectManager ->create (AttributeMetadataInterfaceFactory::class);
@@ -113,11 +138,12 @@ private function getFormFactoryMock(): \PHPUnit_Framework_MockObject_MockObject
113
138
->setDefaultValue ('1 ' )
114
139
->setFrontendLabel ('Yes/No ' );
115
140
141
+ /** @var Form|MockObject $form */
116
142
$ form = $ this ->getMockBuilder (Form::class)
117
143
->disableOriginalConstructor ()
118
144
->getMock ();
119
145
$ form ->method ('getUserAttributes ' )->willReturn ([$ booleanAttribute ]);
120
- $ form ->method ('getSystemAttributes ' )->willReturn ([]);
146
+ $ form ->method ('getSystemAttributes ' )->willReturn ([$ this -> createCustomerGroupAttribute () ]);
121
147
122
148
$ formFactory = $ this ->getMockBuilder (FormFactory::class)
123
149
->disableOriginalConstructor ()
@@ -126,4 +152,33 @@ private function getFormFactoryMock(): \PHPUnit_Framework_MockObject_MockObject
126
152
127
153
return $ formFactory ;
128
154
}
155
+
156
+ /**
157
+ * Creates a customer group attribute object.
158
+ *
159
+ * @return AttributeMetadataInterface
160
+ */
161
+ private function createCustomerGroupAttribute (): AttributeMetadataInterface
162
+ {
163
+ /** @var Option $option1 */
164
+ $ option1 = $ this ->objectManager ->create (Option::class);
165
+ $ option1 ->setValue (3 );
166
+ $ option1 ->setLabel ('Customer Group 1 ' );
167
+
168
+ /** @var Option $option2 */
169
+ $ option2 = $ this ->objectManager ->create (Option::class);
170
+ $ option2 ->setValue (4 );
171
+ $ option2 ->setLabel ('Customer Group 2 ' );
172
+
173
+ /** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
174
+ $ attributeMetadataFactory = $ this ->objectManager ->create (AttributeMetadataInterfaceFactory::class);
175
+ $ attribute = $ attributeMetadataFactory ->create ()
176
+ ->setAttributeCode ('group_id ' )
177
+ ->setBackendType ('static ' )
178
+ ->setFrontendInput ('select ' )
179
+ ->setOptions ([$ option1 , $ option2 ])
180
+ ->setIsRequired (true );
181
+
182
+ return $ attribute ;
183
+ }
129
184
}
0 commit comments