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

StandardTest::testOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
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 testGetItems()
65
	{
66
		$this->context->getConfig()->set( 'client/jsonapi/attribute/types', ['size', 'length', 'width'] );
67
68
		$params = array(
69
			'fields' => array(
70
				'attribute' => 'attribute.id,attribute.type,attribute.code'
71
			),
72
			'include' => 'media,price,text',
73
			'sort' => 'attribute.position',
74
		);
75
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
76
		$this->view->addHelper( 'param', $helper );
77
78
		$response = $this->object->get( $this->view->request(), $this->view->response() );
79
		$result = json_decode( (string) $response->getBody(), true );
80
81
		$this->assertEquals( 200, $response->getStatusCode() );
82
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
83
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
84
85
		$this->assertEquals( 17, $result['meta']['total'] );
86
		$this->assertEquals( 17, count( $result['data'] ) );
87
		$this->assertEquals( 'attribute', $result['data'][0]['type'] );
88
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
89
		$this->assertEquals( 'size', $result['data'][0]['attributes']['attribute.type'] );
90
		$this->assertEquals( 'xs', $result['data'][0]['attributes']['attribute.code'] );
91
		$this->assertEquals( 21, count( $result['included'] ) );
92
93
		foreach( $result['data'] as $entry ) {
94
			$this->assertContains( $entry['attributes']['attribute.type'], ['size', 'length', 'width'] );
95
		}
96
97
		$this->assertArrayNotHasKey( 'errors', $result );
98
	}
99
100
101
	public function testGetItemsCriteria()
102
	{
103
		$params = array(
104
			'filter' => array(
105
				'==' => array( 'attribute.type.code' => 'size' ),
106
			),
107
			'sort' => 'attribute.position',
108
		);
109
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
110
		$this->view->addHelper( 'param', $helper );
111
112
		$response = $this->object->get( $this->view->request(), $this->view->response() );
113
		$result = json_decode( (string) $response->getBody(), true );
114
115
116
		$this->assertEquals( 200, $response->getStatusCode() );
117
		$this->assertEquals( 6, $result['meta']['total'] );
118
		$this->assertArrayNotHasKey( 'errors', $result );
119
	}
120
121
122
	public function testGetMShopException()
123
	{
124
		$templatePaths = \TestHelperJapi::getTemplatePaths();
125
126
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Attribute\Standard' )
127
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'attribute'] )
128
			->setMethods( ['getItems'] )
129
			->getMock();
130
131
		$object->expects( $this->once() )->method( 'getItems' )
132
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
133
134
135
		$response = $object->get( $this->view->request(), $this->view->response() );
136
		$result = json_decode( (string) $response->getBody(), true );
137
138
139
		$this->assertEquals( 404, $response->getStatusCode() );
140
		$this->assertArrayHasKey( 'errors', $result );
141
	}
142
143
144
	public function testGetException()
145
	{
146
		$templatePaths = \TestHelperJapi::getTemplatePaths();
147
148
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Attribute\Standard' )
149
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'attribute'] )
150
			->setMethods( ['getItems'] )
151
			->getMock();
152
153
		$object->expects( $this->once() )->method( 'getItems' )
154
			->will( $this->throwException( new \Exception() ) );
155
156
157
		$response = $object->get( $this->view->request(), $this->view->response() );
158
		$result = json_decode( (string) $response->getBody(), true );
159
160
161
		$this->assertEquals( 500, $response->getStatusCode() );
162
		$this->assertArrayHasKey( 'errors', $result );
163
	}
164
165
166
	public function testOptions()
167
	{
168
		$response = $this->object->options( $this->view->request(), $this->view->response() );
169
		$result = json_decode( (string) $response->getBody(), true );
170
171
		$this->assertEquals( 200, $response->getStatusCode() );
172
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
173
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
174
175
		$this->assertEquals( null, $result['meta']['prefix'] );
176
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
177
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
178
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
179
		$this->assertArrayNotHasKey( 'errors', $result );
180
	}
181
}