Passed
Push — main ( f9f23c...f7616c )
by Aimeos
05:03
created

StandardTest::testGetException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Cms;
10
11
12
class StandardTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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->context->getLocale()->setLanguageId( 'en' );
23
		$this->view = $this->context->getView();
24
25
		$this->object = new \Aimeos\Client\JsonApi\Cms\Standard( $this->context, 'cms' );
26
		$this->object->setView( $this->view );
27
	}
28
29
30
	public function testGetItem()
31
	{
32
		$manager = \Aimeos\MShop::create( $this->context, 'cms' );
33
		$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\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Base\Coupon\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Order\Manager\Base\Service\Iface or Aimeos\MShop\Order\Manager\Base\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Order\Manag...Service\Attribute\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Order\Manag...Product\Attribute\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Base\Address\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\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\Order\Manager\Status\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\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\MShop\Attribute\Manager\Lists\Type\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\Base\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\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

33
		$pageId = $manager->/** @scrutinizer ignore-call */ find( '/contact' )->getId();
Loading history...
34
35
		$params = array(
36
			'id' => $pageId,
37
			'fields' => array(
38
				'cms' => 'cms.id,cms.label'
39
			),
40
			'sort' => 'cms.id',
41
			'include' => 'text'
42
		);
43
44
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
45
		$this->view->addHelper( 'param', $helper );
46
47
		$response = $this->object->get( $this->view->request(), $this->view->response() );
48
		$result = json_decode( (string) $response->getBody(), true );
49
50
		$this->assertEquals( 200, $response->getStatusCode() );
51
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
52
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
53
54
		$this->assertEquals( 1, $result['meta']['total'] );
55
		$this->assertEquals( 'cms', $result['data']['type'] );
56
		$this->assertEquals( 3, count( $result['data']['relationships']['text']['data'] ) );
57
		$this->assertEquals( 3, count( $result['included'] ) );
58
59
		$this->assertArrayNotHasKey( 'errors', $result );
60
	}
61
62
63
	public function testGetItems()
64
	{
65
		$params = array(
66
			'filter' => array(
67
				'=~' => array( 'cms.url' => '/cont' ),
68
			),
69
			'fields' => array(
70
				'cms' => 'cms.id,cms.label,cms.url'
71
			),
72
			'include' => 'text',
73
			'sort' => 'cms.label,-cms.id',
74
		);
75
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
76
		$this->view->addHelper( 'param', $helper );
77
78
		$response = $this->object->get( $this->view->request(), $this->view->response() );
79
		$result = json_decode( (string) $response->getBody(), true );
80
81
		$this->assertEquals( 200, $response->getStatusCode() );
82
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
83
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
84
85
		$this->assertEquals( 1, $result['meta']['total'] );
86
		$this->assertEquals( 1, count( $result['data'] ) );
87
		$this->assertEquals( 'cms', $result['data'][0]['type'] );
88
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
89
		$this->assertEquals( 3, count( $result['included'] ) );
90
91
		$this->assertArrayNotHasKey( 'errors', $result );
92
	}
93
94
95
	public function testGetItemsCriteria()
96
	{
97
		$params = array(
98
			'filter' => array(
99
				'=~' => array( 'cms.url' => '/contact' ),
100
			),
101
			'sort' => 'cms.label',
102
		);
103
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
104
		$this->view->addHelper( 'param', $helper );
105
106
		$response = $this->object->get( $this->view->request(), $this->view->response() );
107
		$result = json_decode( (string) $response->getBody(), true );
108
109
110
		$this->assertEquals( 200, $response->getStatusCode() );
111
		$this->assertEquals( 1, $result['meta']['total'] );
112
		$this->assertArrayNotHasKey( 'errors', $result );
113
	}
114
115
116
	public function testGetMShopException()
117
	{
118
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Cms\Standard::class )
119
			->setConstructorArgs( [$this->context, 'cms'] )
120
			->setMethods( ['getItems'] )
121
			->getMock();
122
123
		$object->expects( $this->once() )->method( 'getItems' )
124
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
125
126
		$object->setView( $this->view );
127
128
		$response = $object->get( $this->view->request(), $this->view->response() );
129
		$result = json_decode( (string) $response->getBody(), true );
130
131
132
		$this->assertEquals( 404, $response->getStatusCode() );
133
		$this->assertArrayHasKey( 'errors', $result );
134
	}
135
136
137
	public function testGetException()
138
	{
139
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Cms\Standard::class )
140
			->setConstructorArgs( [$this->context, 'cms'] )
141
			->setMethods( ['getItems'] )
142
			->getMock();
143
144
		$object->expects( $this->once() )->method( 'getItems' )
145
			->will( $this->throwException( new \Exception() ) );
146
147
		$object->setView( $this->view );
148
149
		$response = $object->get( $this->view->request(), $this->view->response() );
150
		$result = json_decode( (string) $response->getBody(), true );
151
152
153
		$this->assertEquals( 500, $response->getStatusCode() );
154
		$this->assertArrayHasKey( 'errors', $result );
155
	}
156
157
158
	public function testOptions()
159
	{
160
		$response = $this->object->options( $this->view->request(), $this->view->response() );
161
		$result = json_decode( (string) $response->getBody(), true );
162
163
		$this->assertEquals( 200, $response->getStatusCode() );
164
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
165
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
166
167
		$this->assertEquals( null, $result['meta']['prefix'] );
168
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
169
		$this->assertArrayNotHasKey( 'filter', $result['meta'] );
170
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
171
		$this->assertArrayNotHasKey( 'errors', $result );
172
	}
173
}
174