• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

voku / httpful / 5623107679

pending completion
5623107679

push

github

voku
[+]: fix test for the new release

1596 of 2486 relevant lines covered (64.2%)

81.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.57
/src/Httpful/Factory.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Httpful;
6

7
use Psr\Http\Message\RequestFactoryInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestFactoryInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Psr\Http\Message\StreamFactoryInterface;
14
use Psr\Http\Message\StreamInterface;
15
use Psr\Http\Message\UploadedFileFactoryInterface;
16
use Psr\Http\Message\UploadedFileInterface;
17
use Psr\Http\Message\UriFactoryInterface;
18
use Psr\Http\Message\UriInterface;
19

20
/**
21
 * Psr Factory
22
 */
23
class Factory implements RequestFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, ResponseFactoryInterface, UriFactoryInterface, UploadedFileFactoryInterface
24
{
25
    /**
26
     * @param string          $method
27
     * @param string          $uri
28
     * @param string|null     $mime
29
     * @param string|string[] $body
30
     *
31
     * @return Request
32
     */
33
    public function createRequest(string $method, $uri, string $mime = null, $body = ''): RequestInterface
34
    {
35
        $return = (new Request($method, $mime))
28✔
36
            ->withUriFromString($uri);
28✔
37

38
        if (is_array($body)) {
28✔
39
            $return = $return->withBodyFromArray($body);
×
40
        } else {
41
            $return = $return->withBodyFromString($body);
28✔
42
        }
43

44
        return $return;
28✔
45
    }
46

47
    /**
48
     * @param int         $code
49
     * @param string|null $reasonPhrase
50
     *
51
     * @return Response
52
     */
53
    public function createResponse(int $code = 200, string $reasonPhrase = null): ResponseInterface
54
    {
55
        return (new Response())->withStatus($code, $reasonPhrase);
4✔
56
    }
57

58
    /**
59
     * @param string      $method
60
     * @param string      $uri
61
     * @param array       $serverParams
62
     * @param string|null $mime
63
     * @param string      $body
64
     *
65
     * @return ServerRequest
66
     */
67
    public function createServerRequest(string $method, $uri, array $serverParams = [], $mime = null, string $body = ''): ServerRequestInterface
68
    {
69
        return (new ServerRequest($method, $mime, null, $serverParams))
×
70
            ->withUriFromString($uri)
×
71
            ->withBodyFromString($body);
×
72
    }
73

74
    /**
75
     * @param string $content
76
     *
77
     * @return StreamInterface
78
     */
79
    public function createStream(string $content = ''): StreamInterface
80
    {
81
        return Stream::createNotNull($content);
56✔
82
    }
83

84
    /**
85
     * @param string $filename
86
     * @param string $mode
87
     *
88
     * @return StreamInterface
89
     */
90
    public function createStreamFromFile(string $filename, string $mode = 'rb'): StreamInterface
91
    {
92
        /** @noinspection PhpUsageOfSilenceOperatorInspection */
93
        $resource = @\fopen($filename, $mode);
×
94
        if ($resource === false) {
×
95
            if ($mode === '' || \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true) === false) {
×
96
                throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
×
97
            }
98

99
            throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
×
100
        }
101

102
        return Stream::createNotNull($resource);
×
103
    }
104

105
    /**
106
     * @param resource|StreamInterface|string $resource
107
     *
108
     * @return StreamInterface
109
     */
110
    public function createStreamFromResource($resource): StreamInterface
111
    {
112
        return Stream::createNotNull($resource);
×
113
    }
114

115
    /**
116
     * @param StreamInterface $stream
117
     * @param int|null        $size
118
     * @param int             $error
119
     * @param string|null     $clientFilename
120
     * @param string|null     $clientMediaType
121
     *
122
     * @return UploadedFileInterface
123
     */
124
    public function createUploadedFile(
125
        StreamInterface $stream,
126
        int $size = null,
127
        int $error = \UPLOAD_ERR_OK,
128
        string $clientFilename = null,
129
        string $clientMediaType = null
130
    ): UploadedFileInterface {
131
        if ($size === null) {
×
132
            $size = (int) $stream->getSize();
×
133
        }
134

135
        return new UploadedFile(
×
136
            $stream,
×
137
            $size,
×
138
            $error,
×
139
            $clientFilename,
×
140
            $clientMediaType
×
141
        );
×
142
    }
143

144
    /**
145
     * @param string $uri
146
     *
147
     * @return UriInterface
148
     */
149
    public function createUri(string $uri = ''): UriInterface
150
    {
151
        return new Uri($uri);
4✔
152
    }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc