Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RequestParserTest.php « CliMulti « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 883dfe922aef41c2ca047e7b10f4ab916d535c07 (plain)
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
157
158
159
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace PHPUnit\Unit\CliMulti;

use PHPUnit\Framework\TestCase;
use Piwik\API\Request;
use Piwik\CliMulti\RequestParser;

class RequestParserTest extends TestCase
{
    public function test_getInProgressCommands_parsesAndFiltersCorrectly()
    {
        $psOutput = <<<END
USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console climulti:request module=API&trigger=archivephp&idSite=1&date=2015-02-03&period=day
anotheruser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls

theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console --matomo-domain=domain climulti:request --some-param module=API&trigger=archivephp&idSite=2&date=2015-02-03&period=week
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls

theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console --matomo-domain=domain climulti:request module=Actions&action=doSomething

theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 php console --matomo-domain=domain climulti:request --some-param module=API&trigger=archivephp&idSite=2&date=2015-03-03&period=week
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 grep climulti:request whatever
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 /usr/bin/php console --some-arg climulti:request --some-param module=Funnels&trigger=archivephp&idSite=2&date=2015-02-03&period=week --some-other-param anotherarg


END;

        $requestParser = $this->getMockRequestParser(true, $psOutput);

        $actual = $requestParser->getInProgressCommands();
        $expected = [
            [
                'module' => 'API',
                'trigger' => 'archivephp',
                'idSite' => '1',
                'date' => '2015-02-03',
                'period' => 'day',
            ],
            [
                'module' => 'API',
                'trigger' => 'archivephp',
                'idSite' => '2',
                'date' => '2015-02-03',
                'period' => 'week',
            ],
            [
                'module' => 'Actions',
                'action' => 'doSomething',
            ],
            [
                'module' => 'API',
                'trigger' => 'archivephp',
                'idSite' => '2',
                'date' => '2015-03-03',
                'period' => 'week',
            ],
            [
                'module' => 'Funnels',
                'trigger' => 'archivephp',
                'idSite' => '2',
                'date' => '2015-02-03',
                'period' => 'week',
            ],
        ];

        $this->assertEquals($expected, $actual);
    }

    public function test_getInProgressCommands_returnsNothingIfNotSupportingAsync()
    {
        $psOutput = <<<END
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console climulti:request module=API&trigger=archivephp&idSite=1&date=2015-02-03&period=day
anotheruser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console --matomo-domain=domain climulti:request --some-param module=API&trigger=archivephp&idSite=2&date=2015-02-03&period=week
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls
END;

        $requestParser = $this->getMockRequestParser(false, $psOutput);

        $actual = $requestParser->getInProgressCommands();
        $expected = [];

        $this->assertEquals($expected, $actual);
    }

    public function test_getInProgressArchivingCommands_returnsOnlyArchivingJobs()
    {
        $psOutput = <<<END
USER               PID  %CPU %MEM      VSZ    RSS   TT  STAT STARTED      TIME COMMAND
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console climulti:request module=API&trigger=archivephp&idSite=1&date=2015-02-03&period=day
anotheruser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls

theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console --matomo-domain=domain climulti:request --some-param module=API&trigger=archivephp&idSite=2&date=2015-02-03&period=week
theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ls

theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 ./console --matomo-domain=domain climulti:request module=Actions&action=doSomething

theuser     3310   7.3  8.5 11469072 1434212   ??  S    17Apr20 1652:35.90 php console --matomo-domain=domain climulti:request --some-param module=API&trigger=archivephp&idSite=2&date=2015-03-03&period=week

END;

        $requestParser = $this->getMockRequestParser(true, $psOutput);

        $actual = $requestParser->getInProgressArchivingCommands();
        $expected = [
            [
                'module' => 'API',
                'trigger' => 'archivephp',
                'idSite' => '1',
                'date' => '2015-02-03',
                'period' => 'day',
            ],
            [
                'module' => 'API',
                'trigger' => 'archivephp',
                'idSite' => '2',
                'date' => '2015-02-03',
                'period' => 'week',
            ],
            [
                'module' => 'API',
                'trigger' => 'archivephp',
                'idSite' => '2',
                'date' => '2015-03-03',
                'period' => 'week',
            ],
        ];

        $this->assertEquals($expected, $actual);
    }

    private function getMockRequestParser($supportsAsync, $psOutput)
    {
        $mock = new class($supportsAsync, $psOutput) extends RequestParser {
            public function __construct($supportsAsync, $psOutput)
            {
                parent::__construct($supportsAsync);
                $this->psOutput = $psOutput;
            }

            protected function invokePs()
            {
                return $this->psOutput;
            }
        };
        return $mock;
    }
}