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

ConsoleRendererTest.php « Unit « tests « API « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70e3a2113b87b3d7e092dcc1b6b6c44673fa910d (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
<?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\API\tests\Unit;

use Piwik\DataTable;
use Piwik\Date;
use Piwik\Plugins\API\Renderer\Console;
use Piwik\Plugins\CoreHome\Columns\Metrics\AverageTimeOnSite;

/**
 * @group Plugin
 * @group API
 */
class ConsoleRendererTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var Console
     */
    private $builder;

    public function setUp()
    {
        $this->builder = $this->makeBuilder(array());
        DataTable\Manager::getInstance()->deleteAll();
    }

    public function test_renderSuccess_shouldAlwaysReturnTrueAndIgnoreMessage()
    {
        $response = $this->builder->renderSuccess('ok');

        $this->assertEquals('Success:ok', $response);
    }

    public function test_renderException_shouldThrowTheException()
    {
        $response = $this->builder->renderException('This message should be used', new \BadMethodCallException('The other message'));

        $this->assertEquals('Error: This message should be used', $response);
    }

    public function test_renderScalar_shouldReturnTheSameValue()
    {
        $response = $this->builder->renderScalar(true);
        $this->assertSame("- 1 ['0' => 1] [] [idsubtable = ]<br />
", $response);

        $response = $this->builder->renderScalar(5);
        $this->assertSame("- 1 ['0' => 5] [] [idsubtable = ]<br />
", $response);

        $response = $this->builder->renderScalar('string');
        $this->assertSame("- 1 ['0' => 'string'] [] [idsubtable = ]<br />
", $response);
    }

    public function test_renderObject_shouldReturAnError()
    {
        $response = $this->builder->renderObject(new \stdClass());

        $this->assertEquals('Error: The API cannot handle this data structure.', $response);
    }

    public function test_renderResource_shouldReturAnError()
    {
        $response = $this->builder->renderResource(new \stdClass());

        $this->assertEquals('Error: The API cannot handle this data structure.', $response);
    }

    public function test_renderDataTable_shouldReturnResult()
    {
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));

        $response = $this->builder->renderDataTable($dataTable);

        $this->assertSame("- 1 ['nb_visits' => 5, 'nb_random' => 10] [] [idsubtable = ]<br />
", $response);
    }

    public function test_renderDataTableWithObjects_shouldReturnResult()
    {
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable->setMetadata('processedRows', [
            new AverageTimeOnSite(),
            new \stdClass(),
            Date::factory('2016-01-01 00:00:00')
        ]);

        $response = $this->builder->renderDataTable($dataTable);

        $this->assertSame("- 1 ['nb_visits' => 5, 'nb_random' => 10] [] [idsubtable = ]<br />
<hr />Metadata<br /><br /> <b>processedRows</b><br />0 => Object [Piwik\Plugins\CoreHome\Columns\Metrics\AverageTimeOnSite]1 => Object [stdClass]2 => 2016-01-01", $response);
    }

    public function test_renderArray_ShouldReturnConsoleResult()
    {
        $input = array(1, 2, 5, 'string', 10);

        $response = $this->builder->renderArray($input);

        $this->assertSame("- 1 ['0' => 1] [] [idsubtable = ]<br />
- 2 ['0' => 2] [] [idsubtable = ]<br />
- 3 ['0' => 5] [] [idsubtable = ]<br />
- 4 ['0' => 'string'] [] [idsubtable = ]<br />
- 5 ['0' => 10] [] [idsubtable = ]<br />
", $response);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage Data structure returned is not convertible in the requested format
     */
    public function test_renderArray_ShouldConvertMultiDimensionalAssociativeArrayToJson()
    {
        $input = array(
            "firstElement"  => "isFirst",
            "secondElement" => array(
                "firstElement"  => "isFirst",
                "secondElement" => "isSecond",
            ),
            "thirdElement"  => "isThird");

        $actual = $this->builder->renderArray($input);
        $this->assertSame($input, $actual);
    }

    private function makeBuilder($request)
    {
        return new Console($request);
    }
}