Completed
Push — master ( bc1208...c4da36 )
by Neomerx
05:13
created

JsonApiCallsTrait::postJsonApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
crap 1
1
<?php namespace Limoncello\Testing;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\StreamInterface;
21
use Zend\Diactoros\Stream;
22
23
/** @noinspection PhpTooManyParametersInspection
24
 * @package Limoncello\Testing
25
 *
26
 * @method ResponseInterface call(string $method, string $uri, array $queryParams = [], array $parsedBody = [], array $headers = [], array $cookies = [], array $files = [], array $server = [], string $messageBody = 'php://input')
27
 */
28
trait JsonApiCallsTrait
29
{
30
    /**
31
     * @param string $uri
32
     * @param string $json
33
     * @param array  $headers
34
     * @param array  $cookies
35
     *
36
     * @return ResponseInterface
37
     */
38 1
    protected function postJsonApi(string $uri, string $json, array $headers = [], array $cookies = [])
39
    {
40 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
41
42 1
        return $this->call('POST', $uri, [], [], $headers, $cookies, [], [], $this->streamFromString($json));
43
    }
44
45
    /**
46
     * @param string $uri
47
     * @param string $json
48
     * @param array  $headers
49
     * @param array  $cookies
50
     *
51
     * @return ResponseInterface
52
     */
53 1
    protected function putJsonApi(string $uri, string $json, array $headers = [], array $cookies = [])
54
    {
55 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
56
57 1
        return $this->call('PUT', $uri, [], [], $headers, $cookies, [], [], $this->streamFromString($json));
58
    }
59
60
    /**
61
     * @param string $uri
62
     * @param string $json
63
     * @param array  $headers
64
     * @param array  $cookies
65
     *
66
     * @return ResponseInterface
67
     */
68 1
    protected function patchJsonApi(string $uri, string $json, array $headers = [], array $cookies = [])
69
    {
70 1
        $headers['CONTENT_TYPE'] = 'application/vnd.api+json';
71
72 1
        return $this->call('PATCH', $uri, [], [], $headers, $cookies, [], [], $this->streamFromString($json));
73
    }
74
75
    /**
76
     * @param string $content
77
     *
78
     * @return StreamInterface
79
     */
80 4
    protected function streamFromString(string $content): StreamInterface
81
    {
82 4
        $stream = new Stream('php://temp', 'wb+');
83 4
        $stream->write($content);
84
85 4
        return $stream;
86
    }
87
}
88