5
5
*/
6
6
namespace Magento \Checkout \Test \Unit \Controller \Account ;
7
7
8
+ use Magento \Framework \Controller \ResultFactory ;
9
+
8
10
/**
9
11
* Shopping cart edit tests
10
12
*/
@@ -36,9 +38,14 @@ class CreateTest extends \PHPUnit\Framework\TestCase
36
38
protected $ orderCustomerService ;
37
39
38
40
/**
39
- * @var \PHPUnit_Framework_MockObject_MockObject
41
+ * @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject
42
+ */
43
+ private $ resultFactory ;
44
+
45
+ /**
46
+ * @var \Magento\Framework\Controller\ResultInterface|\PHPUnit_Framework_MockObject_MockObject
40
47
*/
41
- protected $ objectManagerMock ;
48
+ private $ resultPage ;
42
49
43
50
protected function setUp ()
44
51
{
@@ -48,9 +55,19 @@ protected function setUp()
48
55
$ this ->orderCustomerService = $ this ->createMock (\Magento \Sales \Api \OrderCustomerManagementInterface::class);
49
56
$ this ->messageManager = $ this ->createMock (\Magento \Framework \Message \ManagerInterface::class);
50
57
51
- $ this ->objectManagerMock = $ this ->createMock (\Magento \Framework \ObjectManagerInterface::class);
52
- $ contextMock = $ this ->createPartialMock (\Magento \Framework \App \Action \Context::class, ['getObjectManager ' ]);
53
- $ contextMock ->expects ($ this ->once ())->method ('getObjectManager ' )->willReturn ($ this ->objectManagerMock );
58
+ $ contextMock = $ this ->createPartialMock (
59
+ \Magento \Framework \App \Action \Context::class,
60
+ ['getObjectManager ' , 'getResultFactory ' ]
61
+ );
62
+ $ this ->resultFactory = $ this ->getMockBuilder (\Magento \Framework \Controller \ResultFactory::class)
63
+ ->disableOriginalConstructor ()
64
+ ->getMock ();
65
+ $ contextMock ->expects ($ this ->once ())
66
+ ->method ('getResultFactory ' )
67
+ ->willReturn ($ this ->resultFactory );
68
+ $ this ->resultPage = $ this ->getMockBuilder (\Magento \Framework \Controller \ResultInterface::class)
69
+ ->setMethods (['setData ' ])
70
+ ->getMockForAbstractClass ();
54
71
55
72
$ this ->action = $ objectManagerHelper ->getObject (
56
73
\Magento \Checkout \Controller \Account \Create::class,
@@ -66,53 +83,48 @@ protected function setUp()
66
83
67
84
public function testExecuteAddsSessionMessageIfCustomerIsLoggedIn ()
68
85
{
69
- $ jsonFactoryMock = $ this ->createMock (\Magento \Framework \Controller \Result \JsonFactory::class);
70
- $ this ->objectManagerMock ->expects ($ this ->once ())
71
- ->method ('get ' )
72
- ->with (\Magento \Framework \Controller \Result \JsonFactory::class)
73
- ->willReturn ($ jsonFactoryMock );
74
- $ jsonMock = $ this ->createMock (\Magento \Framework \Controller \Result \Json::class);
75
- $ jsonFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ jsonMock );
76
-
77
- $ this ->customerSession ->expects ($ this ->once ())->method ('isLoggedIn ' )->will ($ this ->returnValue (true ));
78
-
79
- $ jsonMock ->expects ($ this ->once ())
86
+ $ resultJson = '{"errors": "true", "message": "Customer is already registered"} ' ;
87
+ $ this ->customerSession ->expects ($ this ->once ())
88
+ ->method ('isLoggedIn ' )
89
+ ->will ($ this ->returnValue (true ));
90
+ $ this ->resultFactory ->expects ($ this ->once ())
91
+ ->method ('create ' )
92
+ ->with (ResultFactory::TYPE_JSON )
93
+ ->willReturn ($ this ->resultPage );
94
+ $ this ->resultPage ->expects ($ this ->once ())
80
95
->method ('setData ' )
81
96
->with (
82
97
[
83
98
'errors ' => true ,
84
99
'message ' => __ ('Customer is already registered ' )
85
100
]
86
- )->willReturnSelf ( );
87
- $ this ->action ->execute ();
101
+ )->willReturn ( $ resultJson );
102
+ $ this ->assertEquals ( $ resultJson , $ this -> action ->execute () );
88
103
}
89
104
90
105
public function testExecute ()
91
106
{
92
- $ jsonFactoryMock = $ this ->createMock (\Magento \Framework \Controller \Result \JsonFactory::class);
93
- $ this ->objectManagerMock ->expects ($ this ->once ())
94
- ->method ('get ' )
95
- ->with (\Magento \Framework \Controller \Result \JsonFactory::class)
96
- ->willReturn ($ jsonFactoryMock );
97
- $ jsonMock = $ this ->createMock (\Magento \Framework \Controller \Result \Json::class);
98
- $ jsonFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ jsonMock );
99
-
100
107
$ this ->customerSession ->expects ($ this ->once ())->method ('isLoggedIn ' )->will ($ this ->returnValue (false ));
101
108
$ this ->checkoutSession ->expects ($ this ->once ())->method ('getLastOrderId ' )->will ($ this ->returnValue (100 ));
102
109
$ customer = $ this ->createMock (\Magento \Customer \Api \Data \CustomerInterface::class);
103
- $ this ->orderCustomerService ->expects ($ this ->once ())->method ('create ' )->with (100 )->will (
104
- $ this ->returnValue ($ customer )
105
- );
106
-
107
- $ jsonMock ->expects ($ this ->once ())
110
+ $ this ->orderCustomerService ->expects ($ this ->once ())
111
+ ->method ('create ' )
112
+ ->with (100 )
113
+ ->will ($ this ->returnValue ($ customer ));
114
+
115
+ $ resultJson = '{"errors":"false", "message":"A letter with further instructions will be sent to your email."} ' ;
116
+ $ this ->resultFactory ->expects ($ this ->once ())
117
+ ->method ('create ' )
118
+ ->with (ResultFactory::TYPE_JSON )
119
+ ->willReturn ($ this ->resultPage );
120
+ $ this ->resultPage ->expects ($ this ->once ())
108
121
->method ('setData ' )
109
122
->with (
110
123
[
111
124
'errors ' => false ,
112
125
'message ' => __ ('A letter with further instructions will be sent to your email. ' )
113
126
]
114
- )->willReturnSelf ();
115
-
116
- $ this ->action ->execute ();
127
+ )->willReturn ($ resultJson );
128
+ $ this ->assertEquals ($ resultJson , $ this ->action ->execute ());
117
129
}
118
130
}
0 commit comments