Completed
Push — master ( 9b7730...2487b5 )
by Aimeos
02:06
created

StandardTest::testGetItemProperties()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
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), 2017
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Attribute;
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\Attribute\Standard( $this->context, $this->view, $templatePaths, 'attribute' );
26
	}
27
28
29
	public function testGetItem()
30
	{
31
		$attrManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
32
		$attrId = $attrManager->findItem( 'xs', [], 'product', 'size' )->getId();
33
34
		$params = array(
35
			'id' => $attrId,
36
			'fields' => array(
37
				'attribute' => 'attribute.id,attribute.label'
38
			),
39
			'sort' => 'attribute.id',
40
			'include' => 'media,price,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( 'attribute', $result['data']['type'] );
55
		$this->assertEquals( 3, count( $result['data']['relationships']['text']['data'] ) );
56
		$this->assertEquals( 1, count( $result['data']['relationships']['price']['data'] ) );
57
		$this->assertEquals( 1, count( $result['data']['relationships']['media']['data'] ) );
58
		$this->assertEquals( 5, count( $result['included'] ) );
59
60
		$this->assertArrayNotHasKey( 'errors', $result );
61
	}
62
63
64
	public function testGetItemProperties()
65
	{
66
		$attrManager = \Aimeos\MShop\Factory::createManager( $this->context, 'attribute' );
67
		$attrId = $attrManager->findItem( 'testurl', [], 'product', 'download' )->getId();
68
69
		$params = array(
70
			'id' => $attrId,
71
			'fields' => array(
72
				'attribute' => 'attribute.id,attribute.property.value,attribute.property.type.code'
73
			),
74
			'sort' => 'attribute.id',
75
			'include' => 'attribute/property'
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( 'attribute', $result['data']['type'] );
90
		$this->assertEquals( 2, count( $result['data']['relationships']['attribute/property']['data'] ) );
91
		$this->assertEquals( 2, count( $result['included'] ) );
92
93
		$this->assertArrayNotHasKey( 'errors', $result );
94
	}
95
96
97
	public function testGetItems()
98
	{
99
		$this->context->getConfig()->set( 'client/jsonapi/attribute/types', ['size', 'length', 'width'] );
100
101
		$params = array(
102
			'fields' => array(
103
				'attribute' => 'attribute.id,attribute.type,attribute.code'
104
			),
105
			'include' => 'media,price,text',
106
			'sort' => 'attribute.position',
107
		);
108
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
109
		$this->view->addHelper( 'param', $helper );
110
111
		$response = $this->object->get( $this->view->request(), $this->view->response() );
112
		$result = json_decode( (string) $response->getBody(), true );
113
114
		$this->assertEquals( 200, $response->getStatusCode() );
115
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
116
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
117
118
		$this->assertEquals( 17, $result['meta']['total'] );
119
		$this->assertEquals( 17, count( $result['data'] ) );
120
		$this->assertEquals( 'attribute', $result['data'][0]['type'] );
121
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
122
		$this->assertEquals( 'size', $result['data'][0]['attributes']['attribute.type'] );
123
		$this->assertEquals( 'xs', $result['data'][0]['attributes']['attribute.code'] );
124
		$this->assertEquals( 21, count( $result['included'] ) );
125
126
		foreach( $result['data'] as $entry ) {
127
			$this->assertContains( $entry['attributes']['attribute.type'], ['size', 'length', 'width'] );
128
		}
129
130
		$this->assertArrayNotHasKey( 'errors', $result );
131
	}
132
133
134
	public function testGetItemsCriteria()
135
	{
136
		$params = array(
137
			'filter' => array(
138
				'==' => array( 'attribute.type.code' => 'size' ),
139
			),
140
			'sort' => 'attribute.position',
141
		);
142
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
143
		$this->view->addHelper( 'param', $helper );
144
145
		$response = $this->object->get( $this->view->request(), $this->view->response() );
146
		$result = json_decode( (string) $response->getBody(), true );
147
148
149
		$this->assertEquals( 200, $response->getStatusCode() );
150
		$this->assertEquals( 6, $result['meta']['total'] );
151
		$this->assertArrayNotHasKey( 'errors', $result );
152
	}
153
154
155
	public function testGetMShopException()
156
	{
157
		$templatePaths = \TestHelperJapi::getTemplatePaths();
158
159
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Attribute\Standard' )
160
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'attribute'] )
161
			->setMethods( ['getItems'] )
162
			->getMock();
163
164
		$object->expects( $this->once() )->method( 'getItems' )
165
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
166
167
168
		$response = $object->get( $this->view->request(), $this->view->response() );
169
		$result = json_decode( (string) $response->getBody(), true );
170
171
172
		$this->assertEquals( 404, $response->getStatusCode() );
173
		$this->assertArrayHasKey( 'errors', $result );
174
	}
175
176
177
	public function testGetException()
178
	{
179
		$templatePaths = \TestHelperJapi::getTemplatePaths();
180
181
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Attribute\Standard' )
182
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'attribute'] )
183
			->setMethods( ['getItems'] )
184
			->getMock();
185
186
		$object->expects( $this->once() )->method( 'getItems' )
187
			->will( $this->throwException( new \Exception() ) );
188
189
190
		$response = $object->get( $this->view->request(), $this->view->response() );
191
		$result = json_decode( (string) $response->getBody(), true );
192
193
194
		$this->assertEquals( 500, $response->getStatusCode() );
195
		$this->assertArrayHasKey( 'errors', $result );
196
	}
197
198
199
	public function testOptions()
200
	{
201
		$response = $this->object->options( $this->view->request(), $this->view->response() );
202
		$result = json_decode( (string) $response->getBody(), true );
203
204
		$this->assertEquals( 200, $response->getStatusCode() );
205
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
206
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
207
208
		$this->assertEquals( null, $result['meta']['prefix'] );
209
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
210
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
211
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
212
		$this->assertArrayNotHasKey( 'errors', $result );
213
	}
214
}