Skip to content

Commit

Permalink
some improvements and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raaghu committed Feb 1, 2017
1 parent 8561c4b commit 118323c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/Tests/PersistUnit/DBInsert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace PhpPlatform\Tests\PersistUnit;

class DBInsert extends \PHPUnit_Extensions_Database_Operation_Insert{

/**
* @param PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection
* @param DataSet $dataSet
*/
public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection,\PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
{
$connection->getConnection()->query("SET foreign_key_checks = 0");

// execute the data files first
$dataFiles = $dataSet->getDataFiles();
foreach ($dataFiles as $dataFile){
$dataFileContent = file_get_contents($dataFile);
$connection->getConnection()->exec($dataFileContent);
}

// execute data set
parent::execute($connection, $dataSet);
$connection->getConnection()->query("SET foreign_key_checks = 1");
}

}
18 changes: 18 additions & 0 deletions src/Tests/PersistUnit/DataSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PhpPlatform\Tests\PersistUnit;

class DataSet extends \PHPUnit_Extensions_Database_DataSet_MysqlXmlDataSet{

private $datafiles = array();

function __construct($datafiles,$xmlFile){
$this->datafiles = $datafiles;
parent::__construct($xmlFile);
}

function getDataFiles(){
return $this->datafiles;
}

}
27 changes: 25 additions & 2 deletions src/Tests/PersistUnit/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpPlatform\Config\SettingsCache;
use PhpPlatform\JSONCache\Cache;
use PhpPlatform\Errors\ErrorHandler;
use PhpPlatform\Persist\TransactionManager;

abstract class ModelTest extends DBUnitTestcase{

Expand All @@ -29,6 +30,13 @@ protected static function getSchemaFiles(){
return array();
}

/**
* @return string[] of schema sql files to run before tests
*/
protected static function getDataFiles(){
return array();
}

/**
* @return string[] of data set xml files to create initial data
*/
Expand Down Expand Up @@ -91,7 +99,7 @@ public static function setUpBeforeClass(){
MockSettings::setSettings('php-platform/persist', "outputDateTimeFormat", "%Y-%m-%d %H:%i:%S");

$logFile = getenv('sqlLogFile');
if(!$logFile){
if($logFile){
MockSettings::setSettings('php-platform/persist', "sqlLogFile", $logFile);
}
self::setTriggers(array());
Expand All @@ -111,6 +119,13 @@ public static function setUpBeforeClass(){
}

public static function tearDownAfterClass(){

$transaction = TransactionManager::$transaction;
while ($transaction != null){
TransactionManager::abortTransaction();
$transaction = TransactionManager::$transaction;
}

// create pdo without database
$_pdo = new PDO('mysql:host='.self::$_connectionParams['host'].';port='.self::$_connectionParams['port'], self::$_connectionParams['username'],self::$_connectionParams['password']);

Expand All @@ -123,6 +138,14 @@ public static function tearDownAfterClass(){
unset($result);
unset($_pdo);
}

public function getSetUpOperation()
{
return new \PHPUnit_Extensions_Database_Operation_Composite(array(
\PHPUnit_Extensions_Database_Operation_Factory::TRUNCATE(),
new DBInsert()
));
}

public function getConnection(){
return $this->createDefaultDBConnection(self::$_pdo);
Expand All @@ -143,7 +166,7 @@ public function getDataset($seedxml = null){
$tmpFile = tempnam(sys_get_temp_dir(),self::$_databaseName);
file_put_contents($tmpFile,$seedContent);

return $this->createMySQLXMLDataSet($tmpFile);
return new DataSet(static::getDataFiles(), $tmpFile);
}

public function getDatasetValue($table,$row,$column = null){
Expand Down

0 comments on commit 118323c

Please sign in to comment.