Completed
Push — master ( a81d65...b2e212 )
by Aimeos
03:54
created

StandardTest::testEndCustomGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
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
namespace Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup;
9
10
11
class StandardTest extends \PHPUnit\Framework\TestCase
12
{
13
	private $context;
14
	private $custStub;
15
16
17
	protected function setUp() : void
18
	{
19
		\Aimeos\MShop::cache( true );
20
21
		$this->context = \TestHelperCntl::getContext();
22
		$this->context->getConfig()->set( 'controller/common/subscription/process/processor/cgroup/groupids', ['1', '2'] );
23
24
		$this->custStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Customer\\Manager\\Standard' )
25
			->setConstructorArgs( [$this->context] )
26
			->setMethods( ['getItem', 'saveItem'] )
27
			->getMock();
28
29
		\Aimeos\MShop::inject( 'customer', $this->custStub );
30
31
		$this->custStub->expects( $this->once() )->method( 'getItem' )
32
			->will( $this->returnValue( $this->custStub->createItem() ) );
33
	}
34
35
36
	protected function tearDown() : void
37
	{
38
		\Aimeos\MShop::cache( false );
39
		unset( $this->context );
40
	}
41
42
43
	public function testBegin()
44
	{
45
		$ordProdStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Product\\Standard' )
46
			->setConstructorArgs( [$this->context] )
47
			->setMethods( ['getItem'] )
48
			->getMock();
49
50
		\Aimeos\MShop::inject( 'order/base/product', $ordProdStub );
51
52
		$ordProdAttrManager = $ordProdStub->getSubManager( 'attribute' );
53
		$ordProdAttrItem = $ordProdAttrManager->createItem()->setType( 'hidden' )->setCode( 'customer/group' );
54
55
		$ordProdItem = $ordProdStub->createItem()->setAttributeItems( [
56
			( clone $ordProdAttrItem )->setAttributeId( 10 )->setValue( '3' ),
57
			( clone $ordProdAttrItem )->setAttributeId( 11 )->setValue( '4' ),
58
		] );
59
60
		$ordProdStub->expects( $this->once() )->method( 'getItem' )
61
			->will( $this->returnValue( $ordProdItem ) );
62
63
		$this->custStub->expects( $this->once() )->method( 'saveItem' )
64
			->with( $this->callback( function( $subject ) {
65
				return $subject->getGroups() === ['3', '4'];
66
			} ) );
67
68
69
		$object = new \Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup\Standard( $this->context );
70
		$object->begin( $this->getSubscription() );
0 ignored issues
show
Bug introduced by
It seems like $this->getSubscription() can also be of type null; however, parameter $subscription of Aimeos\Controller\Common...group\Standard::begin() does only seem to accept Aimeos\MShop\Subscription\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
		$object->begin( /** @scrutinizer ignore-type */ $this->getSubscription() );
Loading history...
71
	}
72
73
74
	public function testBeginCustomGroups()
75
	{
76
		$this->custStub->expects( $this->once() )->method( 'saveItem' )
77
			->with( $this->callback( function( $subject ) {
78
				return $subject->getGroups() === ['1', '2'];
79
			} ) );
80
81
82
		$object = new \Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup\Standard( $this->context );
83
		$object->begin( $this->getSubscription() );
0 ignored issues
show
Bug introduced by
It seems like $this->getSubscription() can also be of type null; however, parameter $subscription of Aimeos\Controller\Common...group\Standard::begin() does only seem to accept Aimeos\MShop\Subscription\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
		$object->begin( /** @scrutinizer ignore-type */ $this->getSubscription() );
Loading history...
84
	}
85
86
87
	public function testEnd()
88
	{
89
		$ordProdStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Order\\Manager\\Base\\Product\\Standard' )
90
			->setConstructorArgs( [$this->context] )
91
			->setMethods( ['getItem'] )
92
			->getMock();
93
94
		\Aimeos\MShop::inject( 'order/base/product', $ordProdStub );
95
96
		$ordProdAttrManager = $ordProdStub->getSubManager( 'attribute' );
97
		$ordProdAttrItem = $ordProdAttrManager->createItem()->setType( 'hidden' )->setCode( 'customer/group' );
98
99
		$ordProdItem = $ordProdStub->createItem()->setAttributeItems( [
100
			( clone $ordProdAttrItem )->setAttributeId( 10 )->setValue( '3' ),
101
			( clone $ordProdAttrItem )->setAttributeId( 11 )->setValue( '4' ),
102
		] );
103
104
		$ordProdStub->expects( $this->once() )->method( 'getItem' )
105
			->will( $this->returnValue( $ordProdItem ) );
106
107
		$this->custStub->expects( $this->once() )->method( 'saveItem' )
108
			->with( $this->callback( function( $subject ) {
109
				return $subject->getGroups() === [];
110
			} ) );
111
112
		$object = new \Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup\Standard( $this->context );
113
		$object->end( $this->getSubscription() );
0 ignored issues
show
Bug introduced by
It seems like $this->getSubscription() can also be of type null; however, parameter $subscription of Aimeos\Controller\Common...\Cgroup\Standard::end() does only seem to accept Aimeos\MShop\Subscription\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
		$object->end( /** @scrutinizer ignore-type */ $this->getSubscription() );
Loading history...
114
	}
115
116
117
	public function testEndCustomGroups()
118
	{
119
		$this->custStub->expects( $this->once() )->method( 'saveItem' )
120
			->with( $this->callback( function( $subject ) {
121
				return $subject->getGroups() === [];
122
			} ) );
123
124
		$object = new \Aimeos\Controller\Common\Subscription\Process\Processor\Cgroup\Standard( $this->context );
125
		$object->end( $this->getSubscription() );
0 ignored issues
show
Bug introduced by
It seems like $this->getSubscription() can also be of type null; however, parameter $subscription of Aimeos\Controller\Common...\Cgroup\Standard::end() does only seem to accept Aimeos\MShop\Subscription\Item\Iface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
		$object->end( /** @scrutinizer ignore-type */ $this->getSubscription() );
Loading history...
126
	}
127
128
129
	protected function getSubscription()
130
	{
131
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
132
133
		$search = $manager->createSearch();
134
		$search->setConditions( $search->compare( '==', 'subscription.dateend', '2010-01-01' ) );
135
136
		if( ( $item = $manager->searchItems( $search )->first() ) !== null ) {
137
			return $item;
138
		}
139
140
		throw new \Exception( 'No subscription item found' );
141
	}
142
}
143