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