Passed
Push — master ( 8b8a73...4b518d )
by Aimeos
03:02
created

StandardTest::testGet()   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', 'saveItem'] )
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 testDelete()
49
	{
50
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
51
52
		$this->manager->expects( $this->once() )->method( 'delete' );
53
54
		$this->assertSame( $this->object, $this->object->delete( $this->getReviewItem()->getId() ) );
55
	}
56
57
58
	public function testDeleteException()
59
	{
60
		$this->expectException( \Aimeos\Controller\Frontend\Review\Exception::class );
61
		$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 testGet()
82
	{
83
		$expected = \Aimeos\MShop\Review\Item\Iface::class;
84
		$this->assertInstanceOf( $expected, $this->object->get( $this->getReviewItem()->getId() ) );
85
	}
86
87
88
	public function testList()
89
	{
90
		$total = 0;
91
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
92
93
		$this->assertEquals( 3, count( $this->object->slice( 0, 3 )->list( $total ) ) );
94
		$this->assertGreaterThanOrEqual( 4, $total );
95
96
		$this->assertEquals( 1, count( $this->object->domain( 'product' )->slice( 0, 1 )->list( $total ) ) );
97
		$this->assertGreaterThanOrEqual( 2, $total );
98
	}
99
100
101
	public function testParse()
102
	{
103
		$cond = ['&&' => [['==' => ['review.domain' => 'customer']], ['==' => ['review.status' => 1]]]];
104
		$this->assertEquals( 1, count( $this->object->parse( $cond )->search() ) );
105
	}
106
107
108
	public function testSave()
109
	{
110
		$item = $this->getReviewItem();
111
		$expected = \Aimeos\MShop\Review\Item\Iface::class;
112
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
113
114
		$this->manager->expects( $this->once() )->method( 'saveItem' )
115
			->will( $this->returnValue( $item ) );
116
117
		$this->assertInstanceOf( $expected, $this->object->save( $item ) );
118
	}
119
120
121
	public function testSaveException()
122
	{
123
		$this->expectException( \Aimeos\Controller\Frontend\Review\Exception::class );
124
		$this->object->save( $this->manager->createItem() );
125
	}
126
127
128
	public function testSearch()
129
	{
130
		$total = 0;
131
		$this->assertGreaterThanOrEqual( 2, count( $this->object->search( $total ) ) );
132
		$this->assertGreaterThanOrEqual( 2, $total );
133
	}
134
135
136
	public function testSlice()
137
	{
138
		$this->assertEquals( 1, count( $this->object->slice( 0, 1 )->search() ) );
139
	}
140
141
142
	public function testSort()
143
	{
144
		$this->assertEquals( 2, count( $this->object->sort()->search() ) );
145
	}
146
147
148
	public function testSortMtime()
149
	{
150
		$this->assertEquals( 2, count( $this->object->sort( 'mtime' )->search() ) );
151
	}
152
153
154
	public function testSortRating()
155
	{
156
		$this->assertEquals( 2, count( $this->object->sort( 'rating' )->search() ) );
157
	}
158
159
160
	public function testSortGeneric()
161
	{
162
		$this->assertEquals( 2, count( $this->object->sort( 'review.customerid' )->search() ) );
163
	}
164
165
166
	protected function getReviewItem()
167
	{
168
		$manager = \Aimeos\MShop::create( $this->context, 'review' );
169
170
		$search = $manager->createSearch()->setSlice( 0, 1 );
171
		$search->setConditions( $search->combine( '&&', [
172
			$search->compare( '==', 'review.domain', 'product' ),
173
			$search->compare( '>', 'review.status', 0 )
174
		] ) );
175
176
		return $manager->searchItems( $search )->first( new \RuntimeException( 'No review item found' ) );
177
	}
178
}
179