Completed
Push — master ( ba7de8...b94793 )
by Aimeos
02:24
created

StandardTest::testOptionsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 12
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;
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\Standard( $this->context, $this->view, $templatePaths, '' );
26
	}
27
28
29
	public function testGet()
30
	{
31
		$response = $this->object->get( $this->view->request(), $this->view->response() );
32
		$result = json_decode( (string) $response->getBody(), true );
33
34
		$this->assertEquals( 200, $response->getStatusCode() );
35
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
36
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
37
38
		$this->assertArrayHasKey( 'title', $result['errors'] );
39
	}
40
41
42
	public function testOptions()
43
	{
44
		$response = $this->object->options( $this->view->request(), $this->view->response() );
45
		$result = json_decode( (string) $response->getBody(), true );
46
47
		$this->assertEquals( 200, $response->getStatusCode() );
48
		$this->assertEquals( 1, count( $response->getHeader( 'Allow' ) ) );
49
		$this->assertEquals( 1, count( $response->getHeader( 'Content-Type' ) ) );
50
51
		$this->assertEquals( null, $result['meta']['prefix'] );
52
		$this->assertGreaterThan( 8, count( $result['meta']['resources'] ) );
53
		$this->assertArrayNotHasKey( 'errors', $result );
54
	}
55
56
57
	public function testOptionsException()
58
	{
59
		$templatePaths = \TestHelperJapi::getTemplatePaths();
60
61
		$object = $this->getMockBuilder( '\Aimeos\Client\JsonApi\Standard' )
62
			->setConstructorArgs( [$this->context, $this->view, $templatePaths, ''] )
63
			->setMethods( ['getContext'] )
64
			->getMock();
65
66
		$object->expects( $this->once() )->method( 'getContext' )
67
			->will( $this->throwException( new \Exception() ) );
68
69
70
		$response = $object->options( $this->view->request(), $this->view->response() );
71
		$result = json_decode( (string) $response->getBody(), true );
72
73
74
		$this->assertEquals( 500, $response->getStatusCode() );
75
		$this->assertArrayHasKey( 'errors', $result );
76
	}
77
}