Passed
Push — master ( 782d1a...bdda1f )
by Aimeos
24:22 queued 15:04
created

StandardTest::testCreateOrderBase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
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\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
		\Aimeos\MShop::cache( true );
22
23
		$this->aimeos = \TestHelper::getAimeos();
24
		$this->context = \TestHelper::context();
25
26
		$this->object = new \Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard( $this->context, $this->aimeos );
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->config()->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( ['iterate', 'save'] )
62
			->getMock();
63
64
		$orderStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
65
			->setConstructorArgs( [$this->context] )
66
			->setMethods( ['save'] )
67
			->getMock();
68
69
		\Aimeos\MShop::inject( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard', $managerStub );
70
		\Aimeos\MShop::inject( '\\Aimeos\\MShop\\Order\\Manager\\Standard', $orderStub );
71
72
		$object->expects( $this->once() )->method( 'createPayment' );
73
74
		$managerStub->expects( $this->exactly( 2 ) )->method( 'iterate' )
75
			->will( $this->onConsecutiveCalls( map( [$item] ), null ) );
76
77
		$managerStub->expects( $this->once() )->method( 'save' );
78
		$orderStub->expects( $this->once() )->method( 'save' )->will( $this->returnArgument( 0 ) );
79
80
		$object->run();
81
	}
82
83
84
	public function testRunException()
85
	{
86
		$this->context->config()->set( 'controller/common/subscription/process/processors', ['cgroup'] );
87
88
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
89
			->setConstructorArgs( [$this->context] )
90
			->setMethods( ['iterate', 'save'] )
91
			->getMock();
92
93
		\Aimeos\MShop::inject( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard', $managerStub );
94
95
		$object = $this->getMockBuilder( '\\Aimeos\\Controller\\Jobs\\Subscription\\Process\\Renew\\Standard' )
96
			->setConstructorArgs( [$this->context, $this->aimeos] )
97
			->setMethods( ['process'] )
98
			->getMock();
99
100
		$managerStub->expects( $this->exactly( 2 ) )->method( 'iterate' )
101
			->will( $this->onConsecutiveCalls( map( [$managerStub->create()] ), null ) );
102
103
		$managerStub->expects( $this->never() )->method( 'save' );
104
105
		$object->expects( $this->once() )->method( 'process' )->will( $this->throwException( new \RuntimeException() ) );
106
107
		$object->run();
108
	}
109
110
111
	public function testAddBasketAddresses()
112
	{
113
		$custId = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId();
114
		$basket = \Aimeos\MShop::create( $this->context, 'order' )->create()->setCustomerId( $custId );
115
		$address = \Aimeos\MShop::create( $this->context, 'order/address' )->create();
116
117
		$addresses = map( ['delivery' => [$address]] );
118
		$basket = $this->access( 'addBasketAddresses' )->invokeArgs( $this->object, [$this->context, $basket, $addresses] );
119
120
		$this->assertEquals( 2, count( $basket->getAddresses() ) );
121
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Address\Iface::class, $basket->getAddress( 'payment', 0 ) );
122
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Address\Iface::class, $basket->getAddress( 'delivery', 0 ) );
123
	}
124
125
126
	public function testAddBasketCoupons()
127
	{
128
		$this->context->config()->set( 'controller/jobs/subscription/process/renew/use-coupons', true );
129
130
		$basket = \Aimeos\MShop::create( $this->context, 'order' )->create();
131
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC', ['price'] );
132
		$orderProduct = \Aimeos\MShop::create( $this->context, 'order/product' )->create();
133
134
		$price = $product->getRefItems( 'price', 'default', 'default' )->first();
135
		$basket->addProduct( $orderProduct->copyFrom( $product )->setPrice( $price )->setStockType( 'default' ) );
136
137
		$this->assertEquals( '600.00', $basket->getPrice()->getValue() );
138
		$this->assertEquals( '30.00', $basket->getPrice()->getCosts() );
139
		$this->assertEquals( '0.00', $basket->getPrice()->getRebate() );
140
141
		$basket = $this->access( 'addBasketCoupons' )->invokeArgs( $this->object, [$this->context, $basket, map( ['90AB'] )] );
142
143
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
144
		$this->assertEquals( 2, count( $basket->getProducts() ) );
145
		$this->assertEquals( '537.00', $basket->getPrice()->getValue() );
146
		$this->assertEquals( '30.00', $basket->getPrice()->getCosts() );
147
		$this->assertEquals( '63.00', $basket->getPrice()->getRebate() );
148
	}
149
150
151
	public function testAddBasketProducts()
152
	{
153
		$basket = \Aimeos\MShop::create( $this->context, 'order' )->create();
154
		$product = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' );
155
		$manager = \Aimeos\MShop::create( $this->context, 'order/product' );
156
157
		$orderProducts = map( [
158
			$manager->create()->copyFrom( $product )->setId( 1 )->setStockType( 'default' ),
159
			$manager->create()->copyFrom( $product )->setId( 2 )->setStockType( 'default' ),
160
		] );
161
162
		$basket = $this->access( 'addBasketProducts' )->invokeArgs( $this->object, [$this->context, $basket, $orderProducts, 1] );
163
164
		$this->assertEquals( 1, count( $basket->getProducts() ) );
165
		$this->assertNull( $basket->getProduct( 0 )->getId() );
166
	}
167
168
169
	public function testAddBasketServices()
170
	{
171
		$basket = \Aimeos\MShop::create( $this->context, 'order' )->create();
172
		$manager = \Aimeos\MShop::create( $this->context, 'order/service' );
173
174
		$orderServices = map( [
175
			'delivery' => [$manager->create()->setCode( 'shiptest' )],
176
			'payment' => [$manager->create()->setCode( 'paytest' )],
177
		] );
178
179
		$basket = $this->access( 'addBasketServices' )->invokeArgs( $this->object, [$this->context, $basket, $orderServices] );
180
181
		$class = \Aimeos\MShop\Order\Item\Service\Iface::class;
182
183
		$this->assertEquals( 2, count( $basket->getServices() ) );
184
		$this->assertEquals( 1, count( $basket->getService( 'delivery' ) ) );
185
		$this->assertInstanceOf( $class, $basket->getService( 'delivery', 0 ) );
186
		$this->assertEquals( 1, count( $basket->getService( 'payment' ) ) );
187
		$this->assertInstanceOf( $class, $basket->getService( 'payment', 0 ) );
188
	}
189
190
191
	public function testCreateOrder()
192
	{
193
		$item = $this->getSubscription();
194
195
		$result = $this->access( 'createOrder' )->invokeArgs( $this->object, [$this->context, $item] );
196
197
		$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Iface::class, $result );
198
	}
199
200
201
	public function testCreatePayment()
202
	{
203
		$item = $this->getSubscription();
204
205
		$this->access( 'createPayment' )->invokeArgs( $this->object, [$this->context, $item->getOrderItem()] );
206
	}
207
208
209
	protected function getSubscription()
210
	{
211
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
212
		$search = $manager->filter()->add( 'subscription.dateend', '==', '2010-01-01' );
213
		$domains = ['order', 'order/address', 'order/coupon', 'order/product', 'order/service'];
214
215
		return $manager->search( $search, $domains )->first( new \Exception( 'No subscription item found' ) );
216
	}
217
218
219
	protected function access( $name )
220
	{
221
		$class = new \ReflectionClass( \Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard::class );
222
		$method = $class->getMethod( $name );
223
		$method->setAccessible( true );
224
225
		return $method;
226
	}
227
}
228