Methods summary
public
|
#
__construct( array $aDsn, array $aPdoOptions, integer $sDbBuildFile, string $iMaxDbToKeep = 3, string $sTCName = null, array $aTCData = array(), string $sTCDataName = '', Psr\Log\LoggerInterface $oLogger = null )
Build a database for tests and drop too old ones.
Build a database for tests and drop too old ones.
$aDsn structure:
array(
'driver' => 'pgsql|mysql|…',
'hostname' => '…',
'port' => '…',
'dbname' => '…',
'username' => '…',
'password' => '…',
)
Parameters
- $aDsn
- Data source name of DB to build.
- $aPdoOptions
- Driver-specific options for PDO connection.
- $sDbBuildFile
- $iMaxDbToKeep Max number of test databases to keep.
- $iMaxDbToKeep
- $sDbBuildFile SQL directives to build database (@see loadSqlBuildFile())
- $sTCName
- PHPUnit_Framework_TestCase's name.
- $aTCData
- PHPUnit_Framework_TestCase's data.
- $sTCDataName
- PHPUnit_Framework_TestCase's dataName parameter.
- $oLogger
- Optional PSR-3 logger.
|
private
PDO
|
#
getNewPdoInstance( string $sDbUser, string $sDbName )
Returns a new PDO instance.
Returns a new PDO instance.
Parameters
Returns
PDO PDO
|
protected
|
#
loadSqlBuildFile( string $sDbBuildFile, string $sDbUser = '', string $sDbName = '' )
Load SQL directives from config/build file, describing how to create a fresh
database, roles/users and schemas, with listing of fixtures to load.
Load SQL directives from config/build file, describing how to create a fresh
database, roles/users and schemas, with listing of fixtures to load.
Config/build file is a PHP file returning a list of array('db-user',
'db-name', 'SQL commands or SQL filename'). All filenames must match following
regexp: /(\.sql|\.gz)$/i.
Available/injected variables: $sDbUser, $sDbName.
Build file example here: /doc/db-build-file-example.php
Parameters
- $sDbBuildFile
- SQL directives
- $sDbUser
- Optional username injected into $sInitDbFile
- $sDbName
- Optional DB name injected into $sInitDbFile
SuppressWarnings(PHPMD.UnusedFormalParameter)
|
private
|
#
execSql( string $sSql, string $sDbUser, string $sDbName )
Executes specified SQL statement.
Executes specified SQL statement.
Parameters
- $sSql
- One or more SQL statements
- $sDbUser
- $sDbName
|
private
|
#
loadBigSqlDumpFile( string $sFilePath, string $sDbUser, string $sDbName )
Load SQL dump content for file exceeding 1 Mio.
Load SQL dump content for file exceeding 1 Mio.
Parameters
- $sFilePath
- Path to raw or gzipped SQL file.
- $sDbUser
- $sDbName
See
|
protected
|
#
loadSqlDumpFile( string $sFilePath, string $sDbUser = '', string $sDbName = '' )
Load SQL dump file, possibly gzipped (.gz).
Load SQL dump file, possibly gzipped (.gz).
Parameters
- $sFilePath
- Dump file
- $sDbUser
- By default the user specified in constructor.
- $sDbName
- By default the DB name specified in constructor.
|
protected
|
#
loadSqlBuildArray( array $aSQLDirectives )
Load SQL directives in specified array.
Load SQL directives in specified array.
SQL directives are a list of array('db-user', 'db-name', 'SQL commands or SQL
filename'). All filenames must match following regexp:
/(\.sql|\.gz)$/i
Example here: /doc/db-build-file-example.php
Parameters
- $aSQLDirectives
- SQL directives to build database
See
|
private
array
|
#
getAllDb( string $sPrefixDb )
List all test DBs created with following name pattern: /^{$sPrefixDb}_[0-9]+$/.
List all test DBs created with following name pattern: /^{$sPrefixDb}_[0-9]+$/.
Parameters
Returns
array list of all test DBs
|
protected
array
|
#
pdoFetchAll( string $sQuery, array $aValues = array() )
Fetch all rows.
Parameters
- $sQuery
- $aValues
- Values to bind to the SQL statement.
Returns
array an associative array
|
private
|
#
dropOldDbs( string $sCurrentDbName )
Drop old test DBs.
Parameters
- $sCurrentDbName
- Current DB name, not to remove.
|
protected
|
#
assertQueryReturnsNoRows( string $sQuery )
Asserts that SQL query doesn't return any rows.
Asserts that SQL query doesn't return any rows.
Parameters
Throws
PHPUnit_Framework_AssertionFailedError \PHPUnit_Framework_AssertionFailedError
|
protected
|
#
assertQueryResultEqualsCsv( string $sQuery, string $sCsvPath, string $sDelimiter = ',', string $sEnclosure = '"' )
Asserts that SQL query result is equal to CSV file content.
Asserts that SQL query result is equal to CSV file content.
In CSV files, following field's values are converted:
- '∅' ⇒ null
- 't' ⇒ true
- 'f' ⇒ false
$sCsvPath sample content:
id,name,description
250,FR,France
826,GB,United Kingdom
840,US,United States
Parameters
- $sQuery
- $sCsvPath
- CSV file whose first line contains headers
- $sDelimiter
- CSV field delimiter
- $sEnclosure
- CSV field enclosure
Throws
PHPUnit_Framework_AssertionFailedError \PHPUnit_Framework_AssertionFailedError
|
protected
string
|
#
convertQuery2Csv( string $sQuery, string $sDelimiter = ',', string $sEnclosure = '"' )
Converts SQL query result to CSV string. First CSV line contains headers.
Converts SQL query result to CSV string. First CSV line contains headers.
Returned CSV example:
id,name,description
250,FR,France
826,GB,United Kingdom
840,US,United States
Parameters
- $sQuery
- SQL query, typically SELECT… FROM…
- $sDelimiter
- CSV field delimiter
- $sEnclosure
- CSV field enclosure
Returns
string string
|
public
PHPUnit_Framework_MockObject_MockObject|PDOStatement
|
#
getPdoStmtMock( string $sQuery, array $aData )
Returns a mocked \PDOStatement instance according to specified query, whose
fetch method returns either a user callback or a callback consuming line per
line a user specified CSV file.
Returns a mocked \PDOStatement instance according to specified query, whose
fetch method returns either a user callback or a callback consuming line per
line a user specified CSV file.
Allows to mock a PDO instance for several queries. Each query should be a key
of $aData array parameter. Values are callbacks or CSV filenames.
All queries are internally normalized to simplify matching.
In CSV files, following field's values are converted:
- '∅' ⇒ null
- 't' ⇒ true
- 'f' ⇒ false
Example usage in a test method:
$oMockPDO = $this->getMock('Himedia\PDOTools\Mocks\PDO', array('query'));
$that = $this;
$oPDOMock->expects($this->any())->method('query')->will(
$this->returnCallback(
function ($sQuery) use ($that) {
return $that->getPdoStmtMock(
$sQuery,
array(
'SELECT … FROM A' => '/path/to/csv',
'SELECT … FROM B' => function () {
static $i = 0;
return ++$i > 10 ? false : array('id' => $i, 'name' => md5(rand()));
}
)
);
}
)
);
Parameters
- $sQuery
- SQL query behind \PDOStatement instance
- $aData
- Associative array describing for each PDO query which callback to execute
Returns
PHPUnit_Framework_MockObject_MockObject|PDOStatement \PHPUnit_Framework_MockObject_MockObject|\PDOStatement
See
|
Properties summary
private static
array
|
$aBuiltDbs
List of DB's name already built (PHPUnit_Framework_TestCase are instantiated
more than once).
List of DB's name already built (PHPUnit_Framework_TestCase are instantiated
more than once).
|
|
protected
PDO
|
$oBuiltDbPdo
Backend PDO instance of built DB.
Backend PDO instance of built DB.
|
|
protected
string
|
$sPdoDriverName
PDO driver name, e.g. 'pgsql' or 'mysql'.
PDO driver name, e.g. 'pgsql' or 'mysql'.
|
|
protected
string
|
$sDbHostname
|
|
protected
integer
|
$iDbPort
|
|
protected
string
|
$sDbName
|
|
protected
string
|
$sDbUser
|
|
protected
string
|
$sDbPassword
|
|
protected
array
|
$aPdoOptions
Default PDO options on new instantiation.
Default PDO options on new instantiation.
|
#array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_TIMEOUT => 5
) |
protected
integer
|
$iMaxDbToKeep
Max number of test DBs to keep.
Max number of test DBs to keep.
|
|