Completed
Push — master ( a78af5...025dfa )
by Aimeos
04:56
created

StandardTest::testSortCtime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
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 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
		$expected = \Aimeos\MShop\Review\Item\Iface::class;
93
		$this->assertInstanceOf( $expected, $this->object->get( $this->getReviewItem()->getId() ) );
94
	}
95
96
97
	public function testList()
98
	{
99
		$total = 0;
100
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
101
102
		$this->assertEquals( 3, count( $this->object->slice( 0, 3 )->list( $total ) ) );
103
		$this->assertGreaterThanOrEqual( 4, $total );
104
105
		$this->assertEquals( 1, count( $this->object->domain( 'product' )->slice( 0, 1 )->list( $total ) ) );
106
		$this->assertGreaterThanOrEqual( 2, $total );
107
	}
108
109
110
	public function testParse()
111
	{
112
		$cond = ['&&' => [['==' => ['review.domain' => 'customer']], ['==' => ['review.status' => 1]]]];
113
		$this->assertEquals( 1, count( $this->object->parse( $cond )->search() ) );
114
	}
115
116
117
	public function testSave()
118
	{
119
		$item = $this->getReviewItem();
120
		$expected = \Aimeos\MShop\Review\Item\Iface::class;
121
		$this->context->setUserId( \Aimeos\MShop::create( $this->context, 'customer' )->find( '[email protected]' )->getId() );
122
123
		$this->manager->expects( $this->once() )->method( 'saveItem' )
124
			->will( $this->returnValue( $item ) );
125
126
		$this->assertInstanceOf( $expected, $this->object->save( $item ) );
127
	}
128
129
130
	public function testSaveException()
131
	{
132
		$this->expectException( \Aimeos\Controller\Frontend\Review\Exception::class );
133
		$this->object->save( $this->manager->createItem() );
134
	}
135
136
137
	public function testSearch()
138
	{
139
		$total = 0;
140
		$this->assertGreaterThanOrEqual( 2, count( $this->object->search( $total ) ) );
141
		$this->assertGreaterThanOrEqual( 2, $total );
142
	}
143
144
145
	public function testSlice()
146
	{
147
		$this->assertEquals( 1, count( $this->object->slice( 0, 1 )->search() ) );
148
	}
149
150
151
	public function testSort()
152
	{
153
		$this->assertEquals( 2, count( $this->object->sort()->search() ) );
154
	}
155
156
157
	public function testSortCtime()
158
	{
159
		$this->assertEquals( 2, count( $this->object->sort( 'ctime' )->search() ) );
160
	}
161
162
163
	public function testSortRating()
164
	{
165
		$this->assertEquals( 2, count( $this->object->sort( 'rating' )->search() ) );
166
	}
167
168
169
	public function testSortGeneric()
170
	{
171
		$this->assertEquals( 2, count( $this->object->sort( 'review.mtime' )->search() ) );
172
	}
173
174
175
	protected function getReviewItem()
176
	{
177
		$manager = \Aimeos\MShop::create( $this->context, 'review' );
178
179
		$search = $manager->createSearch()->setSlice( 0, 1 );
180
		$search->setConditions( $search->combine( '&&', [
181
			$search->compare( '==', 'review.domain', 'product' ),
182
			$search->compare( '>', 'review.status', 0 )
183
		] ) );
184
185
		return $manager->searchItems( $search )->first( new \RuntimeException( 'No review item found' ) );
186
	}
187
}
188