Completed
Push — master ( 32309c...101584 )
by Aimeos
03:33
created

StandardTest::testParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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