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

FactoryTest.php « Period « Integration « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 470cacccd159cdb4a0bd45e42d00916aac20a55f (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
<?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\Tests\Integration\Period;

use Piwik\Config;
use Piwik\Period;
use Piwik\Period\Day;
use Piwik\Period\Month;
use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik\Period\Year;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class TestPeriod
{
    // empty
}

class TestPeriodFactory extends Period\Factory
{
    /**
     * @var Config
     */
    private $config;

    // use constructor to make sure period factories are injected
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    public function shouldHandle($strPeriod, $strDate)
    {
        return $strPeriod == 'customperiod';
    }

    public function make($strPeriod, $date, $timezone)
    {
        return new TestPeriod();
    }
}

class MockPluginManager extends \Piwik\Plugin\Manager
{
    public function findComponents($componentName, $expectedSubclass)
    {
        if ($componentName == 'PeriodFactory') {
            return [
                TestPeriodFactory::class,
            ];
        }

        return parent::findComponents($componentName, $expectedSubclass);
    }
}

class FactoryTest extends IntegrationTestCase
{
    /**
     * @dataProvider getBuildTestData
     */
    public function test_build_CreatesCorrectPeriodInstances($strPeriod, $date, $timezone, $expectedPeriodClass,
                                                            $expectedRangeString)
    {
        $period = Period\Factory::build($strPeriod, $date, $timezone);
        $this->assertInstanceOf($expectedPeriodClass, $period);
        $this->assertEquals($expectedRangeString, $period->getRangeString());
    }

    public function getBuildTestData()
    {
        return [
            ['day', '2015-01-01', 'UTC', Day::class, '2015-01-01,2015-01-01'],
            ['week', '2015-01-01', 'UTC', Week::class, '2014-12-29,2015-01-04'],
            ['month', '2015-01-01', 'UTC', Month::class, '2015-01-01,2015-01-31'],
            ['year', '2015-01-01', 'UTC', Year::class, '2015-01-01,2015-12-31'],

            ['range', '2015-01-01,2015-01-10', 'UTC', Range::class, '2015-01-01,2015-01-10'],
            ['range', '2015-01-01,2015-01-10', 'Antarctica/Casey', Range::class, '2015-01-01,2015-01-10'],

            // multiple periods
            ['day', '2015-01-01,2015-01-10', 'UTC', Range::class, '2015-01-01,2015-01-10'],
            ['week', '2015-01-01,2015-01-10', 'UTC', Range::class, '2014-12-29,2015-01-11'],
            ['month', '2015-01-01,2015-02-10', 'UTC', Range::class, '2015-01-01,2015-02-28'],
            ['year', '2015-01-01,2016-01-10', 'UTC', Range::class, '2015-01-01,2016-12-31'],
        ];
    }

    public function test_build_CreatesCustomPeriodInstances()
    {
        Config::getInstance()->General['enabled_periods_API'] .= ',customperiod';

        $period = Period\Factory::build('customperiod', '2015-01-01');
        $this->assertInstanceOf(TestPeriod::class, $period);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage General_ExceptionInvalidPeriod
     */
    public function test_build_ThrowsIfPeriodIsUnrecognized()
    {
        Period\Factory::build('garbageperiod', '2015-01-01');
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage General_ExceptionInvalidPeriod
     */
    public function test_build_ThrowsIfPeriodIsNotEnabledForApi()
    {
        Config::getInstance()->General['enabled_periods_API'] = 'day';
        Period\Factory::build('week', '2015-01-01');
    }

    public function provideContainerConfig()
    {
        return [
            \Piwik\Plugin\Manager::class => \DI\object(MockPluginManager::class),
        ];
    }
}