Passed
Push — master ( 36a836...c7a44e )
by Aimeos
16:28 queued 07:56
created

StandardTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
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), 2017-2022
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, 'locale' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Lo...Standard::__construct() has too many arguments starting with 'locale'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
		$this->object = /** @scrutinizer ignore-call */ new \Aimeos\Client\JsonApi\Locale\Standard( $this->context, 'locale' );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

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