Passed
Push — master ( 36a836...c7a44e )
by Aimeos
16:28 queued 07:56
created

StandardTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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), 2017-2022
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Stock;
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\Stock\Standard( $this->context, 'stock' );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Client\JsonApi\St...Standard::__construct() has too many arguments starting with 'stock'. ( Ignorable by Annotation )

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

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

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...
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 testGetItem()
39
	{
40
		$manager = \Aimeos\MShop::create( $this->context, 'stock' );
41
		$stockId = $manager->search( $manager->filter()->slice( 0, 1 ) )->getId()->first();
42
43
		$params = array(
44
			'id' => $stockId,
45
			'fields' => array(
46
				'stock' => 'stock.id,stock.productid,stock.stocklevel'
47
			),
48
			'sort' => 'stock.id'
49
		);
50
51
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
52
		$this->view->addHelper( 'param', $helper );
53
54
		$response = $this->object->get( $this->view->request(), $this->view->response() );
55
		$result = json_decode( (string) $response->getBody(), true );
56
57
		$this->assertEquals( 200, $response->getStatusCode() );
58
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
59
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
60
61
		$this->assertEquals( 1, $result['meta']['total'] );
62
		$this->assertEquals( 'stock', $result['data']['type'] );
63
		$this->assertEquals( 3, count( $result['data']['attributes'] ) );
64
		$this->assertNotEquals( '', $result['data']['attributes']['stock.productid'] );
65
66
		$this->assertArrayNotHasKey( 'errors', $result );
67
	}
68
69
70
	public function testGetItems()
71
	{
72
		$prodId = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' )->getId();
73
74
		$params = array(
75
			'filter' => ['s_prodid' => [$prodId]],
76
			'sort' => 'stock.productid,-stock.dateback',
77
		);
78
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
79
		$this->view->addHelper( 'param', $helper );
80
81
		$response = $this->object->get( $this->view->request(), $this->view->response() );
82
		$result = json_decode( (string) $response->getBody(), true );
83
84
		$this->assertEquals( 200, $response->getStatusCode() );
85
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
86
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
87
88
		$this->assertEquals( 1, $result['meta']['total'] );
89
		$this->assertEquals( 1, count( $result['data'] ) );
90
		$this->assertEquals( 'stock', $result['data'][0]['type'] );
91
92
		$this->assertArrayNotHasKey( 'errors', $result );
93
	}
94
95
96
	public function testGetMShopException()
97
	{
98
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Stock\Standard::class )
99
			->setConstructorArgs( [$this->context, 'stock'] )
100
			->setMethods( ['getItems'] )
101
			->getMock();
102
103
		$object->expects( $this->once() )->method( 'getItems' )
104
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
105
106
		$object->setView( $this->view );
107
108
		$response = $object->get( $this->view->request(), $this->view->response() );
109
		$result = json_decode( (string) $response->getBody(), true );
110
111
112
		$this->assertEquals( 404, $response->getStatusCode() );
113
		$this->assertArrayHasKey( 'errors', $result );
114
	}
115
116
117
	public function testGetException()
118
	{
119
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Stock\Standard::class )
120
			->setConstructorArgs( [$this->context, 'stock'] )
121
			->setMethods( ['getItems'] )
122
			->getMock();
123
124
		$object->expects( $this->once() )->method( 'getItems' )
125
			->will( $this->throwException( new \Exception() ) );
126
127
		$object->setView( $this->view );
128
129
		$response = $object->get( $this->view->request(), $this->view->response() );
130
		$result = json_decode( (string) $response->getBody(), true );
131
132
133
		$this->assertEquals( 500, $response->getStatusCode() );
134
		$this->assertArrayHasKey( 'errors', $result );
135
	}
136
137
138
	public function testOptions()
139
	{
140
		$response = $this->object->options( $this->view->request(), $this->view->response() );
141
		$result = json_decode( (string) $response->getBody(), true );
142
143
		$this->assertEquals( 200, $response->getStatusCode() );
144
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
145
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
146
147
		$this->assertEquals( null, $result['meta']['prefix'] );
148
		$this->assertEquals( 3, count( $result['meta']['filter'] ) );
149
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
150
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
151
		$this->assertArrayNotHasKey( 'errors', $result );
152
	}
153
}
154