Passed
Push — master ( 23e72e...dc62e4 )
by Aimeos
03:22
created

StandardTest::testGetDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
		$domains = ['order/base', 'order/base/address', 'order/base/product'];
134
135
		$order = $manager->search( $manager->filter()->slice( 0, 1 ), $domains )
136
			->first( new \RuntimeException( 'No orders available' ) );
137
138
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
139
			->setConstructorArgs( [$this->context, \TestHelperJobs::getAimeos()] )
140
			->setMethods( ['createCoupons', 'send', 'status'] )
141
			->getMock();
142
143
		$object->expects( $this->once() )->method( 'createCoupons' )->will( $this->returnValue( map() ) );
144
		$object->expects( $this->once() )->method( 'status' );
145
		$object->expects( $this->once() )->method( 'send' );
146
147
		$this->access( 'notify' )->invokeArgs( $object, [map( [$order] )] );
148
	}
149
150
151
	public function testNotifyException()
152
	{
153
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
154
		$order = $manager->search( $manager->filter()->slice( 0, 1 ), ['order/base', 'order/base/product'] )->first();
155
156
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
157
			->setConstructorArgs( [$this->context, \TestHelperJobs::getAimeos()] )
158
			->setMethods( ['products'] )
159
			->getMock();
160
161
		$object->expects( $this->once() )->method( 'products' )->will( $this->throwException( new \RuntimeException() ) );
162
163
		$this->access( 'notify' )->invokeArgs( $object, [map( [$order] )] );
164
	}
165
166
167
	public function testSaveCoupons()
168
	{
169
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Coupon\\Manager\\Code\\Standard' )
170
			->setConstructorArgs( array( $this->context ) )
171
			->setMethods( ['save'] )
172
			->getMock();
173
174
		$managerStub->expects( $this->once() )->method( 'save' );
175
176
		\Aimeos\MShop::inject( 'coupon/code', $managerStub );
177
178
		$this->access( 'saveCoupons' )->invokeArgs( $this->object, [['test' => 1]] );
179
	}
180
181
182
	public function testSend()
183
	{
184
		$address = \Aimeos\MShop::create( $this->context, 'order/base/address' )->create()->setEmail( '[email protected]' );
185
		$product = \Aimeos\MShop::create( $this->context, 'order/base/product' )->create()->setProductCode( 'voucher-test' );
186
187
188
		$mailStub = $this->getMockBuilder( '\\Aimeos\\Base\\Mail\\None' )
189
			->disableOriginalConstructor()
190
			->getMock();
191
192
		$mailMsgStub = $this->getMockBuilder( '\\Aimeos\\Base\\Mail\\Message\\None' )
193
			->disableOriginalConstructor()
194
			->disableOriginalClone()
195
			->setMethods( ['send'] )
196
			->getMock();
197
198
		$mailStub->expects( $this->once() )->method( 'create' )->will( $this->returnValue( $mailMsgStub ) );
199
		$mailMsgStub->expects( $this->once() )->method( 'send' );
200
201
		$this->context->setMail( $mailStub );
202
203
204
		$object = $this->getMockBuilder( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class )
205
			->setConstructorArgs( [$this->context, \TestHelperJobs::getAimeos()] )
206
			->setMethods( ['test'] )
207
			->getMock();
208
209
		$this->access( 'createCoupons' )->invokeArgs( $object, [map( $product )] );
210
		$this->access( 'send' )->invokeArgs( $object, [$this->context->view(), map( $product ), $address] );
211
	}
212
213
214
	public function testStatus()
215
	{
216
		$statusManagerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Status\\Standard' )
217
			->setConstructorArgs( array( $this->context ) )
218
			->setMethods( ['save'] )
219
			->getMock();
220
221
		$statusManagerStub->expects( $this->once() )->method( 'save' );
222
223
		\Aimeos\MShop::inject( 'order/status', $statusManagerStub );
224
225
		$this->access( 'status' )->invokeArgs( $this->object, array( -1, +1 ) );
226
	}
227
228
229
	public function testView()
230
	{
231
		$base = \Aimeos\MShop::create( $this->context, 'order/base' )->create();
232
		$address = \Aimeos\MShop::create( $this->context, 'order/base/address' )->create();
233
		$base->addAddress( $address->setLanguageId( 'de' )->setEmail( '[email protected]' ), 'delivery' );
234
235
		$result = $this->access( 'view' )->invokeArgs( $this->object, [$base] );
236
		$this->assertInstanceof( \Aimeos\MW\View\Iface::class, $result );
237
	}
238
239
240
	protected function access( $name )
241
	{
242
		$class = new \ReflectionClass( \Aimeos\Controller\Jobs\Order\Email\Voucher\Standard::class );
243
		$method = $class->getMethod( $name );
244
		$method->setAccessible( true );
245
246
		return $method;
247
	}
248
}
249