Skip to content

Commit

Permalink
refs #57 added more tests and some bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 5, 2014
1 parent fe39dc2 commit 4bb969c
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 172 deletions.
6 changes: 4 additions & 2 deletions plugins/Insights/DataTable/Filter/Average.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public function filter($table)
return;
}

foreach ($table->getRows() as $key => $row) {
foreach ($table->getRows() as $row) {

$value = $row->getColumn($this->columnToRead);

$row->setColumn($this->columnToRead, round($value / $this->divisor));
if (false !== $value && is_numeric($value)) {
$row->setColumn($this->columnToRead, round($value / $this->divisor));
}
}
}
}
7 changes: 2 additions & 5 deletions plugins/Insights/Visualizations/Insight.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,11 @@ public function isThereDataToDisplay()
return true;
}

public function afterAllFiltersAreApplied()
public function beforeRender()
{
$this->assignTemplateVar('period', Common::getRequestVar('period', null, 'string'));
}

public function beforeRender()
{
$this->config->datatable_js_type = 'InsightsDataTable';
$this->config->datatable_js_type = 'InsightsDataTable';
$this->config->show_limit_control = true;
$this->config->show_pagination_control = false;
$this->config->show_offset_information = false;
Expand Down
6 changes: 0 additions & 6 deletions plugins/Insights/stylesheets/insightVisualization.less
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,4 @@
color:red;
}

table td {
&.labelodd, &.labeleven {

background-image: none;
}
}
}
47 changes: 47 additions & 0 deletions plugins/Insights/tests/BaseUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\Insights\tests;

use Piwik\DataTable;
use Piwik\DataTable\Row;

/**
* @group Insights
* @group Unit
* @group Core
*/
class BaseUnitTest extends \PHPUnit_Framework_TestCase
{
/**
* @var DataTable
*/
protected $table;

protected function assertOrder($expectedOrder)
{
$this->assertEquals($expectedOrder, $this->table->getColumn('label'));
$this->assertEquals(count($expectedOrder), $this->table->getRowsCount());
}

protected function assertColumnValues($rowsWithValues)
{
$index = 0;
foreach ($this->table->getRows() as $row) {
$rowToCheck = $rowsWithValues[$index];
foreach ($rowToCheck as $columnToCheck => $expectedValue) {
$actualValue = $row->getColumn($columnToCheck);
$this->assertEquals($expectedValue, $actualValue, "$columnToCheck in row $index does not match assumed $actualValue is $expectedValue");
}
$index++;
}

$this->assertEquals(count($rowsWithValues), $this->table->getRowsCount());
}

}
77 changes: 77 additions & 0 deletions plugins/Insights/tests/FilterAverageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

namespace Piwik\Plugins\Insights\tests;

use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Plugins\Insights\DataTable\Filter\Average;

/**
* @group Insights
* @group FilterAverageTest
* @group Unit
* @group Core
*/
class FilterAverageTest extends BaseUnitTest
{
public function setUp()
{
$this->table = new DataTable();
$this->table->addRowsFromArray(array(
array(Row::COLUMNS => array('label' => 'val1', 'growth' => 22)),
array(Row::COLUMNS => array('label' => 'val2', 'growth' => 14)),
array(Row::COLUMNS => array('label' => 'val3', 'growth' => 18)),
array(Row::COLUMNS => array('label' => 'val4', 'growth' => 20)),
array(Row::COLUMNS => array('label' => 'val5', 'growth' => 25)),
array(Row::COLUMNS => array('label' => 'val6', 'growth' => 17)),
array(Row::COLUMNS => array('label' => 'val7', 'growth' => 0)),
array(Row::COLUMNS => array('label' => 'val8', 'growth' => 4)),
array(Row::COLUMNS => array('label' => 'val9', 'growth' => -4)),
array(Row::COLUMNS => array('label' => 'val10', 'growth' => null)),
array(Row::COLUMNS => array('label' => 'val11', 'growth' => false)),
));
}

public function testShouldNotChangeAnythingIfAverageIsZeroOrOne()
{
$rowsBefore = $this->table->getRows();

$this->calculateAverage(0);
$this->assertSame($rowsBefore, $this->table->getRows());

$this->calculateAverage(1);
$this->assertSame($rowsBefore, $this->table->getRows());
}

public function testShouldDivideNumericValuesByDivisorAndRound()
{
$this->calculateAverage(4);

$this->assertColumnValues(array(
array('label' => 'val1', 'growth' => 6),
array('label' => 'val2', 'growth' => 4),
array('label' => 'val3', 'growth' => 5),
array('label' => 'val4', 'growth' => 5),
array('label' => 'val5', 'growth' => 6),
array('label' => 'val6', 'growth' => 4),
array('label' => 'val7', 'growth' => 0),
array('label' => 'val8', 'growth' => 1),
array('label' => 'val9', 'growth' => -1),
array('label' => 'val10', 'growth' => null),
array('label' => 'val11', 'growth' => false),
));
}

private function calculateAverage($divisor)
{
$filter = new Average($this->table, 'growth', $divisor);
$filter->filter($this->table);
}

}
13 changes: 1 addition & 12 deletions plugins/Insights/tests/FilterExcludeLowValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
* @group Unit
* @group Core
*/
class FilterExcludeLowValueTest extends \PHPUnit_Framework_TestCase
class FilterExcludeLowValueTest extends BaseUnitTest
{
/**
* @var DataTable
*/
private $table;

public function setUp()
{
$this->table = new DataTable();
Expand Down Expand Up @@ -81,12 +76,6 @@ public function testShouldRemoveValuesOnlyIfColumnToCheckIsTrue()
$this->assertOrder(array('val1', 'val3', 'val5', 'val6', 'val7', 'val8', 'val9', 'val10'));
}

private function assertOrder($expectedOrder)
{
$this->assertEquals($expectedOrder, $this->table->getColumn('label'));
$this->assertEquals(count($expectedOrder), $this->table->getRowsCount());
}

private function excludeLowValues($minimumValue, $columnToCheck = null)
{
$filter = new ExcludeLowValue($this->table, 'growth', $minimumValue, $columnToCheck);
Expand Down
28 changes: 1 addition & 27 deletions plugins/Insights/tests/FilterInsightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
* @group Unit
* @group Core
*/
class FilterInsightTest extends \PHPUnit_Framework_TestCase
class FilterInsightTest extends BaseUnitTest
{
/**
* @var DataTable
*/
private $table;

/**
* @var DataTable
*/
Expand Down Expand Up @@ -217,27 +212,6 @@ public function testShouldCalculateImporance()
$this->assertColumnValues($values);
}

private function assertOrder($expectedOrder)
{
$this->assertEquals($expectedOrder, $this->table->getColumn('label'));
$this->assertEquals(count($expectedOrder), $this->table->getRowsCount());
}

private function assertColumnValues($rowsWithValues)
{
$this->assertEquals(count($rowsWithValues), $this->table->getRowsCount());

$index = 0;
foreach ($this->table->getRows() as $row) {
$rowToCheck = $rowsWithValues[$index];
foreach ($rowToCheck as $columnToCheck => $expectedValue) {
$actualValue = $row->getColumn($columnToCheck);
$this->assertEquals($expectedValue, $actualValue, "$columnToCheck in row $index does not match assumed $actualValue is $expectedValue");
}
$index++;
}
}

private function applyInsight($considerMovers, $considerNew, $considerDisappeared)
{
$filter = new Insight($this->table, $this->currentTable, $this->pastTable, 'nb_visits', $considerMovers, $considerNew, $considerDisappeared);
Expand Down
13 changes: 1 addition & 12 deletions plugins/Insights/tests/FilterLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
* @group Unit
* @group Core
*/
class FilterLimitTest extends \PHPUnit_Framework_TestCase
class FilterLimitTest extends BaseUnitTest
{
/**
* @var DataTable
*/
private $table;

public function setUp()
{
$this->table = new DataTable();
Expand Down Expand Up @@ -77,12 +72,6 @@ public function testShouldReturnAllRowsIfLimitIsHighEnough()
$this->assertOrder(array('pos1', 'pos2', 'neg1', 'pos3', 'neg2', 'neg3', 'pos4', 'pos5', 'neg4', 'neg5'));
}

private function assertOrder($expectedOrder)
{
$this->assertEquals($expectedOrder, $this->table->getColumn('label'));
$this->assertEquals(count($expectedOrder), $this->table->getRowsCount());
}

private function applyLimit($limitIncrease, $limitDecrease)
{
$filter = new Limit($this->table, 'growth', $limitIncrease, $limitDecrease);
Expand Down
12 changes: 1 addition & 11 deletions plugins/Insights/tests/FilterMinGrowthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
* @group Unit
* @group Core
*/
class FilterMinGrowthTest extends \PHPUnit_Framework_TestCase
class FilterMinGrowthTest extends BaseUnitTest
{
/**
* @var DataTable
*/
private $table;

public function setUp()
{
Expand Down Expand Up @@ -81,12 +77,6 @@ public function testShouldRemoveAllIfMinGrowthIsTooHigh()
$this->assertOrder(array());
}

private function assertOrder($expectedOrder)
{
$this->assertEquals($expectedOrder, $this->table->getColumn('label'));
$this->assertEquals(count($expectedOrder), $this->table->getRowsCount());
}

private function applyMinGrowthFilter($minGrowthPercent)
{
$filter = new MinGrowth($this->table, 'growth', $minGrowthPercent);
Expand Down
13 changes: 1 addition & 12 deletions plugins/Insights/tests/FilterOrderByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
* @group Unit
* @group Core
*/
class FilterOrderByTest extends \PHPUnit_Framework_TestCase
class FilterOrderByTest extends BaseUnitTest
{
/**
* @var DataTable
*/
private $table;

public function setUp()
{
$this->table = new DataTable();
Expand Down Expand Up @@ -91,12 +86,6 @@ public function testOrderByShouldSortDependingOnNbVisitsIfColumnsHaveSameValueAn
$this->assertOrder(array('pos4', 'pos2', 'pos1', 'pos3', 'pos6', 'pos5', 'neg3', 'neg2', 'neg1', 'neg4'));
}

private function assertOrder($expectedOrder)
{
$this->assertEquals($expectedOrder, $this->table->getColumn('label'));
$this->assertEquals(count($expectedOrder), $this->table->getRowsCount());
}

private function applyOrderByFilter()
{
$filter = new OrderBy($this->table, 'growth', 'nb_visits');
Expand Down
Loading

0 comments on commit 4bb969c

Please sign in to comment.