1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Admin\JsonAdm\Catalog; |
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\MShop::cache( true ); |
22
|
|
|
|
23
|
|
|
$this->context = \TestHelper::context(); |
24
|
|
|
$this->view = $this->context->view(); |
25
|
|
|
|
26
|
|
|
$this->object = new \Aimeos\Admin\JsonAdm\Catalog\Standard( $this->context, 'catalog' ); |
27
|
|
|
$this->object->setAimeos( \TestHelper::getAimeos() ); |
28
|
|
|
$this->object->setView( $this->view ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
protected function tearDown() : void |
33
|
|
|
{ |
34
|
|
|
\Aimeos\MShop::cache( false ); |
35
|
|
|
unset( $this->object, $this->view, $this->context ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function testGetSearch() |
40
|
|
|
{ |
41
|
|
|
$params = array( |
42
|
|
|
'filter' => array( |
43
|
|
|
'==' => array( 'catalog.code' => 'cafe' ) |
44
|
|
|
), |
45
|
|
|
'include' => 'text' |
46
|
|
|
); |
47
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
48
|
|
|
$this->view->addHelper( 'param', $helper ); |
49
|
|
|
|
50
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
51
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
55
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
56
|
|
|
|
57
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
58
|
|
|
$this->assertEquals( 1, count( $result['data'] ) ); |
59
|
|
|
$this->assertEquals( 'catalog', $result['data'][0]['type'] ); |
60
|
|
|
$this->assertEquals( 6, count( $result['data'][0]['relationships']['text']['data'] ) ); |
61
|
|
|
$this->assertEquals( 6, count( $result['included'] ) ); |
62
|
|
|
|
63
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
public function testGetTree() |
68
|
|
|
{ |
69
|
|
|
$params = array( |
70
|
|
|
'id' => $this->getCatalogItem( 'root' )->getId(), |
71
|
|
|
'include' => 'catalog,text' |
72
|
|
|
); |
73
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
74
|
|
|
$this->view->addHelper( 'param', $helper ); |
75
|
|
|
|
76
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
77
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
78
|
|
|
|
79
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
80
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
81
|
|
|
|
82
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
83
|
|
|
$this->assertEquals( 'catalog', $result['data']['type'] ); |
84
|
|
|
$this->assertEquals( 2, count( $result['data']['relationships']['catalog']['data'] ) ); |
85
|
|
|
$this->assertEquals( 2, count( $result['included'] ) ); |
86
|
|
|
|
87
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
public function testPatch() |
92
|
|
|
{ |
93
|
|
|
$stub = $this->getCatalogMock( array( 'get', 'move', 'save' ) ); |
94
|
|
|
$item = $stub->create()->setId( '-1' ); |
95
|
|
|
|
96
|
|
|
$stub->expects( $this->once() )->method( 'move' ); |
97
|
|
|
$stub->expects( $this->once() )->method( 'save' ) |
98
|
|
|
->willReturn( $item ); |
99
|
|
|
$stub->expects( $this->exactly( 2 ) )->method( 'get' ) // 2x due to decorator |
100
|
|
|
->willReturn( $item ); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
$params = array( 'id' => '-1' ); |
104
|
|
|
$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params ); |
105
|
|
|
$this->view->addHelper( 'param', $helper ); |
106
|
|
|
|
107
|
|
|
$body = '{"data": {"parentid": "1", "targetid": 2, "type": "catalog", "attributes": {"catalog.label": "test"}}}'; |
108
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
109
|
|
|
|
110
|
|
|
$response = $this->object->patch( $request, $this->view->response() ); |
111
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
115
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
116
|
|
|
|
117
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
118
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
119
|
|
|
$this->assertEquals( 'catalog', $result['data']['type'] ); |
120
|
|
|
|
121
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
122
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function testPost() |
127
|
|
|
{ |
128
|
|
|
$stub = $this->getCatalogMock( array( 'get', 'insert' ) ); |
129
|
|
|
$item = $stub->create()->setId( '-1' ); |
130
|
|
|
|
131
|
|
|
$stub->expects( $this->any() )->method( 'get' ) |
132
|
|
|
->willReturn( $item ); |
133
|
|
|
$stub->expects( $this->once() )->method( 'insert' ) |
134
|
|
|
->willReturn( $item ); |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
$body = '{"data": {"type": "catalog", "attributes": {"catalog.code": "test", "catalog.label": "Test catalog"}}}'; |
138
|
|
|
$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) ); |
139
|
|
|
|
140
|
|
|
$response = $this->object->post( $request, $this->view->response() ); |
141
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->assertEquals( 201, $response->getStatusCode() ); |
145
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
146
|
|
|
|
147
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
148
|
|
|
$this->assertArrayHasKey( 'data', $result ); |
149
|
|
|
$this->assertEquals( 'catalog', $result['data']['type'] ); |
150
|
|
|
|
151
|
|
|
$this->assertArrayNotHasKey( 'included', $result ); |
152
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
protected function getCatalogItem( $code ) |
157
|
|
|
{ |
158
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'catalog' ); |
159
|
|
|
$search = $manager->filter(); |
160
|
|
|
$search->setConditions( $search->compare( '==', 'catalog.code', $code ) ); |
161
|
|
|
|
162
|
|
|
if( ( $item = $manager->search( $search )->first() ) === null ) { |
163
|
|
|
throw new \RuntimeException( sprintf( 'No catalog item with code "%1$s" found', $code ) ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $item; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
protected function getCatalogMock( array $methods ) |
171
|
|
|
{ |
172
|
|
|
$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Catalog\\Manager\\Standard' ) |
173
|
|
|
->setConstructorArgs( array( $this->context ) ) |
174
|
|
|
->onlyMethods( $methods ) |
175
|
|
|
->getMock(); |
176
|
|
|
|
177
|
|
|
\Aimeos\MShop::inject( '\\Aimeos\\MShop\\Catalog\\Manager\\Standard', $stub ); |
178
|
|
|
|
179
|
|
|
return $stub; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|