StandardTest::testGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
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-2025
6
 */
7
8
9
namespace Aimeos\Admin\JQAdm\Order;
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
		$request = $this->getMockBuilder( \Psr\Http\Message\ServerRequestInterface::class )->getMock();
23
		$helper = new \Aimeos\Base\View\Helper\Request\Standard( $this->view, $request, '127.0.0.1', 'test' );
24
		$this->view ->addHelper( 'request', $helper );
25
26
		$this->context = \TestHelper::context();
27
28
		$this->object = new \Aimeos\Admin\JQAdm\Order\Standard( $this->context );
29
		$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Page( $this->object, $this->context );
30
		$this->object->setAimeos( \TestHelper::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( 'order', $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
		$param = ['site' => 'unittest', 'id' => $this->getOrderBaseItem()->getId()];
64
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
65
		$this->view->addHelper( 'param', $helper );
66
67
		$result = $this->object->copy();
68
69
		$this->assertStringContainsString( 'This is another comment.', $result );
70
	}
71
72
73
	public function testCopyException()
74
	{
75
		$object = $this->getClientMock( 'getSubClients' );
76
77
		$object->expects( $this->once() )->method( 'getSubClients' )
78
			->will( $this->throwException( new \RuntimeException() ) );
79
80
		$object->copy();
81
	}
82
83
84
	public function testDelete()
85
	{
86
		$this->assertEmpty( $this->object->delete() );
87
	}
88
89
90
	public function testGet()
91
	{
92
		$param = ['site' => 'unittest', 'id' => $this->getOrderBaseItem()->getId()];
93
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
94
		$this->view->addHelper( 'param', $helper );
95
96
		$result = $this->object->get();
97
98
		$this->assertStringContainsString( 'This is another comment.', $result );
99
	}
100
101
102
	public function testGetException()
103
	{
104
		$object = $this->getClientMock( 'getSubClients' );
105
106
		$object->expects( $this->once() )->method( 'getSubClients' )
107
			->will( $this->throwException( new \RuntimeException() ) );
108
109
		$object->get();
110
	}
111
112
113
	public function testExport()
114
	{
115
		$param = ['site' => 'unittest',
116
			'filter' => [
117
				'key' => [0 => 'order.id'],
118
				'op' => [0 => '=='],
119
				'val' => [0 => '1'],
120
			],
121
			'sort' => ['-order.ctime', 'order.id'],
122
		];
123
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
124
		$this->view->addHelper( 'param', $helper );
125
126
		$result = $this->object->export();
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
127
128
		$mq = $this->context->queue( 'mq-admin', 'order-export' );
129
130
		$msg = $mq->get();
131
		$this->assertNotNull( $msg );
132
		$mq->del( $msg );
133
134
		$expected = '{"sitecode":"unittest","filter":{"&&":[{"==":{"order.id":"1"}}]},"sort":["-order.ctime","order.id"]}';
135
		$this->assertEquals( $expected, $msg->getBody() );
136
	}
137
138
139
	public function testExportException()
140
	{
141
		$object = $this->getClientMock( 'storeFilter' );
142
143
		$object->expects( $this->atLeastOnce() )->method( 'storeFilter' )
144
			->will( $this->throwException( new \RuntimeException() ) );
145
146
		$object->export();
147
	}
148
149
150
	public function testSave()
151
	{
152
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
153
		$this->view->item = $manager->create();
154
155
		$param = array(
156
			'site' => 'unittest',
157
			'item' => array(
158
				'order.comment' => 'jqadm test comment',
159
			),
160
		);
161
162
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
163
		$this->view->addHelper( 'param', $helper );
164
165
		$result = $this->object->save();
166
167
		$manager->delete( $this->getOrderBaseItem( 'jqadm test comment' )->getId() );
168
169
		$this->assertEmpty( $this->view->get( 'errors' ) );
170
		$this->assertNull( $result );
171
	}
172
173
174
	public function testSaveException()
175
	{
176
		$object = $this->getClientMock( 'fromArray' );
177
178
		$object->expects( $this->once() )->method( 'fromArray' )
179
			->will( $this->throwException( new \RuntimeException() ) );
180
181
		$object->save();
182
	}
183
184
185
	public function testSearch()
186
	{
187
		$param = array(
188
			'site' => 'unittest', 'locale' => 'de',
189
			'filter' => array(
190
				'key' => array( 0 => 'order.channel' ),
191
				'op' => array( 0 => '==' ),
192
				'val' => array( 0 => 'web' ),
193
			),
194
			'sort' => array( '-order.id' ),
195
		);
196
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $param );
197
		$this->view->addHelper( 'param', $helper );
198
199
		$result = $this->object->search();
200
201
		$this->assertStringContainsString( '>Unittest<', $result );
202
	}
203
204
205
	public function testSearchException()
206
	{
207
		$object = $this->getClientMock( 'initCriteria' );
208
209
		$object->expects( $this->once() )->method( 'initCriteria' )
210
			->will( $this->throwException( new \RuntimeException() ) );
211
212
		$object->search();
213
	}
214
215
216
	public function testGetSubClient()
217
	{
218
		$result = $this->object->getSubClient( 'transaction' );
219
		$this->assertInstanceOf( \Aimeos\Admin\JQAdm\Iface::class, $result );
220
	}
221
222
223
	public function testGetSubClientInvalid()
224
	{
225
		$this->expectException( \LogicException::class );
226
		$this->object->getSubClient( '$unknown$' );
227
	}
228
229
230
	public function testGetSubClientUnknown()
231
	{
232
		$this->expectException( \LogicException::class );
233
		$this->object->getSubClient( 'unknown' );
234
	}
235
236
237
	public function getClientMock( $method )
238
	{
239
		$object = $this->getMockBuilder( \Aimeos\Admin\JQAdm\Order\Standard::class )
240
			->setConstructorArgs( array( $this->context, \TestHelper::getTemplatePaths() ) )
241
			->onlyMethods( [$method] )
242
			->getMock();
243
244
		$object->setAimeos( \TestHelper::getAimeos() );
245
		$object->setView( $this->getViewNoRender() );
246
247
		return $object;
248
	}
249
250
251
	protected function getViewNoRender()
252
	{
253
		$view = $this->getMockBuilder( \Aimeos\Base\View\Standard::class )
254
			->setConstructorArgs( array( [] ) )
255
			->onlyMethods( ['render'] )->addMethods( ['config'] )
256
			->getMock();
257
258
		$trans = new \Aimeos\Base\Translation\None( 'de_DE' );
259
		$helper = new \Aimeos\Base\View\Helper\Translate\Standard( $view, $trans );
260
		$view->addHelper( 'translate', $helper );
261
262
		$param = ['site' => 'unittest', 'id' => $this->getOrderBaseItem()->getId()];
263
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $param );
264
		$view->addHelper( 'param', $helper );
265
266
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $this->context->config() );
267
		$view->addHelper( 'config', $helper );
268
269
		$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, [] );
270
		$view->addHelper( 'access', $helper );
271
272
		return $view;
273
	}
274
275
276
	protected function getOrderBaseItem( $comment = 'This is another comment.' )
277
	{
278
		$manager = \Aimeos\MShop::create( $this->context, 'order' );
279
		$search = $manager->filter()->add( 'order.comment', '==', $comment );
280
281
		return $manager->search( $search )->first( new \RuntimeException( 'No order item found' ) );
282
	}
283
}
284