Passed
Push — master ( 48eef7...a2ffc3 )
by Aimeos
05:06
created

StandardTest::testGetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
6
 */
7
8
9
namespace Aimeos\Controller\Jobs\Subscription\Process\Renew;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $aimeos;
15
	private $context;
16
	private $object;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->aimeos = \TestHelperJobs::getAimeos();
22
		$this->context = \TestHelperJobs::getContext();
23
24
		$this->object = new \Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard( $this->context, $this->aimeos );
25
26
		\Aimeos\MShop::cache( true );
27
	}
28
29
30
	protected function tearDown() : void
31
	{
32
		\Aimeos\MShop::cache( false );
33
		unset( $this->object, $this->context, $this->aimeos );
34
	}
35
36
37
	public function testGetName()
38
	{
39
		$this->assertEquals( 'Subscription process renew', $this->object->getName() );
40
	}
41
42
43
	public function testGetDescription()
44
	{
45
		$this->assertEquals( 'Renews subscriptions at next date', $this->object->getDescription() );
46
	}
47
48
49
	public function testRun()
50
	{
51
		$this->context->getConfig()->set( 'controller/common/subscription/process/processors', ['cgroup'] );
52
		$item = $this->getSubscription();
53
54
		$object = $this->getMockBuilder( '\\Aimeos\\Controller\\Jobs\\Subscription\\Process\\Renew\\Standard' )
55
			->setConstructorArgs( [$this->context, $this->aimeos] )
56
			->setMethods( ['createPayment'] )
57
			->getMock();
58
59
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
60
			->setConstructorArgs( [$this->context] )
61
			->setMethods( ['searchItems', 'saveItem'] )
62
			->getMock();
63
64
		$orderStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
65
			->setConstructorArgs( [$this->context] )
66
			->setMethods( ['saveItem'] )
67
			->getMock();
68
69
		$baseStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Standard' )
70
			->setConstructorArgs( [$this->context] )
71
			->setMethods( ['store'] )
72
			->getMock();
73
74
		\Aimeos\MShop::inject( 'subscription', $managerStub );
75
		\Aimeos\MShop::inject( 'order/base', $baseStub );
76
		\Aimeos\MShop::inject( 'order', $orderStub );
77
78
		$object->expects( $this->once() )->method( 'createPayment' );
79
80
		$managerStub->expects( $this->once() )->method( 'searchItems' )
81
			->will( $this->returnValue( map( [$item] ) ) );
82
83
		$managerStub->expects( $this->once() )->method( 'saveItem' );
84
		$orderStub->expects( $this->once() )->method( 'saveItem' );
85
		$baseStub->expects( $this->once() )->method( 'store' )
86
			->will( $this->returnCallback( function( $basket ) {
87
				return $basket->setId( -1 );
88
			} ) );
89
90
		$object->run();
91
	}
92
93
94
	public function testRunException()
95
	{
96
		$this->context->getConfig()->set( 'controller/common/subscription/process/processors', ['cgroup'] );
97
98
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
99
			->setConstructorArgs( [$this->context] )
100
			->setMethods( ['searchItems', 'saveItem'] )
101
			->getMock();
102
103
		\Aimeos\MShop::inject( 'subscription', $managerStub );
104
105
		$managerStub->expects( $this->once() )->method( 'searchItems' )
106
			->will( $this->returnValue( map( [$managerStub->createItem()->setOrderBaseId( -1 )] ) ) );
107
108
		$managerStub->expects( $this->once() )->method( 'saveItem' );
109
110
		$this->object->run();
111
	}
112
113
114
	public function testAddBasketAddresses()
115
	{
116
		$basket = \Aimeos\MShop::create( $this->context, 'order/base' )->createItem();
117
		$address = \Aimeos\MShop::create( $this->context, 'order/base/address' )->createItem();
118
119
		$addresses = map( ['payment' => [$address]] );
120
		$basket = $this->access( 'addBasketAddresses' )->invokeArgs( $this->object, [$this->context, $basket, $addresses] );
121
122
		$this->assertEquals( 1, count( $basket->getAddresses() ) );
123
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Address\Iface::class, $basket->getAddress( 'payment', 0 ) );
124
	}
125
126
127
	public function testAddBasketCoupons()
128
	{
129
		$this->context->getConfig()->set( 'controller/jobs/subcription/process/renew/standard/use-coupons', true );
130
131
		$basket = \Aimeos\MShop::create( $this->context, 'order/base' )->createItem();
132
		$product = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'CNC', ['price'] );
133
		$orderProduct = \Aimeos\MShop::create( $this->context, 'order/base/product' )->createItem();
134
135
		$price = $product->getRefItems( 'price', 'default', 'default' )->first();
136
		$basket->addProduct( $orderProduct->copyFrom( $product )->setPrice( $price ) );
137
138
		$this->assertEquals( '600.00', $basket->getPrice()->getValue() );
139
		$this->assertEquals( '30.00', $basket->getPrice()->getCosts() );
140
		$this->assertEquals( '0.00', $basket->getPrice()->getRebate() );
141
142
		$basket = $this->access( 'addBasketCoupons' )->invokeArgs( $this->object, [$this->context, $basket, map( ['90AB'] )] );
143
144
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
145
		$this->assertEquals( 2, count( $basket->getProducts() ) );
146
		$this->assertEquals( '537.00', $basket->getPrice()->getValue() );
147
		$this->assertEquals( '30.00', $basket->getPrice()->getCosts() );
148
		$this->assertEquals( '63.00', $basket->getPrice()->getRebate() );
149
	}
150
151
152
	public function testAddBasketProducts()
153
	{
154
		$basket = \Aimeos\MShop::create( $this->context, 'order/base' )->createItem();
155
		$product = \Aimeos\MShop::create( $this->context, 'product' )->findItem( 'CNC' );
156
		$manager = \Aimeos\MShop::create( $this->context, 'order/base/product' );
157
158
		$orderProducts = map( [
159
			$manager->createItem()->copyFrom( $product )->setId( 1 ),
160
			$manager->createItem()->copyFrom( $product )->setId( 2 ),
161
		] );
162
163
		$basket = $this->access( 'addBasketProducts' )->invokeArgs( $this->object, [$this->context, $basket, $orderProducts, 1] );
164
165
		$this->assertEquals( 1, count( $basket->getProducts() ) );
166
		$this->assertNull( $basket->getProduct( 0 )->getId() );
167
	}
168
169
170
	public function testAddBasketServices()
171
	{
172
		$basket = \Aimeos\MShop::create( $this->context, 'order/base' )->createItem();
173
		$manager = \Aimeos\MShop::create( $this->context, 'order/base/service' );
174
175
		$orderServices = map( [
176
			'delivery' => [$manager->createItem()->setCode( 'shiptest' )],
177
			'payment' => [$manager->createItem()->setCode( 'paytest' )],
178
		] );
179
180
		$basket = $this->access( 'addBasketServices' )->invokeArgs( $this->object, [$this->context, $basket, $orderServices] );
181
182
		$class = \Aimeos\MShop\Order\Item\Base\Service\Iface::class;
183
184
		$this->assertEquals( 2, count( $basket->getServices() ) );
185
		$this->assertEquals( 1, count( $basket->getService( 'delivery' ) ) );
186
		$this->assertInstanceOf( $class, $basket->getService( 'delivery', 0 ) );
187
		$this->assertEquals( 1, count( $basket->getService( 'payment' ) ) );
188
		$this->assertInstanceOf( $class, $basket->getService( 'payment', 0 ) );
189
	}
190
191
192
	public function testCreateOrder()
193
	{
194
		$item = $this->getSubscription();
195
196
		$result = $this->access( 'createOrder' )->invokeArgs( $this->object, [$this->context, $item] );
197
198
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $result );
199
	}
200
201
202
	public function testCreateOrderBase()
203
	{
204
		$item = $this->getSubscription();
205
		$class = \Aimeos\MShop\Order\Item\Base\Iface::class;
206
207
		$result = $this->access( 'createOrderBase' )->invokeArgs( $this->object, [$this->context, $item] );
208
209
		$this->assertInstanceOf( $class, $result );
210
	}
211
212
213
	public function testCreateOrderInvoice()
214
	{
215
		$item = $this->getSubscription();
216
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
217
218
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
219
			->setConstructorArgs( [$this->context] )
220
			->setMethods( ['saveItem'] )
221
			->getMock();
222
223
		\Aimeos\MShop::inject( 'order', $managerStub );
224
225
		$managerStub->expects( $this->once() )->method( 'saveItem' )->will( $this->returnArgument( 0 ) );
226
227
		$this->access( 'createOrderInvoice' )->invokeArgs( $this->object, [$this->context, $baseItem] );
228
	}
229
230
231
	public function testCreatePayment()
232
	{
233
		$item = $this->getSubscription();
234
		$invoice = $this->getOrderItem();
235
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
236
237
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
238
			->setConstructorArgs( [$this->context] )
239
			->setMethods( ['saveItem'] )
240
			->getMock();
241
242
		\Aimeos\MShop::inject( 'order', $managerStub );
243
244
		$managerStub->expects( $this->once() )->method( 'saveItem' );
245
246
		$this->access( 'createPayment' )->invokeArgs( $this->object, [$this->context, $baseItem, $invoice] );
247
	}
248
249
250
	protected function getOrderItem()
251
	{
252
		return \Aimeos\MShop::create( $this->context, 'order' )->createItem();
253
	}
254
255
256
	protected function getOrderBaseItem( $baseId )
257
	{
258
		return \Aimeos\MShop::create( $this->context, 'order/base' )->getItem( $baseId, ['order/base/service'] );
259
	}
260
261
262
	protected function getSubscription()
263
	{
264
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
265
266
		$search = $manager->createSearch();
267
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
268
269
		if( ( $item = $manager->searchItems( $search )->first() ) !== null ) {
270
			return $item;
271
		}
272
273
		throw new \Exception( 'No subscription item found' );
274
	}
275
276
277
	protected function access( $name )
278
	{
279
		$class = new \ReflectionClass( \Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard::class );
280
		$method = $class->getMethod( $name );
281
		$method->setAccessible( true );
282
283
		return $method;
284
	}
285
}
286