Passed
Push — master ( d343ad...3614c7 )
by Aimeos
03:04
created

StandardTest::testGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 11
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), 2015-2022
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Customer;
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\Customer\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
				'customer.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( 'customer', $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, 'customer' );
84
85
		$param = ['site' => 'unittest', 'id' => $manager->find( '[email protected]' )->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( 'Example company', $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->assertEquals( '', $this->getClientMock( ['redirect'], false )->delete() );
109
	}
110
111
112
	public function testDeleteException()
113
	{
114
		$object = $this->getClientMock( ['search'] );
115
		$object->expects( $this->once() )->method( 'search' );
116
117
		$object->delete();
118
	}
119
120
121
	public function testGet()
122
	{
123
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
124
125
		$param = ['site' => 'unittest', 'id' => $manager->find( '[email protected]' )->getId()];
126
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
127
		$this->view->addHelper( 'param', $helper );
128
129
		$result = $this->object->get();
130
131
		$this->assertStringContainsString( 'unitCustomer001', $result );
132
	}
133
134
135
	public function testGetException()
136
	{
137
		$object = $this->getClientMock( 'getSubClients' );
138
139
		$object->expects( $this->once() )->method( 'getSubClients' )
140
			->will( $this->throwException( new \RuntimeException() ) );
141
142
		$object->get();
143
	}
144
145
146
	public function testSave()
147
	{
148
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
149
150
		$param = array(
151
			'site' => 'unittest',
152
			'item' => array(
153
				'customer.id' => '',
154
				'customer.email' => '[email protected]',
155
				'customer.firstname' => 'test',
156
				'customer.lastname' => 'label',
157
			),
158
		);
159
160
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
161
		$this->view->addHelper( 'param', $helper );
162
163
		$result = $this->object->save();
164
165
		$manager->delete( $manager->find( '[email protected]' )->getId() );
166
167
		$this->assertEmpty( $this->view->get( 'errors' ) );
168
		$this->assertNull( $result );
169
	}
170
171
172
	public function testSaveException()
173
	{
174
		$object = $this->getClientMock( 'fromArray' );
175
176
		$object->expects( $this->once() )->method( 'fromArray' )
177
			->will( $this->throwException( new \RuntimeException() ) );
178
179
		$object->save();
180
	}
181
182
183
	public function testSearch()
184
	{
185
		$param = array(
186
			'site' => 'unittest', 'locale' => 'de',
187
			'filter' => array(
188
				'key' => array( 0 => 'customer.code' ),
189
				'op' => array( 0 => '==' ),
190
				'val' => array( 0 => '[email protected]' ),
191
			),
192
			'sort' => array( 'customer.label', '-customer.id' ),
193
		);
194
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
195
		$this->view->addHelper( 'param', $helper );
196
197
		$result = $this->object->search();
198
199
		$this->assertStringContainsString( '>[email protected]<', $result );
200
	}
201
202
203
	public function testSearchException()
204
	{
205
		$object = $this->getClientMock( 'initCriteria' );
206
207
		$object->expects( $this->once() )->method( 'initCriteria' )
208
			->will( $this->throwException( new \RuntimeException() ) );
209
210
		$object->search();
211
	}
212
213
214
	public function testGetSubClient()
215
	{
216
		$result = $this->object->getSubClient( 'address' );
217
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
218
	}
219
220
221
	public function testGetSubClientInvalid()
222
	{
223
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
224
		$this->object->getSubClient( '$unknown$' );
225
	}
226
227
228
	public function testGetSubClientUnknown()
229
	{
230
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
231
		$this->object->getSubClient( 'unknown' );
232
	}
233
234
235
	public function testGetSubClientDecoratorInvalid()
236
	{
237
		$this->context->config()->set( 'admin/jqadm/customer/product/decorators/global', array( 'Invalid' ) );
238
239
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
240
		$this->object->getSubClient( 'product' );
241
	}
242
243
244
	public function getClientMock( $methods, $real = true )
245
	{
246
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Customer\Standard::class )
247
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
248
			->setMethods( (array) $methods )
249
			->getMock();
250
251
		$object->setAimeos( \TestHelper::getAimeos() );
252
		$object->setView( $this->getViewNoRender( $real ) );
253
254
		return $object;
255
	}
256
257
258
	protected function getViewNoRender( $real = true )
259
	{
260
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
261
			->setConstructorArgs( array( [] ) )
262
			->setMethods( array( 'render' ) )
263
			->getMock();
264
265
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
266
267
		$trans = new \Aimeos\Base\Translation\None( 'de_DE' );
268
		$helper = new \Aimeos\Base\View\Helper\Translate\Standard( $view, $trans );
269
		$view->addHelper( 'translate', $helper );
270
271
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( '[email protected]' )->getId() : -1];
272
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
273
		$view->addHelper( 'param', $helper );
274
275
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
276
		$view->addHelper( 'config', $helper );
277
278
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
279
		$view->addHelper( 'access', $helper );
280
281
		return $view;
282
	}
283
}
284