Debian Codegraph @

tests/phpunit/unit/includes/libs/rdbms/querybuilder/InsertQueryBuilderTest.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
9 /**
10 * @covers \Wikimedia\Rdbms\InsertQueryBuilder
11 */
12 class InsertQueryBuilderTest extends TestCase {
13 use MediaWikiCoversValidator;
14
15 /** @var DatabaseTestHelper */
16 private $db;
17
18 /** @var InsertQueryBuilderTest */
19 private $iqb;
20
21 protected function setUp(): void {
22 $this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() );
23 $this->iqb = $this->db->newInsertQueryBuilder();
24 }
25
26 private function assertSQL( $expected, $fname ) {
27 $this->iqb->caller( $fname )->execute();
28 $actual = $this->db->getLastSqls();
29 $actual = preg_replace( '/ +/', ' ', $actual );
30 $actual = rtrim( $actual, " " );
31 $this->assertEquals( $expected, $actual );
32 }
33
34 public function testSimpleInsert() {
35 $this->iqb
36 ->insertInto( 'a' )
37 ->row( [ 'f' => 'g', 'd' => 'l' ] );
38 $this->assertSQL( "INSERT INTO a (f,d) VALUES ('g','l')", __METHOD__ );
39 }
40
41 public function testIgnore() {
42 $this->iqb
43 ->insertInto( 'a' )
44 ->ignore()
45 ->row( [ 'f' => 'g', 'd' => 'l' ] );
46 $this->assertSQL( "INSERT IGNORE INTO a (f,d) VALUES ('g','l')", __METHOD__ );
47 }
48
49 public function testUpsert() {
50 $this->iqb
51 ->insertInto( 'a' )
52 ->row( [ 'f' => 'g', 'd' => 'l' ] )
53 ->onDuplicateKeyUpdate()
54 ->uniqueIndexFields( [ 'd' ] )
55 ->set( [ 'f' => 'm' ] );
56 $this->assertSQL(
57 "BEGIN; UPDATE a SET f = 'm' WHERE (d = 'l'); INSERT INTO a (f,d) VALUES ('g','l'); COMMIT",
58 __METHOD__
59 );
60 }
61
62 public function testUpsertWithStringKey() {
63 $this->iqb
64 ->insertInto( 'a' )
65 ->row( [ 'f' => 'g', 'd' => 'l' ] )
66 ->onDuplicateKeyUpdate()
67 ->uniqueIndexFields( 'd' )
68 ->set( [ 'f' => 'm' ] );
69 $this->assertSQL(
70 "BEGIN; UPDATE a SET f = 'm' WHERE (d = 'l'); INSERT INTO a (f,d) VALUES ('g','l'); COMMIT",
71 __METHOD__
72 );
73 }
74
75 public function testOption() {
76 $this->iqb
77 ->insertInto( 't' )
78 ->row( [ 'f' => 'g' ] )
79 ->option( 'IGNORE' );
80 $this->assertSQL( "INSERT IGNORE INTO t (f) VALUES ('g')", __METHOD__ );
81 }
82
83 public function testOptions() {
84 $this->iqb
85 ->insertInto( 't' )
86 ->row( [ 'f' => 'g' ] )
87 ->options( [ 'IGNORE' ] );
88 $this->assertSQL( "INSERT IGNORE INTO t (f) VALUES ('g')", __METHOD__ );
89 }
90
91 public function testExecute() {
92 $this->iqb->insertInto( 't' )->rows( [ 'a' => 'b' ] )->caller( __METHOD__ );
93 $this->iqb->execute();
94 $this->assertEquals( "INSERT INTO t (a) VALUES ('b')", $this->db->getLastSqls() );
95 }
96
97 public function testGetQueryInfo() {
98 $this->iqb
99 ->insertInto( 't' )
100 ->ignore()
101 ->row( [ 'a' => 'b', 'd' => 'l' ] )
102 ->caller( 'foo' );
103 $this->assertEquals(
104 [
105 'table' => 't',
106 'rows' => [ [ 'a' => 'b', 'd' => 'l' ] ],
107 'options' => [ 'IGNORE' ],
108 'upsert' => false,
109 'set' => [],
110 'uniqueIndexFields' => [],
111 'caller' => 'foo',
112 ],
113 $this->iqb->getQueryInfo() );
114 }
115
116 public function testGetQueryInfoUpsert() {
117 $this->iqb
118 ->insertInto( 't' )
119 ->row( [ 'f' => 'g', 'd' => 'l' ] )
120 ->onDuplicateKeyUpdate()
121 ->uniqueIndexFields( [ 'd' ] )
122 ->set( [ 'f' => 'm' ] )
123 ->caller( 'foo' );
124 $this->assertEquals(
125 [
126 'table' => 't',
127 'rows' => [ [ 'f' => 'g', 'd' => 'l' ] ],
128 'upsert' => true,
129 'set' => [ 'f' => 'm' ],
130 'uniqueIndexFields' => [ 'd' ],
131 'options' => [],
132 'caller' => 'foo'
133 ],
134 $this->iqb->getQueryInfo() );
135 }
136
137 public function testQueryInfo() {
138 $this->iqb->queryInfo(
139 [
140 'table' => 't',
141 'rows' => [ [ 'f' => 'g', 'd' => 'l' ] ],
142 'options' => [ 'IGNORE' ],
143 ]
144 );
145 $this->assertSQL( "INSERT IGNORE INTO t (f,d) VALUES ('g','l')", __METHOD__ );
146 }
147
148 public function testQueryInfoUpsert() {
149 $this->iqb->queryInfo(
150 [
151 'table' => 't',
152 'rows' => [ [ 'f' => 'g', 'd' => 'l' ] ],
153 'upsert' => true,
154 'set' => [ 'f' => 'm' ],
155 'uniqueIndexFields' => [ 'd' ],
156 ]
157 );
158 $this->assertSQL(
159 "BEGIN; UPDATE t SET f = 'm' WHERE (d = 'l'); INSERT INTO t (f,d) VALUES ('g','l'); COMMIT",
160 __METHOD__
161 );
162 }
163 }