Completed
Push — master ( b53863...ac109f )
by Aimeos
07:38
created

StandardTest::testGetItem()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
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 View Code Duplication
	protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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->setSortations( [$search->sort( '+', 'locale.position' )] );
34
		$search->setSlice( 0, 1 );
35
		$localeItems = $localeManager->searchItems( $search );
36
37
		if( ( $localeItem = reset( $localeItems ) ) === false ) {
38
			throw new \Exception( 'No locale item found' );
39
		}
40
41
42
		$params = array(
43
			'id' => $localeItem->getId(),
44
			'fields' => array(
45
				'locale' => 'locale.id,locale.languageid,locale.currencyid'
46
			)
47
		);
48
49
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
50
		$this->view->addHelper( 'param', $helper );
51
52
		$response = $this->object->get( $this->view->request(), $this->view->response() );
53
		$result = json_decode( (string) $response->getBody(), true );
54
55
56
		$this->assertEquals( 200, $response->getStatusCode() );
57
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
58
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
59
60
		$this->assertEquals( 1, $result['meta']['total'] );
61
		$this->assertEquals( 'locale', $result['data']['type'] );
62
		$this->assertGreaterThan( 0, $result['data']['attributes']['locale.id'] );
63
		$this->assertEquals( 'de', $result['data']['attributes']['locale.languageid'] );
64
		$this->assertEquals( 'EUR', $result['data']['attributes']['locale.currencyid'] );
65
66
		$this->assertArrayNotHasKey( 'errors', $result );
67
	}
68
69
70
	public function testGetItems()
71
	{
72
		$params = array(
73
			'fields' => array(
74
				'locale' => 'locale.id,locale.languageid,locale.currencyid'
75
			),
76
			'sort' => 'locale.position',
77
		);
78
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
79
		$this->view->addHelper( 'param', $helper );
80
81
		$response = $this->object->get( $this->view->request(), $this->view->response() );
82
		$result = json_decode( (string) $response->getBody(), true );
83
84
85
		$this->assertEquals( 200, $response->getStatusCode() );
86
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
87
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
88
89
		$this->assertEquals( 1, $result['meta']['total'] );
90
		$this->assertEquals( 1, count( $result['data'] ) );
91
		$this->assertEquals( 'locale', $result['data'][0]['type'] );
92
		$this->assertEquals( 3, count( $result['data'][0]['attributes'] ) );
93
		$this->assertEquals( 'en', $result['data'][0]['attributes']['locale.languageid'] );
94
		$this->assertEquals( 'EUR', $result['data'][0]['attributes']['locale.currencyid'] );
95
96
		$this->assertArrayNotHasKey( 'errors', $result );
97
	}
98
99
100 View Code Duplication
	public function testGetItemsCriteria()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
	{
102
		$params = array(
103
			'filter' => array(
104
				'>=' => array( 'locale.position' => 0 ),
105
			),
106
			'sort' => '-locale.position',
107
		);
108
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $this->view, $params );
109
		$this->view->addHelper( 'param', $helper );
110
111
		$response = $this->object->get( $this->view->request(), $this->view->response() );
112
		$result = json_decode( (string) $response->getBody(), true );
113
114
115
		$this->assertEquals( 200, $response->getStatusCode() );
116
		$this->assertEquals( 1, $result['meta']['total'] );
117
		$this->assertArrayNotHasKey( 'errors', $result );
118
	}
119
120
121 View Code Duplication
	public function testGetMShopException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
	{
123
		$templatePaths = \TestHelperJapi::getTemplatePaths();
124
125
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Locale\Standard' )
126
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'locale'] )
127
			->setMethods( ['getItems'] )
128
			->getMock();
129
130
		$object->expects( $this->once() )->method( 'getItems' )
131
			->will( $this->throwException( new \Aimeos\MShop\Exception() ) );
132
133
134
		$response = $object->get( $this->view->request(), $this->view->response() );
135
		$result = json_decode( (string) $response->getBody(), true );
136
137
138
		$this->assertEquals( 404, $response->getStatusCode() );
139
		$this->assertArrayHasKey( 'errors', $result );
140
	}
141
142
143 View Code Duplication
	public function testGetException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
	{
145
		$templatePaths = \TestHelperJapi::getTemplatePaths();
146
147
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Locale\Standard' )
148
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, 'locale'] )
149
			->setMethods( ['getItems'] )
150
			->getMock();
151
152
		$object->expects( $this->once() )->method( 'getItems' )
153
			->will( $this->throwException( new \Exception() ) );
154
155
156
		$response = $object->get( $this->view->request(), $this->view->response() );
157
		$result = json_decode( (string) $response->getBody(), true );
158
159
160
		$this->assertEquals( 500, $response->getStatusCode() );
161
		$this->assertArrayHasKey( 'errors', $result );
162
	}
163
}