JsonapiControllerTest::testPostPatchDeleteAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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