File: UpdateQueryBuilderTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (148 lines) | stat: -rw-r--r-- 3,605 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
<?php

namespace Wikimedia\Tests\Rdbms;

use DatabaseTestHelper;
use MediaWikiCoversValidator;
use PHPUnit\Framework\TestCase;
use Wikimedia\Rdbms\Platform\ISQLPlatform;
use Wikimedia\Rdbms\UpdateQueryBuilder;

/**
 * @covers \Wikimedia\Rdbms\UpdateQueryBuilder
 */
class UpdateQueryBuilderTest extends TestCase {
	use MediaWikiCoversValidator;

	/** @var DatabaseTestHelper */
	private $db;

	/** @var UpdateQueryBuilder */
	private $uqb;

	protected function setUp(): void {
		$this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() );
		$this->uqb = $this->db->newUpdateQueryBuilder();
	}

	private function assertSQL( $expected, $fname ) {
		$this->uqb->caller( $fname )->execute();
		$actual = $this->db->getLastSqls();
		$actual = preg_replace( '/ +/', ' ', $actual );
		$actual = rtrim( $actual, " " );
		$this->assertEquals( $expected, $actual );
	}

	public function testSet() {
		$this->uqb
			->table( 'a' )
			->set( [ 'f' => 'g' ] )
			->andSet( [ 'd' => 'l' ] )
			->where( '1' )
			->andWhere( '2' )
			->conds( '3' );
		$this->assertSQL( "UPDATE a SET f = 'g',d = 'l' WHERE (1) AND (2) AND (3)", __METHOD__ );
	}

	public function testConflictingSet() {
		// T288882: the empty set is the right answer
		$this->uqb
			->update( 't' )
			->set( [ 'f' => 'g' ] )
			->andSet( [ 'f' => 'l' ] )
			->where( [ 'k' => 'v1' ] );
		$this->assertSQL( "UPDATE t SET f = 'l' WHERE k = 'v1'", __METHOD__ );
	}

	public function testCondsEtc() {
		$this->uqb
			->table( 'a' )
			->set( 'f' )
			->where( '1' )
			->andWhere( '2' )
			->conds( '3' );
		$this->assertSQL( 'UPDATE a SET f WHERE (1) AND (2) AND (3)', __METHOD__ );
	}

	public function testConflictingConds() {
		// T288882: the empty set is the right answer
		$this->uqb
			->update( '1' )
			->set( 'a' )
			->where( [ 'k' => 'v1' ] )
			->andWhere( [ 'k' => 'v2' ] );
		$this->assertSQL( 'UPDATE 1 SET a WHERE k = \'v1\' AND (k = \'v2\')', __METHOD__ );
	}

	public function testCondsAllRows() {
		$this->uqb
			->update( '1' )
			->set( 'a' )
			->where( ISQLPlatform::ALL_ROWS );
		$this->assertSQL( 'UPDATE 1 SET a', __METHOD__ );
	}

	public function testIgnore() {
		$this->uqb
			->update( 'f' )
			->set( 't' )
			->where( 'c' )
			->ignore();
		$this->assertSQL( 'UPDATE IGNORE f SET t WHERE (c)', __METHOD__ );
	}

	public function testOption() {
		$this->uqb
			->update( 't' )
			->set( 'f' )
			->where( 'c' )
			->option( 'IGNORE' );
		$this->assertSQL( 'UPDATE IGNORE t SET f WHERE (c)', __METHOD__ );
	}

	public function testOptions() {
		$this->uqb
			->update( 't' )
			->set( 'f' )
			->where( 'c' )
			->options( [ 'IGNORE' ] );
		$this->assertSQL( 'UPDATE IGNORE t SET f WHERE (c)', __METHOD__ );
	}

	public function testExecute() {
		$this->uqb->update( 't' )->set( 'f' )->where( 'c' )->caller( __METHOD__ );
		$this->uqb->execute();
		$this->assertEquals( 'UPDATE t SET f WHERE (c)', $this->db->getLastSqls() );
	}

	public function testGetQueryInfo() {
		$this->uqb
			->update( 't' )
			->ignore()
			->set( [ 'f' => 'g' ] )
			->andSet( [ 'd' => 'l' ] )
			->where( [ 'a' => 'b' ] )
			->caller( 'foo' );
		$this->assertEquals(
			[
				'table' => 't',
				'set' => [ 'f' => 'g', 'd' => 'l' ],
				'conds' => [ 'a' => 'b' ],
				'options' => [ 'IGNORE' ],
				'caller' => 'foo',
			],
			$this->uqb->getQueryInfo() );
	}

	public function testQueryInfo() {
		$this->uqb->queryInfo(
			[
				'table' => 't',
				'set' => [ 'f' => 'g' ],
				'conds' => [ 'a' => 'b' ],
				'options' => [ 'IGNORE' ],
			]
		);
		$this->assertSQL( "UPDATE IGNORE t SET f = 'g' WHERE a = 'b'", __METHOD__ );
	}
}