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

StandardTest::getViewNoRender()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 9.8666
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::view();
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 testSave()
122
	{
123
		$manager = \Aimeos\MShop::create( $this->context, 'rule' );
124
125
		$param = array(
126
			'site' => 'unittest',
127
			'item' => array(
128
				'rule.id' => '',
129
				'rule.type' =>'catalog',
130
				'rule.provider' => 'Percent',
131
				'rule.label' => 'test label',
132
				'rule.position' => '2',
133
				'config' => array(
134
					'key' => array( 0 => 'percent' ),
135
					'val' => array( 0 => '-10' ),
136
				),
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( $this->getItem( 'test label' )->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 => 'rule.label' ),
169
				'op' => array( 0 => '=~' ),
170
				'val' => array( 0 => 'Home category' ),
171
			),
172
			'sort' => array( 'rule.label', '-rule.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( '>Home category -10%<', $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 testGetSubClientInvalid()
195
	{
196
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
197
		$this->object->getSubClient( '$unknown$' );
198
	}
199
200
201
	public function testGetSubClientUnknown()
202
	{
203
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
204
		$this->object->getSubClient( 'unknown' );
205
	}
206
207
208
	public function getClientMock( $methods, $real = true )
209
	{
210
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Rule\Standard::class )
211
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
212
			->setMethods( (array) $methods )
213
			->getMock();
214
215
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
216
		$object->setView( $this->getViewNoRender( $real ) );
217
218
		return $object;
219
	}
220
221
222
	protected function getViewNoRender( $real = true )
223
	{
224
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
225
			->setConstructorArgs( array( [] ) )
226
			->setMethods( array( 'render' ) )
227
			->getMock();
228
229
		$param = ['site' => 'unittest', 'id' => $real ? $this->getItem()->getId() : -1];
230
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
231
		$view->addHelper( 'param', $helper );
232
233
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $this->context->getConfig() );
234
		$view->addHelper( 'config', $helper );
235
236
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
237
		$view->addHelper( 'access', $helper );
238
239
		return $view;
240
	}
241
242
243
	protected function getItem( $label = 'Home category -10%' )
244
	{
245
		$manager = \Aimeos\MShop::create( $this->context, 'rule' );
246
		$search = $manager->filter()->slice( 0, 1 )->add( ['rule.label' => $label] );
247
248
		return $manager->search( $search )->first( new \Exception( sprintf( 'No rule with label "%1$s" found', $label ) ) );
249
	}
250
}
251