Passed
Push — master ( 413d3b...72c491 )
by Aimeos
06:13 queued 03:36
created

StandardTest::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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