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
98
99
100
101
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
|
<?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;
use Piwik\Segment\SegmentExpression;
/**
* @group SegmentExpressionTest
* @group Segment
*/
class SegmentExpressionTest extends \PHPUnit_Framework_TestCase
{
/**
* Dataprovider for testSegmentSqlSimpleNoOperation
* @return array
*/
public function getSimpleSegmentExpressions()
{
return array(
// classic expressions
array('A', " A "),
array('A,B', " (A OR B )"),
array('A;B', " A AND B "),
array('A;B;C', " A AND B AND C "),
array('A,B;C,D;E,F,G', " (A OR B) AND (C OR D) AND (E OR F OR G )"),
// unescape the backslash
array('A\,B\,C,D', " (A,B,C OR D )"),
array('\,A', ' ,A '),
// unescape only when it was escaping a known delimiter
array('\\\A', ' \\\A '),
// unescape at the end
array('\,\;\A\B,\,C,D\;E\,', ' (,;\A\B OR ,C OR D;E, )'),
// only replace when a following expression is detected
array('A,', ' A, '),
array('A;', ' A; '),
array('A;B;', ' A AND B; '),
array('A,B,', ' (A OR B, )'),
);
}
/**
* @dataProvider getSimpleSegmentExpressions
* @group Core
*/
public function testSegmentSqlSimpleNoOperation($expression, $expectedSql)
{
$segment = new SegmentExpression($expression);
$expected = array('where' => $expectedSql, 'bind' => array(), 'join' => '');
$processed = $segment->getSql();
$this->assertEquals($expected, $processed);
}
/**
* Dataprovider for testSegmentSqlWithOperations
* @return array
*/
public function getOperationSegmentExpressions()
{
// Filter expression => SQL string + Bind values
return array(
array('A==B%', array('where' => " A = ? ", 'bind' => array('B%'))),
array('ABCDEF====B===', array('where' => " ABCDEF = ? ", 'bind' => array('==B==='))),
array('A===B;CDEF!=C!=', array('where' => " A = ? AND ( CDEF IS NULL OR CDEF <> ? ) ", 'bind' => array('=B', 'C!='))),
array('A==B,C==D', array('where' => " (A = ? OR C = ? )", 'bind' => array('B', 'D'))),
array('A!=B;C==D', array('where' => " ( A IS NULL OR A <> ? ) AND C = ? ", 'bind' => array('B', 'D'))),
array('A!=B;C==D,E!=Hello World!=', array('where' => " ( A IS NULL OR A <> ? ) AND (C = ? OR ( E IS NULL OR E <> ? ) )", 'bind' => array('B', 'D', 'Hello World!='))),
array('A>B', array('where' => " A > ? ", 'bind' => array('B'))),
array('A<B', array('where' => " A < ? ", 'bind' => array('B'))),
array('A<=B', array('where' => " A <= ? ", 'bind' => array('B'))),
array('A>=B', array('where' => " A >= ? ", 'bind' => array('B'))),
array('ABCDEF>=>=>=B===', array('where' => " ABCDEF >= ? ", 'bind' => array('>=>=B==='))),
array('A>=<=B;CDEF>G;H>=I;J<K;L<=M', array('where' => " A >= ? AND CDEF > ? AND H >= ? AND J < ? AND L <= ? ", 'bind' => array('<=B', 'G', 'I', 'K', 'M'))),
array('A>=B;C>=D,E<w_ow great!', array('where' => " A >= ? AND (C >= ? OR E < ? )", 'bind' => array('B', 'D', 'w_ow great!'))),
array('A=@B_', array('where' => " A LIKE ? ", 'bind' => array('%B\_%'))),
array('A!@B%', array('where' => " ( A IS NULL OR A NOT LIKE ? ) ", 'bind' => array('%B\%%'))),
);
}
/**
* @dataProvider getOperationSegmentExpressions
* @group Core
*/
public function testSegmentSqlWithOperations($expression, $expectedSql)
{
$segment = new SegmentExpression($expression);
$segment->parseSubExpressions();
$segment->parseSubExpressionsIntoSqlExpressions();
$processed = $segment->getSql();
$expectedSql['join'] = '';
$this->assertEquals($expectedSql, $processed);
}
/**
* Dataprovider for testBogusFiltersExpectExceptionThrown
* @return array
*/
public function getBogusFilters()
{
return array(
array('A=B'),
array('C!D'),
array(''),
array(' '),
array(',;,'),
array(','),
array(',,'),
array('!='),
);
}
/**
* @dataProvider getBogusFilters
* @group Core
*/
public function testBogusFiltersExpectExceptionThrown($bogus)
{
try {
$segment = new SegmentExpression($bogus);
$segment->parseSubExpressions();
$segment->getSql();
} catch (\Exception $e) {
return;
}
$this->fail('Expected exception not raised for:' . var_export($segment->getSql(), true));
}
}
|