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

StandardTest::testSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 9.7
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\Attribute;
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\Attribute\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( 'attribute', $result );
46
		$this->assertEmpty( $this->view->get( 'errors' ) );
47
	}
48
49
50
	public function testCreateException()
51
	{
52
		$object = $this->getClientMock( 'getSubClients' );
53
54
		$object->expects( $this->once() )->method( 'getSubClients' )
55
			->will( $this->throwException( new \RuntimeException() ) );
56
57
		$object->create();
58
	}
59
60
61
	public function testCopy()
62
	{
63
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
64
65
		$param = ['site' => 'unittest', 'id' => $manager->find( '30', [], 'product', 'length' )->getId()];
66
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
67
		$this->view->addHelper( 'param', $helper );
68
69
		$result = $this->object->copy();
70
71
		$this->assertStringContainsString( '30_', $result );
72
	}
73
74
75
	public function testCopyException()
76
	{
77
		$object = $this->getClientMock( 'getSubClients' );
78
79
		$object->expects( $this->once() )->method( 'getSubClients' )
80
			->will( $this->throwException( new \RuntimeException() ) );
81
82
		$object->copy();
83
	}
84
85
86
	public function testDelete()
87
	{
88
		$this->assertNull( $this->getClientMock( ['redirect'], false )->delete() );
89
	}
90
91
92
	public function testDeleteException()
93
	{
94
		$object = $this->getClientMock( ['getSubClients', 'search'] );
95
96
		$object->expects( $this->once() )->method( 'getSubClients' )
97
			->will( $this->throwException( new \RuntimeException() ) );
98
		$object->expects( $this->once() )->method( 'search' );
99
100
		$object->delete();
101
	}
102
103
104
	public function testGet()
105
	{
106
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
107
108
		$param = ['site' => 'unittest', 'id' => $manager->find( '30', [], 'product', 'length' )->getId()];
109
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
110
		$this->view->addHelper( 'param', $helper );
111
112
		$result = $this->object->get();
113
114
		$this->assertStringContainsString( '"30"', $result );
115
	}
116
117
118
	public function testGetException()
119
	{
120
		$object = $this->getClientMock( 'getSubClients' );
121
122
		$object->expects( $this->once() )->method( 'getSubClients' )
123
			->will( $this->throwException( new \RuntimeException() ) );
124
125
		$object->get();
126
	}
127
128
129
	public function testSave()
130
	{
131
		$manager = \Aimeos\MShop::create( $this->context, 'attribute' );
132
133
		$param = array(
134
			'site' => 'unittest',
135
			'item' => array(
136
				'attribute.id' => '',
137
				'attribute.domain' => 'product',
138
				'attribute.type' => 'color',
139
				'attribute.code' => 'test',
140
				'attribute.label' => 'test label',
141
				'attribute.position' => 1,
142
				'attribute.status' => -1,
143
			),
144
		);
145
146
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $param );
147
		$this->view->addHelper( 'param', $helper );
148
149
		$result = $this->object->save();
150
151
		$manager->delete( $manager->find( 'test', [], 'product', 'color' )->getId() );
152
153
		$this->assertEmpty( $this->view->get( 'errors' ) );
154
		$this->assertNull( $result );
155
	}
156
157
158
	public function testSaveException()
159
	{
160
		$object = $this->getClientMock( 'fromArray' );
161
162
		$object->expects( $this->once() )->method( 'fromArray' )
163
			->will( $this->throwException( new \RuntimeException() ) );
164
165
		$object->save();
166
	}
167
168
169
	public function testSearch()
170
	{
171
		$param = array(
172
			'site' => 'unittest', 'locale' => 'de',
173
			'filter' => array(
174
				'key' => array( 0 => 'attribute.code' ),
175
				'op' => array( 0 => '==' ),
176
				'val' => array( 0 => '30' ),
177
			),
178
			'sort' => array( 'attribute.label', '-attribute.id' ),
179
		);
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( '>30<', $result );
186
	}
187
188
189
	public function testSearchException()
190
	{
191
		$object = $this->getClientMock( 'initCriteria' );
192
193
		$object->expects( $this->once() )->method( 'initCriteria' )
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\Attribute\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, 'attribute' );
243
244
		$param = ['site' => 'unittest', 'id' => $real ? $manager->find( '30', [], 'product', 'length' )->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