1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2020-2021 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Client\JsonApi\Review; |
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\Review\Standard( $this->context, 'review' ); |
25
|
|
|
$this->object->setView( $this->view ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
protected function tearDown() : void |
30
|
|
|
{ |
31
|
|
|
\Aimeos\Controller\Frontend\Review\Factory::injectController( '\Aimeos\Controller\Frontend\Review\Standard', null ); |
32
|
|
|
unset( $this->context, $this->object, $this->view ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function testGet() |
37
|
|
|
{ |
38
|
|
|
$params = array( |
39
|
|
|
'fields' => ['review' => 'review.id,review.name,review.comment'], |
40
|
|
|
'sort' => '-rating' |
41
|
|
|
); |
42
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
43
|
|
|
$this->view->addHelper( 'param', $helper ); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
47
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
50
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
51
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
54
|
|
|
$this->assertEquals( 'review', $result['data'][0]['type'] ); |
55
|
|
|
$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) ); |
56
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function testGetById() |
61
|
|
|
{ |
62
|
|
|
$params = ['id' => $this->getReview()->getId()]; |
63
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params ); |
64
|
|
|
$this->view->addHelper( 'param', $helper ); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
$response = $this->object->get( $this->view->request(), $this->view->response() ); |
68
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
69
|
|
|
|
70
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
71
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
72
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( 1, $result['meta']['total'] ); |
75
|
|
|
$this->assertEquals( 'review', $result['data']['type'] ); |
76
|
|
|
$this->assertEquals( 9, count( $result['data']['attributes'] ) ); |
77
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
public function testGetControllerException() |
82
|
|
|
{ |
83
|
|
|
$object = $this->object( 'search', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) ); |
84
|
|
|
|
85
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
86
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( 403, $response->getStatusCode() ); |
89
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
public function testGetMShopException() |
94
|
|
|
{ |
95
|
|
|
$object = $this->object( 'search', $this->throwException( new \Aimeos\MShop\Exception() ) ); |
96
|
|
|
|
97
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
98
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
99
|
|
|
|
100
|
|
|
$this->assertEquals( 404, $response->getStatusCode() ); |
101
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
public function testGetException() |
106
|
|
|
{ |
107
|
|
|
$object = $this->object( 'search', $this->throwException( new \Exception() ) ); |
108
|
|
|
|
109
|
|
|
$response = $object->get( $this->view->request(), $this->view->response() ); |
110
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
111
|
|
|
|
112
|
|
|
$this->assertEquals( 500, $response->getStatusCode() ); |
113
|
|
|
$this->assertArrayHasKey( 'errors', $result ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function testOptions() |
118
|
|
|
{ |
119
|
|
|
$response = $this->object->options( $this->view->request(), $this->view->response() ); |
120
|
|
|
$result = json_decode( (string) $response->getBody(), true ); |
121
|
|
|
|
122
|
|
|
$this->assertEquals( 200, $response->getStatusCode() ); |
123
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) ); |
124
|
|
|
$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) ); |
125
|
|
|
|
126
|
|
|
$this->assertEquals( null, $result['meta']['prefix'] ); |
127
|
|
|
$this->assertEquals( 0, count( $result['meta']['attributes'] ) ); |
128
|
|
|
$this->assertArrayHasKey( 'filter', $result['meta'] ); |
129
|
|
|
$this->assertArrayHasKey( 'sort', $result['meta'] ); |
130
|
|
|
$this->assertArrayNotHasKey( 'errors', $result ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns a test object with a mocked review controller |
136
|
|
|
* |
137
|
|
|
* @param string $method Review controller method name to mock |
138
|
|
|
* @param mixed $result Return value of the mocked method |
139
|
|
|
*/ |
140
|
|
|
protected function object( $method, $result ) |
141
|
|
|
{ |
142
|
|
|
$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Review\Standard::class ) |
143
|
|
|
->setConstructorArgs( [$this->context] ) |
144
|
|
|
->setMethods( [$method] ) |
145
|
|
|
->getMock(); |
146
|
|
|
|
147
|
|
|
$cntl->expects( $this->once() )->method( $method )->will( $result ); |
148
|
|
|
|
149
|
|
|
\Aimeos\Controller\Frontend\Review\Factory::injectController( '\Aimeos\Controller\Frontend\Review\Standard', $cntl ); |
150
|
|
|
|
151
|
|
|
$object = new \Aimeos\Client\JsonApi\Review\Standard( $this->context, 'review' ); |
152
|
|
|
$object->setView( $this->view ); |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
return $object; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
protected function getReview() |
160
|
|
|
{ |
161
|
|
|
$manager = \Aimeos\MShop::create( $this->context, 'review' ); |
162
|
|
|
|
163
|
|
|
$search = $manager->filter()->slice( 0, 1 ); |
164
|
|
|
$search->setConditions( $search->and( [ |
165
|
|
|
$search->compare( '==', 'review.domain', 'product' ), |
166
|
|
|
$search->compare( '>', 'review.status', 0 ) |
167
|
|
|
] ) ); |
168
|
|
|
|
169
|
|
|
return $manager->search( $search )->first( new \RuntimeException( 'No review item found' ) ); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|