Completed
Push — master ( 9144ee...005523 )
by Aimeos
03:27
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 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
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\Locale\Site;
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\Locale\Site\Standard( $this->context, $this->view, $templatePaths, 'locale/site' );
26
	}
27
28
29
	public function testGetSearch()
30
	{
31
		$params = array(
32
			'filter' => array(
33
				'==' => array( 'locale.site.code' => 'unittest' )
34
			),
35
		);
36
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
37
		$this->view->addHelper( 'param', $helper );
38
39
		$header = array();
40
		$status = 500;
41
42
		$result = json_decode( $this->object->get( '', $header, $status ), true );
43
44
		$this->assertEquals( 200, $status );
45
		$this->assertEquals( 1, count( $header ) );
46
		$this->assertEquals( 1, $result['meta']['total'] );
47
		$this->assertEquals( 1, count( $result['data'] ) );
48
		$this->assertEquals( 'locale/site', $result['data'][0]['type'] );
49
		$this->assertEquals( 0, count( $result['included'] ) );
50
		$this->assertArrayNotHasKey( 'errors', $result );
51
	}
52
53
54
	public function testGetTree()
55
	{
56
		$params = array(
57
			'id' => $this->getSiteItem( 'unittest' )->getId(),
58
			'filter' => array(
59
				'==' => array( 'locale.status' => 0 )
60
			),
61
			'include' => 'locale/site'
62
		);
63
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
64
		$this->view->addHelper( 'param', $helper );
65
66
		$header = array();
67
		$status = 500;
68
69
		$result = json_decode( $this->object->get( '', $header, $status ), true );
70
71
		$this->assertEquals( 200, $status );
72
		$this->assertEquals( 1, count( $header ) );
73
		$this->assertEquals( 1, $result['meta']['total'] );
74
		$this->assertEquals( 'locale/site', $result['data']['type'] );
75
		$this->assertEquals( 0, count( $result['included'] ) );
76
		$this->assertArrayNotHasKey( 'errors', $result );
77
	}
78
79
80
	public function testPatch()
81
	{
82
		$stub = $this->getSiteMock( array( 'getItem', 'moveItem', 'saveItem' ) );
83
		$item = $stub->createItem();
84
85
		$stub->expects( $this->once() )->method( 'moveItem' );
86
		$stub->expects( $this->once() )->method( 'saveItem' );
87
		$stub->expects( $this->exactly( 2 ) )->method( 'getItem' ) // 2x due to decorator
88
			->will( $this->returnValue( $item ) );
89
90
91
		$params = array( 'id' => '-1' );
92
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
93
		$this->view->addHelper( 'param', $helper );
94
95
		$body = '{"data": {"parentid": "1", "targetid": 2, "type": "locale/site", "attributes": {"locale.site.label": "test"}}}';
96
		$header = array();
97
		$status = 500;
98
99
		$result = json_decode( $this->object->patch( $body, $header, $status ), true );
100
101
		$this->assertEquals( 200, $status );
102
		$this->assertEquals( 1, count( $header ) );
103
		$this->assertEquals( 1, $result['meta']['total'] );
104
		$this->assertArrayHasKey( 'data', $result );
105
		$this->assertEquals( 'locale/site', $result['data']['type'] );
106
		$this->assertArrayNotHasKey( 'included', $result );
107
		$this->assertArrayNotHasKey( 'errors', $result );
108
	}
109
110
111
	public function testPost()
112
	{
113
		$stub = $this->getSiteMock( array( 'getItem', 'insertItem' ) );
114
		$item = $stub->createItem();
115
116
		$stub->expects( $this->any() )->method( 'getItem' )
117
			->will( $this->returnValue( $item ) );
118
		$stub->expects( $this->once() )->method( 'insertItem' );
119
120
121
		$body = '{"data": {"type": "locale/site", "attributes": {"locale.site.code": "unittest", "locale.site.label": "Unit test"}}}';
122
		$header = array();
123
		$status = 500;
124
125
		$result = json_decode( $this->object->post( $body, $header, $status ), true );
126
127
		$this->assertEquals( 201, $status );
128
		$this->assertEquals( 1, count( $header ) );
129
		$this->assertEquals( 1, $result['meta']['total'] );
130
		$this->assertArrayHasKey( 'data', $result );
131
		$this->assertEquals( 'locale/site', $result['data']['type'] );
132
		$this->assertArrayNotHasKey( 'included', $result );
133
		$this->assertArrayNotHasKey( 'errors', $result );
134
	}
135
136
137
	protected function getSiteItem( $code )
138
	{
139
		$manager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $this->context )->getSubManager( 'site' );
140
		$search = $manager->createSearch();
141
		$search->setConditions( $search->compare( '==', 'locale.site.code', $code ) );
142
		$items = $manager->searchItems( $search );
143
144
		if( ( $item = reset( $items ) ) === false ) {
145
			throw new \Exception( sprintf( 'No locale site item with code "%1$s" found', $code ) );
146
		}
147
148
		return $item;
149
	}
150
151
152
	protected function getSiteMock( array $methods )
153
	{
154
		$name = 'ClientJsonAdmStandard';
155
		$this->context->getConfig()->set( 'mshop/locale/manager/name', $name );
156
157
		$stub = $this->getMockBuilder( '\\Aimeos\\MShop\\Locale\\Manager\\Standard' )
158
			->setConstructorArgs( array( $this->context ) )
159
			->setMethods( array( 'getSubManager' ) )
160
			->getMock();
161
162
		$siteStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Locale\\Manager\\Site\\Standard' )
163
			->setConstructorArgs( array( $this->context ) )
164
			->setMethods( $methods )
165
			->getMock();
166
167
		$stub->expects( $this->once() )->method( 'getSubManager' )
168
			->will( $this->returnValue( $siteStub ) );
169
170
		\Aimeos\MShop\Locale\Manager\Factory::injectManager( '\\Aimeos\\MShop\\Locale\\Manager\\' . $name, $stub );
171
172
		return $siteStub;
173
	}
174
}