Passed
Push — master ( f25c12...e9f33e )
by Aimeos
03:32
created

StandardTest::testBatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2022
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Review;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
13
{
14
	private $context;
15
	private $object;
16
	private $view;
17
18
19
	protected function setUp() : void
20
	{
21
		$this->view = \TestHelper::view();
22
		$this->view ->access( ['super', 'admin', 'test'] );
23
24
		$this->context = \TestHelper::context();
25
26
		$this->object = new \Aimeos\Admin\JQAdm\Review\Standard( $this->context );
27
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
28
		$this->object->setAimeos( \TestHelper::getAimeos() );
29
		$this->object->setView( $this->view );
30
	}
31
32
33
	protected function tearDown() : void
34
	{
35
		unset( $this->object, $this->view, $this->context );
36
	}
37
38
39
	public function testBatch()
40
	{
41
		$param = array(
42
			'site' => 'unittest',
43
			'item' => array(
44
				'review.status' => -1,
45
			),
46
			'id' => [-1, -2]
47
		);
48
49
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
50
		$this->view->addHelper( 'param', $helper );
51
52
		$result = $this->object->batch();
53
54
		$this->assertEmpty( $this->view->get( 'errors' ) );
55
		$this->assertNull( $result );
56
	}
57
58
59
	public function testDelete()
60
	{
61
		$this->assertEmpty( $this->getClientMock( ['redirect'], false )->delete() );
62
	}
63
64
65
	public function testDeleteException()
66
	{
67
		$object = $this->getClientMock( ['getSubClients', 'search'] );
68
69
		$object->expects( $this->once() )->method( 'getSubClients' )
70
			->will( $this->throwException( new \RuntimeException() ) );
71
		$object->expects( $this->once() )->method( 'search' );
72
73
		$object->delete();
74
	}
75
76
77
	public function testGet()
78
	{
79
		$param = ['site' => 'unittest', 'id' => $this->getItem()->getId()];
80
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
81
		$this->view->addHelper( 'param', $helper );
82
83
		$result = $this->object->get();
84
85
		$this->assertStringContainsString( 'an [email protected] comment', $result );
86
	}
87
88
89
	public function testGetException()
90
	{
91
		$object = $this->getClientMock( 'getSubClients' );
92
93
		$object->expects( $this->once() )->method( 'getSubClients' )
94
			->will( $this->throwException( new \RuntimeException() ) );
95
96
		$object->get();
97
	}
98
99
100
	public function testSave()
101
	{
102
		$manager = \Aimeos\MShop::create( $this->context, 'review' );
103
		$item = $manager->save( $manager->create()->setDomain( 'product' )->setRefId( '-1' ) );
104
105
		$param = array(
106
			'site' => 'unittest',
107
			'item' => array(
108
				'review.id' => $item->getId(),
109
				'review.name' => 'test name',
110
				'review.response' => 'test response',
111
				'review.status' => '-2',
112
			),
113
		);
114
115
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
116
		$this->view->addHelper( 'param', $helper );
117
118
		$result = $this->object->save();
119
120
		$manager->delete( $item );
121
122
		$this->assertEmpty( $this->view->get( 'errors' ) );
123
124
		$this->assertNull( $result );
125
	}
126
127
128
	public function testSaveException()
129
	{
130
		$object = $this->getClientMock( 'fromArray' );
131
132
		$object->expects( $this->once() )->method( 'fromArray' )
133
			->will( $this->throwException( new \RuntimeException() ) );
134
135
		$object->save();
136
	}
137
138
139
	public function testSearch()
140
	{
141
		$param = array(
142
			'site' => 'unittest', 'locale' => 'de',
143
			'filter' => array(
144
				'key' => array( 0 => 'review.rating' ),
145
				'op' => array( 0 => '==' ),
146
				'val' => array( 0 => '4' ),
147
			),
148
			'sort' => array( '-review.ctime', '-review.id' ),
149
		);
150
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
151
		$this->view->addHelper( 'param', $helper );
152
153
		$result = $this->object->search();
154
155
		$this->assertStringContainsString( '>an [email protected] comment<', $result );
156
	}
157
158
159
	public function testSearchException()
160
	{
161
		$object = $this->getClientMock( 'initCriteria' );
162
163
		$object->expects( $this->once() )->method( 'initCriteria' )
164
			->will( $this->throwException( new \RuntimeException() ) );
165
166
		$object->search();
167
	}
168
169
170
	public function testGetSubClientInvalid()
171
	{
172
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
173
		$this->object->getSubClient( '$unknown$' );
174
	}
175
176
177
	public function testGetSubClientUnknown()
178
	{
179
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
180
		$this->object->getSubClient( 'unknown' );
181
	}
182
183
184
	public function getClientMock( $methods, $real = true )
185
	{
186
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Review\Standard::class )
187
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
188
			->setMethods( (array) $methods )
189
			->getMock();
190
191
		$object->setAimeos( \TestHelper::getAimeos() );
192
		$object->setView( $this->getViewNoRender( $real ) );
193
194
		return $object;
195
	}
196
197
198
	protected function getViewNoRender( $real = true )
199
	{
200
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
201
			->setConstructorArgs( [[]] )
202
			->setMethods( ['render'] )
203
			->getMock();
204
205
		$param = ['site' => 'unittest', 'id' => $real ? $this->getItem()->getId() : -1];
206
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
207
		$view->addHelper( 'param', $helper );
208
209
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
210
		$view->addHelper( 'config', $helper );
211
212
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, ['admin'] );
213
		$view->addHelper( 'access', $helper );
214
215
		return $view;
216
	}
217
218
219
	protected function getItem( $rating = 0 )
220
	{
221
		$manager = \Aimeos\MShop::create( $this->context, 'review' );
222
		$filter = $manager->filter()->add( ['review.rating' => $rating] )->slice( 0, 1 );
223
224
		return $manager->search( $filter )->first( new \Exception( sprintf( 'No review with rating "%1$d" found', $rating ) ) );
225
	}
226
}
227