Completed
Push — master ( aa2f0e...9144ee )
by Aimeos
03:14
created

StandardTest::testPatch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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