Passed
Push — master ( 68cc04...6c1ea3 )
by Aimeos
03:30
created

StandardTest::testNotifyException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2022
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Order\Email\Voucher;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
17
18
	protected function setUp() : void
19
	{
20
		\Aimeos\MShop::cache( true );
21
22
		$aimeos = \TestHelperJobs::getAimeos();
23
		$this->context = \TestHelperJobs::context();
24
25
		$codeManager = $this->getMockBuilder( '\\Aimeos\\MShop\\Coupon\\Manager\\Code\\Standard' )
26
			->setConstructorArgs( array( $this->context ) )
27
			->setMethods( array( 'save' ) )
28
			->getMock();
29
30
		$codeManager->expects( $this->any() )->method( 'save' );
31
		\Aimeos\MShop::inject( 'coupon/code', $codeManager );
32
33
		$this->object = new \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard( $this->context, $aimeos );
34
	}
35
36
37
	protected function tearDown() : void
38
	{
39
		\Aimeos\MShop::cache( false );
40
		unset( $this->object );
41
	}
42
43
44
	public function testGetName()
45
	{
46
		$this->assertEquals( 'Voucher related e-mails', $this->object->getName() );
47
	}
48
49
50
	public function testGetDescription()
51
	{
52
		$text = 'Sends the e-mail with the voucher to the customer';
53
		$this->assertEquals( $text, $this->object->getDescription() );
54
	}
55
56
57
	public function testRun()
58
	{
59
		$orderManagerStub = $this->getMockBuilder( \Aimeos\MShop\Order\Manager\Standard::class )
60
			->setConstructorArgs( array( $this->context ) )
61
			->setMethods( ['search'] )
62
			->getMock();
63
64
		\Aimeos\MShop::inject( 'order', $orderManagerStub );
65
66
		$orderItem = $orderManagerStub->create();
67
68
		$orderManagerStub->expects( $this->once() )->method( 'search' )
69
			->will( $this->returnValue( map( [$orderItem] ) ) );
70
71
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
72
			->setConstructorArgs( array( $this->context, \TestHelperJobs::getAimeos() ) )
73
			->setMethods( ['notify'] )
74
			->getMock();
75
76
		$object->expects( $this->once() )->method( 'notify' );
77
78
		$object->run();
79
	}
80
81
82
	public function testAddress()
83
	{
84
		$manager = \Aimeos\MShop::create( $this->context, 'order/base' );
85
		$addrManager = \Aimeos\MShop::create( $this->context, 'order/base/address' );
86
87
		$item = $manager->create();
88
		$item->addAddress( $addrManager->create()->setEmail( '[email protected]' ), \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT );
89
		$item->addAddress( $addrManager->create(), \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_DELIVERY );
90
91
		$result = $this->access( 'address' )->invokeArgs( $this->object, [$item] );
92
93
		$this->assertInstanceof( \Aimeos\MShop\Order\Item\Base\Address\Iface::class, $result );
94
	}
95
96
97
	public function testAddressNone()
98
	{
99
		$manager = \Aimeos\MShop::create( $this->context, 'order/base' );
100
101
		$this->expectException( \Aimeos\Controller\Jobs\Exception::class );
102
		$this->access( 'address' )->invokeArgs( $this->object, [$manager->create()] );
103
	}
104
105
106
	public function testCouponId()
107
	{
108
		$this->assertGreaterThan( 0, $this->access( 'couponId' )->invokeArgs( $this->object, [] ) );
109
	}
110
111
112
	public function testCreateCoupons()
113
	{
114
		$orderProductItem = \Aimeos\MShop::create( $this->context, 'order/base/product' )->create();
115
116
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
117
			->setConstructorArgs( [$this->context, \TestHelperJobs::getAimeos()] )
118
			->setMethods( ['saveCoupons'] )
119
			->getMock();
120
121
		$object->expects( $this->once() )->method( 'saveCoupons' );
122
123
		$result = $this->access( 'createCoupons' )->invokeArgs( $object, [map( $orderProductItem )] );
124
125
		$this->assertEquals( 1, $result->count() );
126
		$this->assertEquals( 1, count( $result->first()->getAttribute( 'coupon-code', 'coupon' ) ) );
127
	}
128
129
130
	public function testNotify()
131
	{
132
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
133
		$order = $manager->search( $manager->filter()->slice( 0, 1 ), ['order/base', 'order/base/product'] )->first();
134
135
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
136
			->setConstructorArgs( [$this->context, \TestHelperJobs::getAimeos()] )
137
			->setMethods( ['createCoupons', 'send', 'status'] )
138
			->getMock();
139
140
		$object->expects( $this->once() )->method( 'createCoupons' )->will( $this->returnValue( map() ) );
141
		$object->expects( $this->once() )->method( 'status' );
142
		$object->expects( $this->once() )->method( 'send' );
143
144
		$this->access( 'notify' )->invokeArgs( $object, [map( [$order] )] );
145
	}
146
147
148
	public function testNotifyException()
149
	{
150
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
151
		$order = $manager->search( $manager->filter()->slice( 0, 1 ), ['order/base', 'order/base/product'] )->first();
152
153
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
154
			->setConstructorArgs( [$this->context, \TestHelperJobs::getAimeos()] )
155
			->setMethods( ['products'] )
156
			->getMock();
157
158
		$object->expects( $this->once() )->method( 'products' )->will( $this->throwException( new \RuntimeException() ) );
159
160
		$this->access( 'notify' )->invokeArgs( $object, [map( [$order] )] );
161
	}
162
163
164
	public function testSaveCoupons()
165
	{
166
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Coupon\\Manager\\Code\\Standard' )
167
			->setConstructorArgs( array( $this->context ) )
168
			->setMethods( ['save'] )
169
			->getMock();
170
171
		$managerStub->expects( $this->once() )->method( 'save' );
172
173
		\Aimeos\MShop::inject( 'coupon/code', $managerStub );
174
175
		$this->access( 'saveCoupons' )->invokeArgs( $this->object, [['test' => 1]] );
176
	}
177
178
179
	public function testSend()
180
	{
181
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
182
		$base = $manager->search( $manager->filter()->slice( 0, 1 ), ['order/base', 'order/base/address'] )
183
			->first( new \RuntimeException( 'No orders available' ) )->getBaseItem();
184
185
		$orderProductItem = \Aimeos\MShop::create( $this->context, 'order/base/product' )->create();
186
		$base->addProduct( $orderProductItem->setProductCode( 'voucher-test' ) );
187
188
189
		$mailStub = $this->getMockBuilder( '\\Aimeos\\Base\\Mail\\None' )
190
			->disableOriginalConstructor()
191
			->getMock();
192
193
		$mailMsgStub = $this->getMockBuilder( '\\Aimeos\\Base\\Mail\\Message\\None' )
194
			->disableOriginalConstructor()
195
			->disableOriginalClone()
196
			->setMethods( ['send'] )
197
			->getMock();
198
199
		$mailStub->expects( $this->once() )->method( 'create' )->will( $this->returnValue( $mailMsgStub ) );
200
		$mailMsgStub->expects( $this->once() )->method( 'send' );
201
202
		$this->context->setMail( $mailStub );
203
204
205
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
206
			->setConstructorArgs( array( $this->context, \TestHelperJobs::getAimeos() ) )
207
			->setMethods( ['products'] )
208
			->getMock();
209
210
		$object->expects( $this->once() )->method( 'products' )->will( $this->returnValue( map( $orderProductItem ) ) );
211
212
		$this->access( 'createCoupons' )->invokeArgs( $object, [map( $orderProductItem )] );
213
		$this->access( 'send' )->invokeArgs( $object, [$base, $this->context->locale()->getSiteItem()] );
214
	}
215
216
217
	public function testStatus()
218
	{
219
		$statusManagerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Status\\Standard' )
220
			->setConstructorArgs( array( $this->context ) )
221
			->setMethods( ['save'] )
222
			->getMock();
223
224
		$statusManagerStub->expects( $this->once() )->method( 'save' );
225
226
		\Aimeos\MShop::inject( 'order/status', $statusManagerStub );
227
228
		$this->access( 'status' )->invokeArgs( $this->object, array( -1, +1 ) );
229
	}
230
231
232
	public function testView()
233
	{
234
		$base = \Aimeos\MShop::create( $this->context, 'order/base' )->create();
235
		$address = \Aimeos\MShop::create( $this->context, 'order/base/address' )->create()->setLanguageId( 'de' );
236
237
		$result = $this->access( 'view' )->invokeArgs( $this->object, [$base, $address] );
238
		$this->assertInstanceof( \Aimeos\MW\View\Iface::class, $result );
239
	}
240
241
242
	protected function access( $name )
243
	{
244
		$class = new \ReflectionClass( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class );
245
		$method = $class->getMethod( $name );
246
		$method->setAccessible( true );
247
248
		return $method;
249
	}
250
}
251