Completed
Push — master ( a0f3ba...4d0a57 )
by Aimeos
03:07
created

StandardTest::getSubscription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
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'] )
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
		$managerStub->expects( $this->once() )->method( 'searchItems' )
75
			->will( $this->returnValue( [$item] ) );
76
77
		$managerStub->expects( $this->once() )->method( 'saveItem' );
78
79
		$object->run();
80
	}
81
82
83
	public function testRunException()
84
	{
85
		$this->context->getConfig()->set( 'controller/common/subscription/process/processors', ['cgroup'] );
86
87
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
88
			->setConstructorArgs( [$this->context] )
89
			->setMethods( ['searchItems', 'saveItem'] )
90
			->getMock();
91
92
		\Aimeos\MShop\Factory::injectManager( $this->context, 'subscription', $managerStub );
93
94
		$managerStub->expects( $this->once() )->method( 'searchItems' )
95
			->will( $this->returnValue( [$managerStub->createItem()] ) );
96
97
		$managerStub->expects( $this->never() )->method( 'saveItem' );
98
99
		$this->object->run();
100
	}
101
102
103
	public function testCreateOrderBase()
104
	{
105
		$item = $this->getSubscription();
106
107
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Standard' )
108
			->setConstructorArgs( [$this->context] )
109
			->setMethods( ['store'] )
110
			->getMock();
111
112
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order/base', $managerStub );
113
114
		$managerStub->expects( $this->once() )->method( 'store' );
115
116
		$this->access( 'createOrderBase' )->invokeArgs( $this->object, [$this->context, $item] );
117
	}
118
119
120
	public function testCreateOrderInvoice()
121
	{
122
		$item = $this->getSubscription();
123
		$baseItem = $this->getOrderBaseItem( $item->getOrderBaseId() );
124
125
		$managerStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Standard' )
126
			->setConstructorArgs( [$this->context] )
127
			->setMethods( ['saveItem'] )
128
			->getMock();
129
130
		\Aimeos\MShop\Factory::injectManager( $this->context, 'order', $managerStub );
131
132
		$managerStub->expects( $this->once() )->method( 'saveItem' );
133
134
		$this->access( 'createOrderInvoice' )->invokeArgs( $this->object, [$this->context, $baseItem] );
135
	}
136
137
138
	protected function getOrderItem()
139
	{
140
		return \Aimeos\MShop\Factory::createManager( $this->context, 'order' )->createItem();
141
	}
142
143
144
	protected function getOrderBaseItem( $baseId )
145
	{
146
		return \Aimeos\MShop\Factory::createManager( $this->context, 'order/base' )->getItem( $baseId, ['order/base/service'] );
147
	}
148
149
150
	protected function getSubscription()
151
	{
152
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'subscription' );
153
154
		$search = $manager->createSearch();
155
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
156
157
		$items = $manager->searchItems( $search );
158
159
		if( ( $item = reset( $items ) ) !== false ) {
160
			return $item;
161
		}
162
163
		throw new \Exception( 'No subscription item found' );
164
	}
165
166
167
	protected function access( $name )
168
	{
169
		$class = new \ReflectionClass( '\Aimeos\Controller\Jobs\Subscription\Process\Renew\Standard' );
170
		$method = $class->getMethod( $name );
171
		$method->setAccessible( true );
172
173
		return $method;
174
	}
175
}
176