Passed
Push — master ( bf52e6...ace2f2 )
by Aimeos
03:11
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), 2017-2022
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Type\Media;
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\Media\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
				'media.type.domain' => 'product',
43
				'media.type.position' => 1,
44
				'media.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( 'media', $result );
64
		$this->assertEmpty( $this->view->get( 'errors' ) );
65
	}
66
67
68
	public function testCreateException()
69
	{
70
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Media\Standard::class )
71
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
72
			->setMethods( 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, 'media/type' );
87
88
		$param = ['type' => 'unittest', 'id' => $manager->find( 'default', [], '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( 'default', $result );
95
	}
96
97
98
	public function testCopyException()
99
	{
100
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Media\Standard::class )
101
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
102
			->setMethods( 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, 'media/type' );
135
136
		$param = ['type' => 'unittest', 'id' => $manager->find( 'default', [], '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( 'default', $result );
143
	}
144
145
146
	public function testGetException()
147
	{
148
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Media\Standard::class )
149
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
150
			->setMethods( 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, 'media/type' );
165
166
		$param = array(
167
			'type' => 'unittest',
168
			'item' => array(
169
				'media.type.id' => '',
170
				'media.type.status' => '1',
171
				'media.type.domain' => 'product',
172
				'media.type.code' => 'jqadm@test',
173
				'media.type.label' => 'jqadm test',
174
			),
175
		);
176
177
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
178
		$this->view->addHelper( 'param', $helper );
179
180
		$result = $this->object->save();
181
182
		$manager->delete( $manager->find( 'jqadm@test', [], 'product' )->getId() );
183
184
		$this->assertEmpty( $this->view->get( 'errors' ) );
185
		$this->assertNull( $result );
186
	}
187
188
189
	public function testSaveException()
190
	{
191
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Media\Standard::class )
192
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
193
			->setMethods( array( 'fromArray' ) )
194
			->getMock();
195
196
		$object->expects( $this->once() )->method( 'fromArray' )
197
			->will( $this->throwException( new \RuntimeException() ) );
198
199
		$object->setView( $this->getViewNoRender() );
200
201
		$object->save();
202
	}
203
204
205
	public function testSearch()
206
	{
207
		$param = array(
208
			'type' => 'unittest', 'locale' => 'de',
209
			'filter' => array(
210
				'key' => array( 0 => 'media.type.code' ),
211
				'op' => array( 0 => '==' ),
212
				'val' => array( 0 => 'default' ),
213
			),
214
			'sort' => array( '-media.type.id' ),
215
		);
216
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
217
		$this->view->addHelper( 'param', $helper );
218
219
		$result = $this->object->search();
220
221
		$this->assertStringContainsString( '>default<', $result );
222
	}
223
224
225
	public function testSearchException()
226
	{
227
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Media\Standard::class )
228
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
229
			->setMethods( array( 'initCriteria' ) )
230
			->getMock();
231
232
		$object->expects( $this->once() )->method( 'initCriteria' )
233
			->will( $this->throwException( new \RuntimeException() ) );
234
235
		$object->setView( $this->getViewNoRender() );
236
237
		$object->search();
238
	}
239
240
241
	public function testGetSubClientInvalid()
242
	{
243
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
244
		$this->object->getSubClient( '$unknown$' );
245
	}
246
247
248
	public function testGetSubClientUnknown()
249
	{
250
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
251
		$this->object->getSubClient( 'unknown' );
252
	}
253
254
255
	public function getClientMock( $methods, $real = true )
256
	{
257
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Media\Standard::class )
258
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
259
			->setMethods( (array) $methods )
260
			->getMock();
261
262
		$object->setAimeos( \TestHelper::getAimeos() );
263
		$object->setView( $this->getViewNoRender( $real ) );
264
265
		return $object;
266
	}
267
268
269
	protected function getViewNoRender( $real = true )
270
	{
271
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
272
			->setConstructorArgs( array( [] ) )
273
			->setMethods( array( 'render' ) )
274
			->getMock();
275
276
		$manager = \Aimeos\MShop::create( $this->context, 'media/type' );
277
278
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( 'default', [], 'product' )->getId() : -1];
279
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
280
		$view->addHelper( 'param', $helper );
281
282
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
283
		$view->addHelper( 'config', $helper );
284
285
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
286
		$view->addHelper( 'access', $helper );
287
288
		return $view;
289
	}
290
291
}
292