Passed
Push — master ( 9ad936...22c0df )
by Aimeos
09:54 queued 06:48
created

StandardTest::testSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.7998
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
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 = \TestHelperJqadm::view();
22
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
23
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
24
		$this->view->addHelper( 'request', $helper );
25
26
		$this->context = \TestHelperJqadm::getContext();
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( \TestHelperJqadm::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 testCreate()
42
	{
43
		$result = $this->object->create();
44
45
		$this->assertStringContainsString( 'customer', $result );
46
		$this->assertEmpty( $this->view->get( 'errors' ) );
47
	}
48
49
50
	public function testCreateException()
51
	{
52
		$object = $this->getClientMock( 'getSubClients' );
53
54
		$object->expects( $this->once() )->method( 'getSubClients' )
55
			->will( $this->throwException( new \RuntimeException() ) );
56
57
		$object->create();
58
	}
59
60
61
	public function testCopy()
62
	{
63
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
64
65
		$param = ['site' => 'unittest', 'id' => $manager->find( '[email protected]' )->getId()];
66
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
67
		$this->view->addHelper( 'param', $helper );
68
69
		$result = $this->object->copy();
70
71
		$this->assertStringContainsString( 'Example company', $result );
72
	}
73
74
75
	public function testCopyException()
76
	{
77
		$object = $this->getClientMock( 'getSubClients' );
78
79
		$object->expects( $this->once() )->method( 'getSubClients' )
80
			->will( $this->throwException( new \RuntimeException() ) );
81
82
		$object->copy();
83
	}
84
85
86
	public function testDelete()
87
	{
88
		$this->assertEquals( '', $this->getClientMock( ['redirect'], false )->delete() );
89
	}
90
91
92
	public function testDeleteException()
93
	{
94
		$object = $this->getClientMock( ['search'] );
95
		$object->expects( $this->once() )->method( 'search' );
96
97
		$object->delete();
98
	}
99
100
101
	public function testGet()
102
	{
103
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
104
105
		$param = ['site' => 'unittest', 'id' => $manager->find( '[email protected]' )->getId()];
106
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
107
		$this->view->addHelper( 'param', $helper );
108
109
		$result = $this->object->get();
110
111
		$this->assertStringContainsString( 'unitCustomer001', $result );
112
	}
113
114
115
	public function testGetException()
116
	{
117
		$object = $this->getClientMock( 'getSubClients' );
118
119
		$object->expects( $this->once() )->method( 'getSubClients' )
120
			->will( $this->throwException( new \RuntimeException() ) );
121
122
		$object->get();
123
	}
124
125
126
	public function testSave()
127
	{
128
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
129
130
		$param = array(
131
			'site' => 'unittest',
132
			'item' => array(
133
				'customer.id' => '',
134
				'customer.email' => '[email protected]',
135
				'customer.firstname' => 'test',
136
				'customer.lastname' => 'label',
137
			),
138
		);
139
140
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
141
		$this->view->addHelper( 'param', $helper );
142
143
		$result = $this->object->save();
144
145
		$manager->delete( $manager->find( '[email protected]' )->getId() );
146
147
		$this->assertEmpty( $this->view->get( 'errors' ) );
148
		$this->assertNull( $result );
149
	}
150
151
152
	public function testSaveException()
153
	{
154
		$object = $this->getClientMock( 'fromArray' );
155
156
		$object->expects( $this->once() )->method( 'fromArray' )
157
			->will( $this->throwException( new \RuntimeException() ) );
158
159
		$object->save();
160
	}
161
162
163
	public function testSearch()
164
	{
165
		$param = array(
166
			'site' => 'unittest', 'locale' => 'de',
167
			'filter' => array(
168
				'key' => array( 0 => 'customer.code' ),
169
				'op' => array( 0 => '==' ),
170
				'val' => array( 0 => '[email protected]' ),
171
			),
172
			'sort' => array( 'customer.label', '-customer.id' ),
173
		);
174
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
175
		$this->view->addHelper( 'param', $helper );
176
177
		$result = $this->object->search();
178
179
		$this->assertStringContainsString( '>[email protected]<', $result );
180
	}
181
182
183
	public function testSearchException()
184
	{
185
		$object = $this->getClientMock( 'initCriteria' );
186
187
		$object->expects( $this->once() )->method( 'initCriteria' )
188
			->will( $this->throwException( new \RuntimeException() ) );
189
190
		$object->search();
191
	}
192
193
194
	public function testGetSubClient()
195
	{
196
		$result = $this->object->getSubClient( 'address' );
197
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
198
	}
199
200
201
	public function testGetSubClientInvalid()
202
	{
203
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
204
		$this->object->getSubClient( '$unknown$' );
205
	}
206
207
208
	public function testGetSubClientUnknown()
209
	{
210
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
211
		$this->object->getSubClient( 'unknown' );
212
	}
213
214
215
	public function testGetSubClientDecoratorInvalid()
216
	{
217
		$this->context->getConfig()->set( 'admin/jqadm/customer/product/decorators/global', array( 'Invalid' ) );
218
219
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
220
		$this->object->getSubClient( 'product' );
221
	}
222
223
224
	public function getClientMock( $methods, $real = true )
225
	{
226
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Customer\Standard::class )
227
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
228
			->setMethods( (array) $methods )
229
			->getMock();
230
231
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
232
		$object->setView( $this->getViewNoRender( $real ) );
233
234
		return $object;
235
	}
236
237
238
	protected function getViewNoRender( $real = true )
239
	{
240
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
241
			->setConstructorArgs( array( [] ) )
242
			->setMethods( array( 'render' ) )
243
			->getMock();
244
245
		$manager = \Aimeos\MShop::create( $this->context, 'customer' );
246
247
		$trans = new \Aimeos\MW\Translation\None( 'de_DE' );
248
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
249
		$view->addHelper( 'translate', $helper );
250
251
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( '[email protected]' )->getId() : -1];
252
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
253
		$view->addHelper( 'param', $helper );
254
255
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $this->context->getConfig() );
256
		$view->addHelper( 'config', $helper );
257
258
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
259
		$view->addHelper( 'access', $helper );
260
261
		return $view;
262
	}
263
}
264