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

ConnectProcessorMock   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
wmc 1
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\RemoteRequest\Connection;
8
use kalanis\RemoteRequest\Interfaces;
9
use kalanis\RemoteRequest\Pointers;
10
use kalanis\RemoteRequest\Protocols;
11
use kalanis\RemoteRequest\RequestException;
12
use kalanis\RemoteRequest\Sockets;
13
use kalanis\RemoteRequest\Translations;
14
15
16
class ConnectProcessorMock extends Connection\Processor
17
{
18
    public function __construct(Interfaces\IRRTranslations $lang, Sockets\ASocket $method = null)
19
    {
20
        parent::__construct($lang, $method);
21
        $this->processor = new PointerProcessorMock($lang);
22
    }
23
}
24
25
26
class PointerProcessorMock extends Pointers\Processor
27
{
28
    public function processPointer($filePointer, Interfaces\IConnectionParams $schema): parent
29
    {
30
        $this->checkQuery();
31
        $this->checkPointer($filePointer);
32
        $this->writeRequest($filePointer, $schema);
33
        rewind($filePointer); // FOR REASON
34
        $this->readResponse($filePointer);
35
        return $this;
36
    }
37
}
38
39
40
class ConnectionTest extends CommonTestClass
41
{
42
    /**
43
     * When it runs
44
     * The response is in query init
45
     * @throws RequestException
46
     */
47
    public function testSetsSimple(): void
48
    {
49
        $this->assertEquals('', $this->queryOnMock(''));
50
        $this->assertEquals('abcdefghijkl', $this->queryOnMock('abcdefghijkl'));
51
        $this->assertEquals('Hello.', $this->queryOnMock('Hello.'));
52
    }
53
54
    /**
55
     * @throws RequestException
56
     */
57
    public function testSetsNoSocket(): void
58
    {
59
        $this->expectException(RequestException::class);
60
        $this->queryOnMock(null);
61
    }
62
63
    public function testSetsLongData(): void
64
    {
65
        $content = str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', 200);
66
        $this->assertEquals($content, $this->queryOnMock($content));
67
    }
68
69
    /**
70
     * @throws RequestException
71
     */
72
    public function testSetsLongDataCut(): void
73
    {
74
        $lang = new Translations();
75
        $content = str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', 100);
76
        $query = new Protocols\Dummy\Query();
77
        $query->body = $content;
78
        $query->maxLength = 2000;
79
        $params = new Connection\Params\Php();
80
        $params->setTarget(Connection\Params\Php::HOST_MEMORY);
81
        $processor = new ConnectProcessorMock($lang, new Sockets\Socket($lang));
82
        $processor->setConnectionParams($params);
83
        $processor->setData($query);
84
        $processor = new ConnectProcessorMock($lang, new Sockets\SharedInternal($lang));
85
        $processor->setConnectionParams($params);
86
        $processor->setData($query);
87
        $processor->process();
88
        $this->assertEquals(substr($content, 0, 2000), stream_get_contents($processor->getResponse()));
89
    }
90
91
    /**
92
     * @throws RequestException
93
     */
94
    public function testRepeatConnectionUse(): void
95
    {
96
        $lang = new Translations();
97
        $content1 = str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', 10);
98
        $content2 = str_repeat('ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210zyxwvutsrqponmlkjihgfedcba', 10);
99
        $params = new Connection\Params\Php();
100
        $params->setTarget(Connection\Params\Php::HOST_MEMORY);
101
        $processor = new ConnectProcessorMock($lang, new Sockets\SharedInternal($lang));
102
        $processor->setConnectionParams($params);
103
        $query1 = new Protocols\Dummy\Query();
104
        $query1->body = $content1;
105
        $processor->setData($query1);
106
        $processor->process();
107
        $this->assertEquals($content1, stream_get_contents($processor->getResponse()));
108
109
        $query2 = new Protocols\Dummy\Query();
110
        $query2->body = $content2;
111
        $processor->setData($query2);
112
        $processor->process();
113
        $this->assertEquals($content2, stream_get_contents($processor->getResponse()));
114
    }
115
116
    /**
117
     * @param string|null $message what to send to remote machine
118
     * @throws RequestException
119
     * @return string
120
     */
121
    protected function queryOnMock(?string $message): string
122
    {
123
        $lang = new Translations();
124
        $params = new Connection\Params\Php();
125
        $params->setTarget(Connection\Params\Php::HOST_MEMORY);
126
        $processor = new ConnectProcessorMock($lang, new Sockets\SharedInternal($lang));
127
        $processor->setConnectionParams($params);
128
        if (!is_null($message)) {
129
            $query = new Protocols\Dummy\Query();
130
            $query->body = $message;
131
            $processor->setData($query);
132
        }
133
        $processor->process();
134
        $response = $processor->getResponse();
135
        return $response ? stream_get_contents($response) : '' ;
136
    }
137
}
138