StandardTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 92
c 1
b 0
f 0
dl 0
loc 169
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A setUp() 0 9 1
A testGetException() 0 19 1
A testGetMShopException() 0 19 1
A testOptions() 0 14 1
A testGetItemsCriteria() 0 18 1
A testGetItems() 0 22 1
A testGetItem() 0 36 1
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\Locale;
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\Locale\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, 'locale' );
41
		$search = $manager->filter()->add( 'locale.status', '==', 1 )
42
			->add( 'locale.languageid', '==', 'en' )
43
			->add( 'locale.currencyid', '==', 'EUR' )
44
			->slice( 0, 1 );
45
46
		$item = $manager->search( $search )->first( new \Exception( 'No locale item found' ) );
47
48
49
		$params = array(
50
			'id' => $item->getId(),
51
			'fields' => array(
52
				'locale' => 'locale.id,locale.languageid,locale.currencyid'
53
			),
54
		);
55
56
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
57
		$this->view->addHelper( 'param', $helper );
58
59
		$response = $this->object->get( $this->view->request(), $this->view->response() );
60
		$result = json_decode( (string) $response->getBody(), true );
61
62
63
		$this->assertEquals( 200, $response->getStatusCode() );
64
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
65
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
66
67
		$this->assertEquals( 1, $result['meta']['total'] );
68
		$this->assertEquals( 'locale', $result['data']['type'] );
69
		$this->assertGreaterThan( 0, $result['data']['attributes']['locale.id'] );
70
		$this->assertEquals( 'en', $result['data']['attributes']['locale.languageid'] );
71
		$this->assertEquals( 'EUR', $result['data']['attributes']['locale.currencyid'] );
72
73
		$this->assertArrayNotHasKey( 'errors', $result );
74
	}
75
76
77
	public function testGetItems()
78
	{
79
		$params = array( 'sort' => '-locale.languageid,locale.position' );
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
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( 1, $result['meta']['total'] );
92
		$this->assertEquals( 1, count( $result['data'] ) );
93
		$this->assertEquals( 'locale', $result['data'][0]['type'] );
94
		$this->assertGreaterThanOrEqual( 6, count( $result['data'][0]['attributes'] ) );
95
		$this->assertEquals( 'en', $result['data'][0]['attributes']['locale.languageid'] );
96
		$this->assertEquals( 'EUR', $result['data'][0]['attributes']['locale.currencyid'] );
97
98
		$this->assertArrayNotHasKey( 'errors', $result );
99
	}
100
101
102
	public function testGetItemsCriteria()
103
	{
104
		$params = array(
105
			'filter' => array(
106
				'>=' => array( 'locale.position' => 0 ),
107
			),
108
			'sort' => '-locale.position',
109
		);
110
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
111
		$this->view->addHelper( 'param', $helper );
112
113
		$response = $this->object->get( $this->view->request(), $this->view->response() );
114
		$result = json_decode( (string) $response->getBody(), true );
115
116
117
		$this->assertEquals( 200, $response->getStatusCode() );
118
		$this->assertEquals( 1, $result['meta']['total'] );
119
		$this->assertArrayNotHasKey( 'errors', $result );
120
	}
121
122
123
	public function testGetMShopException()
124
	{
125
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Locale\Standard::class )
126
			->setConstructorArgs( [$this->context, 'locale'] )
127
			->onlyMethods( ['getItems'] )
128
			->getMock();
129
130
		$object->expects( $this->once() )->method( 'getItems' )
131
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
132
133
134
		$object->setView( $this->view );
135
136
		$response = $object->get( $this->view->request(), $this->view->response() );
137
		$result = json_decode( (string) $response->getBody(), true );
138
139
140
		$this->assertEquals( 404, $response->getStatusCode() );
141
		$this->assertArrayHasKey( 'errors', $result );
142
	}
143
144
145
	public function testGetException()
146
	{
147
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Locale\Standard::class )
148
			->setConstructorArgs( [$this->context, 'locale'] )
149
			->onlyMethods( ['getItems'] )
150
			->getMock();
151
152
		$object->expects( $this->once() )->method( 'getItems' )
153
			->will( $this->throwException( new \Exception() ) );
154
155
156
		$object->setView( $this->view );
157
158
		$response = $object->get( $this->view->request(), $this->view->response() );
159
		$result = json_decode( (string) $response->getBody(), true );
160
161
162
		$this->assertEquals( 500, $response->getStatusCode() );
163
		$this->assertArrayHasKey( 'errors', $result );
164
	}
165
166
167
	public function testOptions()
168
	{
169
		$response = $this->object->options( $this->view->request(), $this->view->response() );
170
		$result = json_decode( (string) $response->getBody(), true );
171
172
		$this->assertEquals( 200, $response->getStatusCode() );
173
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
174
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
175
176
		$this->assertEquals( null, $result['meta']['prefix'] );
177
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
178
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
179
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
180
		$this->assertArrayNotHasKey( 'errors', $result );
181
	}
182
}
183