StandardTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 15
rs 9.9332
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-2026
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
		$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\Service\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
				'service.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( 'service', $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, 'service' );
86
87
		$param = ['site' => 'unittest', 'id' => $manager->find( 'unitpaymentcode' )->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( 'unitpaymentcode_', $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, 'service' );
129
130
		$param = ['site' => 'unittest', 'id' => $manager->find( 'unitpaymentcode' )->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( 'unitpaymentlabel', $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 testSave()
152
	{
153
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
154
155
		$param = array(
156
			'site' => 'unittest',
157
			'item' => array(
158
				'service.id' => '',
159
				'service.type' => 'delivery',
160
				'service.code' => 'test',
161
				'service.provider' => 'Standard',
162
				'service.label' => 'test label',
163
				'service.datestart' => null,
164
				'service.dateend' => null,
165
				'service.position' => '2',
166
				'config' => [
167
					['key' => 'test key', 'val' => 'test value'],
168
				],
169
			),
170
		);
171
172
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
173
		$this->view->addHelper( 'param', $helper );
174
175
		$result = $this->object->save();
176
177
		$manager->delete( $manager->find( 'test' )->getId() );
178
179
		$this->assertEmpty( $this->view->get( 'errors' ) );
180
		$this->assertNull( $result );
181
	}
182
183
184
	public function testSaveException()
185
	{
186
		$object = $this->getClientMock( 'fromArray' );
187
188
		$object->expects( $this->once() )->method( 'fromArray' )
189
			->will( $this->throwException( new \RuntimeException() ) );
190
191
		$object->save();
192
	}
193
194
195
	public function testSearch()
196
	{
197
		$param = array(
198
			'site' => 'unittest', 'locale' => 'de',
199
			'filter' => array(
200
				'key' => array( 0 => 'service.code' ),
201
				'op' => array( 0 => '==' ),
202
				'val' => array( 0 => 'unitpaymentcode' ),
203
			),
204
			'sort' => array( 'service.label', '-service.id' ),
205
		);
206
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
207
		$this->view->addHelper( 'param', $helper );
208
209
		$result = $this->object->search();
210
211
		$this->assertStringContainsString( '>unitpaymentlabel<', $result );
212
	}
213
214
215
	public function testSearchException()
216
	{
217
		$object = $this->getClientMock( 'initCriteria' );
218
219
		$object->expects( $this->once() )->method( 'initCriteria' )
220
			->will( $this->throwException( new \RuntimeException() ) );
221
222
		$object->search();
223
	}
224
225
226
	public function testGetSubClient()
227
	{
228
		$result = $this->object->getSubClient( 'media' );
229
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
230
	}
231
232
233
	public function testGetSubClientInvalid()
234
	{
235
		$this->expectException( \LogicException::class );
236
		$this->object->getSubClient( '$unknown$' );
237
	}
238
239
240
	public function testGetSubClientUnknown()
241
	{
242
		$this->expectException( \LogicException::class );
243
		$this->object->getSubClient( 'unknown' );
244
	}
245
246
247
	public function getClientMock( $methods, $real = true )
248
	{
249
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Service\Standard::class )
250
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
251
			->onlyMethods( (array) $methods )
252
			->getMock();
253
254
		$object->setAimeos( \TestHelper::getAimeos() );
255
		$object->setView( $this->getViewNoRender( $real ) );
256
257
		return $object;
258
	}
259
260
261
	protected function getViewNoRender( $real = true )
262
	{
263
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
264
			->setConstructorArgs( array( [] ) )
265
			->onlyMethods( array( 'render' ) )
266
			->getMock();
267
268
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
269
270
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( 'unitpaymentcode' )->getId() : -1];
271
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
272
		$view->addHelper( 'param', $helper );
273
274
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
275
		$view->addHelper( 'config', $helper );
276
277
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
278
		$view->addHelper( 'access', $helper );
279
280
		return $view;
281
	}
282
}
283