Completed
Push — master ( e8440a...f62092 )
by Aimeos
02:50
created

StandardTest::testGetItems()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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