Passed
Push — master ( b1d82b...4261f4 )
by Aimeos
03:11
created

StandardTest::testGetSubClientDecorators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Product;
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
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
23
		$request->expects( $this->any() )->method( 'getUploadedFiles' )->willReturn( [] );
24
25
		$helper = new \Aimeos\Base\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
26
		$this->view ->addHelper( 'request', $helper );
27
28
		$this->context = \TestHelper::context();
29
30
		$this->object = new \Aimeos\Admin\JQAdm\Product\Standard( $this->context );
31
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
32
		$this->object->setAimeos( \TestHelper::getAimeos() );
33
		$this->object->setView( $this->view );
34
	}
35
36
37
	protected function tearDown() : void
38
	{
39
		unset( $this->object, $this->view, $this->context );
40
	}
41
42
43
	public function testBatch()
44
	{
45
		$param = array(
46
			'site' => 'unittest',
47
			'item' => array(
48
				'product.status' => -1,
49
			),
50
			'id' => [-1, -2]
51
		);
52
53
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
54
		$this->view->addHelper( 'param', $helper );
55
56
		$result = $this->object->batch();
57
58
		$this->assertEmpty( $this->view->get( 'errors' ) );
59
		$this->assertNull( $result );
60
	}
61
62
63
	public function testCreate()
64
	{
65
		$result = $this->object->create();
66
67
		$this->assertStringContainsString( 'product', $result );
68
		$this->assertEmpty( $this->view->get( 'errors' ) );
69
	}
70
71
72
	public function testCreateException()
73
	{
74
		$object = $this->getClientMock( 'getSubClients' );
75
76
		$object->expects( $this->once() )->method( 'getSubClients' )
77
			->will( $this->throwException( new \RuntimeException() ) );
78
79
		$object->create();
80
	}
81
82
83
	public function testCopy()
84
	{
85
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
86
87
		$param = ['site' => 'unittest', 'id' => $manager->find( 'CNC' )->getId()];
88
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
89
		$this->view->addHelper( 'param', $helper );
90
91
		$result = $this->object->copy();
92
93
		$this->assertStringContainsString( 'CNC_', $result );
94
	}
95
96
97
	public function testCopyException()
98
	{
99
		$object = $this->getClientMock( 'getSubClients' );
100
101
		$object->expects( $this->once() )->method( 'getSubClients' )
102
			->will( $this->throwException( new \RuntimeException() ) );
103
104
		$object->copy();
105
	}
106
107
108
	public function testDelete()
109
	{
110
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
111
	}
112
113
114
	public function testDeleteException()
115
	{
116
		$object = $this->getClientMock( ['getSubClients', 'search'] );
117
118
		$object->expects( $this->once() )->method( 'getSubClients' )
119
			->will( $this->throwException( new \RuntimeException() ) );
120
		$object->expects( $this->once() )->method( 'search' );
121
122
		$object->delete();
123
	}
124
125
126
	public function testGet()
127
	{
128
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
129
130
		$param = ['site' => 'unittest', 'id' => $manager->find( 'CNC' )->getId()];
131
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
132
		$this->view->addHelper( 'param', $helper );
133
134
		$result = $this->object->get();
135
136
		$this->assertStringContainsString( 'CNC', $result );
137
	}
138
139
140
	public function testGetException()
141
	{
142
		$object = $this->getClientMock( 'getSubClients' );
143
144
		$object->expects( $this->once() )->method( 'getSubClients' )
145
			->will( $this->throwException( new \RuntimeException() ) );
146
147
		$object->get();
148
	}
149
150
151
	public function testImport()
152
	{
153
		$param = array(
154
			'site' => 'unittest',
155
		);
156
157
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
158
		$this->view->addHelper( 'param', $helper );
159
160
		$result = $this->object->import();
161
162
		$this->assertNull( $result );
163
	}
164
165
166
	public function testSave()
167
	{
168
		$param = array(
169
			'site' => 'unittest',
170
			'item' => array(
171
				'product.id' => '',
172
				'product.type' => 'default',
173
				'product.code' => 'test',
174
				'product.label' => 'test label',
175
				'product.datestart' => null,
176
				'product.dateend' => null,
177
				'config' => [[
178
					'key' => 'test',
179
					'val' => 'value',
180
				]],
181
			),
182
		);
183
184
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
185
		$this->view->addHelper( 'param', $helper );
186
187
		$this->object->save();
188
189
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
190
		$item = $manager->find( 'test' );
191
		$manager->delete( $item->getId() );
192
193
		$this->assertEquals( ['test' => 'value'], $item->getConfig() );
194
	}
195
196
197
	public function testSaveException()
198
	{
199
		$object = $this->getClientMock( 'fromArray' );
200
201
		$object->expects( $this->once() )->method( 'fromArray' )
202
			->will( $this->throwException( new \RuntimeException() ) );
203
204
		$object->save();
205
	}
206
207
208
	public function testSearch()
209
	{
210
		$param = array(
211
			'site' => 'unittest', 'locale' => 'de',
212
			'filter' => array(
213
				'key' => array( 0 => 'product.code' ),
214
				'op' => array( 0 => '==' ),
215
				'val' => array( 0 => 'CNE' ),
216
			),
217
			'sort' => array( 'product.label', '-product.id' ),
218
		);
219
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
220
		$this->view->addHelper( 'param', $helper );
221
222
		$result = $this->object->search();
223
224
		$this->assertStringContainsString( '>CNE<', $result );
225
	}
226
227
228
	public function testSearchException()
229
	{
230
		$object = $this->getClientMock( 'initCriteria' );
231
232
		$object->expects( $this->once() )->method( 'initCriteria' )
233
			->will( $this->throwException( new \RuntimeException() ) );
234
235
		$object->search();
236
	}
237
238
239
	public function testGetSubClient()
240
	{
241
		$result = $this->object->getSubClient( 'media' );
242
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
243
	}
244
245
246
	public function testGetSubClientInvalid()
247
	{
248
		$this->expectException( \LogicException::class );
249
		$this->object->getSubClient( '$unknown$' );
250
	}
251
252
253
	public function testGetSubClientUnknown()
254
	{
255
		$this->expectException( \LogicException::class );
256
		$this->object->getSubClient( 'unknown' );
257
	}
258
259
260
	public function testGetSubClientDecorators()
261
	{
262
		$this->context->config()->set( 'admin/jqadm/product/media/decorators/global', array( 'Page' ) );
263
264
		$result = $this->object->getSubClient( 'media' );
265
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
266
	}
267
268
269
	public function testGetSubClientDecoratorInvalid()
270
	{
271
		$this->context->config()->set( 'admin/jqadm/product/media/decorators/global', array( 'Invalid' ) );
272
273
		$this->expectException( \LogicException::class );
274
		$this->object->getSubClient( 'media' );
275
	}
276
277
278
	public function getClientMock( $methods, $real = true )
279
	{
280
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Product\Standard::class )
281
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
282
			->onlyMethods( (array) $methods )
283
			->getMock();
284
285
		$object->setAimeos( \TestHelper::getAimeos() );
286
		$object->setView( $this->getViewNoRender( $real ) );
287
288
		return $object;
289
	}
290
291
292
	protected function getViewNoRender( $real = true )
293
	{
294
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
295
			->setConstructorArgs( array( [] ) )
296
			->onlyMethods( array( 'render' ) )
297
			->getMock();
298
299
		$manager = \Aimeos\MShop::create( $this->context, 'product' );
300
301
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( 'CNC' )->getId() : -1];
302
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
303
		$view->addHelper( 'param', $helper );
304
305
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
306
		$view->addHelper( 'config', $helper );
307
308
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
309
		$view->addHelper( 'access', $helper );
310
311
		return $view;
312
	}
313
}
314