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