Passed
Push — master ( 1ab0ac...0f6196 )
by Aimeos
03:16
created

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