Completed
Push — master ( 3c8021...ec484d )
by Aimeos
11:06 queued 07:58
created

StandardTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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