Passed
Push — master ( ee42b0...a87169 )
by Aimeos
07:53
created

StandardTest::testSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
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-2022
6
 */
7
8
9
namespace Aimeos\Controller\Frontend\Subscription;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $manager;
16
	private $object;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->context = \TestHelperFrontend::context();
22
		$this->context->setUserId( $this->getCustomerId() );
23
24
		$this->manager = $this->getMockBuilder( '\\Aimeos\\MShop\\Subscription\\Manager\\Standard' )
25
			->setConstructorArgs( [$this->context] )
26
			->setMethods( ['save'] )
27
			->getMock();
28
29
		\Aimeos\MShop::cache( true );
30
		\Aimeos\MShop::inject( 'subscription', $this->manager );
31
32
		$this->object = new \Aimeos\Controller\Frontend\Subscription\Standard( $this->context );
33
	}
34
35
36
	protected function tearDown() : void
37
	{
38
		\Aimeos\MShop::cache( false );
39
		unset( $this->object, $this->manager, $this->context );
40
	}
41
42
43
	public function testCancel()
44
	{
45
		$expected = \Aimeos\MShop\Subscription\Item\Iface::class;
46
		$item = \Aimeos\MShop::create( $this->context, 'subscription' )->create();
47
48
		$this->manager->expects( $this->once() )->method( 'save' )
49
			->will( $this->returnValue( $item ) );
50
51
		$this->assertInstanceOf( $expected, $this->object->cancel( $this->getSubscriptionId() ) );
52
	}
53
54
55
	public function testCompare()
56
	{
57
		$this->assertEquals( 2, count( $this->object->compare( '>=', 'subscription.datenext', '2000-01-01' )->search() ) );
58
	}
59
60
61
	public function testGet()
62
	{
63
		$expected = \Aimeos\MShop\Subscription\Item\Iface::class;
64
		$this->assertInstanceOf( $expected, $this->object->get( $this->getSubscriptionId() ) );
65
	}
66
67
68
	public function testGetException()
69
	{
70
		$this->expectException( \Aimeos\Controller\Frontend\Subscription\Exception::class );
71
		$this->object->get( -1 );
72
	}
73
74
75
	public function testGetIntervals()
76
	{
77
		$this->assertGreaterThan( 0, count( $this->object->getIntervals() ) );
78
	}
79
80
81
	public function testParse()
82
	{
83
		$cond = ['&&' => [['==' => ['subscription.datenext' => '2000-01-01']], ['==' => ['subscription.status' => 1]]]];
84
		$this->assertEquals( 1, count( $this->object->parse( $cond )->search() ) );
85
	}
86
87
88
	public function testSave()
89
	{
90
		$item = $this->manager->create();
91
		$expected = \Aimeos\MShop\Subscription\Item\Iface::class;
92
93
		$this->manager->expects( $this->once() )->method( 'save' )
94
			->will( $this->returnValue( $item ) );
95
96
		$this->assertInstanceOf( $expected, $this->object->save( $item ) );
97
	}
98
99
100
	public function testSearch()
101
	{
102
		$total = 0;
103
		$items = $this->object->uses( ['order/base'] )->search( $total );
104
105
		$this->assertGreaterThanOrEqual( 2, count( $items ) );
106
		$this->assertGreaterThanOrEqual( 2, $total );
107
108
		foreach( $items as $item ) {
109
			$this->assertInstanceOf( \Aimeos\MShop\Order\Item\Base\Iface::class, $item->getBaseItem() );
110
		}
111
	}
112
113
114
	public function testSlice()
115
	{
116
		$this->assertEquals( 1, count( $this->object->slice( 0, 1 )->search() ) );
117
	}
118
119
120
	public function testSort()
121
	{
122
		$this->assertEquals( 2, count( $this->object->sort()->search() ) );
123
	}
124
125
126
	public function testSortInterval()
127
	{
128
		$this->assertEquals( 2, count( $this->object->sort( 'interval' )->search() ) );
129
	}
130
131
132
	public function testSortGeneric()
133
	{
134
		$this->assertEquals( 2, count( $this->object->sort( 'subscription.dateend' )->search() ) );
135
	}
136
137
138
	public function testSortMultiple()
139
	{
140
		$this->assertEquals( 2, count( $this->object->sort( 'subscription.dateend,-subscription.id' )->search() ) );
141
	}
142
143
144
	public function testUses()
145
	{
146
		$this->assertSame( $this->object, $this->object->uses( ['order/base'] ) );
147
	}
148
149
150
	protected function getCustomerId()
151
	{
152
		return \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId();
153
	}
154
155
156
	protected function getSubscriptionId()
157
	{
158
		$manager = \Aimeos\MShop::create( $this->context, 'subscription' );
159
160
		$search = $manager->filter()->slice( 0, 1 );
161
		$search->setConditions( $search->compare( '==', 'order.base.customerid', $this->context->user() ) );
162
163
		if( ( $item = $manager->search( $search )->first() ) === null ) {
164
			throw new \RuntimeException( 'No subscription item found' );
165
		}
166
167
		return $item->getId();
168
	}
169
}
170