1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2021-2025 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Client\JsonApi\Cms; |
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 = \TestHelper::context(); |
22
|
|
|
$this->context->locale()->setLanguageId( 'en' ); |
23
|
|
|
$this->view = $this->context->view(); |
24
|
|
|
|
25
|
|
|
$this->object = new \Aimeos\Client\JsonApi\Cms\Standard( $this->context, 'cms' ); |
|
|
|
|
26
|
|
|
$this->object->setView( $this->view ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
protected function tearDown() : void |
31
|
|
|
{ |
32
|
|
|
unset( $this->object, $this->view, $this->context ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function testGetItem() |
37
|
|
|
{ |
38
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'cms' ); |
39
|
|
|
$pageId = $manager->find( '/contact' )->getId(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$params = array( |
42
|
|
|
'id' => $pageId, |
43
|
|
|
'fields' => array( |
44
|
|
|
'cms' => 'cms.id,cms.label' |
45
|
|
|
), |
46
|
|
|
'sort' => 'cms.id', |
47
|
|
|
'include' => 'text' |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
51
|
|
|
$this->view->addHelper( 'param', $helper ); |
52
|
|
|
|
53
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
54
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
55
|
|
|
|
56
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
57
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
58
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
59
|
|
|
|
60
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
61
|
|
|
$this->assertEquals( 'cms', $result['data']['type'] ); |
62
|
|
|
$this->assertEquals( 3, count( $result['data']['relationships']['text']['data'] ) ); |
63
|
|
|
$this->assertEquals( 3, count( $result['included'] ) ); |
64
|
|
|
|
65
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function testGetItems() |
70
|
|
|
{ |
71
|
|
|
$params = array( |
72
|
|
|
'filter' => array( |
73
|
|
|
'=~' => array( 'cms.url' => '/cont' ), |
74
|
|
|
), |
75
|
|
|
'fields' => array( |
76
|
|
|
'cms' => 'cms.id,cms.label,cms.url' |
77
|
|
|
), |
78
|
|
|
'include' => 'text', |
79
|
|
|
'sort' => 'cms.label,-cms.id', |
80
|
|
|
); |
81
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
82
|
|
|
$this->view->addHelper( 'param', $helper ); |
83
|
|
|
|
84
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
85
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
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( 'cms', $result['data'][0]['type'] ); |
94
|
|
|
$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) ); |
95
|
|
|
$this->assertEquals( 3, count( $result['included'] ) ); |
96
|
|
|
|
97
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
public function testGetItemsCriteria() |
102
|
|
|
{ |
103
|
|
|
$params = array( |
104
|
|
|
'filter' => array( |
105
|
|
|
'=~' => array( 'cms.url' => '/contact' ), |
106
|
|
|
), |
107
|
|
|
'sort' => 'cms.label', |
108
|
|
|
); |
109
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
110
|
|
|
$this->view->addHelper( 'param', $helper ); |
111
|
|
|
|
112
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
113
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
117
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
118
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
public function testGetMShopException() |
123
|
|
|
{ |
124
|
|
|
$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Cms\Standard::class ) |
125
|
|
|
->setConstructorArgs( [$this->context, 'cms'] ) |
126
|
|
|
->onlyMethods( ['getItems'] ) |
127
|
|
|
->getMock(); |
128
|
|
|
|
129
|
|
|
$object->expects( $this->once() )->method( 'getItems' ) |
130
|
|
|
->will( $this->throwException( new \Aimeos\MShop\Exception() ) ); |
131
|
|
|
|
132
|
|
|
$object->setView( $this->view ); |
133
|
|
|
|
134
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
135
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
139
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
public function testGetException() |
144
|
|
|
{ |
145
|
|
|
$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Cms\Standard::class ) |
146
|
|
|
->setConstructorArgs( [$this->context, 'cms'] ) |
147
|
|
|
->onlyMethods( ['getItems'] ) |
148
|
|
|
->getMock(); |
149
|
|
|
|
150
|
|
|
$object->expects( $this->once() )->method( 'getItems' ) |
151
|
|
|
->will( $this->throwException( new \Exception() ) ); |
152
|
|
|
|
153
|
|
|
$object->setView( $this->view ); |
154
|
|
|
|
155
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
156
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
160
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
public function testOptions() |
165
|
|
|
{ |
166
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
167
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
168
|
|
|
|
169
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
170
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
171
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
172
|
|
|
|
173
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
174
|
|
|
$this->assertArrayNotHasKey( 'attributes', $result['meta'] ); |
175
|
|
|
$this->assertArrayNotHasKey( 'filter', $result['meta'] ); |
176
|
|
|
$this->assertArrayNotHasKey( 'sort', $result['meta'] ); |
177
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
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.