Passed
Push — master ( 8c606e...aebd94 )
by Aimeos
04:08
created

StandardTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 264
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 125
c 1
b 0
f 0
dl 0
loc 264
rs 10
wmc 20

19 Methods

Rating   Name   Duplication   Size   Complexity  
A testDelete() 0 3 1
A testCopy() 0 11 1
A testGetSubClientInvalid() 0 4 1
A testGetSubClientUnknown() 0 4 1
A testGetException() 0 13 1
A tearDown() 0 3 1
A getClientMock() 0 11 1
A testGet() 0 11 1
A setUp() 0 9 1
A getViewNoRender() 0 20 2
A testGetViewException() 0 6 1
A testSave() 0 24 1
A testCopyException() 0 13 1
A testSearchException() 0 13 1
A testCreate() 0 6 1
A testDeleteException() 0 9 1
A testSearch() 0 17 1
A testSaveException() 0 13 1
A testCreateException() 0 13 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Type\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 = \TestHelperJqadm::getView();
22
		$this->context = \TestHelperJqadm::getContext();
23
24
		$this->object = new \Aimeos\Admin\JQAdm\Type\Rule\Standard( $this->context );
25
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
26
		$this->object->setAimeos( \TestHelperJqadm::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 testCreate()
38
	{
39
		$result = $this->object->create();
40
41
		$this->assertStringContainsString( 'rule', $result );
42
		$this->assertEmpty( $this->view->get( 'errors' ) );
43
	}
44
45
46
	public function testCreateException()
47
	{
48
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Rule\Standard::class )
49
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
50
			->setMethods( array( 'getSubClients' ) )
51
			->getMock();
52
53
		$object->expects( $this->once() )->method( 'getSubClients' )
54
			->will( $this->throwException( new \RuntimeException() ) );
55
56
		$object->setView( $this->getViewNoRender() );
57
58
		$object->create();
59
	}
60
61
62
	public function testCopy()
63
	{
64
		$manager = \Aimeos\MShop::create( $this->context, 'rule/type' );
65
66
		$param = ['type' => 'unittest', 'id' => $manager->find( 'catalog', [], 'rule' )->getId()];
67
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
68
		$this->view->addHelper( 'param', $helper );
69
70
		$result = $this->object->copy();
71
72
		$this->assertStringContainsString( 'catalog', $result );
73
	}
74
75
76
	public function testCopyException()
77
	{
78
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Rule\Standard::class )
79
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
80
			->setMethods( array( 'getSubClients' ) )
81
			->getMock();
82
83
		$object->expects( $this->once() )->method( 'getSubClients' )
84
			->will( $this->throwException( new \RuntimeException() ) );
85
86
		$object->setView( $this->getViewNoRender() );
87
88
		$object->copy();
89
	}
90
91
92
	public function testDelete()
93
	{
94
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
95
	}
96
97
98
	public function testDeleteException()
99
	{
100
		$object = $this->getClientMock( ['getSubClients', 'search'] );
101
102
		$object->expects( $this->once() )->method( 'getSubClients' )
103
			->will( $this->throwException( new \RuntimeException() ) );
104
		$object->expects( $this->once() )->method( 'search' );
105
106
		$object->delete();
107
	}
108
109
110
	public function testGet()
111
	{
112
		$manager = \Aimeos\MShop::create( $this->context, 'rule/type' );
113
114
		$param = ['type' => 'unittest', 'id' => $manager->find( 'catalog', [], 'rule' )->getId()];
115
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
116
		$this->view->addHelper( 'param', $helper );
117
118
		$result = $this->object->get();
119
120
		$this->assertStringContainsString( 'catalog', $result );
121
	}
122
123
124
	public function testGetException()
125
	{
126
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Rule\Standard::class )
127
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
128
			->setMethods( array( 'getSubClients' ) )
129
			->getMock();
130
131
		$object->expects( $this->once() )->method( 'getSubClients' )
132
			->will( $this->throwException( new \RuntimeException() ) );
133
134
		$object->setView( $this->getViewNoRender() );
135
136
		$object->get();
137
	}
138
139
140
	public function testGetViewException()
141
	{
142
		$object = new \Aimeos\Admin\JQAdm\Type\Rule\Standard( $this->context, [] );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Admin\JQAdm\Type\...Standard::__construct() has too many arguments starting with array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
		$object = /** @scrutinizer ignore-call */ new \Aimeos\Admin\JQAdm\Type\Rule\Standard( $this->context, [] );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
143
144
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
145
		$object->getView();
146
	}
147
148
149
	public function testSave()
150
	{
151
		$manager = \Aimeos\MShop::create( $this->context, 'rule/type' );
152
153
		$param = array(
154
			'type' => 'unittest',
155
			'item' => array(
156
				'rule.type.id' => '',
157
				'rule.type.status' => '1',
158
				'rule.type.domain' => 'rule',
159
				'rule.type.code' => 'jqadm@test',
160
				'rule.type.label' => 'jqadm test',
161
			),
162
		);
163
164
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
165
		$this->view->addHelper( 'param', $helper );
166
167
		$result = $this->object->save();
168
169
		$manager->delete( $manager->find( 'jqadm@test', [], 'rule' )->getId() );
170
171
		$this->assertEmpty( $this->view->get( 'errors' ) );
172
		$this->assertNull( $result );
173
	}
174
175
176
	public function testSaveException()
177
	{
178
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Rule\Standard::class )
179
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
180
			->setMethods( array( 'fromArray' ) )
181
			->getMock();
182
183
		$object->expects( $this->once() )->method( 'fromArray' )
184
			->will( $this->throwException( new \RuntimeException() ) );
185
186
		$object->setView( $this->getViewNoRender() );
187
188
		$object->save();
189
	}
190
191
192
	public function testSearch()
193
	{
194
		$param = array(
195
			'type' => 'unittest', 'lang' => 'de',
196
			'filter' => array(
197
				'key' => array( 0 => 'rule.type.code' ),
198
				'op' => array( 0 => '==' ),
199
				'val' => array( 0 => 'catalog' ),
200
			),
201
			'sort' => array( '-rule.type.id' ),
202
		);
203
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
204
		$this->view->addHelper( 'param', $helper );
205
206
		$result = $this->object->search();
207
208
		$this->assertStringContainsString( '>catalog<', $result );
209
	}
210
211
212
	public function testSearchException()
213
	{
214
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Rule\Standard::class )
215
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
216
			->setMethods( array( 'initCriteria' ) )
217
			->getMock();
218
219
		$object->expects( $this->once() )->method( 'initCriteria' )
220
			->will( $this->throwException( new \RuntimeException() ) );
221
222
		$object->setView( $this->getViewNoRender() );
223
224
		$object->search();
225
	}
226
227
228
	public function testGetSubClientInvalid()
229
	{
230
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
231
		$this->object->getSubClient( '$unknown$' );
232
	}
233
234
235
	public function testGetSubClientUnknown()
236
	{
237
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
238
		$this->object->getSubClient( 'unknown' );
239
	}
240
241
242
	public function getClientMock( $methods, $real = true )
243
	{
244
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Type\Rule\Standard::class )
245
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
246
			->setMethods( (array) $methods )
247
			->getMock();
248
249
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
250
		$object->setView( $this->getViewNoRender( $real ) );
251
252
		return $object;
253
	}
254
255
256
	protected function getViewNoRender( $real = true )
257
	{
258
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
259
			->setConstructorArgs( array( [] ) )
260
			->setMethods( array( 'render' ) )
261
			->getMock();
262
263
		$manager = \Aimeos\MShop::create( $this->context, 'rule/type' );
264
265
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( 'catalog', [], 'rule' )->getId() : -1];
266
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
267
		$view->addHelper( 'param', $helper );
268
269
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $this->context->getConfig() );
270
		$view->addHelper( 'config', $helper );
271
272
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
273
		$view->addHelper( 'access', $helper );
274
275
		return $view;
276
	}
277
278
}
279