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

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiosmosis <benaka@piwik.pro>2014-09-15 22:10:16 +0400
committerdiosmosis <benaka@piwik.pro>2014-09-15 22:10:16 +0400
commit718c1bb9c6bb94b8342c82fe99486ad88dffd041 (patch)
tree6164eb0aeced9df2069c5f4ba2afa8cd6d31a803 /tests/PHPUnit/Core
parent451f607b3d69e80092426e054d4124e037a77ba8 (diff)
Refs #6078, extract factory logic from Report::factory and move to new ComponentFactory utility class and reuse in Dimension to allow creating Dimension instances by human readable string IDs.
Diffstat (limited to 'tests/PHPUnit/Core')
-rw-r--r--tests/PHPUnit/Core/Columns/DimensionTest.php240
-rw-r--r--tests/PHPUnit/Core/Plugin/ComponentFactoryTest.php86
2 files changed, 217 insertions, 109 deletions
diff --git a/tests/PHPUnit/Core/Columns/DimensionTest.php b/tests/PHPUnit/Core/Columns/DimensionTest.php
index df694ee030..477ce7114a 100644
--- a/tests/PHPUnit/Core/Columns/DimensionTest.php
+++ b/tests/PHPUnit/Core/Columns/DimensionTest.php
@@ -6,148 +6,170 @@
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
-namespace Piwik\Plugins\Test;
-// there is a test that requires the class to be defined in a plugin
-
-use Piwik\Columns\Dimension;
-use Piwik\Config;
-use Piwik\Plugin\Segment;
-use Piwik\Plugin\Manager;
-
-class DimensionTest extends Dimension
+namespace Piwik\Plugins\Test\Columns
{
- protected $columnName = 'test_dimension';
- protected $columnType = 'INTEGER (10) DEFAULT 0';
+ // there is a test that requires the class to be defined in a plugin
- public function set($param, $value)
- {
- $this->$param = $value;
- }
+ use Piwik\Columns\Dimension;
+ use Piwik\Plugin\Segment;
- protected function configureSegments()
+ class DimensionTest extends Dimension
{
- $segment = new Segment();
- $segment->setSegment('exitPageUrl');
- $segment->setName('Actions_ColumnExitPageURL');
- $segment->setCategory('General_Visit');
- $this->addSegment($segment);
-
- // custom type and sqlSegment
- $segment = new Segment();
- $segment->setSegment('exitPageUrl');
- $segment->setSqlSegment('customValue');
- $segment->setType(Segment::TYPE_METRIC);
- $segment->setName('Actions_ColumnExitPageURL');
- $segment->setCategory('General_Visit');
- $this->addSegment($segment);
+ protected $columnName = 'test_dimension';
+ protected $columnType = 'INTEGER (10) DEFAULT 0';
+
+ public function set($param, $value)
+ {
+ $this->$param = $value;
+ }
+
+ protected function configureSegments()
+ {
+ $segment = new Segment();
+ $segment->setSegment('exitPageUrl');
+ $segment->setName('Actions_ColumnExitPageURL');
+ $segment->setCategory('General_Visit');
+ $this->addSegment($segment);
+
+ // custom type and sqlSegment
+ $segment = new Segment();
+ $segment->setSegment('exitPageUrl');
+ $segment->setSqlSegment('customValue');
+ $segment->setType(Segment::TYPE_METRIC);
+ $segment->setName('Actions_ColumnExitPageURL');
+ $segment->setCategory('General_Visit');
+ $this->addSegment($segment);
+ }
}
}
-/**
- * @group Core
- */
-class Core_DimensionTest extends \PHPUnit_Framework_TestCase
+namespace Piwik\Tests\Core\Columns
{
+ use Piwik\Columns\Dimension;
+ use Piwik\Config;
+ use Piwik\Plugin\Segment;
+ use Piwik\Plugin\Manager;
+ use Piwik\Plugins\Test\Columns\DimensionTest;
+ use Piwik\Plugins\Test\FakeActionDimension;
+
/**
- * @var FakeActionDimension
+ * @group Core
*/
- private $dimension;
-
- public function setUp()
+ class Core_DimensionTest extends \PHPUnit_Framework_TestCase
{
- Manager::getInstance()->unloadPlugins();
- Manager::getInstance()->doNotLoadAlwaysActivatedPlugins();
- Config::getInstance()->clear();
- Config::getInstance()->init();
+ /**
+ * @var FakeActionDimension
+ */
+ private $dimension;
+
+ public function setUp()
+ {
+ Manager::getInstance()->unloadPlugins();
+ Manager::getInstance()->doNotLoadAlwaysActivatedPlugins();
+ Config::getInstance()->clear();
+ Config::getInstance()->init();
+
+ $this->dimension = new DimensionTest();
+ }
- $this->dimension = new DimensionTest();
- }
+ public function tearDown()
+ {
+ Config::unsetInstance();
+ Manager::unsetInstance();
+ parent::tearDown();
+ }
- public function tearDown()
- {
- Config::unsetInstance();
- Manager::unsetInstance();
- parent::tearDown();
- }
+ public function test_hasImplementedEvent_shouldDetectWhetherAMethodWasOverwrittenInTheActualPluginClass()
+ {
+ $this->assertTrue($this->dimension->hasImplementedEvent('set'));
+ $this->assertTrue($this->dimension->hasImplementedEvent('configureSegments'));
- public function test_hasImplementedEvent_shouldDetectWhetherAMethodWasOverwrittenInTheActualPluginClass()
- {
- $this->assertTrue($this->dimension->hasImplementedEvent('set'));
- $this->assertTrue($this->dimension->hasImplementedEvent('configureSegments'));
+ $this->assertFalse($this->dimension->hasImplementedEvent('getSegments'));
+ }
- $this->assertFalse($this->dimension->hasImplementedEvent('getSegments'));
- }
+ public function test_getColumnName_shouldReturnTheNameOfTheColumn()
+ {
+ $this->assertSame('test_dimension', $this->dimension->getColumnName());
+ }
- public function test_getColumnName_shouldReturnTheNameOfTheColumn()
- {
- $this->assertSame('test_dimension', $this->dimension->getColumnName());
- }
+ public function test_hasColumnType_shouldDetectWhetherAColumnTypeIsSet()
+ {
+ $this->assertTrue($this->dimension->hasColumnType());
- public function test_hasColumnType_shouldDetectWhetherAColumnTypeIsSet()
- {
- $this->assertTrue($this->dimension->hasColumnType());
+ $this->dimension->set('columnType', '');
+ $this->assertFalse($this->dimension->hasColumnType());
+ }
- $this->dimension->set('columnType', '');
- $this->assertFalse($this->dimension->hasColumnType());
- }
+ public function test_getName_ShouldNotReturnANameByDefault()
+ {
+ $this->assertSame('', $this->dimension->getName());
+ }
- public function test_getName_ShouldNotReturnANameByDefault()
- {
- $this->assertSame('', $this->dimension->getName());
- }
+ public function test_getAllDimensions_shouldReturnActionVisitAndConversionDimensions()
+ {
+ Manager::getInstance()->loadPlugins(array('Actions', 'Events', 'DevicesDetector', 'Goals'));
- public function test_getAllDimensions_shouldReturnActionVisitAndConversionDimensions()
- {
- Manager::getInstance()->loadPlugins(array('Actions', 'Events', 'DevicesDetector', 'Goals'));
+ $dimensions = Dimension::getAllDimensions();
- $dimensions = Dimension::getAllDimensions();
+ $this->assertGreaterThan(20, count($dimensions));
- $this->assertGreaterThan(20, count($dimensions));
+ $foundConversion = false;
+ $foundVisit = false;
+ $foundAction = false;
- $foundConversion = false;
- $foundVisit = false;
- $foundAction = false;
+ foreach ($dimensions as $dimension) {
+ if ($dimension instanceof \Piwik\Plugin\Dimension\ConversionDimension) {
+ $foundConversion = true;
+ } else if ($dimension instanceof \Piwik\Plugin\Dimension\ActionDimension) {
+ $foundAction = true;
+ } else if ($dimension instanceof \Piwik\Plugin\Dimension\VisitDimension) {
+ $foundVisit = true;
+ } else {
+ $this->fail('Unexpected dimension class found');
+ }
- foreach ($dimensions as $dimension) {
- if ($dimension instanceof \Piwik\Plugin\Dimension\ConversionDimension) {
- $foundConversion = true;
- } else if ($dimension instanceof \Piwik\Plugin\Dimension\ActionDimension) {
- $foundAction = true;
- } else if ($dimension instanceof \Piwik\Plugin\Dimension\VisitDimension) {
- $foundVisit = true;
- } else {
- $this->fail('Unexpected dimension class found');
+ $this->assertRegExp('/Piwik.Plugins.(Actions|Events|DevicesDetector|Goals).Columns/', get_class($dimension));
}
- $this->assertRegExp('/Piwik.Plugins.(Actions|Events|DevicesDetector|Goals).Columns/', get_class($dimension));
+ $this->assertTrue($foundConversion);
+ $this->assertTrue($foundAction);
+ $this->assertTrue($foundVisit);
}
- $this->assertTrue($foundConversion);
- $this->assertTrue($foundAction);
- $this->assertTrue($foundVisit);
- }
+ public function test_getSegment_ShouldReturnConfiguredSegments()
+ {
+ $segments = $this->dimension->getSegments();
- public function test_getSegment_ShouldReturnConfiguredSegments()
- {
- $segments = $this->dimension->getSegments();
+ $this->assertCount(2, $segments);
+ $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]);
+ $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]);
+ }
- $this->assertCount(2, $segments);
- $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[0]);
- $this->assertInstanceOf('\Piwik\Plugin\Segment', $segments[1]);
- }
+ public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet()
+ {
+ $segments = $this->dimension->getSegments();
- public function test_addSegment_ShouldPrefilSomeSegmentValuesIfNotDefinedYet()
- {
- $segments = $this->dimension->getSegments();
+ $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType());
+ }
- $this->assertEquals(Segment::TYPE_DIMENSION, $segments[0]->getType());
- }
+ public function test_addSegment_ShouldNotOverwritePreAssignedValues()
+ {
+ $segments = $this->dimension->getSegments();
- public function test_addSegment_ShouldNotOverwritePreAssignedValues()
- {
- $segments = $this->dimension->getSegments();
+ $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType());
+ }
- $this->assertEquals(Segment::TYPE_METRIC, $segments[1]->getType());
- }
+ public function test_getId_ShouldCorrectlyGenerateIdFromDimensionsQualifiedClassName()
+ {
+ $this->assertEquals("Test.DimensionTest", $this->dimension->getId());
+ }
+
+ public function test_factory_ShouldCreateDimensionFromDimensionId()
+ {
+ Manager::getInstance()->loadPlugins(array('ExampleTracker'));
+ $dimension = Dimension::factory("ExampleTracker.ExampleDimension");
+ $this->assertInstanceOf("Piwik\\Plugins\\ExampleTracker\\Columns\\ExampleDimension", $dimension);
+ }
+ }
} \ No newline at end of file
diff --git a/tests/PHPUnit/Core/Plugin/ComponentFactoryTest.php b/tests/PHPUnit/Core/Plugin/ComponentFactoryTest.php
new file mode 100644
index 0000000000..d41149c8cd
--- /dev/null
+++ b/tests/PHPUnit/Core/Plugin/ComponentFactoryTest.php
@@ -0,0 +1,86 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.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;
+
+/**
+ * @group Core
+ */
+class ComponentFactoryTest extends PHPUnit_Framework_TestCase
+{
+ const REPORT_CLASS_NAME = 'Piwik\\Plugin\\Report';
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ Config::getInstance()->setTestEnvironment();
+ 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);
+ }
+
+ private function unloadAllPlugins()
+ {
+ PluginManager::getInstance()->loadPlugins(array());
+ }
+
+ private function loadExampleReportPlugin()
+ {
+ PluginManager::getInstance()->loadPlugins(array('ExampleReport'));
+ }
+} \ No newline at end of file