PEAR2_Net_RouterOS-1.0.0b4PEAR2_Net_RouterOS-1.0.0b4/tests/Util/SafeTest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

namespace PEAR2\Net\RouterOS\Util\Test;

use PEAR2\Net\RouterOS\Client;
use PEAR2\Net\RouterOS\Query;
use PEAR2\Net\RouterOS\Request;
use PEAR2\Net\RouterOS\Response;
use PEAR2\Net\RouterOS\Util;
use PHPUnit_Framework_TestCase;

abstract class SafeTest extends PHPUnit_Framework_TestCase
{
    const REGEX_ID = '\*[A-F0-9]+';
    const REGEX_IDLIST = '/^(\*[A-F0-9]+\,)*(\*[A-F0-9]+)?$/';

    /**
     * @var Util
     */
    protected $util;

    /**
     * @var Client
     */
    protected $client;

    public function testChangeMenu()
    {
        $this->assertSame('/', $this->util->changeMenu());
        $this->assertSame('/', $this->util->changeMenu('queue'));
        $this->assertSame('/queue', $this->util->changeMenu('simple'));
        $this->assertSame('/queue/simple', $this->util->changeMenu('.. tree'));
        $this->assertSame('/queue/tree', $this->util->changeMenu('../type'));
        $this->assertSame('/queue/type', $this->util->changeMenu('/interface'));
        $this->assertSame('/interface', $this->util->changeMenu('/ip/arp'));
        $this->assertSame('/ip/arp', $this->util->changeMenu('/ip hotspot'));
        $this->assertSame('/ip/hotspot', $this->util->changeMenu());
    }

    public function testFindByQuery()
    {
        $this->util->changeMenu('/queue/simple');
        $this->assertRegExp(
            '/^' . self::REGEX_ID . '$/',
            $this->util->find(
                Query::where('target', HOSTNAME_INVALID . '/32')
            )
        );
    }

    public function testFindNoCriteria()
    {
        $this->util->changeMenu('/queue/simple');
        $findResults = $this->util->find();
        $this->assertRegExp(
            self::REGEX_IDLIST,
            $findResults
        );
        $this->assertSame(
            count(explode(',', $findResults)),
            count(
                $this->client->sendSync(
                    new Request('/queue/simple/print')
                )->getAllOfType(Response::TYPE_DATA)
            )
        );
    }

    public function testFindCallback()
    {
        $this->util->changeMenu('/queue/simple');
        $findResults = $this->util->find(
            function ($entry) {
                return $entry->getArgument(
                    'target'
                ) === HOSTNAME_INVALID . '/32';
            }
        );
        $this->assertRegExp(
            '/^' . self::REGEX_ID . '$/',
            $findResults
        );
        $this->assertSame(
            $findResults,
            $this->client->sendSync(
                new Request(
                    '/queue/simple/print',
                    Query::where('target', HOSTNAME_INVALID . '/32')
                )
            )->getArgument('.id')
        );
    }
    
    public function testByCallbackName()
    {
        if (!function_exists('isHostnameInvalid')) {
            include 'data://text/plain;base64,' . base64_encode(
                <<<HEREDOC
<?php
function isHostnameInvalid(\$entry) {
    return \$entry->getArgument(
        'target'
    ) === \PEAR2\Net\RouterOS\Client\Test\HOSTNAME_INVALID . '/32';
}
HEREDOC
            );
        }
        $this->util->changeMenu('/queue/simple');
        $findResults = $this->util->find('isHostnameInvalid');
        $this->assertRegExp(
            '/^' . self::REGEX_ID . '$/',
            $findResults
        );
        $this->assertSame(
            $findResults,
            $this->client->sendSync(
                new Request(
                    '/queue/simple/print',
                    Query::where('target', HOSTNAME_INVALID . '/32')
                )
            )->getArgument('.id')
        );
    }

    public function testFindById()
    {
        $originalResult = $this->client->sendSync(
            new Request(
                '/queue/simple/print',
                Query::where('target', HOSTNAME_INVALID . '/32')
            )
        );
        $this->assertSame(
            $originalResult->getArgument('.id'),
            $this->util->find($originalResult->getArgument('.id'))
        );
    }
    
    public function testFindByCommaSeparatedValue()
    {
        $this->util->changeMenu('/queue/simple');
        $findResults = $this->util->find('0,1');
        $this->assertRegExp(
            self::REGEX_IDLIST,
            $findResults
        );
        $this->assertCount(2, explode(',', $findResults));

        $findResults = $this->util->find('0,,1');
        $this->assertRegExp(
            self::REGEX_IDLIST,
            $findResults
        );
        $this->assertCount(2, explode(',', $findResults));
    }
}
EOF