Completed
Push — master ( a13539...bd1b6d )
by Aimeos
04:27
created

StandardTest::testCopyMShopException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
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-2020
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Catalog;
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
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
25
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
26
		$this->view ->addHelper( 'request', $helper );
27
28
		$this->object = new \Aimeos\Admin\JQAdm\Catalog\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 testCopy()
42
	{
43
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
44
45
		$param = ['site' => 'unittest', 'id' => $manager->findItem( 'cafe' )->getId()];
46
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
47
		$this->view->addHelper( 'param', $helper );
48
49
		$result = $this->object->copy();
50
51
		$this->assertStringContainsString( 'cafe_copy', $result );
52
	}
53
54
55
	public function testCopyException()
56
	{
57
		$object = $this->getClientMock( 'getSubClients' );
58
59
		$object->expects( $this->once() )->method( 'getSubClients' )
60
			->will( $this->throwException( new \RuntimeException() ) );
61
62
		$object->copy();
63
	}
64
65
66
	public function testCreate()
67
	{
68
		$this->assertStringContainsString( 'item-catalog', $this->object->create() );
69
	}
70
71
72
	public function testCreateException()
73
	{
74
		$object = $this->getClientMock( 'getSubClients' );
75
76
		$object->expects( $this->once() )->method( 'getSubClients' )
77
			->will( $this->throwException( new \RuntimeException() ) );
78
79
		$object->create();
80
	}
81
82
83
	public function testDelete()
84
	{
85
		$this->assertNull( $this->getClientMock( ['nextAction'], 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
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
104
105
		$param = ['site' => 'unittest', 'id' => $manager->findItem( 'cafe' )->getId()];
106
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
107
		$this->view->addHelper( 'param', $helper );
108
109
		$result = $this->object->get();
110
111
		$this->assertStringContainsString( 'cafe', $result );
112
	}
113
114
115
	public function testGetException()
116
	{
117
		$object = $this->getClientMock( 'getSubClients' );
118
119
		$object->expects( $this->once() )->method( 'getSubClients' )
120
			->will( $this->throwException( new \RuntimeException() ) );
121
122
		$object->get();
123
	}
124
125
126
	public function testGetViewException()
127
	{
128
		$object = new \Aimeos\Admin\JQAdm\Catalog\Standard( $this->context, [] );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Admin\JQAdm\Catalog\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

128
		$object = /** @scrutinizer ignore-call */ new \Aimeos\Admin\JQAdm\Catalog\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...
129
130
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
131
		$object->getView();
132
	}
133
134
135
	public function testSave()
136
	{
137
		$param = array(
138
			'site' => 'unittest',
139
			'item' => array(
140
				'catalog.id' => '',
141
				'catalog.parentid' => '',
142
				'catalog.code' => 'jqadm catalog test',
143
				'catalog.label' => 'test label',
144
				'catalog.datestart' => null,
145
				'catalog.dateend' => null,
146
				'config' => [[
147
					'key' => 'test',
148
					'val' => 'value',
149
				]],
150
			),
151
		);
152
153
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
154
		$this->view->addHelper( 'param', $helper );
155
156
		$this->object->save();
157
158
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
159
		$item = $manager->findItem( 'jqadm catalog test' );
160
		$manager->deleteItem( $item->getId() );
161
162
		$this->assertEquals( ['test' => 'value'], $item->getConfig() );
163
	}
164
165
166
	public function testSaveException()
167
	{
168
		$object = $this->getClientMock( 'fromArray' );
169
170
		$object->expects( $this->once() )->method( 'fromArray' )
171
			->will( $this->throwException( new \RuntimeException() ) );
172
173
		$object->save();
174
	}
175
176
177
	public function testSearch()
178
	{
179
		$param = array( 'site' => 'unittest' );
180
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
181
		$this->view->addHelper( 'param', $helper );
182
183
		$result = $this->object->search();
184
185
		$this->assertStringContainsString( '<div class="tree-content">', $result );
186
	}
187
188
189
	public function testSearchException()
190
	{
191
		$object = $this->getClientMock( 'getRootId' );
192
193
		$object->expects( $this->once() )->method( 'getRootId' )
194
			->will( $this->throwException( new \RuntimeException() ) );
195
196
		$object->search();
197
	}
198
199
200
	public function testGetSubClient()
201
	{
202
		$result = $this->object->getSubClient( 'media' );
203
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
204
	}
205
206
207
	public function testGetSubClientInvalid()
208
	{
209
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
210
		$this->object->getSubClient( '$unknown$' );
211
	}
212
213
214
	public function testGetSubClientUnknown()
215
	{
216
		$this->expectException( \Aimeos\Admin\JQAdm\Exception::class );
217
		$this->object->getSubClient( 'unknown' );
218
	}
219
220
221
	public function getClientMock( $methods, $real = true )
222
	{
223
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Catalog\Standard::class )
224
			->setConstructorArgs( array( $this->context, \TestHelperJqadm::getTemplatePaths() ) )
225
			->setMethods( (array) $methods )
226
			->getMock();
227
228
		$object->setAimeos( \TestHelperJqadm::getAimeos() );
229
		$object->setView( $this->getViewNoRender( $real ) );
230
231
		return $object;
232
	}
233
234
235
	protected function getViewNoRender( $real = true )
236
	{
237
		$view = $this->getMockBuilder( \Aimeos\MW\View\Standard::class )
238
			->setConstructorArgs( array( [] ) )
239
			->setMethods( array( 'render' ) )
240
			->getMock();
241
242
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
243
244
		$param = ['site' => 'unittest', 'id' => $real ? $manager->findItem( 'cafe' )->getId() : -1];
245
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
246
		$view->addHelper( 'param', $helper );
247
248
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $this->context->getConfig() );
249
		$view->addHelper( 'config', $helper );
250
251
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
252
		$view->addHelper( 'access', $helper );
253
254
		return $view;
255
	}
256
}
257