Completed
Branch master (136162)
by Aimeos
06:57
created

JsonapiControllerTest::testGetAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 28
rs 9.6333
c 2
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
class JsonapiControllerTest extends AimeosTestAbstract
4
{
5
	public function setUp()
6
	{
7
		parent::setUp();
8
		View::addLocation(dirname(__DIR__).'/fixtures/views');
9
	}
10
11
12
	public function testOptionsAction()
13
	{
14
		$params = ['site' => 'unittest'];
15
		$response = $this->action('OPTIONS', '\Aimeos\Shop\Controller\JsonapiController@optionsAction', $params);
16
17
		$json = json_decode( $response->getContent(), true );
18
19
		$this->assertNotNull( $json );
20
		$this->assertEquals( 200, $response->getStatusCode() );
21
		$this->assertArrayHasKey( 'resources', $json['meta'] );
22
		$this->assertGreaterThan( 1, count( $json['meta']['resources'] ) );
23
	}
24
25
26
	public function testGetAction()
27
	{
28
		$params = ['site' => 'unittest', 'resource' => 'product'];
29
		$getParams = ['filter' => ['f_search' => 'Cafe Noire Cap']];
30
		$response = $this->action('GET', '\Aimeos\Shop\Controller\JsonapiController@getAction', $params, $getParams);
31
32
		$json = json_decode( $response->getContent(), true );
33
34
		$this->assertResponseOk();
35
		$this->assertNotNull( $json );
36
		$this->assertEquals( 2, $json['meta']['total'] );
37
		$this->assertEquals( 2, count( $json['data'] ) );
38
		$this->assertArrayHasKey( 'id', $json['data'][0] );
39
		$this->assertEquals( 'CNC', $json['data'][0]['attributes']['product.code'] );
40
41
		$id = $json['data'][0]['id'];
42
43
44
		$params = ['site' => 'unittest', 'resource' => 'product', 'id' => $id ];
45
		$response = $this->action('GET', '\Aimeos\Shop\Controller\JsonapiController@getAction', $params);
46
47
		$json = json_decode( $response->getContent(), true );
48
49
		$this->assertResponseOk();
50
		$this->assertNotNull( $json );
51
		$this->assertEquals( 1, $json['meta']['total'] );
52
		$this->assertArrayHasKey( 'id', $json['data'] );
53
		$this->assertEquals( 'CNC', $json['data']['attributes']['product.code'] );
54
	}
55
56
57
	public function testPostPatchDeleteAction()
58
	{
59
		// get CNC product
60
		$params = ['site' => 'unittest', 'resource' => 'product'];
61
		$getParams = ['filter' => ['f_search' => 'Cafe Noire Cap', 'f_listtype' => 'unittype19']];
62
		$response = $this->action('GET', '\Aimeos\Shop\Controller\JsonapiController@getAction', $params, $getParams);
63
64
		$json = json_decode( $response->getContent(), true );
65
		$this->assertEquals( 'CNC', $json['data'][0]['attributes']['product.code'] );
66
67
		// add CNC product to basket
68
		$params = ['site' => 'unittest', 'resource' => 'basket', 'id' => 'default', 'related' => 'product'];
69
		$content = json_encode( ['data' => ['attributes' => ['product.id' => $json['data'][0]['id']]]] );
70
		$response = $this->action('POST', '\Aimeos\Shop\Controller\JsonapiController@postAction', $params, [], [], [], [], $content);
71
72
		$json = json_decode( $response->getContent(), true );
73
		$this->assertEquals( 'CNC', $json['included'][0]['attributes']['order.base.product.prodcode'] );
74
75
		// change product quantity in basket
76
		$params = ['site' => 'unittest', 'resource' => 'basket', 'id' => 'default', 'related' => 'product', 'relatedid' => 0];
77
		$content = json_encode( ['data' => ['attributes' => ['quantity' => 2]]] );
78
		$response = $this->action('PATCH', '\Aimeos\Shop\Controller\JsonapiController@patchAction', $params, [], [], [], [], $content);
79
80
		$json = json_decode( $response->getContent(), true );
81
		$this->assertEquals( 2, $json['included'][0]['attributes']['order.base.product.quantity'] );
82
83
		// delete product from basket
84
		$params = ['site' => 'unittest', 'resource' => 'basket', 'id' => 'default', 'related' => 'product', 'relatedid' => 0];
85
		$response = $this->action('DELETE', '\Aimeos\Shop\Controller\JsonapiController@deleteAction', $params);
86
87
		$json = json_decode( $response->getContent(), true );
88
		$this->assertEquals( 0, count( $json['included'] ) );
89
	}
90
91
92
	public function testPutAction()
93
	{
94
		$params = ['site' => 'unittest', 'resource' => 'basket'];
95
		$response = $this->action('PUT', '\Aimeos\Shop\Controller\JsonapiController@putAction', $params);
96
97
		$json = json_decode( $response->getContent(), true );
98
		$this->assertArrayHasKey( 'errors', $json );
99
	}
100
}
101