5
5
*/
6
6
namespace Magento \Quote \Test \Unit \Observer ;
7
7
8
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
9
+ use Magento \Sales \Model \Order \Email \Sender \InvoiceSender ;
10
+ use Magento \Sales \Model \Order \Invoice ;
11
+ use Magento \Sales \Model \ResourceModel \Order \Invoice \Collection as InvoiceCollection ;
12
+
8
13
class SubmitObserverTest extends \PHPUnit \Framework \TestCase
9
14
{
10
15
/**
@@ -42,6 +47,21 @@ class SubmitObserverTest extends \PHPUnit\Framework\TestCase
42
47
*/
43
48
protected $ paymentMock ;
44
49
50
+ /**
51
+ * @var InvoiceSender|\PHPUnit_Framework_MockObject_MockObject
52
+ */
53
+ private $ invoiceSenderMock ;
54
+
55
+ /**
56
+ * @var Invoice|\PHPUnit_Framework_MockObject_MockObject
57
+ */
58
+ private $ invoiceMock ;
59
+
60
+ /**
61
+ * @var InvoiceCollection|\PHPUnit_Framework_MockObject_MockObject
62
+ */
63
+ private $ invoiceCollectionMock ;
64
+
45
65
protected function setUp ()
46
66
{
47
67
$ this ->loggerMock = $ this ->createMock (\Psr \Log \LoggerInterface::class);
@@ -59,9 +79,18 @@ protected function setUp()
59
79
$ eventMock ->expects ($ this ->once ())->method ('getQuote ' )->willReturn ($ this ->quoteMock );
60
80
$ eventMock ->expects ($ this ->once ())->method ('getOrder ' )->willReturn ($ this ->orderMock );
61
81
$ this ->quoteMock ->expects ($ this ->once ())->method ('getPayment ' )->willReturn ($ this ->paymentMock );
62
- $ this ->model = new \Magento \Quote \Observer \SubmitObserver (
63
- $ this ->loggerMock ,
64
- $ this ->orderSenderMock
82
+ $ this ->invoiceSenderMock = $ this ->createMock (InvoiceSender::class);
83
+ $ this ->invoiceMock = $ this ->createMock (Invoice::class);
84
+ $ this ->invoiceCollectionMock = $ this ->createMock (InvoiceCollection::class);
85
+ $ objectManager = new ObjectManager ($ this );
86
+
87
+ $ this ->model = $ objectManager ->getObject (
88
+ \Magento \Quote \Observer \SubmitObserver::class,
89
+ [
90
+ 'logger ' => $ this ->loggerMock ,
91
+ 'orderSender ' => $ this ->orderSenderMock ,
92
+ 'invoiceSender ' => $ this ->invoiceSenderMock ,
93
+ ]
65
94
);
66
95
}
67
96
@@ -70,6 +99,10 @@ public function testSendEmail()
70
99
$ this ->paymentMock ->expects ($ this ->once ())->method ('getOrderPlaceRedirectUrl ' )->willReturn ('' );
71
100
$ this ->orderMock ->expects ($ this ->once ())->method ('getCanSendNewEmailFlag ' )->willReturn (true );
72
101
$ this ->orderSenderMock ->expects ($ this ->once ())->method ('send ' )->willReturn (true );
102
+ $ this ->orderMock ->expects ($ this ->once ())
103
+ ->method ('getInvoiceCollection ' )
104
+ ->willReturn ($ this ->invoiceCollectionMock );
105
+ $ this ->invoiceCollectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([]);
73
106
$ this ->loggerMock ->expects ($ this ->never ())->method ('critical ' );
74
107
$ this ->model ->execute ($ this ->observerMock );
75
108
}
@@ -93,4 +126,38 @@ public function testSendEmailWhenRedirectUrlExists()
93
126
$ this ->loggerMock ->expects ($ this ->never ())->method ('critical ' );
94
127
$ this ->model ->execute ($ this ->observerMock );
95
128
}
129
+
130
+ public function testSendEmailWithPaidInvoice ()
131
+ {
132
+ $ this ->prepareDataForSendInvoice ();
133
+ $ this ->invoiceMock ->expects ($ this ->once ())->method ('getState ' )->willReturn (Invoice::STATE_PAID );
134
+ $ this ->invoiceSenderMock ->expects ($ this ->once ())
135
+ ->method ('send ' )
136
+ ->with ($ this ->invoiceMock )
137
+ ->willReturn (true );
138
+ $ this ->loggerMock ->expects ($ this ->never ())->method ('critical ' );
139
+
140
+ $ this ->model ->execute ($ this ->observerMock );
141
+ }
142
+
143
+ public function testSendEmailWithNotPaidInvoice ()
144
+ {
145
+ $ this ->prepareDataForSendInvoice ();
146
+ $ this ->invoiceMock ->expects ($ this ->once ())->method ('getState ' )->willReturn (Invoice::STATE_OPEN );
147
+ $ this ->invoiceSenderMock ->expects ($ this ->never ())->method ('send ' );
148
+ $ this ->loggerMock ->expects ($ this ->never ())->method ('critical ' );
149
+
150
+ $ this ->model ->execute ($ this ->observerMock );
151
+ }
152
+
153
+ private function prepareDataForSendInvoice ()
154
+ {
155
+ $ this ->paymentMock ->expects ($ this ->once ())->method ('getOrderPlaceRedirectUrl ' )->willReturn ('' );
156
+ $ this ->orderMock ->expects ($ this ->once ())->method ('getCanSendNewEmailFlag ' )->willReturn (true );
157
+ $ this ->orderSenderMock ->expects ($ this ->once ())->method ('send ' )->willReturn (true );
158
+ $ this ->orderMock ->expects ($ this ->once ())
159
+ ->method ('getInvoiceCollection ' )
160
+ ->willReturn ($ this ->invoiceCollectionMock );
161
+ $ this ->invoiceCollectionMock ->expects ($ this ->once ())->method ('getItems ' )->willReturn ([$ this ->invoiceMock ]);
162
+ }
96
163
}
0 commit comments