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