Completed
Push — master ( b0e633...7bc86c )
by Aimeos
04:36
created

StandardTest::testCreatePayment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 11
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 testCreateOrderBase()
106
	{
107
		$item = $this->getSubscription();
108
109
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Standard' )
110
			->setConstructorArgs( [$this->context] )
111
			->setMethods( ['store'] )
112
			->getMock();
113
114
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order/base', $managerStub );
115
116
		$managerStub->expects( $this->once() )->method( 'store' );
117
118
		$this->access( 'createOrderBase' )->invokeArgs( $this->object, [$this->context, $item] );
119
	}
120
121
122
	public function testCreateOrderInvoice()
123
	{
124
		$item = $this->getSubscription();
125
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
126
127
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
128
			->setConstructorArgs( [$this->context] )
129
			->setMethods( ['saveItem'] )
130
			->getMock();
131
132
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order', $managerStub );
133
134
		$managerStub->expects( $this->once() )->method( 'saveItem' );
135
136
		$this->access( 'createOrderInvoice' )->invokeArgs( $this->object, [$this->context, $baseItem] );
137
	}
138
139
140
	public function testCreatePayment()
141
	{
142
		$item = $this->getSubscription();
143
		$invoice = $this->getOrderItem();
144
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
145
146
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
147
			->setConstructorArgs( [$this->context] )
148
			->setMethods( ['saveItem'] )
149
			->getMock();
150
151
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order', $managerStub );
152
153
		$managerStub->expects( $this->once() )->method( 'saveItem' );
154
155
		$this->access( 'createPayment' )->invokeArgs( $this->object, [$this->context, $baseItem, $invoice] );
156
	}
157
158
159
	protected function getOrderItem()
160
	{
161
		return \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
162
	}
163
164
165
	protected function getOrderBaseItem( $baseId )
166
	{
167
		return \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' )->getItem( $baseId, ['order/base/service'] );
168
	}
169
170
171
	protected function getSubscription()
172
	{
173
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'subscription' );
174
175
		$search = $manager->createSearch();
176
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
177
178
		$items = $manager->searchItems( $search );
179
180
		if( ( $item = reset( $items ) ) !== false ) {
181
			return $item;
182
		}
183
184
		throw new \Exception( 'No subscription item found' );
185
	}
186
187
188
	protected function access( $name )
189
	{
190
		$class = new \ReflectionClass( '\Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard' );
191
		$method = $class->getMethod( $name );
192
		$method->setAccessible( true );
193
194
		return $method;
195
	}
196
}
197