1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-25 00:17:37 +00:00
Files
matomo/tests/PHPUnit/Unit/Archive/DataCollectionTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

282 خطوط
9.4 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Unit\Archive;
use Piwik\Archive\DataCollection;
use Piwik\Archive\DataTableFactory;
use Piwik\Period;
use Piwik\Segment;
/**
* @group DataCollectionTest
* @group Archive
*/
class DataCollectionTest extends \PHPUnit\Framework\TestCase
{
private $site1 = 1;
private $site2 = 2;
private $date1 = '2012-12-12,2012-12-12';
private $date2 = '2012-12-13,2012-12-13';
private function createCollection($onlyOnePeriod = false, $onlyOneSite = false)
{
$periods = array(
Period\Factory::build('day', '2012-12-12'),
Period\Factory::build('day', '2012-12-13'),
);
$dataType = 'numeric';
$siteIds = array($this->site1, $this->site2);
$dataNames = array('Name1', 'Name2');
$defaultRow = array(
'default' => 1
);
if ($onlyOnePeriod) {
$periods = array($periods[0]);
}
if ($onlyOneSite) {
$siteIds = array($siteIds[0]);
}
// using mock since Segment makes API queries
$mockSegment = $this->getMockBuilder(Segment::class)
->disableOriginalConstructor()
->getMock()
->method('getString')->willReturn('');
return new DataCollection($dataNames, $dataType, $siteIds, $periods, $mockSegment, $defaultRow);
}
public function testGetIndexedArrayNumericNoResultIndicesNoData()
{
$collection = $this->createCollection($onlyOnePeriod = true, $onlyOneSite = true);
$this->assertEquals(array(), $collection->getIndexedArray($resultIndices = array()));
}
public function testGetIndexedArrayNumericNoResultIndicesWithData()
{
$collection = $this->createCollection($onlyOnePeriod = true, $onlyOneSite = true);
$collection->set($this->site1, '2012-12-12,2012-12-12', 'nb_visits', '5');
$collection->set($this->site1, '2012-12-12,2012-12-12', 'nb_unique_visits', '10');
$expected = array(
'default' => 1,
'nb_visits' => '5',
'nb_unique_visits' => '10',
);
$this->assertEquals($expected, $collection->getIndexedArray($resultIndices = array()));
}
public function testGetIndexedArrayNumericNoResultIndicesWithDefaultOverwritten()
{
$collection = $this->createCollection($onlyOnePeriod = true, $onlyOneSite = true);
$collection->set($this->site1, '2012-12-12,2012-12-12', 'nb_visits', '5');
$collection->set($this->site1, '2012-12-12,2012-12-12', 'default', '10');
$expected = array(
'default' => '10',
'nb_visits' => '5'
);
$this->assertEquals($expected, $collection->getIndexedArray($resultIndices = array()));
}
private function getSiteResultIndices()
{
return array(DataTableFactory::TABLE_METADATA_SITE_INDEX => 'idSite');
}
public function testGetIndexedArrayNumericWithSiteResultIndicesNoData()
{
$collection = $this->createCollection();
$this->assertEquals(array(
1 => array(),
2 => array()
), $collection->getIndexedArray($this->getSiteResultIndices()));
}
public function testGetIndexedArrayNumericWithSiteResultIndicesWithData()
{
$collection = $this->createCollection();
$collection->set($this->site1, '2012-12-12,2012-12-12', 'nb_visits', '5');
$collection->set($this->site1, '2012-12-12,2012-12-12', 'nb_unique_visits', '10');
$expected = array(
1 => array(
'default' => 1,
'nb_visits' => '5',
'nb_unique_visits' => '10',
),
2 => array(
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getSiteResultIndices()));
}
public function testGetIndexedArrayNumericWithSiteResultIndicesWithDefaultOverwritten()
{
$collection = $this->createCollection();
$collection->set($this->site1, '2012-12-12,2012-12-12', 'nb_visits', '5');
$collection->set($this->site1, '2012-12-12,2012-12-12', 'default', '10');
$collection->set($this->site2, '2012-12-12,2012-12-12', 'nb_visits', '15');
$expected = array(
1 => array(
'default' => '10',
'nb_visits' => '5'
),
2 => array(
'default' => 1,
'nb_visits' => '15'
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getSiteResultIndices()));
}
private function getPeriodResultIndices()
{
return array(DataTableFactory::TABLE_METADATA_PERIOD_INDEX => 'date');
}
public function testGetIndexedArrayNumericWithPeriodResultIndicesNoData()
{
$collection = $this->createCollection($onlyOnePeriod = false, $onlyOneSite = true);
$this->assertEquals(array(
$this->date1 => array(),
$this->date2 => array()
), $collection->getIndexedArray($this->getPeriodResultIndices()));
}
public function testGetIndexedArrayNumericWithPeriodResultIndicesWithData()
{
$collection = $this->createCollection($onlyOnePeriod = false, $onlyOneSite = true);
$collection->set($this->site1, $this->date1, 'nb_visits', '5');
$collection->set($this->site1, $this->date1, 'nb_unique_visits', '10');
$expected = array(
$this->date1 => array(
'default' => 1,
'nb_visits' => '5',
'nb_unique_visits' => '10',
),
$this->date2 => array(
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getPeriodResultIndices()));
}
public function testGetIndexedArrayNumericWithPeriodResultIndicesWithDefaultOverwritten()
{
$collection = $this->createCollection($onlyOnePeriod = false, $onlyOneSite = true);
$collection->set($this->site1, $this->date1, 'nb_visits', '5');
$collection->set($this->site1, $this->date1, 'default', '10');
$collection->set($this->site1, $this->date2, 'nb_visits', '15');
$expected = array(
$this->date1 => array(
'default' => '10',
'nb_visits' => '5'
),
$this->date2 => array(
'default' => 1,
'nb_visits' => '15'
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getPeriodResultIndices()));
}
private function getPeriodAndSiteResultIndices()
{
return array_merge($this->getSiteResultIndices(), $this->getPeriodResultIndices());
}
public function testGetIndexedArrayNumericWithPeriodAndSiteResultIndicesNoData()
{
$collection = $this->createCollection();
$expected = array(
$this->site1 => array(
$this->date1 => array(),
$this->date2 => array()
),
$this->site2 => array(
$this->date1 => array(),
$this->date2 => array()
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getPeriodAndSiteResultIndices()));
}
public function testGetIndexedArrayNumericWithPeriodAndSiteResultIndicesWithData()
{
$collection = $this->createCollection();
$collection->set($this->site1, $this->date1, 'nb_visits', '5');
$collection->set($this->site1, $this->date1, 'nb_unique_visits', '10');
$collection->set($this->site2, $this->date1, 'nb_unique_visits', '21');
$collection->set($this->site2, $this->date2, 'nb_unique_visits', '22');
$expected = array(
$this->site1 => array(
$this->date1 => array(
'default' => 1,
'nb_visits' => '5',
'nb_unique_visits' => '10',
),
$this->date2 => array()
),
$this->site2 => array(
$this->date1 => array(
'default' => 1,
'nb_unique_visits' => '21',
),
$this->date2 => array(
'default' => 1,
'nb_unique_visits' => '22',
)
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getPeriodAndSiteResultIndices()));
}
public function testGetIndexedArrayNumericWithPeriodAndSiteResultIndicesWithDefaultOverwritten()
{
$collection = $this->createCollection();
$collection->set($this->site1, $this->date1, 'nb_visits', '5');
$collection->set($this->site1, $this->date1, 'default', '10');
$collection->set($this->site2, $this->date1, 'default', '21');
$expected = array(
$this->site1 => array(
$this->date1 => array(
'default' => 10,
'nb_visits' => '5',
),
$this->date2 => array()
),
$this->site2 => array(
$this->date1 => array('default' => 21),
$this->date2 => array()
)
);
$this->assertEquals($expected, $collection->getIndexedArray($this->getPeriodAndSiteResultIndices()));
}
}