File: UnionQueryBuilderTest.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 (103 lines) | stat: -rw-r--r-- 2,632 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
<?php

namespace Wikimedia\Tests\Rdbms;

use DatabaseTestHelper;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\UnionQueryBuilder;

/**
 * @covers \Wikimedia\Rdbms\UnionQueryBuilder
 */
class UnionQueryBuilderTest extends \MediaWikiUnitTestCase {
	/** @var DatabaseTestHelper */
	private $db;

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

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

	private function assertSql( $expected ) {
		$actual = $this->uqb->getSQL();
		$actual = preg_replace( '/ +/', ' ', $actual );
		$actual = rtrim( $actual, " " );
		$this->assertEquals( $expected, $actual );
	}

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

	public function testGetSql() {
		$this->uqb
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't1' )
			)
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't2' )
			)
			->orderBy( 'f', UnionQueryBuilder::SORT_DESC )
			->limit( 10 )
			->offset( 20 )
			->caller( __METHOD__ );
		$this->assertSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 ) ' .
			'ORDER BY f DESC LIMIT 20,10' );
	}

	public function testFetchResultSet() {
		$this->uqb
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't1' )
			)
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't2' )
			)
			->caller( __METHOD__ );
		$res = $this->uqb->fetchResultSet();
		$this->assertInstanceOf( IResultWrapper::class, $res );
		$this->assertLastSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 )' );
	}

	public function testFetchField() {
		$this->uqb
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't1' )
			)
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't2' )
			)
			->caller( __METHOD__ );
		$f = $this->uqb->fetchField();
		$this->assertFalse( $f );
		$this->assertLastSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 ) LIMIT 1' );
	}

	public function testFetchRow() {
		$this->uqb
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't1' )
			)
			->add( $this->db->newSelectQueryBuilder()
				->select( 'f' )
				->from( 't2' )
			)
			->caller( __METHOD__ );
		$row = $this->uqb->fetchRow();
		$this->assertFalse( $row );
		$this->assertLastSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 ) LIMIT 1' );
	}
}