@@ -36,6 +36,26 @@ class CheckoutAgreementsRepositoryTest extends \PHPUnit_Framework_TestCase
36
36
*/
37
37
private $ objectManager ;
38
38
39
+ /**
40
+ * @var \PHPUnit_Framework_MockObject_MockObject
41
+ */
42
+ private $ resourceMock ;
43
+
44
+ /**
45
+ * @var \PHPUnit_Framework_MockObject_MockObject
46
+ */
47
+ private $ agrFactoryMock ;
48
+
49
+ /**
50
+ * @var \PHPUnit_Framework_MockObject_MockObject
51
+ */
52
+ private $ agreementMock ;
53
+
54
+ /**
55
+ * @var \PHPUnit_Framework_MockObject_MockObject
56
+ */
57
+ private $ storeMock ;
58
+
39
59
protected function setUp ()
40
60
{
41
61
$ this ->objectManager = new ObjectManager ($ this );
@@ -49,10 +69,24 @@ protected function setUp()
49
69
);
50
70
$ this ->storeManagerMock = $ this ->getMock ('Magento\Store\Model\StoreManagerInterface ' );
51
71
$ this ->scopeConfigMock = $ this ->getMock ('Magento\Framework\App\Config\ScopeConfigInterface ' );
72
+ $ this ->resourceMock = $ this ->getMock ('Magento\CheckoutAgreements\Model\Resource\Agreement ' , [], [], '' , false );
73
+ $ this ->agrFactoryMock = $ this ->getMock (
74
+ 'Magento\CheckoutAgreements\Model\AgreementFactory ' ,
75
+ ['create ' ],
76
+ [],
77
+ '' ,
78
+ false
79
+ );
80
+ $ methods = ['addData ' , 'getData ' , 'setStores ' , 'getAgreementId ' , 'getId ' ];
81
+ $ this ->agreementMock =
82
+ $ this ->getMock ('\Magento\CheckoutAgreements\Model\Agreement ' , $ methods , [], '' , false );
83
+ $ this ->storeMock = $ this ->getMock ('Magento\Store\Model\Store ' , [], [], '' , false );
52
84
$ this ->model = new \Magento \CheckoutAgreements \Model \CheckoutAgreementsRepository (
53
85
$ this ->factoryMock ,
54
86
$ this ->storeManagerMock ,
55
- $ this ->scopeConfigMock
87
+ $ this ->scopeConfigMock ,
88
+ $ this ->resourceMock ,
89
+ $ this ->agrFactoryMock
56
90
);
57
91
}
58
92
@@ -73,27 +107,99 @@ public function testGetListReturnsTheListOfActiveCheckoutAgreements()
73
107
->with ('checkout/options/enable_agreements ' , ScopeInterface::SCOPE_STORE , null )
74
108
->will ($ this ->returnValue (true ));
75
109
76
- $ agreementDataObject = $ this ->getMock (
77
- 'Magento\CheckoutAgreements\Model\Agreement ' ,
78
- [],
79
- [],
80
- '' ,
81
- false
82
- );
83
-
84
110
$ storeId = 1 ;
85
111
$ storeMock = $ this ->getMock ('Magento\Store\Model\Store ' , [], [], '' , false );
86
112
$ storeMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ($ storeId ));
87
113
$ this ->storeManagerMock ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ storeMock ));
88
114
89
115
$ collectionMock = $ this ->objectManager ->getCollectionMock (
90
116
'Magento\CheckoutAgreements\Model\Resource\Agreement\Collection ' ,
91
- [$ agreementDataObject ]
117
+ [$ this -> agreementMock ]
92
118
);
93
119
$ this ->factoryMock ->expects ($ this ->once ())->method ('create ' )->will ($ this ->returnValue ($ collectionMock ));
94
120
$ collectionMock ->expects ($ this ->once ())->method ('addStoreFilter ' )->with ($ storeId );
95
121
$ collectionMock ->expects ($ this ->once ())->method ('addFieldToFilter ' )->with ('is_active ' , 1 );
96
122
97
- $ this ->assertEquals ([$ agreementDataObject ], $ this ->model ->getList ());
123
+ $ this ->assertEquals ([$ this ->agreementMock ], $ this ->model ->getList ());
124
+ }
125
+
126
+ public function testSave ()
127
+ {
128
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('getAgreementId ' )->willReturn (null );
129
+ $ this ->agrFactoryMock ->expects ($ this ->never ())->method ('create ' );
130
+ $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ this ->storeMock );
131
+ $ this ->storeMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('storeId ' );
132
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('setStores ' );
133
+ $ this ->resourceMock ->expects ($ this ->once ())->method ('save ' )->with ($ this ->agreementMock );
134
+ $ this ->model ->save ($ this ->agreementMock );
135
+ }
136
+
137
+ public function testUpdate ()
138
+ {
139
+ $ agreementId = 1 ;
140
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('getAgreementId ' )->willReturn ($ agreementId );
141
+ $ this ->agrFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->agreementMock );
142
+ $ this ->resourceMock
143
+ ->expects ($ this ->once ())
144
+ ->method ('load ' )
145
+ ->with ($ this ->agreementMock , $ agreementId );
146
+ $ this ->storeManagerMock ->expects ($ this ->never ())->method ('getStore ' );
147
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('setStores ' );
148
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ agreementId );
149
+ $ this ->agreementMock ->expects ($ this ->any ())->method ('getData ' )->willReturn (['data ' ]);
150
+ $ this ->agreementMock
151
+ ->expects ($ this ->once ())
152
+ ->method ('addData ' )->with (['data ' ])
153
+ ->willReturn ($ this ->agreementMock );
154
+ $ this ->resourceMock ->expects ($ this ->once ())->method ('save ' )->with ($ this ->agreementMock );
155
+ $ this ->assertEquals ($ this ->agreementMock , $ this ->model ->save ($ this ->agreementMock , 1 ));
156
+ }
157
+
158
+ /**
159
+ * @expectedException \Magento\Framework\Exception\CouldNotSaveException
160
+ */
161
+ public function testSaveWithException ()
162
+ {
163
+ $ this ->agreementMock ->expects ($ this ->exactly (2 ))->method ('getAgreementId ' )->willReturn (null );
164
+ $ this ->agrFactoryMock ->expects ($ this ->never ())->method ('create ' );
165
+ $ this ->storeManagerMock ->expects ($ this ->once ())->method ('getStore ' )->willReturn ($ this ->storeMock );
166
+ $ this ->storeMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ('storeId ' );
167
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('setStores ' );
168
+ $ this ->resourceMock
169
+ ->expects ($ this ->once ())
170
+ ->method ('save ' )
171
+ ->with ($ this ->agreementMock )->willThrowException (new \Exception ());
172
+ $ this ->model ->save ($ this ->agreementMock );
173
+ }
174
+
175
+ public function testDeleteById ()
176
+ {
177
+ $ agreementId = 1 ;
178
+ $ this ->agrFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->agreementMock );
179
+ $ this ->resourceMock
180
+ ->expects ($ this ->once ())
181
+ ->method ('load ' )
182
+ ->with ($ this ->agreementMock , $ agreementId )
183
+ ->willReturn ($ this ->agreementMock );
184
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ agreementId );
185
+ $ this ->resourceMock ->expects ($ this ->once ())->method ('delete ' );
186
+ $ this ->assertTrue ($ this ->model ->deleteById (1 ));
187
+ }
188
+
189
+ /**
190
+ * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
191
+ */
192
+ public function testDeleteByIdWithException ()
193
+ {
194
+ $ agreementId = 1 ;
195
+ $ this ->agrFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this ->agreementMock );
196
+ $ this ->resourceMock
197
+ ->expects ($ this ->once ())
198
+ ->method ('load ' )
199
+ ->with ($ this ->agreementMock , $ agreementId )
200
+ ->willReturn ($ this ->agreementMock );
201
+ $ this ->agreementMock ->expects ($ this ->once ())->method ('getId ' )->willReturn ($ agreementId );
202
+ $ this ->resourceMock ->expects ($ this ->once ())->method ('delete ' )->willThrowException (new \Exception ());
203
+ $ this ->assertTrue ($ this ->model ->deleteById (1 ));
98
204
}
99
205
}
0 commit comments