StandardTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 117
c 2
b 0
f 0
dl 0
loc 204
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetItemsCriteria() 0 18 1
A testGetException() 0 18 1
A setUp() 0 9 1
A testGetMShopException() 0 18 1
A testGetItems() 0 31 1
A testOptions() 0 14 1
A testGetItem() 0 31 1
A tearDown() 0 4 1
A testGetItemAddress() 0 30 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2025
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() : 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\Supplier\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
		$manager = \Aimeos\MShop::create( $this->context, 'supplier' );
41
		$supId = $manager->find( 'unitSupplier001' )->getId();
42
43
		$params = array(
44
			'id' => $supId,
45
			'fields' => array(
46
				'supplier' => 'supplier.id,supplier.label'
47
			),
48
			'sort' => 'supplier.id',
49
			'include' => 'media,text'
50
		);
51
52
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
53
		$this->view->addHelper( 'param', $helper );
54
55
		$response = $this->object->get( $this->view->request(), $this->view->response() );
56
		$result = json_decode( (string) $response->getBody(), true );
57
58
		$this->assertEquals( 200, $response->getStatusCode() );
59
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
60
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
61
62
		$this->assertEquals( 1, $result['meta']['total'] );
63
		$this->assertEquals( 'supplier', $result['data']['type'] );
64
		$this->assertEquals( 3, count( $result['data']['relationships']['text']['data'] ) );
65
		$this->assertEquals( 1, count( $result['data']['relationships']['media']['data'] ) );
66
		$this->assertEquals( 4, count( $result['included'] ) );
67
68
		$this->assertArrayNotHasKey( 'errors', $result );
69
	}
70
71
72
	public function testGetItemAddress()
73
	{
74
		$manager = \Aimeos\MShop::create( $this->context, 'supplier' );
75
		$supId = $manager->find( 'unitSupplier001' )->getId();
76
77
		$params = array(
78
			'id' => $supId,
79
			'fields' => array(
80
				'supplier' => 'supplier.id,supplier.address.firstname,supplier.address.lastname'
81
			),
82
			'sort' => 'supplier.id',
83
			'include' => 'supplier.address'
84
		);
85
86
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
87
		$this->view->addHelper( 'param', $helper );
88
89
		$response = $this->object->get( $this->view->request(), $this->view->response() );
90
		$result = json_decode( (string) $response->getBody(), true );
91
92
		$this->assertEquals( 200, $response->getStatusCode() );
93
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
94
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
95
96
		$this->assertEquals( 1, $result['meta']['total'] );
97
		$this->assertEquals( 'supplier', $result['data']['type'] );
98
		$this->assertEquals( 1, count( $result['data']['relationships']['supplier.address']['data'] ) );
99
		$this->assertEquals( 1, count( $result['included'] ) );
100
101
		$this->assertArrayNotHasKey( 'errors', $result );
102
	}
103
104
105
	public function testGetItems()
106
	{
107
		$params = array(
108
			'filter' => array(
109
				'=~' => array( 'supplier.code' => 'unitSupplier00' ),
110
			),
111
			'fields' => array(
112
				'supplier' => 'supplier.id,supplier.label,supplier.code'
113
			),
114
			'include' => 'media,text,supplier.address',
115
			'sort' => 'supplier.label,-supplier.id',
116
		);
117
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
118
		$this->view->addHelper( 'param', $helper );
119
120
		$response = $this->object->get( $this->view->request(), $this->view->response() );
121
		$result = json_decode( (string) $response->getBody(), true );
122
123
		$this->assertEquals( 200, $response->getStatusCode() );
124
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
125
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
126
127
		$this->assertEquals( 2, $result['meta']['total'] );
128
		$this->assertEquals( 2, count( $result['data'] ) );
129
		$this->assertEquals( 'supplier', $result['data'][0]['type'] );
130
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
131
		$this->assertEquals( 'unitSupplier001', $result['data'][0]['attributes']['supplier.code'] );
132
		$this->assertEquals( 'unitSupplier002', $result['data'][1]['attributes']['supplier.code'] );
133
		$this->assertEquals( 6, count( $result['included'] ) );
134
135
		$this->assertArrayNotHasKey( 'errors', $result );
136
	}
137
138
139
	public function testGetItemsCriteria()
140
	{
141
		$params = array(
142
			'filter' => array(
143
				'=~' => array( 'supplier.code' => 'unitSupplier00' ),
144
			),
145
			'sort' => 'supplier.label',
146
		);
147
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
148
		$this->view->addHelper( 'param', $helper );
149
150
		$response = $this->object->get( $this->view->request(), $this->view->response() );
151
		$result = json_decode( (string) $response->getBody(), true );
152
153
154
		$this->assertEquals( 200, $response->getStatusCode() );
155
		$this->assertEquals( 2, $result['meta']['total'] );
156
		$this->assertArrayNotHasKey( 'errors', $result );
157
	}
158
159
160
	public function testGetMShopException()
161
	{
162
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Supplier\Standard::class )
163
			->setConstructorArgs( [$this->context, 'supplier'] )
164
			->onlyMethods( ['getItems'] )
165
			->getMock();
166
167
		$object->expects( $this->once() )->method( 'getItems' )
168
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
169
170
		$object->setView( $this->view );
171
172
		$response = $object->get( $this->view->request(), $this->view->response() );
173
		$result = json_decode( (string) $response->getBody(), true );
174
175
176
		$this->assertEquals( 404, $response->getStatusCode() );
177
		$this->assertArrayHasKey( 'errors', $result );
178
	}
179
180
181
	public function testGetException()
182
	{
183
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Supplier\Standard::class )
184
			->setConstructorArgs( [$this->context, 'supplier'] )
185
			->onlyMethods( ['getItems'] )
186
			->getMock();
187
188
		$object->expects( $this->once() )->method( 'getItems' )
189
			->will( $this->throwException( new \Exception() ) );
190
191
		$object->setView( $this->view );
192
193
		$response = $object->get( $this->view->request(), $this->view->response() );
194
		$result = json_decode( (string) $response->getBody(), true );
195
196
197
		$this->assertEquals( 500, $response->getStatusCode() );
198
		$this->assertArrayHasKey( 'errors', $result );
199
	}
200
201
202
	public function testOptions()
203
	{
204
		$response = $this->object->options( $this->view->request(), $this->view->response() );
205
		$result = json_decode( (string) $response->getBody(), true );
206
207
		$this->assertEquals( 200, $response->getStatusCode() );
208
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
209
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
210
211
		$this->assertEquals( null, $result['meta']['prefix'] );
212
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
213
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
214
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
215
		$this->assertArrayNotHasKey( 'errors', $result );
216
	}
217
}
218