StandardTest::testSortGeneric()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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