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

QueryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

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