Completed
Push — master ( 2b6933...74f0c0 )
by Aimeos
02:02
created

StandardTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 */
7
8
9
namespace Aimeos\Admin\JsonAdm\Catalog;
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()
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelperJadm::getContext();
24
		$this->view = $this->context->getView();
25
26
		$this->object = new \Aimeos\Admin\JsonAdm\Catalog\Standard( $this->context, 'catalog' );
27
		$this->object->setAimeos( \TestHelperJadm::getAimeos() );
28
		$this->object->setView( $this->view );
29
	}
30
31
32
	protected function tearDown()
33
	{
34
		\Aimeos\MShop::cache( false );
35
	}
36
37
38
	public function testGetSearch()
39
	{
40
		$params = array(
41
			'filter' => array(
42
				'==' => array( 'catalog.code' => 'cafe' )
43
			),
44
			'include' => 'text'
45
		);
46
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
47
		$this->view->addHelper( 'param', $helper );
48
49
		$response = $this->object->get( $this->view->request(), $this->view->response() );
50
		$result = json_decode( (string) $response->getBody(), true );
51
52
53
		$this->assertEquals( 200, $response->getStatusCode() );
54
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
55
56
		$this->assertEquals( 1, $result['meta']['total'] );
57
		$this->assertEquals( 1, count( $result['data'] ) );
58
		$this->assertEquals( 'catalog', $result['data'][0]['type'] );
59
		$this->assertEquals( 6, count( $result['data'][0]['relationships']['text'] ) );
60
		$this->assertEquals( 6, count( $result['included'] ) );
61
62
		$this->assertArrayNotHasKey( 'errors', $result );
63
	}
64
65
66
	public function testGetTree()
67
	{
68
		$params = array(
69
			'id' => $this->getCatalogItem( 'root' )->getId(),
70
			'include' => 'catalog,text'
71
		);
72
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
73
		$this->view->addHelper( 'param', $helper );
74
75
		$response = $this->object->get( $this->view->request(), $this->view->response() );
76
		$result = json_decode( (string) $response->getBody(), true );
77
78
		$this->assertEquals( 200, $response->getStatusCode() );
79
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
80
81
		$this->assertEquals( 1, $result['meta']['total'] );
82
		$this->assertEquals( 'catalog', $result['data']['type'] );
83
		$this->assertEquals( 2, count( $result['data']['relationships']['catalog'] ) );
84
		$this->assertEquals( 2, count( $result['included'] ) );
85
86
		$this->assertArrayNotHasKey( 'errors', $result );
87
	}
88
89
90
	public function testPatch()
91
	{
92
		$stub = $this->getCatalogMock( array( 'getItem', 'moveItem', 'saveItem' ) );
93
94
		$stub->expects( $this->once() )->method( 'moveItem' );
95
		$stub->expects( $this->once() )->method( 'saveItem' )
96
			->will( $this->returnValue( $stub->createItem() ) );
97
		$stub->expects( $this->exactly( 2 ) )->method( 'getItem' ) // 2x due to decorator
98
			->will( $this->returnValue( $stub->createItem() ) );
99
100
101
		$params = array( 'id' => '-1' );
102
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
103
		$this->view->addHelper( 'param', $helper );
104
105
		$body = '{"data": {"parentid": "1", "targetid": 2, "type": "catalog", "attributes": {"catalog.label": "test"}}}';
106
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
107
108
		$response = $this->object->patch( $request, $this->view->response() );
109
		$result = json_decode( (string) $response->getBody(), true );
110
111
112
		$this->assertEquals( 200, $response->getStatusCode() );
113
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
114
115
		$this->assertEquals( 1, $result['meta']['total'] );
116
		$this->assertArrayHasKey( 'data', $result );
117
		$this->assertEquals( 'catalog', $result['data']['type'] );
118
119
		$this->assertArrayNotHasKey( 'included', $result );
120
		$this->assertArrayNotHasKey( 'errors', $result );
121
	}
122
123
124
	public function testPost()
125
	{
126
		$stub = $this->getCatalogMock( array( 'getItem', 'insertItem' ) );
127
128
		$stub->expects( $this->any() )->method( 'getItem' )
129
			->will( $this->returnValue( $stub->createItem() ) );
130
		$stub->expects( $this->once() )->method( 'insertItem' );
131
132
133
		$body = '{"data": {"type": "catalog", "attributes": {"catalog.code": "test", "catalog.label": "Test catalog"}}}';
134
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
135
136
		$response = $this->object->post( $request, $this->view->response() );
137
		$result = json_decode( (string) $response->getBody(), true );
138
139
140
		$this->assertEquals( 201, $response->getStatusCode() );
141
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
142
143
		$this->assertEquals( 1, $result['meta']['total'] );
144
		$this->assertArrayHasKey( 'data', $result );
145
		$this->assertEquals( 'catalog', $result['data']['type'] );
146
147
		$this->assertArrayNotHasKey( 'included', $result );
148
		$this->assertArrayNotHasKey( 'errors', $result );
149
	}
150
151
152
	protected function getCatalogItem( $code )
153
	{
154
		$manager = \Aimeos\MShop::create( $this->context, 'catalog' );
155
		$search = $manager->createSearch();
156
		$search->setConditions( $search->compare( '==', 'catalog.code', $code ) );
157
		$items = $manager->searchItems( $search );
158
159
		if( ( $item = reset( $items ) ) === false ) {
160
			throw new \RuntimeException( sprintf( 'No catalog item with code "%1$s" found', $code ) );
161
		}
162
163
		return $item;
164
	}
165
166
167
	protected function getCatalogMock( array $methods )
168
	{
169
		$name = 'ClientJsonAdmStandard';
170
		$this->context->getConfig()->set( 'mshop/catalog/manager/name', $name );
171
172
		$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Catalog\\Manager\\Standard' )
173
			->setConstructorArgs( array( $this->context ) )
174
			->setMethods( $methods )
175
			->getMock();
176
177
		\Aimeos\MShop\Product\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Catalog\\Manager\\' . $name, $stub );
178
179
		return $stub;
180
	}
181
}