StandardTest::testGetById()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.552
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\Service;
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\Service\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 testGet()
39
	{
40
		$params = ['filter' => ['cs_type' => 'payment'], 'include' => ''];
41
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
42
		$this->view->addHelper( 'param', $helper );
43
44
45
		$response = $this->object->get( $this->view->request(), $this->view->response() );
46
		$result = json_decode( (string) $response->getBody(), true );
47
48
		$this->assertEquals( 200, $response->getStatusCode() );
49
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
50
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
51
52
		$this->assertEquals( 3, $result['meta']['total'] );
53
		$this->assertEquals( 'service', $result['data'][0]['type'] );
54
		$this->assertGreaterThan( 8, count( $result['data'][0]['attributes'] ) );
55
		$this->assertArrayHasKey( 'price.costs', $result['data'][0]['attributes']['price'] );
56
		$this->assertArrayNotHasKey( 'config', $result['data'][0]['attributes'] );
57
		$this->assertEquals( 0, count( $result['included'] ) );
58
59
		$this->assertArrayNotHasKey( 'errors', $result );
60
	}
61
62
63
	public function testGetById()
64
	{
65
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
66
		$item = $manager->find( 'directdebit-test', [], 'service', 'payment' );
67
68
		$params = array(
69
			'id' => $item->getId(),
70
			'fields' => array( 'service' => 'service.id,service.code' ),
71
			'include' => 'service.type',
72
		);
73
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
74
		$this->view->addHelper( 'param', $helper );
75
76
77
		$response = $this->object->get( $this->view->request(), $this->view->response() );
78
		$result = json_decode( (string) $response->getBody(), true );
79
80
		$this->assertEquals( 200, $response->getStatusCode() );
81
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
82
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
83
84
		$this->assertEquals( 1, $result['meta']['total'] );
85
		$this->assertEquals( 'service', $result['data']['type'] );
86
		$this->assertEquals( 3, count( $result['data']['attributes'] ) );
87
		$this->assertArrayHasKey( 'price.costs', $result['data']['attributes']['price'] );
88
		$this->assertEquals( 'directdebit-test', $result['data']['attributes']['service.code'] );
89
		$this->assertEquals( 4, count( $result['data']['links']['basket.service']['meta'] ) );
90
		$this->assertArrayHasKey( 'code', $result['data']['links']['basket.service']['meta']['directdebit.accountowner'] );
91
		$this->assertEquals( 1, count( $result['data']['relationships']['service.type']['data'] ) );
92
		$this->assertEquals( 1, count( $result['included'] ) );
93
94
		$this->assertArrayNotHasKey( 'errors', $result );
95
	}
96
97
98
	public function testGetIncluded()
99
	{
100
		$manager = \Aimeos\MShop::create( $this->context, 'service' );
101
		$item = $manager->find( 'unitdeliverycode', [], 'service', 'delivery' );
102
103
		$params = array(
104
			'id' => $item->getId(),
105
			'include' => 'media,price,text',
106
			'sort' => 'service.type,-service.position'
107
		);
108
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
109
		$this->view->addHelper( 'param', $helper );
110
111
112
		$response = $this->object->get( $this->view->request(), $this->view->response() );
113
		$result = json_decode( (string) $response->getBody(), true );
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( 'service', $result['data']['type'] );
121
		$this->assertArrayHasKey( 'relationships', $result['data'] );
122
		$this->assertEquals( 3, count( $result['data']['relationships'] ) );
123
		$this->assertEquals( 8, count( $result['included'] ) );
124
125
		$this->assertArrayNotHasKey( 'errors', $result );
126
	}
127
128
129
	public function testGetMShopException()
130
	{
131
		$object = $this->object( 'getProvider', $this->throwException( new \Aimeos\MShop\Exception() ) );
132
133
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, ['id' => -1] );
134
		$this->view->addHelper( 'param', $helper );
135
136
137
		$response = $object->get( $this->view->request(), $this->view->response() );
138
		$result = json_decode( (string) $response->getBody(), true );
139
140
		$this->assertEquals( 404, $response->getStatusCode() );
141
		$this->assertArrayHasKey( 'errors', $result );
142
	}
143
144
145
	public function testGetException()
146
	{
147
		$object = $this->object( 'getProvider', $this->throwException( new \Exception() ) );
148
149
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, ['id' => -1] );
150
		$this->view->addHelper( 'param', $helper );
151
152
153
		$response = $object->get( $this->view->request(), $this->view->response() );
154
		$result = json_decode( (string) $response->getBody(), true );
155
156
		$this->assertEquals( 500, $response->getStatusCode() );
157
		$this->assertArrayHasKey( 'errors', $result );
158
	}
159
160
161
	public function testOptions()
162
	{
163
		$response = $this->object->options( $this->view->request(), $this->view->response() );
164
		$result = json_decode( (string) $response->getBody(), true );
165
166
		$this->assertEquals( 200, $response->getStatusCode() );
167
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
168
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
169
170
		$this->assertEquals( null, $result['meta']['prefix'] );
171
		$this->assertEquals( 1, count( $result['meta']['filter'] ) );
172
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
173
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
174
		$this->assertArrayNotHasKey( 'errors', $result );
175
	}
176
177
178
	/**
179
	 * Returns a test object with a mocked service controller
180
	 *
181
	 * @param string $method Service controller method name to mock
182
	 * @param mixed $result Return value of the mocked method
183
	 */
184
	protected function object( $method, $result )
185
	{
186
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Service\Standard::class )
187
			->setConstructorArgs( [$this->context] )
188
			->onlyMethods( [$method] )
189
			->getMock();
190
191
		$cntl->expects( $this->once() )->method( $method )->will( $result );
192
193
		\Aimeos\Controller\Frontend::inject( '\Aimeos\Controller\Frontend\Service\Standard', $cntl );
194
195
		$object = new \Aimeos\Client\JsonApi\Service\Standard( $this->context, 'service' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Se...Standard::__construct() has too many arguments starting with 'service'. ( Ignorable by Annotation )

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

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

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...
196
		$object->setView( $this->view );
197
198
		return $object;
199
	}
200
}
201