StandardTest::tearDown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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