Passed
Push — master ( 0fb3b0...cd77f5 )
by Aimeos
04:53
created

StandardTest::testCreateException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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