Passed
Push — master ( 3cd384...c09dd4 )
by Aimeos
07:10
created

StandardTest::getViewNoRender()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 20
rs 9.8333
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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 = \TestHelper::view();
22
		$this->context = \TestHelper::context();
23
24
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
25
		$request->expects( $this->any() )->method( 'getUploadedFiles' )->willReturn( [] );
26
27
		$helper = new \Aimeos\Base\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
28
		$this->view ->addHelper( 'request', $helper );
29
30
		$this->object = new \Aimeos\Admin\JQAdm\Catalog\Standard( $this->context );
31
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
32
		$this->object->setAimeos( \TestHelper::getAimeos() );
33
		$this->object->setView( $this->view );
34
	}
35
36
37
	protected function tearDown() : void
38
	{
39
		unset( $this->object, $this->view, $this->context );
40
	}
41
42
43
	public function testCopy()
44
	{
45
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
46
47
		$param = ['site' => 'unittest', 'id' => $manager->find( 'cafe' )->getId()];
48
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
49
		$this->view->addHelper( 'param', $helper );
50
51
		$result = $this->object->copy();
52
53
		$this->assertStringContainsString( 'cafe_', $result );
54
	}
55
56
57
	public function testCopyException()
58
	{
59
		$object = $this->getClientMock( 'getSubClients' );
60
61
		$object->expects( $this->once() )->method( 'getSubClients' )
62
			->will( $this->throwException( new \RuntimeException() ) );
63
64
		$object->copy();
65
	}
66
67
68
	public function testCreate()
69
	{
70
		$this->assertStringContainsString( 'item-catalog', $this->object->create() );
71
	}
72
73
74
	public function testCreateException()
75
	{
76
		$object = $this->getClientMock( 'getSubClients' );
77
78
		$object->expects( $this->once() )->method( 'getSubClients' )
79
			->will( $this->throwException( new \RuntimeException() ) );
80
81
		$object->create();
82
	}
83
84
85
	public function testDelete()
86
	{
87
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
88
	}
89
90
91
	public function testDeleteException()
92
	{
93
		$object = $this->getClientMock( ['getSubClients', 'search'] );
94
95
		$object->expects( $this->once() )->method( 'getSubClients' )
96
			->will( $this->throwException( new \RuntimeException() ) );
97
		$object->expects( $this->once() )->method( 'search' );
98
99
		$object->delete();
100
	}
101
102
103
	public function testGet()
104
	{
105
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
106
107
		$param = ['site' => 'unittest', 'id' => $manager->find( 'cafe' )->getId()];
108
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
109
		$this->view->addHelper( 'param', $helper );
110
111
		$result = $this->object->get();
112
113
		$this->assertStringContainsString( 'cafe', $result );
114
	}
115
116
117
	public function testGetException()
118
	{
119
		$object = $this->getClientMock( 'getSubClients' );
120
121
		$object->expects( $this->once() )->method( 'getSubClients' )
122
			->will( $this->throwException( new \RuntimeException() ) );
123
124
		$object->get();
125
	}
126
127
128
	public function testSave()
129
	{
130
		$id = \Aimeos\MShop::create( $this->context, 'catalog' )->find( 'cafe' )->getId();
131
132
		$param = array(
133
			'id' => $id,
134
			'site' => 'unittest',
135
			'item' => array(
136
				'catalog.code' => 'cafe',
137
				'catalog.label' => 'Kaffee',
138
				'config' => [[
139
					'key' => 'css-class',
140
					'val' => 'coffee',
141
				]],
142
			),
143
		);
144
145
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
146
		$this->view->addHelper( 'param', $helper );
147
148
		$result = $this->object->save();
149
150
		$this->assertNotNull( $result );
151
	}
152
153
154
	public function testSaveException()
155
	{
156
		$object = $this->getClientMock( 'fromArray' );
157
158
		$object->expects( $this->once() )->method( 'fromArray' )
159
			->will( $this->throwException( new \RuntimeException() ) );
160
161
		$object->save();
162
	}
163
164
165
	public function testSearch()
166
	{
167
		$param = array( 'site' => 'unittest' );
168
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
169
		$this->view->addHelper( 'param', $helper );
170
171
		$result = $this->object->search();
172
173
		$this->assertStringContainsString( '</catalog-tree>', $result );
174
	}
175
176
177
	public function testGetSubClient()
178
	{
179
		$result = $this->object->getSubClient( 'media' );
180
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
181
	}
182
183
184
	public function testGetSubClientInvalid()
185
	{
186
		$this->expectException( \LogicException::class );
187
		$this->object->getSubClient( '$unknown$' );
188
	}
189
190
191
	public function testGetSubClientUnknown()
192
	{
193
		$this->expectException( \LogicException::class );
194
		$this->object->getSubClient( 'unknown' );
195
	}
196
197
198
	public function getClientMock( $methods, $real = true )
199
	{
200
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Catalog\Standard::class )
201
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
202
			->onlyMethods( (array) $methods )
203
			->getMock();
204
205
		$object->setAimeos( \TestHelper::getAimeos() );
206
		$object->setView( $this->getViewNoRender( $real ) );
207
208
		return $object;
209
	}
210
211
212
	protected function getViewNoRender( $real = true )
213
	{
214
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
215
			->setConstructorArgs( array( [] ) )
216
			->onlyMethods( array( 'render' ) )
217
			->getMock();
218
219
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
220
221
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( 'cafe' )->getId() : -1];
222
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
223
		$view->addHelper( 'param', $helper );
224
225
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
226
		$view->addHelper( 'config', $helper );
227
228
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
229
		$view->addHelper( 'access', $helper );
230
231
		return $view;
232
	}
233
}
234