Completed
Push — master ( 56f888...ae1907 )
by Aimeos
03:01
created

StandardTest::testFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
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), 2020
6
 */
7
8
9
namespace Aimeos\Controller\Frontend\Review;
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::getContext();
22
23
		$this->manager = $this->getMockBuilder( '\\Aimeos\\MShop\\Review\\Manager\\Standard' )
24
			->setConstructorArgs( [$this->context] )
25
			->setMethods( ['delete', 'save'] )
26
			->getMock();
27
28
		\Aimeos\MShop::cache( true );
29
		\Aimeos\MShop::inject( 'review', $this->manager );
30
31
		$this->object = new \Aimeos\Controller\Frontend\Review\Standard( $this->context );
32
	}
33
34
35
	protected function tearDown() : void
36
	{
37
		\Aimeos\MShop::cache( false );
38
		unset( $this->object, $this->manager, $this->context );
39
	}
40
41
42
	public function testAggregate()
43
	{
44
		$list = $this->object->domain( 'product' )->aggregate( 'review.rating' );
45
46
		$this->assertEquals( 1, count( $list ) );
47
		$this->assertEquals( 1, $list[4] );
48
	}
49
50
51
	public function testCompare()
52
	{
53
		$this->assertCount( 1, $this->object->compare( '==', 'review.domain', 'customer' )->search() );
54
	}
55
56
57
	public function testCreate()
58
	{
59
		$item = $this->object->create( ['review.rating' => 5] );
60
		$this->assertEquals( 5, $item->getRating() );
61
	}
62
63
64
	public function testDelete()
65
	{
66
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
67
68
		$this->manager->expects( $this->once() )->method( 'delete' );
69
70
		$this->assertSame( $this->object, $this->object->delete( $this->getReviewItem()->getId() ) );
71
	}
72
73
74
	public function testDomain()
75
	{
76
		$this->assertCount( 1, $this->object->domain( 'customer' )->search() );
77
	}
78
79
80
	public function testFor()
81
	{
82
		$refId = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNE' )->getId();
83
		$result = $this->object->for( 'product', $refId );
84
85
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Review\Iface::class, $result );
86
		$this->assertCount( 1, $result->search() );
87
	}
88
89
90
	public function testForDomain()
91
	{
92
		$result = $this->object->for( 'product', null );
93
94
		$this->assertInstanceOf( \Aimeos\Controller\Frontend\Review\Iface::class, $result );
95
		$this->assertCount( 1, $result->search() );
96
	}
97
98
99
	public function testGet()
100
	{
101
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
102
		$result = $this->object->get( $this->getReviewItem() );
103
		$this->assertInstanceOf( \Aimeos\MShop\Review\Item\Iface::class, $result );
104
	}
105
106
107
	public function testList()
108
	{
109
		$total = 0;
110
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
111
112
		$this->assertEquals( 3, count( $this->object->slice( 0, 3 )->list( $total ) ) );
113
		$this->assertGreaterThanOrEqual( 4, $total );
114
115
		$this->assertEquals( 1, count( $this->object->domain( 'product' )->slice( 0, 1 )->list( $total ) ) );
116
		$this->assertGreaterThanOrEqual( 2, $total );
117
	}
118
119
120
	public function testParse()
121
	{
122
		$cond = ['&&' => [['==' => ['review.domain' => 'customer']], ['==' => ['review.status' => 1]]]];
123
		$this->assertEquals( 1, count( $this->object->parse( $cond )->search() ) );
124
	}
125
126
127
	public function testSave()
128
	{
129
		$customerId = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId();
130
		$this->context->setUserId( $customerId );
131
		$item = $this->getReviewItem();
132
133
		$this->manager->expects( $this->once() )->method( 'save' )
134
			->will( $this->returnValue( $item ) );
135
136
		$this->assertInstanceOf( \Aimeos\MShop\Review\Item\Iface::class, $this->object->save( $item ) );
137
	}
138
139
140
	public function testSaveCreate()
141
	{
142
		$customerId = \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId();
143
		$this->context->setUserId( $customerId );
144
145
		$item = $this->object->create( $this->getReviewItem()->setId( null )->toArray( true ) );
146
147
		$this->manager->expects( $this->once() )->method( 'save' )
148
			->will( $this->returnValue( ( clone $item )->setId( 123 ) ) );
149
150
		$this->assertInstanceOf( \Aimeos\MShop\Review\Item\Iface::class, $this->object->save( $item ) );
151
	}
152
153
154
	public function testSaveInvalidDomain()
155
	{
156
		$this->expectException( \Aimeos\Controller\Frontend\Review\Exception::class );
157
		$this->object->save( $this->getReviewItem()->setDomain( 'invalid' ) );
158
	}
159
160
161
	public function testSaveInvalidOrderProductId()
162
	{
163
		$this->expectException( \Aimeos\Controller\Frontend\Review\Exception::class );
164
		$this->object->save( $this->getReviewItem()->setOrderProductId( 0 ) );
165
	}
166
167
168
	public function testSaveException()
169
	{
170
		$this->expectException( \Aimeos\Controller\Frontend\Review\Exception::class );
171
		$this->object->save( $this->manager->createItem() );
172
	}
173
174
175
	public function testSearch()
176
	{
177
		$total = 0;
178
		$this->assertGreaterThanOrEqual( 2, count( $this->object->search( $total ) ) );
179
		$this->assertGreaterThanOrEqual( 2, $total );
180
	}
181
182
183
	public function testSlice()
184
	{
185
		$this->assertEquals( 1, count( $this->object->slice( 0, 1 )->search() ) );
186
	}
187
188
189
	public function testSort()
190
	{
191
		$this->assertEquals( 2, count( $this->object->sort()->search() ) );
192
	}
193
194
195
	public function testSortCtime()
196
	{
197
		$this->assertEquals( 2, count( $this->object->sort( 'ctime' )->search() ) );
198
	}
199
200
201
	public function testSortRating()
202
	{
203
		$this->assertEquals( 2, count( $this->object->sort( 'rating' )->search() ) );
204
	}
205
206
207
	public function testSortGeneric()
208
	{
209
		$this->assertEquals( 2, count( $this->object->sort( 'review.mtime' )->search() ) );
210
	}
211
212
213
	protected function getReviewItem()
214
	{
215
		$manager = \Aimeos\MShop::create( $this->context, 'review' );
216
217
		$search = $manager->createSearch()->setSlice( 0, 1 );
218
		$search->setConditions( $search->combine( '&&', [
219
			$search->compare( '==', 'review.domain', 'product' ),
220
			$search->compare( '>', 'review.status', 0 )
221
		] ) );
222
223
		return $manager->searchItems( $search )->first( new \RuntimeException( 'No review item found' ) );
224
	}
225
}
226