StandardTest::testPost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
nc 1
nop 0
dl 0
loc 27
c 0
b 0
f 0
cc 1
rs 9.7
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
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() : void
20
	{
21
		\Aimeos\MShop::cache( true );
22
23
		$this->context = \TestHelper::context();
24
		$this->view = $this->context->view();
25
26
		$this->object = new \Aimeos\Admin\JsonAdm\Locale\Site\Standard( $this->context, 'locale/site' );
27
		$this->object->setAimeos( \TestHelper::getAimeos() );
28
		$this->object->setView( $this->view );
29
	}
30
31
32
	protected function tearDown() : void
33
	{
34
		\Aimeos\MShop::cache( false );
35
		unset( $this->object, $this->view, $this->context );
36
	}
37
38
39
	public function testGet()
40
	{
41
		$params = array(
42
			'id' => $this->getSiteItem( 'unittest' )->getId(),
43
			'filter' => array(
44
				'==' => array( 'locale.site.status' => 1 )
45
			),
46
		);
47
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
48
		$this->view->addHelper( 'param', $helper );
49
50
		$response = $this->object->get( $this->view->request(), $this->view->response() );
51
		$result = json_decode( (string) $response->getBody(), true );
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( 'locale/site', $result['data']['type'] );
58
59
		$this->assertArrayNotHasKey( 'errors', $result );
60
	}
61
62
63
	public function testGetSearch()
64
	{
65
		$params = array(
66
			'filter' => array(
67
				'==' => array( 'locale.site.code' => 'unittest' )
68
			),
69
		);
70
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
71
		$this->view->addHelper( 'param', $helper );
72
73
		$response = $this->object->get( $this->view->request(), $this->view->response() );
74
		$result = json_decode( (string) $response->getBody(), true );
75
76
77
		$this->assertEquals( 200, $response->getStatusCode() );
78
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
79
80
		$this->assertEquals( 1, $result['meta']['total'] );
81
		$this->assertEquals( 1, count( $result['data'] ) );
82
		$this->assertEquals( 'locale/site', $result['data'][0]['type'] );
83
84
		$this->assertArrayNotHasKey( 'errors', $result );
85
	}
86
87
88
	public function testPatch()
89
	{
90
		$stub = $this->getSiteMock( array( 'get', 'move', 'save' ) );
91
		$item = $stub->create()->setId( '-1' );
92
93
		$stub->expects( $this->once() )->method( 'save' )
94
			->willReturn( $item );
95
		$stub->expects( $this->exactly( 2 ) )->method( 'get' ) // 2x due to decorator
96
			->willReturn( $item );
97
98
99
		$params = array( 'id' => '-1' );
100
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $this->view, $params );
101
		$this->view->addHelper( 'param', $helper );
102
103
		$body = '{"data": {"parentid": "1", "targetid": 2, "type": "locale/site", "attributes": {"locale.site.label": "test"}}}';
104
		$request = $this->view->request()->withBody( $this->view->response()->createStreamFromString( $body ) );
105
106
		$response = $this->object->patch( $request, $this->view->response() );
107
		$result = json_decode( (string) $response->getBody(), true );
108
109
110
		$this->assertEquals( 200, $response->getStatusCode() );
111
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
112
113
		$this->assertEquals( 1, $result['meta']['total'] );
114
		$this->assertArrayHasKey( 'data', $result );
115
		$this->assertEquals( 'locale/site', $result['data']['type'] );
116
117
		$this->assertArrayNotHasKey( 'included', $result );
118
		$this->assertArrayNotHasKey( 'errors', $result );
119
	}
120
121
122
	public function testPost()
123
	{
124
		$stub = $this->getSiteMock( array( 'get', 'insert' ) );
125
		$item = $stub->create();
126
127
		$stub->expects( $this->any() )->method( 'get' )
128
			->willReturn( $item );
129
		$stub->expects( $this->once() )->method( 'insert' )
130
			->willReturn( $item->setId( '-1' ) );
131
132
133
		$body = '{"data": {"type": "locale/site", "attributes": {"locale.site.code": "unittest", "locale.site.label": "Unit test"}}}';
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( 'locale/site', $result['data']['type'] );
146
147
		$this->assertArrayNotHasKey( 'included', $result );
148
		$this->assertArrayNotHasKey( 'errors', $result );
149
	}
150
151
152
	protected function getSiteItem( $code )
153
	{
154
		$manager = \Aimeos\MShop::create( $this->context, 'locale/site' );
155
		$search = $manager->filter();
156
		$search->setConditions( $search->compare( '==', 'locale.site.code', $code ) );
157
158
		if( ( $item = $manager->search( $search )->first() ) === null ) {
159
			throw new \RuntimeException( sprintf( 'No locale site item with code "%1$s" found', $code ) );
160
		}
161
162
		return $item;
163
	}
164
165
166
	protected function getSiteMock( array $methods )
167
	{
168
		$this->context->config()->set( 'mshop/locale/manager/site/name', 'Standard' );
169
170
		$siteStub = $this->getMockBuilder( '\\Aimeos\\MShop\\Locale\\Manager\\Site\\Standard' )
171
			->setConstructorArgs( array( $this->context ) )
172
			->onlyMethods( $methods )
173
			->getMock();
174
175
		\Aimeos\MShop::inject( '\\Aimeos\\MShop\\Locale\\Manager\\Site\\Standard', $siteStub );
176
177
		return $siteStub;
178
	}
179
}
180