Completed
Push — master ( 9ee11d...42e540 )
by Aimeos
02:00
created

StandardTest::testPost()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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