Completed
Push — master ( 3a2973...d2fd5c )
by Aimeos
04:15
created

StandardTest::testGetItemAddress()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Supplier;
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
		$this->context = \TestHelperJapi::getContext();
22
		$this->view = $this->context->getView();
23
24
		$this->object = new \Aimeos\Client\JsonApi\Supplier\Standard( $this->context, 'supplier' );
25
		$this->object->setView( $this->view );
26
	}
27
28
29
	public function testGetItem()
30
	{
31
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'supplier' );
32
		$supId = $manager->findItem( 'unitCode001' )->getId();
33
34
		$params = array(
35
			'id' => $supId,
36
			'fields' => array(
37
				'supplier' => 'supplier.id,supplier.label'
38
			),
39
			'sort' => 'supplier.id',
40
			'include' => 'media,product,text'
41
		);
42
43
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
44
		$this->view->addHelper( 'param', $helper );
45
46
		$response = $this->object->get( $this->view->request(), $this->view->response() );
47
		$result = json_decode( (string) $response->getBody(), true );
48
49
		$this->assertEquals( 200, $response->getStatusCode() );
50
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
51
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
52
53
		$this->assertEquals( 1, $result['meta']['total'] );
54
		$this->assertEquals( 'supplier', $result['data']['type'] );
55
		$this->assertEquals( 3, count( $result['data']['relationships']['text']['data'] ) );
56
		$this->assertEquals( 2, count( $result['data']['relationships']['product']['data'] ) );
57
		$this->assertEquals( 1, count( $result['data']['relationships']['media']['data'] ) );
58
		$this->assertEquals( 6, count( $result['included'] ) );
59
60
		$this->assertArrayNotHasKey( 'errors', $result );
61
	}
62
63
64
	public function testGetItemAddress()
65
	{
66
		$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'supplier' );
67
		$supId = $manager->findItem( 'unitCode001' )->getId();
68
69
		$params = array(
70
			'id' => $supId,
71
			'fields' => array(
72
				'supplier' => 'supplier.id,supplier.address.firstname,supplier.address.lastname'
73
			),
74
			'sort' => 'supplier.id',
75
			'include' => 'supplier/address'
76
		);
77
78
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
79
		$this->view->addHelper( 'param', $helper );
80
81
		$response = $this->object->get( $this->view->request(), $this->view->response() );
82
		$result = json_decode( (string) $response->getBody(), true );
83
84
		$this->assertEquals( 200, $response->getStatusCode() );
85
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
86
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
87
88
		$this->assertEquals( 1, $result['meta']['total'] );
89
		$this->assertEquals( 'supplier', $result['data']['type'] );
90
		$this->assertEquals( 1, count( $result['data']['relationships']['supplier/address']['data'] ) );
91
		$this->assertEquals( 1, count( $result['included'] ) );
92
93
		$this->assertArrayNotHasKey( 'errors', $result );
94
	}
95
96
97
	public function testGetItems()
98
	{
99
		$params = array(
100
			'fields' => array(
101
				'supplier' => 'supplier.id,supplier.label,supplier.code'
102
			),
103
			'include' => 'media,product,text,supplier/address',
104
			'sort' => 'supplier.label',
105
		);
106
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
107
		$this->view->addHelper( 'param', $helper );
108
109
		$response = $this->object->get( $this->view->request(), $this->view->response() );
110
		$result = json_decode( (string) $response->getBody(), true );
111
112
		$this->assertEquals( 200, $response->getStatusCode() );
113
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
114
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
115
116
		$this->assertEquals( 2, $result['meta']['total'] );
117
		$this->assertEquals( 2, count( $result['data'] ) );
118
		$this->assertEquals( 'supplier', $result['data'][0]['type'] );
119
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
120
		$this->assertEquals( 'unitCode001', $result['data'][0]['attributes']['supplier.code'] );
121
		$this->assertEquals( 'unitCode002', $result['data'][1]['attributes']['supplier.code'] );
122
		$this->assertEquals( 8, count( $result['included'] ) );
123
124
		$this->assertArrayNotHasKey( 'errors', $result );
125
	}
126
127
128
	public function testGetItemsCriteria()
129
	{
130
		$params = array(
131
			'filter' => array(
132
				'=~' => array( 'supplier.code' => 'unitCode00' ),
133
			),
134
			'sort' => 'supplier.label',
135
		);
136
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
137
		$this->view->addHelper( 'param', $helper );
138
139
		$response = $this->object->get( $this->view->request(), $this->view->response() );
140
		$result = json_decode( (string) $response->getBody(), true );
141
142
143
		$this->assertEquals( 200, $response->getStatusCode() );
144
		$this->assertEquals( 2, $result['meta']['total'] );
145
		$this->assertArrayNotHasKey( 'errors', $result );
146
	}
147
148
149
	public function testGetMShopException()
150
	{
151
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Supplier\Standard' )
152
			->setConstructorArgs( [$this->context, 'supplier'] )
153
			->setMethods( ['getItems'] )
154
			->getMock();
155
156
		$object->expects( $this->once() )->method( 'getItems' )
157
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
158
159
		$object->setView( $this->view );
160
161
		$response = $object->get( $this->view->request(), $this->view->response() );
162
		$result = json_decode( (string) $response->getBody(), true );
163
164
165
		$this->assertEquals( 404, $response->getStatusCode() );
166
		$this->assertArrayHasKey( 'errors', $result );
167
	}
168
169
170
	public function testGetException()
171
	{
172
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Supplier\Standard' )
173
			->setConstructorArgs( [$this->context, 'supplier'] )
174
			->setMethods( ['getItems'] )
175
			->getMock();
176
177
		$object->expects( $this->once() )->method( 'getItems' )
178
			->will( $this->throwException( new \Exception() ) );
179
180
		$object->setView( $this->view );
181
182
		$response = $object->get( $this->view->request(), $this->view->response() );
183
		$result = json_decode( (string) $response->getBody(), true );
184
185
186
		$this->assertEquals( 500, $response->getStatusCode() );
187
		$this->assertArrayHasKey( 'errors', $result );
188
	}
189
190
191
	public function testOptions()
192
	{
193
		$response = $this->object->options( $this->view->request(), $this->view->response() );
194
		$result = json_decode( (string) $response->getBody(), true );
195
196
		$this->assertEquals( 200, $response->getStatusCode() );
197
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
198
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
199
200
		$this->assertEquals( null, $result['meta']['prefix'] );
201
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
202
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
203
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
204
		$this->assertArrayNotHasKey( 'errors', $result );
205
	}
206
}