|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2011 |
|
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
namespace Aimeos\Base\Criteria\Expression\Compare; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class SQLTest extends \PHPUnit\Framework\TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private $conn = null; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
protected function setUp() : void |
|
19
|
|
|
{ |
|
20
|
|
|
if( \TestHelper::getConfig()->get( 'resource/db/adapter', false ) === false ) { |
|
21
|
|
|
$this->markTestSkipped( 'No database configured' ); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$this->conn = \TestHelper::getConnection(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
public function testGetOperators() |
|
29
|
|
|
{ |
|
30
|
|
|
$expected = ['=~', '~=', '==', '!=', '>', '>=', '<', '<=', '-']; |
|
31
|
|
|
$actual = \Aimeos\Base\Criteria\Expression\Compare\SQL::getOperators(); |
|
32
|
|
|
$this->assertEquals( $expected, $actual ); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
public function testGetOperator() |
|
37
|
|
|
{ |
|
38
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'name', 'value' ); |
|
39
|
|
|
$this->assertEquals( '==', $expr->getOperator() ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function testGetName() |
|
44
|
|
|
{ |
|
45
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'name', 'value' ); |
|
46
|
|
|
$this->assertEquals( 'name', $expr->getName() ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function testGetValue() |
|
51
|
|
|
{ |
|
52
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'name', 'value' ); |
|
53
|
|
|
$this->assertEquals( 'value', $expr->getValue() ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
public function testToSource() |
|
58
|
|
|
{ |
|
59
|
|
|
$types = array( |
|
60
|
|
|
'list' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
|
61
|
|
|
'string' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
|
62
|
|
|
'float' => \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT, |
|
63
|
|
|
'int' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
|
64
|
|
|
'undefined' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
|
65
|
|
|
'bool' => \Aimeos\Base\DB\Statement\Base::PARAM_BOOL, |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$translations = array( |
|
69
|
|
|
'list' => 't.list', |
|
70
|
|
|
'string' => 't.string', |
|
71
|
|
|
'float' => 't.float', |
|
72
|
|
|
'int' => 't.int', |
|
73
|
|
|
'undefined' => 't.undefined', |
|
74
|
|
|
'bool' => 't.bool', |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'list', array( 'a', 'b', 'c' ) ); |
|
78
|
|
|
$this->assertEquals( "t.list IN ('a','b','c')", $expr->toSource( $types, $translations ) ); |
|
79
|
|
|
|
|
80
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '!=', 'list', array( 'a', 'b', 'c' ) ); |
|
81
|
|
|
$this->assertEquals( "t.list NOT IN ('a','b','c')", $expr->toSource( $types, $translations ) ); |
|
82
|
|
|
|
|
83
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '~=', 'string', 'value' ); |
|
84
|
|
|
$this->assertEquals( "t.string LIKE '%value%' ESCAPE '#'", $expr->toSource( $types, $translations ) ); |
|
85
|
|
|
|
|
86
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '<', 'float', 0.1 ); |
|
87
|
|
|
$this->assertEquals( "t.float < 0.1", $expr->toSource( $types, $translations ) ); |
|
88
|
|
|
|
|
89
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'float', '' ); |
|
90
|
|
|
$this->assertEquals( "t.float IS NULL", $expr->toSource( $types, $translations ) ); |
|
91
|
|
|
|
|
92
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '>', 'int', 10 ); |
|
93
|
|
|
$this->assertEquals( "t.int > 10", $expr->toSource( $types, $translations ) ); |
|
94
|
|
|
|
|
95
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'int', '' ); |
|
96
|
|
|
$this->assertEquals( "t.int IS NULL", $expr->toSource( $types, $translations ) ); |
|
97
|
|
|
|
|
98
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '!=', 'undefined', null ); |
|
99
|
|
|
$this->assertEquals( "t.undefined IS NOT NULL", $expr->toSource( $types, $translations ) ); |
|
100
|
|
|
|
|
101
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'bool', true ); |
|
102
|
|
|
$this->assertEquals( "t.bool = 1", $expr->toSource( $types, $translations ) ); |
|
103
|
|
|
|
|
104
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '-', 'int', '10 - 20' ); |
|
105
|
|
|
$this->assertEquals( "t.int >= 10 AND t.int <= 20", $expr->toSource( $types, $translations ) ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
public function testToSourceFunction() |
|
110
|
|
|
{ |
|
111
|
|
|
$types = array( |
|
112
|
|
|
'pcounter()' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
|
113
|
|
|
'strconcat()' => \Aimeos\Base\DB\Statement\Base::PARAM_STR, |
|
114
|
|
|
'lcounter()' => \Aimeos\Base\DB\Statement\Base::PARAM_INT, |
|
115
|
|
|
'isnull()' => \Aimeos\Base\DB\Statement\Base::PARAM_NULL, |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
$translations = array( |
|
119
|
|
|
'pcounter()' => 'count($1,$2,$3)', |
|
120
|
|
|
'strconcat()' => 'concat($1,$2)', |
|
121
|
|
|
'lcounter()' => 'count(name IN ($1))', |
|
122
|
|
|
'isnull()' => '($1 IS NULL)', |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'pcounter("name",10,0.1)', 3 ); |
|
126
|
|
|
$this->assertEquals( "count('name',10,0.1) = 3", $expr->toSource( $types, $translations ) ); |
|
127
|
|
|
|
|
128
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '~=', 'strconcat("hello","world")', 'low' ); |
|
129
|
|
|
$this->assertEquals( "concat('hello','world') LIKE '%low%' ESCAPE '#'", $expr->toSource( $types, $translations ) ); |
|
130
|
|
|
|
|
131
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'lcounter(["a","b","c","\'d"])', 4 ); |
|
132
|
|
|
$this->assertMatchesRegularExpression( "/^count\(name IN \('a','b','c','('|\\\\)'d'\)\) = 4$/", $expr->toSource( $types, $translations ) ); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'lcounter([])', 0 ); |
|
135
|
|
|
$this->assertEquals( "count(name IN ()) = 0", $expr->toSource( $types, $translations ) ); |
|
136
|
|
|
|
|
137
|
|
|
$expr = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '!=', 'isnull(null)', null ); |
|
138
|
|
|
$this->assertEquals( "(null IS NULL) IS NOT NULL", $expr->toSource( $types, $translations ) ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
public function testToArray() |
|
143
|
|
|
{ |
|
144
|
|
|
$expected = ['==' => ['stringvar' => 'value']]; |
|
145
|
|
|
$object = new \Aimeos\Base\Criteria\Expression\Compare\SQL( $this->conn, '==', 'stringvar', 'value' ); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertEquals( $expected, $object->__toArray() ); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|