Completed
Push — master ( d6822c...031891 )
by Aimeos
02:59
created

StandardTest::testGetItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 32
rs 9.568
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Catalog;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
{
14
	private $context;
15
	private $object;
16
	private $view;
17
18
19
	protected function setUp()
20
	{
21
		$this->context = \TestHelperJapi::getContext();
22
		$this->view = $this->context->getView();
23
24
		$this->object = new \Aimeos\Client\JsonApi\Catalog\Standard( $this->context, 'catalog' );
25
		$this->object->setView( $this->view );
26
	}
27
28
29
	protected function tearDown()
30
	{
31
		unset( $this->object, $this->context, $this->view );
32
	}
33
34
35
	public function testGetItem()
36
	{
37
		$catId = \Aimeos\MShop::create( $this->context, 'catalog' )->findItem( 'cafe' )->getId();
38
		$params = array(
39
			'id' => $catId,
40
			'fields' => array(
41
				'catalog' => 'catalog.id,catalog.label'
42
			),
43
			'sort' => 'catalog.id',
44
			'include' => 'catalog,media,text'
45
		);
46
47
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
48
		$this->view->addHelper( 'param', $helper );
49
50
		$response = $this->object->get( $this->view->request(), $this->view->response() );
51
		$result = json_decode( (string) $response->getBody(), true );
52
53
54
		$this->assertEquals( 200, $response->getStatusCode() );
55
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
56
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
57
58
		$this->assertEquals( 1, $result['meta']['total'] );
59
		$this->assertEquals( 'catalog', $result['data']['type'] );
60
		$this->assertEquals( 1, count( $result['data']['relationships']['text']['data'] ) );
61
		$this->assertEquals( 2, count( $result['data']['relationships']['media']['data'] ) );
62
		$this->assertEquals( 3, count( $result['included'] ) );
63
64
		$this->assertArrayNotHasKey( 'errors', $result );
65
	}
66
67
68
	public function testGetItems()
69
	{
70
		$params = array(
71
			'filter' => [
72
				'==' => ['catalog.code' => ['cafe', 'tea']],
73
			],
74
			'fields' => array(
75
				'catalog' => 'catalog.id,catalog.label'
76
			),
77
			'sort' => 'catalog.id',
78
			'include' => 'media,text'
79
		);
80
81
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
82
		$this->view->addHelper( 'param', $helper );
83
84
		$response = $this->object->get( $this->view->request(), $this->view->response() );
85
		$result = json_decode( (string) $response->getBody(), true );
86
87
		$this->assertEquals( 200, $response->getStatusCode() );
88
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
89
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
90
91
		$this->assertEquals( 2, $result['meta']['total'] );
92
		$this->assertEquals( 'catalog', $result['data'][0]['type'] );
93
		$this->assertEquals( 1, count( $result['data'][0]['relationships']['text']['data'] ) );
94
		$this->assertEquals( 2, count( $result['data'][0]['relationships']['media']['data'] ) );
95
		$this->assertEquals( 'catalog', $result['data'][1]['type'] );
96
		$this->assertEquals( 5, count( $result['data'][1]['relationships']['text']['data'] ) );
97
		$this->assertEquals( 9, count( $result['included'] ) );
98
99
		$this->assertArrayNotHasKey( 'errors', $result );
100
	}
101
102
103
	public function testGetItemNoID()
104
	{
105
		$params = array(
106
			'include' => 'catalog,media'
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
115
		$this->assertEquals( 200, $response->getStatusCode() );
116
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
117
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
118
119
		$this->assertEquals( 1, $result['meta']['total'] );
120
		$this->assertEquals( 'catalog', $result['data']['type'] );
121
		$this->assertEquals( 'root', $result['data']['attributes']['catalog.code'] );
122
		$this->assertEquals( 'Root', $result['data']['attributes']['catalog.label'] );
123
		$this->assertEquals( 2, count( $result['data']['relationships']['catalog']['data'] ) );
124
		$this->assertEquals( 'catalog', $result['data']['relationships']['catalog']['data'][0]['type'] );
125
		$this->assertEquals( 3, count( $result['included'] ) );
126
		$this->assertArrayHaskey( 'self', $result['included'][0]['links'] );
127
128
		$this->assertArrayNotHasKey( 'errors', $result );
129
	}
130
131
132
	public function testGetMShopException()
133
	{
134
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Catalog\Standard::class )
135
			->setConstructorArgs( [$this->context, 'catalog'] )
136
			->setMethods( ['getItem'] )
137
			->getMock();
138
139
		$object->expects( $this->once() )->method( 'getItem' )
140
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
141
142
143
		$object->setView( $this->view );
144
145
		$response = $object->get( $this->view->request(), $this->view->response() );
146
		$result = json_decode( (string) $response->getBody(), true );
147
148
149
		$this->assertEquals( 404, $response->getStatusCode() );
150
		$this->assertArrayHasKey( 'errors', $result );
151
	}
152
153
154
	public function testGetException()
155
	{
156
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Catalog\Standard::class )
157
			->setConstructorArgs( [$this->context, 'catalog'] )
158
			->setMethods( ['getItem'] )
159
			->getMock();
160
161
		$object->expects( $this->once() )->method( 'getItem' )
162
			->will( $this->throwException( new \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( 500, $response->getStatusCode() );
172
		$this->assertArrayHasKey( 'errors', $result );
173
	}
174
175
176
	public function testOptions()
177
	{
178
		$response = $this->object->options( $this->view->request(), $this->view->response() );
179
		$result = json_decode( (string) $response->getBody(), true );
180
181
		$this->assertEquals( 200, $response->getStatusCode() );
182
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
183
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
184
185
		$this->assertEquals( null, $result['meta']['prefix'] );
186
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
187
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
188
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
189
		$this->assertArrayNotHasKey( 'errors', $result );
190
	}
191
}
192