Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4111834
MockDatabase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
MockDatabase.php
View Options
<?php
namespace
MediaWiki\Tests
;
use
MediaWiki\Logger\LoggerFactory
;
use
Wikimedia\ObjectCache\HashBagOStuff
;
use
Wikimedia\ObjectCache\WANObjectCache
;
use
Wikimedia\Rdbms\Database
;
use
Wikimedia\Rdbms\FakeResultWrapper
;
use
Wikimedia\Rdbms\IDatabase
;
use
Wikimedia\Rdbms\QueryBuilderFromRawSql
;
use
Wikimedia\Rdbms\QueryStatus
;
use
Wikimedia\Rdbms\Replication\ReplicationReporter
;
use
Wikimedia\Rdbms\TransactionProfiler
;
/**
* A default-constructible Database subclass that doesn't access any services so
* should be safe to use in unit tests.
*
* The best way to validate a database query is by actually running it against
* a real database. So the acceptable use cases for this class are narrow. It
* may be useful for testing query-building services, or queries against tables
* that don't actually exist. It can be used for service injection when there
* is only a minor dependency on the database, for example for quoting.
*
* Query collection and fake result generation could easily be added if there is
* a need for that.
*
* @since 1.42
*/
class
MockDatabase
extends
Database
{
/** @var int */
private
$nextInsertId
=
0
;
public
function
__construct
(
$options
=
[]
)
{
// Use a real logger because tests need logging, maybe more than production
$logger
=
$options
[
'logger'
]
??
LoggerFactory
::
getInstance
(
'MockDatabase'
);
parent
::
__construct
(
$options
+
[
'cliMode'
=>
true
,
'agent'
=>
''
,
'profiler'
=>
null
,
'trxProfiler'
=>
new
TransactionProfiler
,
'logger'
=>
$logger
,
'errorLogger'
=>
$logger
,
'deprecationLogger'
=>
$logger
,
'dbname'
=>
'test'
,
'schema'
=>
''
,
'tablePrefix'
=>
''
,
'password'
=>
''
,
'flags'
=>
0
,
'serverName'
=>
''
,
]
);
$this
->
replicationReporter
=
new
ReplicationReporter
(
$options
[
'topologyRole'
]
??
IDatabase
::
ROLE_STREAMING_MASTER
,
$logger
,
$options
[
'srvCache'
]
??
new
WANObjectCache
(
[
'cache'
=>
new
HashBagOStuff
(),
'logger'
=>
$logger
,
]
)
);
}
protected
function
open
(
$server
,
$user
,
$password
,
$db
,
$schema
,
$tablePrefix
)
{
}
public
function
isOpen
()
{
return
true
;
}
public
function
indexInfo
(
$table
,
$index
,
$fname
=
__METHOD__
)
{
throw
new
\RuntimeException
(
'Not implemented'
);
}
public
function
strencode
(
$s
)
{
return
addslashes
(
$s
);
}
protected
function
closeConnection
()
{
}
protected
function
doSingleStatementQuery
(
string
$sql
):
QueryStatus
{
$query
=
QueryBuilderFromRawSql
::
buildQuery
(
$sql
,
0
);
if
(
$query
->
isWriteQuery
()
)
{
return
new
QueryStatus
(
true
,
0
,
0
,
''
);
}
else
{
return
new
QueryStatus
(
new
FakeResultWrapper
(
[]
),
0
,
0
,
''
);
}
}
public
function
tableExists
(
$table
,
$fname
=
__METHOD__
)
{
return
true
;
}
protected
function
lastInsertId
()
{
return
$this
->
nextInsertId
++;
}
public
function
fieldInfo
(
$table
,
$field
)
{
throw
new
\RuntimeException
(
'Not implemented'
);
}
public
function
getType
()
{
return
'mock'
;
}
public
function
lastErrno
()
{
return
0
;
}
public
function
lastError
()
{
return
''
;
}
public
function
getSoftwareLink
()
{
return
''
;
}
public
function
getServerVersion
()
{
return
''
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Tue, Aug 18, 22:34 (4 h, 51 m ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
14/10/7f8c77e70f69555cb1ca7d0538e3
Default Alt Text
MockDatabase.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment