Passed
Push — master ( edb799...f409da )
by Petr
07:56
created

QueryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 66
rs 10
c 1
b 0
f 0
wmc 5
1
<?php
2
3
namespace ProtocolsTests\WebDav;
4
5
6
use CommonTestClass;
7
use kalanis\RemoteRequest\Connection;
8
use kalanis\RemoteRequest\Protocols\Http;
9
use kalanis\RemoteRequest\Protocols\WebDAV;
10
11
12
class QueryMock extends WebDAV\Query
13
{
14
    /**
15
     * Overwrite because random string in testing does not work
16
     * @return string
17
     */
18
    protected function generateBoundary(): string
19
    {
20
        return '--PHPFSock--';
21
    }
22
}
23
24
25
class QueryTest extends CommonTestClass
26
{
27
    public function testSimple(): void
28
    {
29
        $lib = $this->prepareQuerySimple();
30
        $lib->setHost('somewhere.example');
31
        $lib->setPort(80);
32
        $lib->addValues(['foo' => 'bar', 'abc' => 'def',]);
33
34
        $this->assertEquals("GET /example HTTP/1.1\r\nHost: somewhere.example\r\nContent-Length: 15\r\n\r\n"
35
            . 'foo=bar&abc=def'
36
            , stream_get_contents($lib->getData(), -1, 0));
37
        $lib->setPort(444);
38
        $this->assertEquals("GET /example HTTP/1.1\r\nHost: somewhere.example:444\r\nContent-Length: 15\r\n\r\n"
39
            . 'foo=bar&abc=def'
40
            , stream_get_contents($lib->getData(), -1, 0));
41
        $lib->setPath('/example?baz=abc');
42
        $this->assertEquals("GET /example?baz=abc HTTP/1.1\r\nHost: somewhere.example:444\r\nContent-Length: 15\r\n\r\n"
43
            . 'foo=bar&abc=def'
44
            , stream_get_contents($lib->getData(), -1, 0));
45
    }
46
47
    public function testFiles(): void
48
    {
49
        $lib = $this->prepareQuerySimple();
50
        $lib->setRequestSettings($this->prepareProtocolSchema('somewhere.example', 512))
51
            ->addValues(['foo' => 'bar', 'up' => $this->prepareTestFile('mnbvcx')]);
52
        $this->assertEquals(
53
            "GET /example HTTP/1.1\r\nHost: somewhere.example:512\r\nContent-Length: 202\r\n\r\n"
54
            . "----PHPFSock--\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n"
55
            . "----PHPFSock--\r\nContent-Disposition: form-data; name=\"up\"; filename=\"dummy.txt\"\r\nContent-Type: text/plain\r\n\r\nmnbvcx\r\n"
56
            . "----PHPFSock----\r\n"
57
            , stream_get_contents($lib->getData(), -1, 0));
58
        $lib->setPath('/example?baz=abc');
59
        $this->assertEquals(
60
            "GET /example?baz=abc HTTP/1.1\r\nHost: somewhere.example:512\r\nContent-Length: 202\r\n\r\n"
61
            . "----PHPFSock--\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n"
62
            . "----PHPFSock--\r\nContent-Disposition: form-data; name=\"up\"; filename=\"dummy.txt\"\r\nContent-Type: text/plain\r\n\r\nmnbvcx\r\n"
63
            . "----PHPFSock----\r\n"
64
            , stream_get_contents($lib->getData(), -1, 0));
65
    }
66
67
    protected function prepareQuerySimple(): WebDAV\Query
68
    {
69
        $lib = new QueryMock();
70
        $lib->setMethod('get');
71
        $lib->setPath('/example');
72
        $lib->setInline(true);
73
        $lib->removeHeader('Accept');
74
        $lib->removeHeader('User-Agent');
75
        $lib->removeHeader('Connection');
76
        return $lib;
77
    }
78
79
    protected function prepareTestFile($content): Http\Query\File
80
    {
81
        $libValue = new Http\Query\File($content);
82
        $libValue->filename = 'dummy.txt';
83
        $libValue->mime = 'text/plain';
84
        return $libValue;
85
    }
86
87
    protected function prepareProtocolSchema(string $host = 'unable.example', int $port = 80): Connection\Params\Tcp
88
    {
89
        $request = new Connection\Params\Tcp();
90
        return $request->setTarget($host, $port);
91
    }
92
}
93