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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SegmentEditor\tests\Unit;
use Piwik\Plugins\SegmentEditor\SegmentQueryDecorator;
use Piwik\Plugins\SegmentEditor\Services\StoredSegmentService;
use Piwik\Segment\SegmentExpression;
use Piwik\Tests\Framework\Mock\Plugin\LogTablesProvider;
/**
* @group SegmentEditor
* @group SegmentEditor_Unit
*/
class SegmentQueryDecoratorTest extends \PHPUnit\Framework\TestCase
{
public static $storedSegments = array(
array('definition' => 'countryCode==abc', 'idsegment' => 1),
array('definition' => 'region!=FL', 'idsegment' => 2),
array('definition' => 'browserCode==def;visitCount>2', 'idsegment' => 3),
array('definition' => 'region!=FL', 'idsegment' => 4),
);
/**
* @var SegmentQueryDecorator
*/
private $decorator;
public function setUp(): void
{
parent::setUp();
/** @var StoredSegmentService $service */
$service = $this->getMockSegmentEditorService();
$logTables = new LogTablesProvider();
$this->decorator = new SegmentQueryDecorator($service, $logTables);
}
public function test_getSelectQueryString_DoesNotDecorateSql_WhenNoSegmentUsed()
{
$expression = new SegmentExpression('');
$expression->parseSubExpressions();
$query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', '');
$this->assertStringStartsNotWith('/* idSegments', $query['sql']);
}
public function test_getSelectQueryString_DoesNotDecorateSql_WhenNoSegmentMatchesUsedSegment()
{
$expression = new SegmentExpression('referrerName==ooga');
$expression->parseSubExpressions();
$query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', '');
$this->assertStringStartsNotWith('/* idSegments', $query['sql']);
}
public function test_getSelectQueryString_DecoratesSql_WhenOneSegmentMatchesUsedSegment()
{
$expression = new SegmentExpression('browserCode==def;visitCount>2');
$expression->parseSubExpressions();
$query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', '');
$this->assertStringStartsWith('SELECT /* idSegments = [3] */', $query['sql']);
$this->assertEquals(1, substr_count($query['sql'], 'SELECT'));
}
public function test_getSelectQueryString_DecoratesSql_WhenMultipleStoredSegmentsMatchUsedSegment()
{
$expression = new SegmentExpression('region!=FL');
$expression->parseSubExpressions();
$query = $this->decorator->getSelectQueryString($expression, '*', 'log_visit', '', array(), '', '', '');
$this->assertStringStartsWith('SELECT /* idSegments = [2, 4] */', $query['sql']);
$this->assertEquals(1, substr_count($query['sql'], 'SELECT'));
}
private function getMockSegmentEditorService()
{
$mock = $this->getMockBuilder('Piwik\Plugins\SegmentEditor\Services\StoredSegmentService')
->setMethods(['getAllSegmentsAndIgnoreVisibility'])
->setConstructorArgs([])
->disableOriginalConstructor()
->getMock();
$mock->expects($this->any())->method('getAllSegmentsAndIgnoreVisibility')->willReturn(self::$storedSegments);
return $mock;
}
}
|