Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/querybuilder/UpdateQueryBuilderTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Rdbms;
4
5 use DatabaseTestHelper;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8 use Wikimedia\Rdbms\Platform\ISQLPlatform;
9 use Wikimedia\Rdbms\UpdateQueryBuilder;
10
11 /**
12 * @covers \Wikimedia\Rdbms\UpdateQueryBuilder
13 */
14 class UpdateQueryBuilderTest extends TestCase {
15 use MediaWikiCoversValidator;
16
17 /** @var DatabaseTestHelper */
18 private $db;
19
20 /** @var UpdateQueryBuilder */
21 private $uqb;
22
23 protected function setUp(): void {
24 $this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() );
25 $this->uqb = $this->db->newUpdateQueryBuilder();
26 }
27
28 private function assertSQL( $expected, $fname ) {
29 $this->uqb->caller( $fname )->execute();
30 $actual = $this->db->getLastSqls();
31 $actual = preg_replace( '/ +/', ' ', $actual );
32 $actual = rtrim( $actual, " " );
33 $this->assertEquals( $expected, $actual );
34 }
35
36 public function testSet() {
37 $this->uqb
38 ->table( 'a' )
39 ->set( [ 'f' => 'g' ] )
40 ->andSet( [ 'd' => 'l' ] )
41 ->where( '1' )
42 ->andWhere( '2' )
43 ->conds( '3' );
44 $this->assertSQL( "UPDATE a SET f = 'g',d = 'l' WHERE (1) AND (2) AND (3)", __METHOD__ );
45 }
46
47 public function testConflictingSet() {
48 // T288882: the empty set is the right answer
49 $this->uqb
50 ->update( 't' )
51 ->set( [ 'f' => 'g' ] )
52 ->andSet( [ 'f' => 'l' ] )
53 ->where( [ 'k' => 'v1' ] );
54 $this->assertSQL( "UPDATE t SET f = 'l' WHERE k = 'v1'", __METHOD__ );
55 }
56
57 public function testCondsEtc() {
58 $this->uqb
59 ->table( 'a' )
60 ->set( 'f' )
61 ->where( '1' )
62 ->andWhere( '2' )
63 ->conds( '3' );
64 $this->assertSQL( 'UPDATE a SET f WHERE (1) AND (2) AND (3)', __METHOD__ );
65 }
66
67 public function testConflictingConds() {
68 // T288882: the empty set is the right answer
69 $this->uqb
70 ->update( '1' )
71 ->set( 'a' )
72 ->where( [ 'k' => 'v1' ] )
73 ->andWhere( [ 'k' => 'v2' ] );
74 $this->assertSQL( 'UPDATE 1 SET a WHERE k = \'v1\' AND (k = \'v2\')', __METHOD__ );
75 }
76
77 public function testCondsAllRows() {
78 $this->uqb
79 ->update( '1' )
80 ->set( 'a' )
81 ->where( ISQLPlatform::ALL_ROWS );
82 $this->assertSQL( 'UPDATE 1 SET a', __METHOD__ );
83 }
84
85 public function testIgnore() {
86 $this->uqb
87 ->update( 'f' )
88 ->set( 't' )
89 ->where( 'c' )
90 ->ignore();
91 $this->assertSQL( 'UPDATE IGNORE f SET t WHERE (c)', __METHOD__ );
92 }
93
94 public function testOption() {
95 $this->uqb
96 ->update( 't' )
97 ->set( 'f' )
98 ->where( 'c' )
99 ->option( 'IGNORE' );
100 $this->assertSQL( 'UPDATE IGNORE t SET f WHERE (c)', __METHOD__ );
101 }
102
103 public function testOptions() {
104 $this->uqb
105 ->update( 't' )
106 ->set( 'f' )
107 ->where( 'c' )
108 ->options( [ 'IGNORE' ] );
109 $this->assertSQL( 'UPDATE IGNORE t SET f WHERE (c)', __METHOD__ );
110 }
111
112 public function testExecute() {
113 $this->uqb->update( 't' )->set( 'f' )->where( 'c' )->caller( __METHOD__ );
114 $this->uqb->execute();
115 $this->assertEquals( 'UPDATE t SET f WHERE (c)', $this->db->getLastSqls() );
116 }
117
118 public function testGetQueryInfo() {
119 $this->uqb
120 ->update( 't' )
121 ->ignore()
122 ->set( [ 'f' => 'g' ] )
123 ->andSet( [ 'd' => 'l' ] )
124 ->where( [ 'a' => 'b' ] )
125 ->caller( 'foo' );
126 $this->assertEquals(
127 [
128 'table' => 't',
129 'set' => [ 'f' => 'g', 'd' => 'l' ],
130 'conds' => [ 'a' => 'b' ],
131 'options' => [ 'IGNORE' ],
132 'caller' => 'foo',
133 ],
134 $this->uqb->getQueryInfo() );
135 }
136
137 public function testQueryInfo() {
138 $this->uqb->queryInfo(
139 [
140 'table' => 't',
141 'set' => [ 'f' => 'g' ],
142 'conds' => [ 'a' => 'b' ],
143 'options' => [ 'IGNORE' ],
144 ]
145 );
146 $this->assertSQL( "UPDATE IGNORE t SET f = 'g' WHERE a = 'b'", __METHOD__ );
147 }
148 }