Newer
Older
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
<?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\Unit\DataAccess;
use Piwik\DataAccess\LogQueryBuilder\JoinTables;
use Piwik\Tests\Framework\Mock\Plugin\LogTablesProvider;
use Piwik\Tracker\Visit;
/**
* @group Core
*/
class JoinTablesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var JoinTables
*/
private $tables;
public function setUp()
{
$this->tables = $this->makeTables(array(
'log_visit',
array('table' => 'log_conversion', 'joinOn' => 'log_conversion.idvisit = log_visit.idvisit'),
'log_action'));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Table 'log_foo_bar_baz' can't be used for segmentation
*/
public function test_construct_shouldThrowException_IfTableIsNotPossibleToJoin()
{
$this->makeTables(array('log_visit', 'log_foo_bar_baz'));
}
public function test_hasJoinedTable_shouldDetectIfTableIsAlreadyAdded()
{
$this->assertTrue($this->tables->hasJoinedTable('log_visit'));
$this->assertTrue($this->tables->hasJoinedTable('log_action'));
$this->assertFalse($this->tables->hasJoinedTable('log_foo_bar_baz'));
$this->assertFalse($this->tables->hasJoinedTable('log_conversion')); // we do not check for manually joined tables
}
public function test_addTableToJoin_shouldAddGivenTable()
{
$table = 'log_conversion_item';
$this->assertFalse($this->tables->hasJoinedTable($table));
$this->tables->addTableToJoin($table);
$this->assertTrue($this->tables->hasJoinedTable($table));
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Table 'log_foo_bar_baz' can't be used for segmentation
*/
public function test_addTableToJoin_shouldCheckIfTableCanBeUsedForSegmentation()
{
$table = 'log_foo_bar_baz';
$this->assertFalse($this->tables->hasJoinedTable($table));
$this->tables->addTableToJoin($table);
$this->assertTrue($this->tables->hasJoinedTable($table));
}
public function test_hasJoinedTableManually_shouldReturnTrue_IfTableJoinExistsExactlyAsGiven()
{
$result = $this->tables->hasJoinedTableManually('log_conversion', 'log_conversion.idvisit = log_visit.idvisit');
$this->assertTrue($result);
}
public function test_hasJoinedTableManually_shouldReturnFalse_IfTableOrJoinDoesNotMatch()
{
$result = $this->tables->hasJoinedTableManually('log_foo_bar_baz', 'log_conversion.idvisit = log_visit.idvisit');
$this->assertFalse($result);
$result = $this->tables->hasJoinedTableManually('log_conversion', 'log_foo_bar_baz.idvisit = log_visit.idvisit');
$this->assertFalse($result);
}
public function test_hasJoinedTableManually_shouldReturnFalse_IfTableOrJoinHasCustomJoin()
{
$this->tables = $this->makeTables(array(
'log_visit',
array('table' => 'log_conversion', 'join' => 'right JOIN', 'joinOn' => 'log_conversion.idvisit = log_visit.idvisit'),
'log_action'));
$result = $this->tables->hasJoinedTableManually('log_conversion', 'log_conversion.idvisit = log_visit.idvisit');
$this->assertFalse($result);
}
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
public function test_hasAddedTableManually_shouldReturnTrue_IfTableWasAddedManually()
{
$result = $this->tables->hasAddedTableManually('log_conversion');
$this->assertTrue($result);
}
public function test_hasAddedTableManually_shouldReturnFalse_IfTableWasNotAddedManually()
{
$result = $this->tables->hasAddedTableManually('log_foo_bar_baz');
$this->assertFalse($result);
$result = $this->tables->hasAddedTableManually('log_conversion_item');
$this->assertFalse($result);
}
public function test_getLogTable_shouldReturnInstanceOfLogTable_IfTableExists()
{
$visit = $this->tables->getLogTable('log_visit');
$this->assertFalse($visit instanceof Visit);
}
public function test_getLogTable_shouldReturnNull_IfLogTableDoesNotExist()
{
$visit = $this->tables->getLogTable('log_foo_bar_baz');
$this->assertNull($visit);
}
public function test_findIndexOfManuallyAddedTable_shouldReturnTheIndex_IfTableWasAddedManually()
{
$this->assertSame(1, $this->tables->findIndexOfManuallyAddedTable('log_conversion'));
}
public function test_findIndexOfManuallyAddedTable_shouldReturnNull_IfTableWasNotAddedManually()
{
$this->assertNull($this->tables->findIndexOfManuallyAddedTable('log_visit'));
$this->assertNull($this->tables->findIndexOfManuallyAddedTable('log_action'));
$this->assertNull($this->tables->findIndexOfManuallyAddedTable('log_foo_bar_baz'));
}
public function test_sort_shouldNeverSortFirstEntry_AndNotMaintainKeys()
{
$tables = $this->makeTables(array('log_conversion', 'log_visit', 'log_action', 'log_conversion_item'));
$tables->sort(function($a, $b) {
return strcmp($a, $b);
});
$expected = array('log_conversion', 'log_action', 'log_conversion_item', 'log_visit');
$this->assertEquals($expected, $tables->getTables());
}
private function makeTables($tables)
{
return new JoinTables(new LogTablesProvider(), $tables);
}
}