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

StandardTest::testSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.9
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\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\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->getClientMock( 'getSubClients' );
49
50
		$object->expects( $this->once() )->method( 'getSubClients' )
51
			->will( $this->throwException( new \RuntimeException() ) );
52
53
		$object->create();
54
	}
55
56
57
	public function testCopy()
58
	{
59
		$param = ['site' => 'unittest', 'id' => $this->getItem()->getId()];
60
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
61
		$this->view->addHelper( 'param', $helper );
62
63
		$result = $this->object->copy();
64
65
		$this->assertStringContainsString( 'Home category', $result );
66
	}
67
68
69
	public function testCopyException()
70
	{
71
		$object = $this->getClientMock( 'getSubClients' );
72
73
		$object->expects( $this->once() )->method( 'getSubClients' )
74
			->will( $this->throwException( new \RuntimeException() ) );
75
76
		$object->copy();
77
	}
78
79
80
	public function testDelete()
81
	{
82
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
83
	}
84
85
86
	public function testDeleteException()
87
	{
88
		$object = $this->getClientMock( ['getSubClients', 'search'] );
89
90
		$object->expects( $this->once() )->method( 'getSubClients' )
91
			->will( $this->throwException( new \RuntimeException() ) );
92
		$object->expects( $this->once() )->method( 'search' );
93
94
		$object->delete();
95
	}
96
97
98
	public function testGet()
99
	{
100
		$param = ['site' => 'unittest', 'id' => $this->getItem()->getId()];
101
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
102
		$this->view->addHelper( 'param', $helper );
103
104
		$result = $this->object->get();
105
106
		$this->assertStringContainsString( 'Home category', $result );
107
	}
108
109
110
	public function testGetException()
111
	{
112
		$object = $this->getClientMock( 'getSubClients' );
113
114
		$object->expects( $this->once() )->method( 'getSubClients' )
115
			->will( $this->throwException( new \RuntimeException() ) );
116
117
		$object->get();
118
	}
119
120
121
	public function testGetViewException()
122
	{
123
		$object = new \Aimeos\Admin\JQAdm\Rule\Standard( $this->context, [] );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Admin\JQAdm\Rule\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

123
		$object = /** @scrutinizer ignore-call */ new \Aimeos\Admin\JQAdm\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...
124
125
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
126
		$object->getView();
127
	}
128
129
130
	public function testSave()
131
	{
132
		$manager = \Aimeos\MShop::create( $this->context, 'rule' );
133
134
		$param = array(
135
			'site' => 'unittest',
136
			'item' => array(
137
				'rule.id' => '',
138
				'rule.type' =>'catalog',
139
				'rule.provider' => 'Example',
140
				'rule.label' => 'test label',
141
				'rule.position' => '2',
142
				'config' => array(
143
					'key' => array( 0 => 'test key' ),
144
					'val' => array( 0 => 'test value' ),
145
				),
146
			),
147
		);
148
149
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
150
		$this->view->addHelper( 'param', $helper );
151
152
		$result = $this->object->save();
153
154
		$manager->delete( $this->getItem( 'test label' )->getId() );
155
156
		$this->assertEmpty( $this->view->get( 'errors' ) );
157
		$this->assertNull( $result );
158
	}
159
160
161
	public function testSaveException()
162
	{
163
		$object = $this->getClientMock( 'fromArray' );
164
165
		$object->expects( $this->once() )->method( 'fromArray' )
166
			->will( $this->throwException( new \RuntimeException() ) );
167
168
		$object->save();
169
	}
170
171
172
	public function testSearch()
173
	{
174
		$param = array(
175
			'site' => 'unittest', 'lang' => 'de',
176
			'filter' => array(
177
				'key' => array( 0 => 'rule.label' ),
178
				'op' => array( 0 => '=~' ),
179
				'val' => array( 0 => 'Home category' ),
180
			),
181
			'sort' => array( 'rule.label', '-rule.id' ),
182
		);
183
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
184
		$this->view->addHelper( 'param', $helper );
185
186
		$result = $this->object->search();
187
188
		$this->assertStringContainsString( '>Home category -10%<', $result );
189
	}
190
191
192
	public function testSearchException()
193
	{
194
		$object = $this->getClientMock( 'initCriteria' );
195
196
		$object->expects( $this->once() )->method( 'initCriteria' )
197
			->will( $this->throwException( new \RuntimeException() ) );
198
199
		$object->search();
200
	}
201
202
203
	public function testGetSubClientInvalid()
204
	{
205
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
206
		$this->object->getSubClient( '$unknown$' );
207
	}
208
209
210
	public function testGetSubClientUnknown()
211
	{
212
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
213
		$this->object->getSubClient( 'unknown' );
214
	}
215
216
217
	public function getClientMock( $methods, $real = true )
218
	{
219
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Rule\Standard::class )
220
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
221
			->setMethods( (array) $methods )
222
			->getMock();
223
224
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
225
		$object->setView( $this->getViewNoRender( $real ) );
226
227
		return $object;
228
	}
229
230
231
	protected function getViewNoRender( $real = true )
232
	{
233
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
234
			->setConstructorArgs( array( [] ) )
235
			->setMethods( array( 'render' ) )
236
			->getMock();
237
238
		$param = ['site' => 'unittest', 'id' => $real ? $this->getItem()->getId() : -1];
239
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
240
		$view->addHelper( 'param', $helper );
241
242
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $this->context->getConfig() );
243
		$view->addHelper( 'config', $helper );
244
245
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
246
		$view->addHelper( 'access', $helper );
247
248
		return $view;
249
	}
250
251
252
	protected function getItem( $label = 'Home category -10%' )
253
	{
254
		$manager = \Aimeos\MShop::create( $this->context, 'rule' );
255
		$search = $manager->filter()->slice( 0, 1 )->add( ['rule.label' => $label] );
256
257
		return $manager->search( $search )->first( new \Exception( sprintf( 'No rule with label "%1$s" found', $label ) ) );
258
	}
259
}
260