Passed
Push — master ( 27b7fa...a364ac )
by Aimeos
06:09 queued 04:13
created

StandardTest::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
nc 1
nop 0
dl 0
loc 18
c 0
b 0
f 0
cc 1
rs 9.9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2022
6
 */
7
8
9
namespace Aimeos\Admin\JsonAdm\Index;
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\MShop::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Admin\JsonAdm\Index\Standard( $this->context, 'index' );
27
		$this->object->setAimeos( \TestHelper::getAimeos() );
28
		$this->object->setView( $this->view );
29
	}
30
31
32
	protected function tearDown() : void
33
	{
34
		\Aimeos\MShop::cache( false );
35
		unset( $this->object, $this->view, $this->context );
36
	}
37
38
39
	public function testDelete()
40
	{
41
		$this->getIndexMock( ['remove'] )->expects( $this->once() )->method( 'remove' );
42
		$id = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' )->getId();
43
44
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, ['id' => $id] );
45
		$this->view->addHelper( 'param', $helper );
46
47
		$response = $this->object->delete( $this->view->request(), $this->view->response() );
48
		$result = json_decode( (string) $response->getBody(), true );
49
50
51
		$this->assertEquals( 200, $response->getStatusCode() );
52
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
53
54
		$this->assertEquals( 1, $result['meta']['total'] );
55
		$this->assertArrayNotHasKey( 'included', $result );
56
		$this->assertArrayNotHasKey( 'errors', $result );
57
	}
58
59
60
	public function testDeleteMultiple()
61
	{
62
		$this->getIndexMock( ['remove'] )->expects( $this->once() )->method( 'remove' );
63
		$id = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' )->getId();
64
65
		$body = '{"data": ["' . $id . '"]}';
66
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
67
68
		$response = $this->object->delete( $request, $this->view->response() );
69
		$result = json_decode( (string) $response->getBody(), true );
70
71
72
		$this->assertEquals( 200, $response->getStatusCode() );
73
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
74
75
		$this->assertEquals( 1, $result['meta']['total'] );
76
		$this->assertArrayNotHasKey( 'included', $result );
77
		$this->assertArrayNotHasKey( 'errors', $result );
78
	}
79
80
81
	public function testPost()
82
	{
83
		$this->getIndexMock( ['rebuild'] )->expects( $this->once() )->method( 'rebuild' );
84
		$id = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' )->getId();
85
86
		$body = '{"data": "' . $id . '"}';
87
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
88
89
		$response = $this->object->post( $request, $this->view->response() );
90
		$result = json_decode( (string) $response->getBody(), true );
91
92
93
		$this->assertEquals( 201, $response->getStatusCode() );
94
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
95
96
		$this->assertEquals( 1, $result['meta']['total'] );
97
		$this->assertArrayNotHasKey( 'included', $result );
98
		$this->assertArrayNotHasKey( 'errors', $result );
99
	}
100
101
102
	public function testPostMultiple()
103
	{
104
		$this->getIndexMock( ['rebuild'] )->expects( $this->once() )->method( 'rebuild' );
105
		$id = \Aimeos\MShop::create( $this->context, 'product' )->find( 'CNC' )->getId();
106
107
		$body = '{"data": ["' . $id . '"]}';
108
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
109
110
		$response = $this->object->post( $request, $this->view->response() );
111
		$result = json_decode( (string) $response->getBody(), true );
112
113
114
		$this->assertEquals( 201, $response->getStatusCode() );
115
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
116
117
		$this->assertEquals( 1, $result['meta']['total'] );
118
		$this->assertArrayNotHasKey( 'included', $result );
119
		$this->assertArrayNotHasKey( 'errors', $result );
120
	}
121
122
123
	protected function getIndexMock( array $methods )
124
	{
125
		$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Index\\Manager\\Standard' )
126
			->setConstructorArgs( array( $this->context ) )
127
			->setMethods( $methods )
128
			->getMock();
129
130
		\Aimeos\MShop::inject( '\\Aimeos\\MShop\\Index\\Manager\\Standard', $stub );
131
132
		return $stub;
133
	}
134
}
135