StandardTest::testGetItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 30
rs 9.6
c 0
b 0
f 0
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' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\Cms\Standard::__construct() has too many arguments starting with 'cms'. ( Ignorable by Annotation )

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

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

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...
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();
0 ignored issues
show
Bug introduced by
The method find() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Type\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Basket\Manager\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Cms\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Property\Type\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

39
		$pageId = $manager->/** @scrutinizer ignore-call */ find( '/contact' )->getId();
Loading history...
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