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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2025
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Site;
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\Site\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
		$siteId = \Aimeos\MShop::create( $this->context, 'locale/site' )->find( 'unittest' )->getId();
41
		$params = [
42
			'id' => $siteId,
43
			'fields' => [
44
				'locale/site' => 'locale.site.id,locale.site.label'
45
			],
46
			'sort' => 'locale.site.id'
47
		];
48
49
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
50
		$this->view->addHelper( 'param', $helper );
51
52
		$response = $this->object->get( $this->view->request(), $this->view->response() );
53
		$result = json_decode( (string) $response->getBody(), true );
54
55
56
		$this->assertEquals( 200, $response->getStatusCode() );
57
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
58
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
59
60
		$this->assertEquals( 1, $result['meta']['total'] );
61
		$this->assertEquals( 'locale/site', $result['data']['type'] );
62
		$this->assertEquals( 0, count( $result['included'] ) );
63
64
		$this->assertArrayNotHasKey( 'errors', $result );
65
	}
66
67
68
	public function testGetItems()
69
	{
70
		$params = [
71
			'filter' => [
72
				'==' => ['locale.site.code' => ['unittest']],
73
			],
74
			'fields' => [
75
				'locale/site' => 'locale.site.id,locale.site.label'
76
			],
77
			'sort' => 'locale.site.id',
78
		];
79
80
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
81
		$this->view->addHelper( 'param', $helper );
82
83
		$response = $this->object->get( $this->view->request(), $this->view->response() );
84
		$result = json_decode( (string) $response->getBody(), true );
85
86
		$this->assertEquals( 200, $response->getStatusCode() );
87
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
88
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
89
90
		$this->assertEquals( 1, $result['meta']['total'] );
91
		$this->assertEquals( 'locale/site', $result['data'][0]['type'] );
92
		$this->assertEquals( 0, count( $result['included'] ) );
93
94
		$this->assertArrayNotHasKey( 'errors', $result );
95
	}
96
97
98
	public function testGetItemDeep()
99
	{
100
		$config = $this->context->config()->set( 'client/jsonapi/site/deep', true );
101
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $this->view, $config );
102
		$this->view->addHelper( 'config', $helper );
103
104
		$siteId = \Aimeos\MShop::create( $this->context, 'locale/site' )->find( 'unittest' )->getId();
105
		$params = [
106
			'id' => $siteId,
107
			'fields' => [
108
				'locale/site' => 'locale.site.id,locale.site.label'
109
			],
110
			'sort' => 'locale.site.id',
111
			'include' => 'locale/site'
112
		];
113
114
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
115
		$this->view->addHelper( 'param', $helper );
116
117
		$response = $this->object->get( $this->view->request(), $this->view->response() );
118
		$result = json_decode( (string) $response->getBody(), true );
119
120
		$this->assertEquals( 200, $response->getStatusCode() );
121
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
122
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
123
124
		$this->assertEquals( 1, $result['meta']['total'] );
125
		$this->assertEquals( 'locale/site', $result['data']['type'] );
126
		$this->assertGreaterThanOrEqual( 0, count( $result['included'] ) );
127
128
		$this->assertArrayNotHasKey( 'errors', $result );
129
	}
130
131
132
	public function testGetMShopException()
133
	{
134
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Site\Standard::class )
135
			->setConstructorArgs( [$this->context, 'locale/site'] )
136
			->onlyMethods( ['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\Site\Standard::class )
157
			->setConstructorArgs( [$this->context, 'locale/site'] )
158
			->onlyMethods( ['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