StandardTest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 160
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetById() 0 18 1
A tearDown() 0 4 1
A testGetControllerException() 0 9 1
A testGet() 0 21 1
A object() 0 16 1
A setUp() 0 9 1
A getReview() 0 11 1
A testOptions() 0 14 1
A testGetException() 0 9 1
A testGetMShopException() 0 9 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2025
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
		\Aimeos\Controller\Frontend::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Client\JsonApi\Review\Standard( $this->context );
27
		$this->object->setView( $this->view );
28
	}
29
30
31
	protected function tearDown() : void
32
	{
33
		\Aimeos\Controller\Frontend::cache( false );
34
		unset( $this->view, $this->object, $this->context );
35
	}
36
37
38
	public function testGet()
39
	{
40
		$params = array(
41
			'fields' => ['review' => 'review.id,review.name,review.comment'],
42
			'sort' => '-rating'
43
		);
44
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
45
		$this->view->addHelper( 'param', $helper );
46
47
48
		$response = $this->object->get( $this->view->request(), $this->view->response() );
49
		$result = json_decode( (string) $response->getBody(), true );
50
51
		$this->assertEquals( 200, $response->getStatusCode() );
52
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
53
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
54
55
		$this->assertEquals( 1, $result['meta']['total'] );
56
		$this->assertEquals( 'review', $result['data'][0]['type'] );
57
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
58
		$this->assertArrayNotHasKey( 'errors', $result );
59
	}
60
61
62
	public function testGetById()
63
	{
64
		$params = ['id' => $this->getReview()->getId()];
65
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
66
		$this->view->addHelper( 'param', $helper );
67
68
69
		$response = $this->object->get( $this->view->request(), $this->view->response() );
70
		$result = json_decode( (string) $response->getBody(), true );
71
72
		$this->assertEquals( 200, $response->getStatusCode() );
73
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
74
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
75
76
		$this->assertEquals( 1, $result['meta']['total'] );
77
		$this->assertEquals( 'review', $result['data']['type'] );
78
		$this->assertEquals( 9, count( $result['data']['attributes'] ) );
79
		$this->assertArrayNotHasKey( 'errors', $result );
80
	}
81
82
83
	public function testGetControllerException()
84
	{
85
		$object = $this->object( 'search', $this->throwException( new \Aimeos\Controller\Frontend\Exception() ) );
86
87
		$response = $object->get( $this->view->request(), $this->view->response() );
88
		$result = json_decode( (string) $response->getBody(), true );
89
90
		$this->assertEquals( 403, $response->getStatusCode() );
91
		$this->assertArrayHasKey( 'errors', $result );
92
	}
93
94
95
	public function testGetMShopException()
96
	{
97
		$object = $this->object( 'search', $this->throwException( new \Aimeos\MShop\Exception() ) );
98
99
		$response = $object->get( $this->view->request(), $this->view->response() );
100
		$result = json_decode( (string) $response->getBody(), true );
101
102
		$this->assertEquals( 404, $response->getStatusCode() );
103
		$this->assertArrayHasKey( 'errors', $result );
104
	}
105
106
107
	public function testGetException()
108
	{
109
		$object = $this->object( 'search', $this->throwException( new \Exception() ) );
110
111
		$response = $object->get( $this->view->request(), $this->view->response() );
112
		$result = json_decode( (string) $response->getBody(), true );
113
114
		$this->assertEquals( 500, $response->getStatusCode() );
115
		$this->assertArrayHasKey( 'errors', $result );
116
	}
117
118
119
	public function testOptions()
120
	{
121
		$response = $this->object->options( $this->view->request(), $this->view->response() );
122
		$result = json_decode( (string) $response->getBody(), true );
123
124
		$this->assertEquals( 200, $response->getStatusCode() );
125
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
126
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
127
128
		$this->assertEquals( null, $result['meta']['prefix'] );
129
		$this->assertEquals( 0, count( $result['meta']['attributes'] ) );
130
		$this->assertArrayHasKey( 'filter', $result['meta'] );
131
		$this->assertArrayHasKey( 'sort', $result['meta'] );
132
		$this->assertArrayNotHasKey( 'errors', $result );
133
	}
134
135
136
	/**
137
	 * Returns a test object with a mocked review controller
138
	 *
139
	 * @param string $method Review controller method name to mock
140
	 * @param mixed $result Return value of the mocked method
141
	 */
142
	protected function object( $method, $result )
143
	{
144
		$cntl = $this->getMockBuilder( \Aimeos\Controller\Frontend\Review\Standard::class )
145
			->setConstructorArgs( [$this->context] )
146
			->onlyMethods( [$method] )
147
			->getMock();
148
149
		$cntl->expects( $this->once() )->method( $method )->will( $result );
150
151
		\Aimeos\Controller\Frontend::inject( '\Aimeos\Controller\Frontend\Review\Standard', $cntl );
152
153
		$object = new \Aimeos\Client\JsonApi\Review\Standard( $this->context, 'review' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Re...Standard::__construct() has too many arguments starting with 'review'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
		$object = /** @scrutinizer ignore-call */ new \Aimeos\Client\JsonApi\Review\Standard( $this->context, 'review' );

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.

Loading history...
154
		$object->setView( $this->view );
155
156
157
		return $object;
158
	}
159
160
161
	protected function getReview()
162
	{
163
		$manager = \Aimeos\MShop::create( $this->context, 'review' );
164
165
		$search = $manager->filter()->slice( 0, 1 );
166
		$search->setConditions( $search->and( [
167
			$search->compare( '==', 'review.domain', 'product' ),
168
			$search->compare( '>', 'review.status', 0 )
169
		] ) );
170
171
		return $manager->search( $search )->first( new \RuntimeException( 'No review item found' ) );
172
	}
173
}
174