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

ComponentFactoryTest.php « Plugin « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57f0718c6c94f2956e75961b999f7ee43f885cef (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
<?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\Core\Plugin;

use PHPUnit_Framework_TestCase;
use Piwik\Config;
use Piwik\Plugin\ComponentFactory;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Plugin\Report;
use Piwik\Tests\Framework\Mock\TestConfig;

/**
 * @group Core
 */
class ComponentFactoryTest extends PHPUnit_Framework_TestCase
{
    const REPORT_CLASS_NAME = 'Piwik\\Plugin\\Report';

    public function setUp()
    {
        parent::setUp();

        Config::getInstance()->Plugins['Plugins'] = array();

        $this->unloadAllPlugins();
    }

    public function test_factory_shouldNotFindAComponentIfComponentExistsButPluginIsNotLoaded()
    {
        $this->unloadAllPlugins();

        $report = ComponentFactory::factory('ExampleReport', 'GetExampleReport', self::REPORT_CLASS_NAME);

        $this->assertNull($report);
    }

    public function test_factory_shouldFindAComponent_ThatExists()
    {
        $this->loadExampleReportPlugin();

        $module = 'ExampleReport';
        $action = 'GetExampleReport';

        $report = ComponentFactory::factory($module, $action, self::REPORT_CLASS_NAME);

        $this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report);
    }

    public function test_factory_shouldNotFindAComponent_IfPluginIsActivatedButComponentNotExists()
    {
        $this->loadExampleReportPlugin();

        $module = 'ExampleReport';
        $action = 'NotExistingReport';

        $report = ComponentFactory::factory($module, $action, self::REPORT_CLASS_NAME);

        $this->assertNull($report);
    }

    public function test_factory_shouldNotFindAComponent_IfPluginIsLoadedButNotActivated()
    {
        PluginManager::getInstance()->loadPlugin('ExampleReport');

        $module = 'ExampleReport';
        $action = 'GetExampleReport';

        $report = ComponentFactory::factory($module, $action, self::REPORT_CLASS_NAME);

        $this->assertNull($report);
    }

    public function test_getComponentIf_shouldNotFindAComponentIfComponentExistsButPluginIsNotLoaded()
    {
        $this->unloadAllPlugins();

        $report = ComponentFactory::getComponentIf(self::REPORT_CLASS_NAME, 'ExampleReport', function (Report $report) {
            return $report->getAction() == 'getExampleReport';
        });

        $this->assertNull($report);
    }

    public function test_getComponentIf_shouldFindAComponent_ThatExists()
    {
        $this->loadExampleReportPlugin();

        $report = ComponentFactory::getComponentIf(self::REPORT_CLASS_NAME, 'ExampleReport', function (Report $report) {
            return $report->getAction() == 'getExampleReport';
        });

        $this->assertInstanceOf('Piwik\Plugins\ExampleReport\Reports\GetExampleReport', $report);
    }

    public function test_getComponentIf_shouldNotFindAComponent_IfPluginIsActivatedButComponentNotExists()
    {
        $this->loadExampleReportPlugin();

        $report = ComponentFactory::getComponentIf(self::REPORT_CLASS_NAME, 'ExampleReport', function (Report $report) {
            return false;
        });

        $this->assertNull($report);
    }

    public function test_getComponentIf_shouldNotFindAComponent_IfPluginIsLoadedButNotActivated()
    {
        PluginManager::getInstance()->loadPlugin('ExampleReport');

        $report = ComponentFactory::getComponentIf(self::REPORT_CLASS_NAME, 'ExampleReport', function (Report $report) {
            return $report->getAction() == 'getExampleReport';
        });

        $this->assertNull($report);
    }

    public function test_getComponentIf_shouldSearchThroughAllPlugins_IfNoPluginNameIsSupplied()
    {
        PluginManager::getInstance()->loadPlugins(array('ExampleReport', 'Referrers'));

        $reports = array();
        ComponentFactory::getComponentIf(self::REPORT_CLASS_NAME, null, function (Report $report) use (&$reports) {
            $reports[] = $report;
        });

        $this->assertGreaterThan(1, count($reports));
    }

    private function unloadAllPlugins()
    {
        PluginManager::getInstance()->loadPlugins(array());
    }

    private function loadExampleReportPlugin()
    {
        PluginManager::getInstance()->loadPlugins(array('ExampleReport'));
    }
}