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

StandardTest::testGetViewException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Coupon;
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
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
23
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
24
		$this->view->addHelper( 'request', $helper );
25
26
		$this->context = \TestHelperJqadm::getContext();
27
28
		$this->object = new \Aimeos\Admin\JQAdm\Coupon\Standard( $this->context );
29
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
30
		$this->object->setAimeos( \TestHelperJqadm::getAimeos() );
31
		$this->object->setView( $this->view );
32
	}
33
34
35
	protected function tearDown() : void
36
	{
37
		unset( $this->object, $this->view, $this->context );
38
	}
39
40
41
	public function testCreate()
42
	{
43
		$result = $this->object->create();
44
45
		$this->assertStringContainsString( 'item-coupon', $result );
46
	}
47
48
49
	public function testCreateException()
50
	{
51
		$object = $this->getClientMock( 'getSubClients' );
52
53
		$object->expects( $this->once() )->method( 'getSubClients' )
54
			->will( $this->throwException( new \RuntimeException() ) );
55
56
		$object->create();
57
	}
58
59
60
	public function testCopy()
61
	{
62
		$param = ['site' => 'unittest', 'id' => $this->getCoupon()->getId()];
63
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
64
		$this->view->addHelper( 'param', $helper );
65
66
		$result = $this->object->copy();
67
68
		$this->assertStringContainsString( 'Unit test example', $result );
69
	}
70
71
72
	public function testCopyException()
73
	{
74
		$object = $this->getClientMock( 'getSubClients' );
75
76
		$object->expects( $this->once() )->method( 'getSubClients' )
77
			->will( $this->throwException( new \RuntimeException() ) );
78
79
		$object->copy();
80
	}
81
82
83
	public function testDelete()
84
	{
85
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
86
	}
87
88
89
	public function testDeleteException()
90
	{
91
		$object = $this->getClientMock( ['getSubClients', 'search'] );
92
93
		$object->expects( $this->once() )->method( 'getSubClients' )
94
			->will( $this->throwException( new \RuntimeException() ) );
95
		$object->expects( $this->once() )->method( 'search' );
96
97
		$object->delete();
98
	}
99
100
101
	public function testGet()
102
	{
103
		$param = ['site' => 'unittest', 'id' => $this->getCoupon()->getId()];
104
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
105
		$this->view->addHelper( 'param', $helper );
106
107
		$result = $this->object->get();
108
109
		$this->assertStringContainsString( 'Unit test example', $result );
110
	}
111
112
113
	public function testGetException()
114
	{
115
		$object = $this->getClientMock( 'getSubClients' );
116
117
		$object->expects( $this->once() )->method( 'getSubClients' )
118
			->will( $this->throwException( new \RuntimeException() ) );
119
120
		$object->get();
121
	}
122
123
124
	public function testSave()
125
	{
126
		$manager = \Aimeos\MShop::create( $this->context, 'coupon' );
127
128
		$param = array(
129
			'site' => 'unittest',
130
			'item' => array(
131
				'coupon.id' => '',
132
				'coupon.label' => 'jqadm test label',
133
				'coupon.provider' => 'None',
134
				'coupon.datestart' => null,
135
				'coupon.dateend' => null,
136
				'config' => array(
137
					'key' => array( 0 => 'test key' ),
138
					'val' => array( 0 => 'test value' ),
139
				),
140
			),
141
		);
142
143
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
144
		$this->view->addHelper( 'param', $helper );
145
146
		$result = $this->object->save();
147
148
		$manager->delete( $this->getCoupon( 'jqadm test label' )->getId() );
149
150
		$this->assertEmpty( $this->view->get( 'errors' ) );
151
		$this->assertNull( $result );
152
	}
153
154
155
	public function testSaveException()
156
	{
157
		$object = $this->getClientMock( 'fromArray' );
158
159
		$object->expects( $this->once() )->method( 'fromArray' )
160
			->will( $this->throwException( new \RuntimeException() ) );
161
162
		$object->save();
163
	}
164
165
166
	public function testSearch()
167
	{
168
		$param = array(
169
			'site' => 'unittest', 'locale' => 'de',
170
			'filter' => array(
171
				'key' => array( 0 => 'coupon.label' ),
172
				'op' => array( 0 => '==' ),
173
				'val' => array( 0 => 'Unit test example' ),
174
			),
175
			'sort' => array( '-coupon.id' ),
176
		);
177
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
178
		$this->view->addHelper( 'param', $helper );
179
180
		$result = $this->object->search();
181
182
		$this->assertStringContainsString( '>Unit test example<', $result );
183
	}
184
185
186
	public function testSearchException()
187
	{
188
		$object = $this->getClientMock( 'initCriteria' );
189
190
		$object->expects( $this->once() )->method( 'initCriteria' )
191
			->will( $this->throwException( new \RuntimeException() ) );
192
193
		$object->search();
194
	}
195
196
197
	public function testSearchMShopException()
198
	{
199
		$object = $this->getClientMock( 'initCriteria' );
200
201
		$object->expects( $this->once() )->method( 'initCriteria' )
202
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
203
204
		$object->search();
205
	}
206
207
208
	public function testGetSubClient()
209
	{
210
		$result = $this->object->getSubClient( 'code' );
211
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
212
	}
213
214
215
	public function testGetSubClientInvalid()
216
	{
217
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
218
		$this->object->getSubClient( '$unknown$' );
219
	}
220
221
222
	public function testGetSubClientUnknown()
223
	{
224
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
225
		$this->object->getSubClient( 'unknown' );
226
	}
227
228
229
	public function testGetSubClientDecoratorInvalid()
230
	{
231
		$this->context->getConfig()->set( 'admin/jqadm/coupon/code/decorators/global', array( 'Invalid' ) );
232
233
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
234
		$this->object->getSubClient( 'code' );
235
	}
236
237
238
	public function getClientMock( $methods, $real = true )
239
	{
240
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Coupon\Standard::class )
241
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
242
			->setMethods( (array) $methods )
243
			->getMock();
244
245
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
246
		$object->setView( $this->getViewNoRender( $real ) );
247
248
		return $object;
249
	}
250
251
252
	protected function getViewNoRender( $real = true )
253
	{
254
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
255
			->setConstructorArgs( array( [] ) )
256
			->setMethods( array( 'render' ) )
257
			->getMock();
258
259
		$param = ['site' => 'unittest', 'id' => $real ? $this->getCoupon()->getId() : -1];
260
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
261
		$view->addHelper( 'param', $helper );
262
263
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $this->context->getConfig() );
264
		$view->addHelper( 'config', $helper );
265
266
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
267
		$view->addHelper( 'access', $helper );
268
269
		return $view;
270
	}
271
272
273
	protected function getCoupon( $label = 'Unit test example' )
274
	{
275
		$manager = \Aimeos\MShop::create( $this->context, 'coupon' );
276
277
		$search = $manager->filter();
278
		$search->setConditions( $search->compare( '==', 'coupon.label', $label ) );
279
280
		if( ( $item = $manager->search( $search )->first() ) === null ) {
281
			throw new \RuntimeException( sprintf( 'No coupon for label "%1$s" found', $label ) );
282
		}
283
284
		return $item;
285
	}
286
}
287