|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
|
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() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->context = \TestHelperJapi::getContext(); |
|
22
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
|
23
|
|
|
$this->view = $this->context->getView(); |
|
24
|
|
|
|
|
25
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Service\Standard( $this->context, $this->view, $templatePaths, 'service' ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
protected function tearDown() |
|
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( 13, 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\Factory::createManager( $this->context, 'service' ); |
|
64
|
|
|
$item = $manager->findItem( '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( 4, count( $result['data']['attributes'] ) ); |
|
85
|
|
|
$this->assertEquals( 4, count( $result['data']['attributes']['config'] ) ); |
|
86
|
|
|
$this->assertArrayHasKey( 'code', $result['data']['attributes']['config']['directdebit.accountowner'] ); |
|
87
|
|
|
$this->assertArrayHasKey( 'price.costs', $result['data']['attributes']['price'] ); |
|
88
|
|
|
$this->assertEquals( 'directdebit-test', $result['data']['attributes']['service.code'] ); |
|
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\Factory::createManager( $this->context, 'service' ); |
|
98
|
|
|
$item = $manager->findItem( 'unitcode', [], 'service', 'delivery' ); |
|
99
|
|
|
|
|
100
|
|
|
$params = array( |
|
101
|
|
|
'id' => $item->getId(), |
|
102
|
|
|
'include' => 'media,price,text', |
|
103
|
|
|
); |
|
104
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
|
105
|
|
|
$this->view->addHelper( 'param', $helper ); |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
|
109
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
|
112
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
|
113
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
|
116
|
|
|
$this->assertEquals( 'service', $result['data']['type'] ); |
|
117
|
|
|
$this->assertArrayHasKey( 'relationships', $result['data'] ); |
|
118
|
|
|
$this->assertEquals( 3, count( $result['data']['relationships'] ) ); |
|
119
|
|
|
$this->assertEquals( 5, count( $result['included'] ) ); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
public function testGetMShopException() |
|
126
|
|
|
{ |
|
127
|
|
|
$object = $this->getObject( 'getProvider', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
|
128
|
|
|
|
|
129
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['id' => -1] ); |
|
130
|
|
|
$this->view->addHelper( 'param', $helper ); |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
|
134
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
135
|
|
|
|
|
136
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
|
137
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
public function testGetException() |
|
142
|
|
|
{ |
|
143
|
|
|
$object = $this->getObject( 'getProvider', $this->throwException( new \Exception() ) ); |
|
144
|
|
|
|
|
145
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, ['id' => -1] ); |
|
146
|
|
|
$this->view->addHelper( 'param', $helper ); |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
|
150
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
|
153
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns a test object with a mocked service controller |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $method Service controller method name to mock |
|
161
|
|
|
* @param mixed $result Return value of the mocked method |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getObject( $method, $result ) |
|
164
|
|
|
{ |
|
165
|
|
|
$cntl = $this->getMockBuilder( '\Aimeos\Controller\Frontend\Service\Standard' ) |
|
166
|
|
|
->setConstructorArgs( [$this->context] ) |
|
167
|
|
|
->setMethods( [$method] ) |
|
168
|
|
|
->getMock(); |
|
169
|
|
|
|
|
170
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
|
171
|
|
|
|
|
172
|
|
|
\Aimeos\Controller\Frontend\Service\Factory::injectController( '\Aimeos\Controller\Frontend\Service\Standard', $cntl ); |
|
173
|
|
|
|
|
174
|
|
|
$templatePaths = \TestHelperJapi::getTemplatePaths(); |
|
175
|
|
|
$object = new \Aimeos\Client\JsonApi\Service\Standard( $this->context, $this->view, $templatePaths, 'service' ); |
|
176
|
|
|
|
|
177
|
|
|
return $object; |
|
178
|
|
|
} |
|
179
|
|
|
} |