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

SetNumberOfCustomVariablesTest.php « Commands « tests « CustomVariables « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f4d6f4954814cf210742a368c1fb5e6a76bd8b2e (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\CustomVariables\tests\Commands;

use Piwik\Plugins\CustomVariables\Commands\SetNumberOfCustomVariables;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group CustomVariables
 * @group CustomVariablesTest
 * @group Plugins
 * @group Plugins
 */
class SetNumberOfCustomVariablesTest extends IntegrationTestCase
{
    public function testExecute_ShouldThrowException_IfArgumentIsMissing()
    {
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionMessage('Not enough arguments');

        $this->executeCommand(null);
    }

    public function testExecute_ShouldThrowException_HasToBeANumber()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('The number of available custom variables has to be a number');

        $this->executeCommand('a');
    }

    public function testExecute_ShouldThrowException_Minimum2CustomVarsRequired()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('There has to be at least five custom variables');

        $this->executeCommand(4);
    }

    public function testExecute_ShouldThrowException_IfUserCancelsConfirmation()
    {
        $result = $this->executeCommand(7, false);
        $this->assertStringEndsWith('Are you sure you want to perform these actions? (y/N)', $result);
    }

    public function testExecute_ShouldDoNothingIfExpectedResult_IsAlreadyTheCase()
    {
        $result = $this->executeCommand(5);

        self::assertStringContainsString('Your Piwik is already configured for 5 custom variables', $result);
    }

    public function testExecute_ShouldAddMaxCustomVars_IfNumberIsHigherThanActual()
    {
        $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());

        $result = $this->executeCommand(6);

        self::assertStringContainsString('Configuring Piwik for 6 custom variables', $result);
        self::assertStringContainsString('1 new custom variables having the index(es) 6 will be ADDED', $result);
        self::assertStringContainsString('Starting to apply changes', $result);
        self::assertStringContainsString('Added a variable in scope "Page" having the index 6', $result);
        self::assertStringContainsString('Added a variable in scope "Visit" having the index 6', $result);
        self::assertStringContainsString('Added a variable in scope "Conversion" having the index 6', $result);
        self::assertStringContainsString('Your Piwik is now configured for 6 custom variables.', $result);

        $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());
    }

    public function testExecute_ShouldRemoveMaxCustomVars_IfNumberIsLessThanActual()
    {
        $this->executeCommand(6, true);
        $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());

        $result = $this->executeCommand(5);

        self::assertStringContainsString('Configuring Piwik for 5 custom variables', $result);
        self::assertStringContainsString('1 existing custom variables having the index(es) 6 will be REMOVED.', $result);
        self::assertStringContainsString('Starting to apply changes', $result);
        self::assertStringContainsString('Removed a variable in scope "Page" having the index 6', $result);
        self::assertStringContainsString('Removed a variable in scope "Visit" having the index 6', $result);
        self::assertStringContainsString('Removed a variable in scope "Conversion" having the index 6', $result);
        self::assertStringContainsString('Your Piwik is now configured for 5 custom variables.', $result);

        $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());
    }

    public function testExecute_AddMultiple_RemoveMultiple()
    {
        $this->assertEquals(5, CustomVariables::getNumUsableCustomVariables());

        $this->executeCommand(9);
        $this->assertEquals(9, CustomVariables::getNumUsableCustomVariables());

        $this->executeCommand(6);
        $this->assertEquals(6, CustomVariables::getNumUsableCustomVariables());
    }

    /**
     * @param int|null $maxCustomVars
     * @param bool  $confirm
     *
     * @return string
     */
    private function executeCommand($maxCustomVars, $confirm = true)
    {
        $setNumberCmd = new SetNumberOfCustomVariables();

        $application = new Application();
        $application->add($setNumberCmd);

        $commandTester = new CommandTester($setNumberCmd);

        $dialog = $setNumberCmd->getHelper('dialog');
        $dialog->setInputStream($this->getInputStream($confirm ? 'yes' : 'no' . '\n'));

        if (is_null($maxCustomVars)) {
            $params = array();
        } else {
            $params = array('maxCustomVars' => $maxCustomVars);
        }

        $params['command'] = $setNumberCmd->getName();
        $commandTester->execute($params);
        $result = $commandTester->getDisplay();

        return $result;
    }

    protected function getInputStream($input)
    {
        $stream = fopen('php://memory', 'r+', false);
        fputs($stream, $input);
        rewind($stream);

        return $stream;
    }
}