Completed
Push — master ( 1098fc...255b7f )
by Aimeos
02:28
created

StandardTest::testGetTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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