Passed
Push — master ( d931d2...a33bf6 )
by Aimeos
04:32
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
nc 1
nop 0
dl 0
loc 17
rs 9.9332
c 1
b 0
f 0
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\Locale;
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\Locale\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
				'locale.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( 'locale', $result );
62
		$this->assertEmpty( $this->view->get( 'errors' ) );
63
	}
64
65
66
	public function testCreateException()
67
	{
68
		$object = $this->getClientMock( 'getSubClients' );
69
70
		$object->expects( $this->once() )->method( 'getSubClients' )
71
			->will( $this->throwException( new \RuntimeException() ) );
72
73
		$object->create();
74
	}
75
76
77
	public function testCopy()
78
	{
79
		$param = ['site' => 'unittest', 'id' => $this->getItem( 'de', 'EUR' )->getId()];
80
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
81
		$this->view->addHelper( 'param', $helper );
82
83
		$result = $this->object->copy();
84
85
		$this->assertStringContainsString( 'EUR', $result );
86
	}
87
88
89
	public function testCopyMShopException()
90
	{
91
		$object = $this->getClientMock( 'getSubClients' );
92
93
		$object->expects( $this->once() )->method( 'getSubClients' )
94
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
95
96
		$object->copy();
97
	}
98
99
100
	public function testDelete()
101
	{
102
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
103
	}
104
105
106
	public function testDeleteException()
107
	{
108
		$object = $this->getClientMock( ['getSubClients', 'search'] );
109
110
		$object->expects( $this->once() )->method( 'getSubClients' )
111
			->will( $this->throwException( new \RuntimeException() ) );
112
		$object->expects( $this->once() )->method( 'search' );
113
114
		$object->delete();
115
	}
116
117
118
	public function testGet()
119
	{
120
		$param = ['site' => 'unittest', 'id' => $this->getItem( 'de', 'EUR' )->getId()];
121
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
122
		$this->view->addHelper( 'param', $helper );
123
124
		$result = $this->object->get();
125
126
		$this->assertStringContainsString( 'EUR', $result );
127
	}
128
129
130
	public function testGetException()
131
	{
132
		$object = $this->getClientMock( 'getSubClients' );
133
134
		$object->expects( $this->once() )->method( 'getSubClients' )
135
			->will( $this->throwException( new \RuntimeException() ) );
136
137
		$object->get();
138
	}
139
140
141
	public function testSave()
142
	{
143
		$manager = \Aimeos\MShop::create( $this->context, 'locale' );
144
145
		$param = array(
146
			'site' => 'unittest',
147
			'item' => array(
148
				'locale.id' => '',
149
				'locale.status' => '1',
150
				'locale.languageid' => 'da',
151
				'locale.currencyid' => 'DKK',
152
				'locale.position' => '0',
153
			),
154
		);
155
156
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
157
		$this->view->addHelper( 'param', $helper );
158
159
		$result = $this->object->save();
160
161
		$manager->delete( $this->getItem( 'da', 'DKK' )->getId() );
162
163
		$this->assertEmpty( $this->view->get( 'errors' ) );
164
		$this->assertNull( $result );
165
	}
166
167
168
	public function testSaveException()
169
	{
170
		$object = $this->getClientMock( 'fromArray' );
171
172
		$object->expects( $this->once() )->method( 'fromArray' )
173
			->will( $this->throwException( new \RuntimeException() ) );
174
175
		$object->save();
176
	}
177
178
179
	public function testSearch()
180
	{
181
		$param = array(
182
			'site' => 'unittest', 'locale' => 'de',
183
			'filter' => array(
184
				'key' => array( 0 => 'locale.languageid' ),
185
				'op' => array( 0 => '==' ),
186
				'val' => array( 0 => 'de' ),
187
			),
188
			'sort' => array( 'locale.position', '-locale.id' ),
189
		);
190
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
191
		$this->view->addHelper( 'param', $helper );
192
193
		$result = $this->object->search();
194
195
		$this->assertStringContainsString( '>EUR<', $result );
196
	}
197
198
199
	public function testSearchException()
200
	{
201
		$object = $this->getClientMock( 'initCriteria' );
202
203
		$object->expects( $this->once() )->method( 'initCriteria' )
204
			->will( $this->throwException( new \RuntimeException() ) );
205
206
		$object->search();
207
	}
208
209
210
	public function testGetSubClientInvalid()
211
	{
212
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
213
		$this->object->getSubClient( '$unknown$' );
214
	}
215
216
217
	public function testGetSubClientUnknown()
218
	{
219
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
220
		$this->object->getSubClient( 'unknown' );
221
	}
222
223
224
	public function getClientMock( $methods, $real = true )
225
	{
226
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Locale\Standard::class )
227
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
228
			->setMethods( (array) $methods )
229
			->getMock();
230
231
		$object->setAimeos( \TestHelper::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\Base\View\Standard::class )
241
			->setConstructorArgs( array( [] ) )
242
			->setMethods( array( 'render' ) )
243
			->getMock();
244
245
		$param = ['site' => 'unittest', 'id' => $real ? $this->getItem()->getId() : -1];
246
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
247
		$view->addHelper( 'param', $helper );
248
249
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
250
		$view->addHelper( 'config', $helper );
251
252
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
253
		$view->addHelper( 'access', $helper );
254
255
		return $view;
256
	}
257
258
259
	protected function getItem( $langid = null, $currid = null )
260
	{
261
		$manager = \Aimeos\MShop::create( $this->context, 'locale' );
262
263
		$search = $manager->filter();
264
		$search->slice( 0, 1 );
265
266
		$expr = [];
267
268
		if( $langid ) {
269
			$expr[] = $search->compare( '==', 'locale.languageid', $langid );
270
		}
271
272
		if( $currid ) {
273
			$expr[] = $search->compare( '==', 'locale.currencyid', $currid );
274
		}
275
276
		$search->setConditions( $search->and( $expr ) );
277
278
		if( ( $item = $manager->search( $search )->first() ) === null ) {
279
			throw new \Exception( sprintf( 'No locale found' ) );
280
		}
281
282
		return $item;
283
	}
284
}
285