Completed
Push — master ( 44d316...b85694 )
by Aimeos
02:03
created

StandardTest::testAddCoupons()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
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()
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\Factory::setCache( true );
27
	}
28
29
30
	protected function tearDown()
31
	{
32
		\Aimeos\MShop\Factory::setCache( false );
33
		\Aimeos\MShop\Factory::clear();
34
35
		unset( $this->object, $this->context, $this->aimeos );
36
	}
37
38
39
	public function testGetName()
40
	{
41
		$this->assertEquals( 'Subscription process renew', $this->object->getName() );
42
	}
43
44
45
	public function testGetDescription()
46
	{
47
		$this->assertEquals( 'Renews subscriptions at next date', $this->object->getDescription() );
48
	}
49
50
51
	public function testRun()
52
	{
53
		$this->context->getConfig()->set( 'controller/common/subscription/process/processors', ['cgroup'] );
54
		$item = $this->getSubscription();
55
56
		$object = $this->getMockBuilder( '\\Aimeos\\Controller\\Jobs\\Subscription\\Process\\Renew\\Standard' )
57
			->setConstructorArgs( [$this->context, $this->aimeos] )
58
			->setMethods( ['createOrderBase', 'createOrderInvoice', 'createPayment'] )
59
			->getMock();
60
61
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
62
			->setConstructorArgs( [$this->context] )
63
			->setMethods( ['searchItems', 'saveItem'] )
64
			->getMock();
65
66
		\Aimeos\MShop\Factory::injectManager( $this->context, 'subscription', $managerStub );
67
68
		$object->expects( $this->once() )->method( 'createOrderBase' )
69
			->will( $this->returnValue( $this->getOrderBaseItem( $item->getOrderBaseId() ) ) );
70
71
		$object->expects( $this->once() )->method( 'createOrderInvoice' )
72
			->will( $this->returnValue( $this->getOrderItem() ) );
73
74
		$object->expects( $this->once() )->method( 'createPayment' );
75
76
		$managerStub->expects( $this->once() )->method( 'searchItems' )
77
			->will( $this->returnValue( [$item] ) );
78
79
		$managerStub->expects( $this->once() )->method( 'saveItem' );
80
81
		$object->run();
82
	}
83
84
85
	public function testRunException()
86
	{
87
		$this->context->getConfig()->set( 'controller/common/subscription/process/processors', ['cgroup'] );
88
89
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
90
			->setConstructorArgs( [$this->context] )
91
			->setMethods( ['searchItems', 'saveItem'] )
92
			->getMock();
93
94
		\Aimeos\MShop\Factory::injectManager( $this->context, 'subscription', $managerStub );
95
96
		$managerStub->expects( $this->once() )->method( 'searchItems' )
97
			->will( $this->returnValue( [$managerStub->createItem()] ) );
98
99
		$managerStub->expects( $this->never() )->method( 'saveItem' );
100
101
		$this->object->run();
102
	}
103
104
105
	public function testAddCoupons()
106
	{
107
		$this->context->getConfig()->set( 'controller/jobs/subcription/process/renew/standard/use-coupons', true );
108
109
		$basket = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' )->createItem();
110
		$product = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC', ['price'] );
111
		$orderProduct = \Aimeos\MShop\Factory::createManager( $this->context, 'order/base/product' )->createItem();
112
113
		$price = $product->getRefItems( 'price', 'default', 'default' );
114
		$basket->addProduct( $orderProduct->copyFrom( $product )->setPrice( reset( $price ) ) );
0 ignored issues
show
Bug introduced by
The method copyFrom() does not seem to exist on object<Aimeos\MShop\Attribute\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method addProduct() does not seem to exist on object<Aimeos\MShop\Attribute\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
116
		$this->assertEquals( '600.00', $basket->getPrice()->getValue() );
0 ignored issues
show
Bug introduced by
The method getPrice() does not seem to exist on object<Aimeos\MShop\Attribute\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
		$this->assertEquals( '30.00', $basket->getPrice()->getCosts() );
0 ignored issues
show
Bug introduced by
The method getPrice() does not seem to exist on object<Aimeos\MShop\Attribute\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
		$this->assertEquals( '0.00', $basket->getPrice()->getRebate() );
0 ignored issues
show
Bug introduced by
The method getPrice() does not seem to exist on object<Aimeos\MShop\Attribute\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120
		$basket = $this->access( 'addBasketCoupons' )->invokeArgs( $this->object, [$this->context, $basket, ['90AB']] );
121
122
		$this->assertEquals( 1, count( $basket->getCoupons() ) );
123
		$this->assertEquals( 2, count( $basket->getProducts() ) );
124
		$this->assertEquals( '537.00', $basket->getPrice()->getValue() );
125
		$this->assertEquals( '30.00', $basket->getPrice()->getCosts() );
126
		$this->assertEquals( '63.00', $basket->getPrice()->getRebate() );
127
	}
128
129
130
	public function testCreateOrderBase()
131
	{
132
		$item = $this->getSubscription();
133
134
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Standard' )
135
			->setConstructorArgs( [$this->context] )
136
			->setMethods( ['store'] )
137
			->getMock();
138
139
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order/base', $managerStub );
140
141
		$managerStub->expects( $this->once() )->method( 'store' );
142
143
		$this->access( 'createOrderBase' )->invokeArgs( $this->object, [$this->context, $item] );
144
	}
145
146
147
	public function testCreateOrderInvoice()
148
	{
149
		$item = $this->getSubscription();
150
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
151
152
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
153
			->setConstructorArgs( [$this->context] )
154
			->setMethods( ['saveItem'] )
155
			->getMock();
156
157
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order', $managerStub );
158
159
		$managerStub->expects( $this->once() )->method( 'saveItem' );
160
161
		$this->access( 'createOrderInvoice' )->invokeArgs( $this->object, [$this->context, $baseItem] );
162
	}
163
164
165
	public function testCreatePayment()
166
	{
167
		$item = $this->getSubscription();
168
		$invoice = $this->getOrderItem();
169
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
170
171
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
172
			->setConstructorArgs( [$this->context] )
173
			->setMethods( ['saveItem'] )
174
			->getMock();
175
176
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order', $managerStub );
177
178
		$managerStub->expects( $this->once() )->method( 'saveItem' );
179
180
		$this->access( 'createPayment' )->invokeArgs( $this->object, [$this->context, $baseItem, $invoice] );
181
	}
182
183
184
	protected function getOrderItem()
185
	{
186
		return \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
187
	}
188
189
190
	protected function getOrderBaseItem( $baseId )
191
	{
192
		return \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' )->getItem( $baseId, ['order/base/service'] );
193
	}
194
195
196
	protected function getSubscription()
197
	{
198
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'subscription' );
199
200
		$search = $manager->createSearch();
201
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
202
203
		$items = $manager->searchItems( $search );
204
205
		if( ( $item = reset( $items ) ) !== false ) {
206
			return $item;
207
		}
208
209
		throw new \Exception( 'No subscription item found' );
210
	}
211
212
213
	protected function access( $name )
214
	{
215
		$class = new \ReflectionClass( '\Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard' );
216
		$method = $class->getMethod( $name );
217
		$method->setAccessible( true );
218
219
		return $method;
220
	}
221
}
222