StandardTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 80
c 1
b 0
f 0
dl 0
loc 143
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A tearDown() 0 4 1
A testGetMShopException() 0 18 1
A testGetException() 0 18 1
A testGetItem() 0 32 1
A testOptions() 0 14 1
A testGetItems() 0 23 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2025
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 );
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
			'include' => 'stock.type'
50
		);
51
52
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
53
		$this->view->addHelper( 'param', $helper );
54
55
		$response = $this->object->get( $this->view->request(), $this->view->response() );
56
		$result = json_decode( (string) $response->getBody(), true );
57
58
		$this->assertEquals( 200, $response->getStatusCode() );
59
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
60
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
61
62
		$this->assertEquals( 1, $result['meta']['total'] );
63
		$this->assertEquals( 'stock', $result['data']['type'] );
64
		$this->assertEquals( 3, count( $result['data']['attributes'] ) );
65
		$this->assertNotEquals( '', $result['data']['attributes']['stock.productid'] );
66
		$this->assertEquals( 1, count( $result['data']['relationships']['stock.type']['data'] ) );
67
		$this->assertEquals( 1, count( $result['included'] ) );
68
69
		$this->assertArrayNotHasKey( 'errors', $result );
70
	}
71
72
73
	public function testGetItems()
74
	{
75
		$prodId = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' )->getId();
76
77
		$params = array(
78
			'filter' => ['s_prodid' => [$prodId]],
79
			'sort' => 'stock.productid,-stock.dateback',
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( 'stock', $result['data'][0]['type'] );
94
95
		$this->assertArrayNotHasKey( 'errors', $result );
96
	}
97
98
99
	public function testGetMShopException()
100
	{
101
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Stock\Standard::class )
102
			->setConstructorArgs( [$this->context, 'stock'] )
103
			->onlyMethods( ['getItems'] )
104
			->getMock();
105
106
		$object->expects( $this->once() )->method( 'getItems' )
107
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
108
109
		$object->setView( $this->view );
110
111
		$response = $object->get( $this->view->request(), $this->view->response() );
112
		$result = json_decode( (string) $response->getBody(), true );
113
114
115
		$this->assertEquals( 404, $response->getStatusCode() );
116
		$this->assertArrayHasKey( 'errors', $result );
117
	}
118
119
120
	public function testGetException()
121
	{
122
		$object = $this->getMockBuilder( \Aimeos\Client\JsonApi\Stock\Standard::class )
123
			->setConstructorArgs( [$this->context, 'stock'] )
124
			->onlyMethods( ['getItems'] )
125
			->getMock();
126
127
		$object->expects( $this->once() )->method( 'getItems' )
128
			->will( $this->throwException( new \Exception() ) );
129
130
		$object->setView( $this->view );
131
132
		$response = $object->get( $this->view->request(), $this->view->response() );
133
		$result = json_decode( (string) $response->getBody(), true );
134
135
136
		$this->assertEquals( 500, $response->getStatusCode() );
137
		$this->assertArrayHasKey( 'errors', $result );
138
	}
139
140
141
	public function testOptions()
142
	{
143
		$response = $this->object->options( $this->view->request(), $this->view->response() );
144
		$result = json_decode( (string) $response->getBody(), true );
145
146
		$this->assertEquals( 200, $response->getStatusCode() );
147
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
148
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
149
150
		$this->assertEquals( null, $result['meta']['prefix'] );
151
		$this->assertEquals( 3, count( $result['meta']['filter'] ) );
152
		$this->assertArrayNotHasKey( 'attributes', $result['meta'] );
153
		$this->assertArrayNotHasKey( 'sort', $result['meta'] );
154
		$this->assertArrayNotHasKey( 'errors', $result );
155
	}
156
}
157