Completed
Push — master ( a7f437...7b274b )
by Aimeos
33:37
created

StandardTest::testGetItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 */
7
8
9
namespace Aimeos\Client\JsonApi\Locale;
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 = \TestHelperJapi::getContext();
22
		$templatePaths = \TestHelperJapi::getTemplatePaths();
23
		$this->view = $this->context->getView();
24
25
		$this->object = new \Aimeos\Client\JsonApi\Locale\Standard( $this->context, $this->view, $templatePaths, 'locale' );
26
	}
27
28
29
	public function testGetItem()
30
	{
31
		$localeManager = \Aimeos\MShop\Factory::createManager( $this->context, 'locale' );
32
		$search = $localeManager->createSearch();
33
		$search->setConditions( $search->compare( '==', 'locale.status', 1 ) );
34
		$search->setSortations( [$search->sort( '+', 'locale.position' )] );
35
		$search->setSlice( 0, 1 );
36
		$localeItems = $localeManager->searchItems( $search );
37
38
		if( ( $localeItem = reset( $localeItems ) ) === false ) {
39
			throw new \Exception( 'No locale item found' );
40
		}
41
42
43
		$params = array(
44
			'id' => $localeItem->getId(),
45
			'fields' => array(
46
				'locale' => 'locale.id,locale.languageid,locale.currencyid'
47
			)
48
		);
49
50
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
51
		$this->view->addHelper( 'param', $helper );
52
53
		$response = $this->object->get( $this->view->request(), $this->view->response() );
54
		$result = json_decode( (string) $response->getBody(), true );
55
56
57
		$this->assertEquals( 200, $response->getStatusCode() );
58
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
59
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
60
61
		$this->assertEquals( 1, $result['meta']['total'] );
62
		$this->assertEquals( 'locale', $result['data']['type'] );
63
		$this->assertGreaterThan( 0, $result['data']['attributes']['locale.id'] );
64
		$this->assertEquals( 'en', $result['data']['attributes']['locale.languageid'] );
65
		$this->assertEquals( 'EUR', $result['data']['attributes']['locale.currencyid'] );
66
67
		$this->assertArrayNotHasKey( 'errors', $result );
68
	}
69
70
71
	public function testGetItems()
72
	{
73
		$params = array( 'sort' => 'locale.position' );
74
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
75
		$this->view->addHelper( 'param', $helper );
76
77
		$response = $this->object->get( $this->view->request(), $this->view->response() );
78
		$result = json_decode( (string) $response->getBody(), true );
79
80
81
		$this->assertEquals( 200, $response->getStatusCode() );
82
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
83
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
84
85
		$this->assertEquals( 1, $result['meta']['total'] );
86
		$this->assertEquals( 1, count( $result['data'] ) );
87
		$this->assertEquals( 'locale', $result['data'][0]['type'] );
88
		$this->assertEquals( 9, count( $result['data'][0]['attributes'] ) );
89
		$this->assertEquals( 'en', $result['data'][0]['attributes']['locale.languageid'] );
90
		$this->assertEquals( 'EUR', $result['data'][0]['attributes']['locale.currencyid'] );
91
92
		$this->assertArrayNotHasKey( 'errors', $result );
93
	}
94
95
96
	public function testGetItemsCriteria()
97
	{
98
		$params = array(
99
			'filter' => array(
100
				'>=' => array( 'locale.position' => 0 ),
101
			),
102
			'sort' => '-locale.position',
103
		);
104
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
105
		$this->view->addHelper( 'param', $helper );
106
107
		$response = $this->object->get( $this->view->request(), $this->view->response() );
108
		$result = json_decode( (string) $response->getBody(), true );
109
110
111
		$this->assertEquals( 200, $response->getStatusCode() );
112
		$this->assertEquals( 1, $result['meta']['total'] );
113
		$this->assertArrayNotHasKey( 'errors', $result );
114
	}
115
116
117
	public function testGetMShopException()
118
	{
119
		$templatePaths = \TestHelperJapi::getTemplatePaths();
120
121
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Locale\Standard' )
122
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'locale'] )
123
			->setMethods( ['getItems'] )
124
			->getMock();
125
126
		$object->expects( $this->once() )->method( 'getItems' )
127
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
128
129
130
		$response = $object->get( $this->view->request(), $this->view->response() );
131
		$result = json_decode( (string) $response->getBody(), true );
132
133
134
		$this->assertEquals( 404, $response->getStatusCode() );
135
		$this->assertArrayHasKey( 'errors', $result );
136
	}
137
138
139
	public function testGetException()
140
	{
141
		$templatePaths = \TestHelperJapi::getTemplatePaths();
142
143
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Locale\Standard' )
144
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'locale'] )
145
			->setMethods( ['getItems'] )
146
			->getMock();
147
148
		$object->expects( $this->once() )->method( 'getItems' )
149
			->will( $this->throwException( new \Exception() ) );
150
151
152
		$response = $object->get( $this->view->request(), $this->view->response() );
153
		$result = json_decode( (string) $response->getBody(), true );
154
155
156
		$this->assertEquals( 500, $response->getStatusCode() );
157
		$this->assertArrayHasKey( 'errors', $result );
158
	}
159
}