Completed
Push — master ( 1a27eb...ec4895 )
by Aimeos
08:55
created

ExtadmControllerTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
c 2
b 1
f 0
lcom 1
cbo 9
dl 0
loc 324
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 21 1
A tearDown() 0 4 1
A indexAction() 0 49 1
B doAction() 0 38 1
A fileAction() 0 15 1
A getJsonLanguages() 0 14 1
A getJsonClientConfig() 0 19 1
A getJsonClientI18n() 0 17 1
B getJsonSiteItem() 0 29 1
B setLocale() 0 30 1
B setLocaleException() 0 33 1
1
<?php
2
3
4
namespace Aimeos\Shop\Tests\Unit\Controller;
5
6
7
class ExtadmControllerTest extends \TYPO3\Flow\Tests\UnitTestCase
8
{
9
	private $object;
10
	private $request;
11
	private $view;
12
13
14
	public function setUp()
15
	{
16
		$this->object = $this->getMockBuilder( '\Aimeos\Shop\Controller\ExtadmController' )
17
			->setMethods( null )
18
			->disableOriginalConstructor()
19
			->getMock();
20
21
		$this->view = $this->getMockBuilder( '\TYPO3\Flow\Mvc\View\JsonView' )
22
			->setMethods( array( 'assign', 'assignMultiple' ) )
23
			->disableOriginalConstructor()
24
			->getMock();
25
26
		$this->inject( $this->object, 'view', $this->view );
27
28
		$this->request = $this->getMockBuilder( '\TYPO3\Flow\Mvc\ActionRequest' )
29
			->setMethods( array( 'getArguments' ) )
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$this->inject( $this->object, 'request', $this->request );
34
	}
35
36
37
	public function tearDown()
38
	{
39
		\Aimeos\MShop\Factory::clear();
40
	}
41
42
43
	/**
44
	 * @test
45
	 */
46
	public function indexAction()
47
	{
48
		$this->object = $this->getMockBuilder( '\Aimeos\Shop\Controller\ExtadmController' )
49
			->setMethods( array( 'setLocale', 'getJsonLanguages', 'getJsonClientConfig', 'getJsonSiteItem', 'getJsonClientI18n' ) )
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$this->inject( $this->object, 'view', $this->view );
54
		$this->inject( $this->object, 'request', $this->request );
55
56
57
		$aimeos = new \Aimeos\Shop\Base\Aimeos();
58
		$this->inject( $this->object, 'aimeos', $aimeos );
59
60
61
		$context = $this->getMockBuilder( '\Aimeos\Shop\Base\Context' )
62
			->setMethods( array( 'get' ) )
63
			->disableOriginalConstructor()
64
			->getMock();
65
66
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
67
		$ctx->setConfig( new \Aimeos\MW\Config\PHPArray() );
68
		$ctx->setLocale( new \Aimeos\MShop\Locale\Item\Standard( array( 'langid' => 'de' ) ) );
69
70
		$this->inject( $this->object, 'context', $context );
71
72
73
		$uriBuilder = $this->getMockBuilder('\TYPO3\Flow\Mvc\Routing\UriBuilder')
74
			->setMethods( array( 'uriFor' ) )
75
			->disableOriginalConstructor()
76
			->getMock();
77
78
		$this->inject( $this->object, 'uriBuilder', $uriBuilder );
79
80
81
		$uriBuilder->expects( $this->exactly( 2 ) )->method( 'uriFor' )
82
			->will( $this->returnValue( '/test/uri' ) );
83
84
		$context->expects( $this->once() )->method( 'get' )
85
			->will( $this->returnValue( $ctx ) );
86
87
		$this->object->expects( $this->once() )->method( 'setLocale' )
88
			->will( $this->returnArgument( 0 ) );
89
90
		$this->view->expects( $this->once() )->method( 'assignMultiple' );
91
92
93
		$this->object->indexAction();
94
	}
95
96
97
	/**
98
	 * @test
99
	 */
100
	public function doAction()
101
	{
102
		$this->object = $this->getMockBuilder( '\Aimeos\Shop\Controller\ExtadmController' )
103
			->setMethods( array( 'setLocale' ) )
104
			->disableOriginalConstructor()
105
			->getMock();
106
107
		$this->object->expects( $this->once() )->method( 'setLocale' )
108
			->will( $this->returnArgument( 0 ) );
109
110
111
		$context = $this->getMockBuilder( '\Aimeos\Shop\Base\Context' )
112
			->setMethods( array( 'get' ) )
113
			->disableOriginalConstructor()
114
			->getMock();
115
116
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
117
		$ctx->setConfig( new \Aimeos\MW\Config\PHPArray() );
118
		$ctx->setLocale( new \Aimeos\MShop\Locale\Item\Standard( array( 'langid' => 'de' ) ) );
119
120
		$context->expects( $this->once() )->method( 'get' )
121
			->will( $this->returnValue( $ctx ) );
122
123
		$this->inject( $this->object, 'context', $context );
124
125
126
		$aimeos = new \Aimeos\Shop\Base\Aimeos();
127
		$this->inject( $this->object, 'aimeos', $aimeos );
128
129
130
		$this->request->expects( $this->once() )->method( 'getArguments' )
131
			->will( $this->returnValue( array()  ) );
132
133
		$this->inject( $this->object, 'request', $this->request );
134
135
136
		$this->assertStringStartsWith( '{', $this->object->doAction() );
137
	}
138
139
140
	/**
141
	 * @test
142
	 */
143
	public function fileAction()
144
	{
145
		$aimeos = new \Aimeos\Shop\Base\Aimeos();
146
		$this->inject( $this->object, 'aimeos', $aimeos );
147
148
149
		$response = $this->getMockBuilder( '\TYPO3\Flow\Http\Response' )
150
			->disableOriginalConstructor()
151
			->getMock();
152
153
		$this->inject( $this->object, 'response', $response );
154
155
156
		$this->assertInstanceOf( '\TYPO3\Flow\Mvc\ResponseInterface', $this->object->fileAction() );
157
	}
158
159
160
	/**
161
	 * @test
162
	 */
163
	public function getJsonLanguages()
164
	{
165
		$aimeos = new \Aimeos\Shop\Base\Aimeos();
166
		$this->inject( $this->object, 'aimeos', $aimeos );
167
168
		$class = new \ReflectionClass( '\Aimeos\Shop\Controller\ExtadmController' );
169
		$method = $class->getMethod( 'getJsonLanguages' );
170
		$method->setAccessible( true );
171
172
173
		$result = json_decode( $method->invoke( $this->object ), true );
174
175
		$this->assertGreaterThan( 0, count( $result ) );
176
	}
177
178
179
	/**
180
	 * @test
181
	 */
182
	public function getJsonClientConfig()
183
	{
184
		$ctx = $this->getMockBuilder( '\Aimeos\MShop\Context\Item\Standard' )
185
			->setMethods( array( 'setEditor' ) )
186
			->getMock();
187
188
		$ctx->setConfig( new \Aimeos\MW\Config\PHPArray() );
189
190
191
		$class = new \ReflectionClass( '\Aimeos\Shop\Controller\ExtadmController' );
192
		$method = $class->getMethod( 'getJsonClientConfig' );
193
		$method->setAccessible( true );
194
195
196
		$result = json_decode( $method->invoke( $this->object, $ctx ), true );
197
198
		$this->assertInternalType( 'array', $result );
199
		$this->assertArrayHasKey( 'admin', $result );
200
	}
201
202
203
	/**
204
	 * @test
205
	 */
206
	public function getJsonClientI18n()
207
	{
208
		$aimeos = new \Aimeos\Shop\Base\Aimeos();
209
		$i18nPaths = $aimeos->get()->getI18nPaths();
210
211
212
		$class = new \ReflectionClass( '\Aimeos\Shop\Controller\ExtadmController' );
213
		$method = $class->getMethod( 'getJsonClientI18n' );
214
		$method->setAccessible( true );
215
216
217
		$result = json_decode( $method->invoke( $this->object, $i18nPaths, 'de' ), true );
218
219
		$this->assertInternalType( 'array', $result );
220
		$this->assertArrayHasKey( 'admin', $result );
221
		$this->assertArrayHasKey( 'admin/ext', $result );
222
	}
223
224
225
	/**
226
	 * @test
227
	 */
228
	public function getJsonSiteItem()
229
	{
230
		$ctx = $this->getMock( '\Aimeos\MShop\Context\Item\Standard' );
231
232
233
		$siteManager = $this->getMockBuilder( '\Aimeos\MShop\Locale\Manager\Site\Standard' )
234
			->setMethods( array( 'searchItems' ) )
235
			->disableOriginalConstructor()
236
			->getMock();
237
238
		$items = array( new \Aimeos\MShop\Locale\Item\Site\Standard( array( 'id' => '1', 'label' => 'default' ) ) );
239
240
		$siteManager->expects( $this->once() )->method( 'searchItems' )
241
			->will( $this->returnValue( $items ) );
242
243
		\Aimeos\MShop\Factory::injectManager( $ctx, 'locale/site', $siteManager );
244
245
246
		$class = new \ReflectionClass( '\Aimeos\Shop\Controller\ExtadmController' );
247
		$method = $class->getMethod( 'getJsonSiteItem' );
248
		$method->setAccessible( true );
249
250
251
		$result = json_decode( $method->invoke( $this->object, $ctx, 'default' ), true );
252
253
		$this->assertInternalType( 'array', $result );
254
		$this->assertArrayHasKey( 'locale.site.id', $result );
255
		$this->assertArrayHasKey( 'locale.site.label', $result );
256
	}
257
258
259
	/**
260
	 * @test
261
	 */
262
	public function setLocale()
263
	{
264
		$ctx = $this->getMockBuilder( '\Aimeos\MShop\Context\Item\Standard' )
265
			->setMethods( array( 'setLocale' ) )
266
			->getMock();
267
268
		$ctx->expects( $this->once() )->method( 'setLocale' );
269
270
		$ctx->setConfig( new \Aimeos\MW\Config\PHPArray() );
271
272
273
		$localeManager = $this->getMockBuilder( '\Aimeos\MShop\Locale\Manager\Standard' )
274
			->setMethods( array( 'bootstrap' ) )
275
			->disableOriginalConstructor()
276
			->getMock();
277
278
		$localeManager->expects( $this->once() )->method( 'bootstrap' )
279
			->will( $this->returnValue( new \Aimeos\MShop\Locale\Item\Standard() ) );
280
281
		\Aimeos\MShop\Locale\Manager\Factory::injectManager( '\Aimeos\MShop\Locale\Manager\Standard', $localeManager );
282
283
284
		$class = new \ReflectionClass( '\Aimeos\Shop\Controller\ExtadmController' );
285
		$method = $class->getMethod( 'setLocale' );
286
		$method->setAccessible( true );
287
288
		$result = $method->invokeArgs( $this->object, array( $ctx ) );
289
290
		$this->assertInstanceOf( '\Aimeos\MShop\Context\Item\Iface', $result );
291
	}
292
293
294
	/**
295
	 * @test
296
	 */
297
	public function setLocaleException()
298
	{
299
		$ctx = $this->getMockBuilder( '\Aimeos\MShop\Context\Item\Standard' )
300
			->setMethods( array( 'setLocale' ) )
301
			->getMock();
302
303
		$ctx->expects( $this->once() )->method( 'setLocale' );
304
305
		$ctx->setConfig( new \Aimeos\MW\Config\PHPArray() );
306
307
308
		$localeManager = $this->getMockBuilder( '\Aimeos\MShop\Locale\Manager\Standard' )
309
			->setMethods( array( 'bootstrap', 'createItem' ) )
310
			->disableOriginalConstructor()
311
			->getMock();
312
313
		$localeManager->expects( $this->once() )->method( 'bootstrap' )
314
			->will( $this->throwException( new \Aimeos\MShop\Locale\Exception() ) );
315
316
		$localeManager->expects( $this->once() )->method( 'createItem' )
317
			->will( $this->returnValue( new \Aimeos\MShop\Locale\Item\Standard() ) );
318
319
		\Aimeos\MShop\Locale\Manager\Factory::injectManager( '\Aimeos\MShop\Locale\Manager\Standard', $localeManager );
320
321
322
		$class = new \ReflectionClass( '\Aimeos\Shop\Controller\ExtadmController' );
323
		$method = $class->getMethod( 'setLocale' );
324
		$method->setAccessible( true );
325
326
		$result = $method->invokeArgs( $this->object, array( $ctx ) );
327
328
		$this->assertInstanceOf( '\Aimeos\MShop\Context\Item\Iface', $result );
329
	}
330
}