Passed
Push — master ( e9f33e...6a445e )
by Aimeos
03:39
created

StandardTest::testCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
nc 1
nop 0
dl 0
loc 9
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2022
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Rule;
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\Rule\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
				'rule.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( 'rule', $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()->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( 'Home category', $result );
86
	}
87
88
89
	public function testCopyException()
90
	{
91
		$object = $this->getClientMock( 'getSubClients' );
92
93
		$object->expects( $this->once() )->method( 'getSubClients' )
94
			->will( $this->throwException( new \RuntimeException() ) );
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()->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( 'Home category', $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, 'rule' );
144
145
		$param = array(
146
			'site' => 'unittest',
147
			'item' => array(
148
				'rule.id' => '',
149
				'rule.type' =>'catalog',
150
				'rule.provider' => 'Percent',
151
				'rule.label' => 'test label',
152
				'rule.position' => '2',
153
				'config' => array(
154
					'key' => array( 0 => 'percent' ),
155
					'val' => array( 0 => '-10' ),
156
				),
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( $this->getItem( 'test label' )->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 => 'rule.label' ),
189
				'op' => array( 0 => '=~' ),
190
				'val' => array( 0 => 'Home category' ),
191
			),
192
			'sort' => array( 'rule.label', '-rule.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( '>Home category -10%<', $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 testGetSubClientInvalid()
215
	{
216
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
217
		$this->object->getSubClient( '$unknown$' );
218
	}
219
220
221
	public function testGetSubClientUnknown()
222
	{
223
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
224
		$this->object->getSubClient( 'unknown' );
225
	}
226
227
228
	public function getClientMock( $methods, $real = true )
229
	{
230
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Rule\Standard::class )
231
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
232
			->setMethods( (array) $methods )
233
			->getMock();
234
235
		$object->setAimeos( \TestHelper::getAimeos() );
236
		$object->setView( $this->getViewNoRender( $real ) );
237
238
		return $object;
239
	}
240
241
242
	protected function getViewNoRender( $real = true )
243
	{
244
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
245
			->setConstructorArgs( array( [] ) )
246
			->setMethods( array( 'render' ) )
247
			->getMock();
248
249
		$param = ['site' => 'unittest', 'id' => $real ? $this->getItem()->getId() : -1];
250
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
251
		$view->addHelper( 'param', $helper );
252
253
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
254
		$view->addHelper( 'config', $helper );
255
256
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
257
		$view->addHelper( 'access', $helper );
258
259
		return $view;
260
	}
261
262
263
	protected function getItem( $label = 'Home category -10%' )
264
	{
265
		$manager = \Aimeos\MShop::create( $this->context, 'rule' );
266
		$search = $manager->filter()->slice( 0, 1 )->add( ['rule.label' => $label] );
267
268
		return $manager->search( $search )->first( new \Exception( sprintf( 'No rule with label "%1$s" found', $label ) ) );
269
	}
270
}
271