Completed
Push — master ( 2b6933...74f0c0 )
by Aimeos
02:02
created

StandardTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 177
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A tearDown() 0 4 1
A testGet() 0 23 1
A testGetSearch() 0 23 1
A testPatch() 0 32 1
A testPost() 0 27 1
A getSiteItem() 0 13 2
A getSiteMock() 0 22 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 */
7
8
9
namespace Aimeos\Admin\JsonAdm\Locale\Site;
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()
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperJadm::getContext();
24
		$this->view = $this->context->getView();
25
26
		$this->object = new \Aimeos\Admin\JsonAdm\Locale\Site\Standard( $this->context, 'locale/site' );
27
		$this->object->setAimeos( \TestHelperJadm::getAimeos() );
28
		$this->object->setView( $this->view );
29
	}
30
31
32
	protected function tearDown()
33
	{
34
		\Aimeos\MShop::cache( false );
35
	}
36
37
38
	public function testGet()
39
	{
40
		$params = array(
41
			'id' => $this->getSiteItem( 'unittest' )->getId(),
42
			'filter' => array(
43
				'==' => array( 'locale.status' => 0 )
44
			),
45
		);
46
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
47
		$this->view->addHelper( 'param', $helper );
48
49
		$response = $this->object->get( $this->view->request(), $this->view->response() );
50
		$result = json_decode( (string) $response->getBody(), true );
51
52
53
		$this->assertEquals( 200, $response->getStatusCode() );
54
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
55
56
		$this->assertEquals( 1, $result['meta']['total'] );
57
		$this->assertEquals( 'locale/site', $result['data']['type'] );
58
59
		$this->assertArrayNotHasKey( 'errors', $result );
60
	}
61
62
63
	public function testGetSearch()
64
	{
65
		$params = array(
66
			'filter' => array(
67
				'==' => array( 'locale.site.code' => 'unittest' )
68
			),
69
		);
70
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
71
		$this->view->addHelper( 'param', $helper );
72
73
		$response = $this->object->get( $this->view->request(), $this->view->response() );
74
		$result = json_decode( (string) $response->getBody(), true );
75
76
77
		$this->assertEquals( 200, $response->getStatusCode() );
78
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
79
80
		$this->assertEquals( 1, $result['meta']['total'] );
81
		$this->assertEquals( 1, count( $result['data'] ) );
82
		$this->assertEquals( 'locale/site', $result['data'][0]['type'] );
83
84
		$this->assertArrayNotHasKey( 'errors', $result );
85
	}
86
87
88
	public function testPatch()
89
	{
90
		$stub = $this->getSiteMock( array( 'getItem', 'moveItem', 'saveItem' ) );
91
		$item = $stub->createItem();
92
93
		$stub->expects( $this->once() )->method( 'saveItem' )
94
			->will( $this->returnValue( $stub->createItem() ) );
95
		$stub->expects( $this->exactly( 2 ) )->method( 'getItem' ) // 2x due to decorator
96
			->will( $this->returnValue( $item ) );
97
98
99
		$params = array( 'id' => '-1' );
100
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
101
		$this->view->addHelper( 'param', $helper );
102
103
		$body = '{"data": {"parentid": "1", "targetid": 2, "type": "locale/site", "attributes": {"locale.site.label": "test"}}}';
104
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
105
106
		$response = $this->object->patch( $request, $this->view->response() );
107
		$result = json_decode( (string) $response->getBody(), true );
108
109
110
		$this->assertEquals( 200, $response->getStatusCode() );
111
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
112
113
		$this->assertEquals( 1, $result['meta']['total'] );
114
		$this->assertArrayHasKey( 'data', $result );
115
		$this->assertEquals( 'locale/site', $result['data']['type'] );
116
117
		$this->assertArrayNotHasKey( 'included', $result );
118
		$this->assertArrayNotHasKey( 'errors', $result );
119
	}
120
121
122
	public function testPost()
123
	{
124
		$stub = $this->getSiteMock( array( 'getItem', 'insertItem' ) );
125
		$item = $stub->createItem();
126
127
		$stub->expects( $this->any() )->method( 'getItem' )
128
			->will( $this->returnValue( $item ) );
129
		$stub->expects( $this->once() )->method( 'insertItem' );
130
131
132
		$body = '{"data": {"type": "locale/site", "attributes": {"locale.site.code": "unittest", "locale.site.label": "Unit test"}}}';
133
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
134
135
		$response = $this->object->post( $request, $this->view->response() );
136
		$result = json_decode( (string) $response->getBody(), true );
137
138
139
		$this->assertEquals( 201, $response->getStatusCode() );
140
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
141
142
		$this->assertEquals( 1, $result['meta']['total'] );
143
		$this->assertArrayHasKey( 'data', $result );
144
		$this->assertEquals( 'locale/site', $result['data']['type'] );
145
146
		$this->assertArrayNotHasKey( 'included', $result );
147
		$this->assertArrayNotHasKey( 'errors', $result );
148
	}
149
150
151
	protected function getSiteItem( $code )
152
	{
153
		$manager = \Aimeos\MShop::create( $this->context, 'locale/site' );
154
		$search = $manager->createSearch();
155
		$search->setConditions( $search->compare( '==', 'locale.site.code', $code ) );
156
		$items = $manager->searchItems( $search );
157
158
		if( ( $item = reset( $items ) ) === false ) {
159
			throw new \RuntimeException( sprintf( 'No locale site item with code "%1$s" found', $code ) );
160
		}
161
162
		return $item;
163
	}
164
165
166
	protected function getSiteMock( array $methods )
167
	{
168
		$name = 'AdminJsonAdmStandard';
169
		$this->context->getConfig()->set( 'mshop/locale/manager/name', $name );
170
171
		$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Locale\\Manager\\Standard' )
172
			->setConstructorArgs( array( $this->context ) )
173
			->setMethods( array( 'getSubManager' ) )
174
			->getMock();
175
176
		$siteStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Locale\\Manager\\Site\\Standard' )
177
			->setConstructorArgs( array( $this->context ) )
178
			->setMethods( $methods )
179
			->getMock();
180
181
		$stub->expects( $this->once() )->method( 'getSubManager' )
182
			->will( $this->returnValue( $siteStub ) );
183
184
		\Aimeos\MShop\Locale\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Locale\\Manager\\' . $name, $stub );
185
186
		return $siteStub;
187
	}
188
}