Skip to content

Commit

Permalink
Remove dependency on the deprecated DbUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
blackcoder87 committed Jun 3, 2021
1 parent a27903c commit 83dfd16
Show file tree
Hide file tree
Showing 33 changed files with 498 additions and 511 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"require-dev": {
"maximebf/debugbar": "^1.15",
"phpunit/phpunit": "6.*",
"phpunit/dbunit": ">=3.0"
"symfony/yaml": "^3.3.6"
},
"autoload": {
"psr-0": { "": "application/libraries/" },
Expand Down
107 changes: 21 additions & 86 deletions tests/PHPUnit/Ilch/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace PHPUnit\Ilch;

use Ilch\Registry as Registry;
use Ilch\Database\Factory as Factory;
use Ilch\Registry;
use Ilch\Database\Factory;

/**
* Base class for database test cases for Ilch.
Expand All @@ -16,7 +16,7 @@
*
* @package ilch_phpunit
*/
abstract class DatabaseTestCase extends \PHPUnit\DbUnit\TestCase
abstract class DatabaseTestCase extends \PHPUnit\Framework\TestCase
{
/** @var int don't automatically provision - for individual proovsioning */
const PROVISION_DISABLED = 0;
Expand All @@ -33,12 +33,10 @@ abstract class DatabaseTestCase extends \PHPUnit\DbUnit\TestCase
static protected $configData = [];

/**
* Only instantiate pdo once for test clean-up/fixture load
*
* @static Static so we can don't have to connect for every test again.
* @var \PDO
* Files used for creating the database schema
* @var array
*/
static private $pdo = null;
protected static $dbSchemaFiles = [__DIR__ . '/_files/db_schema.sql'];

/**
* @var bool
Expand All @@ -51,36 +49,19 @@ abstract class DatabaseTestCase extends \PHPUnit\DbUnit\TestCase
*/
static protected $fillDbOnSetUp = self::PROVISION_ON_SETUP;

/**
* Files used for creating the database schema
* @var array
*/
protected static $dbSchemaFiles = [__DIR__ . '/_files/db_schema.sql'];

/**
* Instantiated PHPUnit_Extensions_Database_DB_IDatabaseConnection for the tests.
*
* @var \PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
private $conn = null;

/**
* The db instance to test with.
*
* @var \Ilch\Database\MySQL
*/
protected $db = null;
protected $db;

/**
* Save provisioning state for PROVISION_ON_SETUP_BEFORE_CLASS
* @var bool
*/
private $dbProvisioned;

/**
* Setup config
* This method is called before the first test of this test class is run.
*
* @return void
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
TestHelper::setConfigInRegistry(static::$configData);
Expand All @@ -97,8 +78,8 @@ public static function setUpBeforeClass()
if (static::$dropTablesOnProvision) {
static::dropTables($db);
}
$db->queryMulti(static::getSchemaSQLQueries());
}
$db->queryMulti(static::getSchemaSQLQueries());
}

/**
Expand All @@ -109,14 +90,12 @@ protected function setUp()
$this->db = Registry::get('db');

if ($this->db === false) {
$this->markTestIncomplete('Necessary DB configuration is not set.');
self::markTestIncomplete('Necessary DB configuration is not set.');
parent::setUp();
return;
}

/*
* Deleting all tables from the db and setting up the db using the given schema
*/
// Deleting all tables from the db and setting up the db using the given schema
if (static::$fillDbOnSetUp === self::PROVISION_ON_SETUP) {
if (static::$dropTablesOnProvision) {
static::dropTables($this->db);
Expand All @@ -128,40 +107,14 @@ protected function setUp()
}

/**
* Creates the db connection to the test database.
* This method is called after the last test of this test class is run.
*
* @return \PHPUnit_Extensions_Database_DB_IDatabaseConnection|null Returns null if the necessary config was not set
* @return void
*/
final public function getConnection()
public static function tearDownAfterClass(): void
{
$dbData = [];
$config = Registry::get('config');

foreach (['dbEngine', 'dbHost', 'dbUser', 'dbPassword', 'dbName', 'dbPrefix'] as $configKey) {
/*
* Using the data for the db from the config.
* We check if special config variables for this test execution exist.
* If so we gonna use it. Otherwise we have to skip the tests.
*/
if ($config->get($configKey) !== null) {
$dbData[$configKey] = $config->get($configKey);
} else {
$this->markTestSkipped('Necessary DB configuration is not set.');
}
}

$dsn = strtolower($dbData['dbEngine']) . ':dbname=' . $dbData['dbName'] . ';host=' . $dbData['dbHost'];
$dbData['dbDsn'] = $dsn;

if ($this->conn === null) {
if (self::$pdo === null) {
self::$pdo = new \PDO($dbData['dbDsn'], $dbData['dbUser'], $dbData['dbPassword']);
}

$this->conn = $this->createDefaultDBConnection(self::$pdo, $dbData['dbName']);
}

return $this->conn;
$db = Registry::get('db');
static::dropTables($db);
}

/**
Expand All @@ -171,14 +124,15 @@ final public function getConnection()
*/
protected static function getSchemaSQLQueries()
{
return array_reduce(self::$dbSchemaFiles, function($carry, $fileName) {
return array_reduce(self::$dbSchemaFiles, static function($carry, $fileName) {
$carry .= file_get_contents($fileName);
return $carry;
}, '');
}

/**
* Deleting all tables from the db
*
* @param \Ilch\Database\Mysql $db
*/
protected static function dropTables(\Ilch\Database\Mysql $db)
Expand All @@ -191,23 +145,4 @@ protected static function dropTables(\Ilch\Database\Mysql $db)
$db->query($sql);
}
}

/**
* Returns the database operation executed in test setup.
*
* @return \PHPUnit_Extensions_Database_Operation_IDatabaseOperation
*/
protected function getSetUpOperation()
{
if (static::$fillDbOnSetUp === self::PROVISION_ON_SETUP_BEFORE_CLASS) {
if ($this->dbProvisioned) {
return \PHPUnit\DbUnit\Operation\Factory::NONE();
}
$this->dbProvisioned = true;
} elseif (static::$fillDbOnSetUp === self::PROVISION_DISABLED) {
return \PHPUnit\DbUnit\Operation\Factory::NONE();
}

return \PHPUnit\DbUnit\Operation\Factory::CLEAN_INSERT();
}
}
95 changes: 95 additions & 0 deletions tests/PHPUnit/Ilch/PhpunitDataset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace PHPUnit\Ilch;

use Symfony\Component\Yaml\Yaml;

/**
* Class PhpunitDataset
* @package PHPUnit\Ilch
*/
class PhpunitDataset extends DatabaseTestCase
{
public function __construct($db)
{
parent::__construct();
$this->db = $db;
}

/**
* Load information from multiple files (yml).
*
* @param array $fullpaths full paths to yml files to load.
*/
public function loadFromFiles(array $fullpaths): void
{
foreach ($fullpaths as $table => $fullpath) {
// Only a table when it's an associative array.
$table = \is_int($table) ? null : $table;
$this->loadFromFile($fullpath, $table);
}
}

/**
* Load information from one file (yml).
*
* @param string $fullpath full path to yml file to load.
*/
public function loadFromFile(string $fullpath): void
{
if (!file_exists($fullpath)) {
throw new coding_exception('File not found: ' . $fullpath);
}

if (!is_readable($fullpath)) {
throw new coding_exception('File not readable: ' . $fullpath);
}

$extension = strtolower(pathinfo($fullpath, PATHINFO_EXTENSION));
if ($extension !== 'yml') {
throw new coding_exception('Cannot handle files with extension: ' . $extension);
}

$this->loadFromString(file_get_contents($fullpath));
}

/**
* Load information from a string (yaml).
*
* @param string $content contents of yaml file to load.
*/
public function loadFromString(string $content): void
{
try {
$parsedYaml = Yaml::parse($content);
$this->sendToDatabase($parsedYaml);
} catch (ParseException $exception) {
printf('Unable to parse the YAML string: %s', $exception->getMessage());
}
}

/**
* Send all the information to the database.
*
* @param array $tables
*/
public function sendToDatabase(array $tables): void
{
foreach($tables as $table => $rows) {
$tableNotEmpty = (bool) $this->db->select('*')
->from($table)
->execute()
->fetchCell();

if ($tableNotEmpty) {
$this->db->truncate($table);
}

foreach($rows as $row => $columns) {
$this->db->insert($table)
->values($columns)
->execute();
}
}
}
}
2 changes: 1 addition & 1 deletion tests/PHPUnit/Ilch/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function tearDown()
parent::tearDown();

foreach ($this->tearDownCallbacks as $callback) {
if (is_callable($callback)) {
if (\is_callable($callback)) {
$callback();
}
}
Expand Down
14 changes: 6 additions & 8 deletions tests/PHPUnit/Ilch/TestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace PHPUnit\Ilch;

use Ilch\Registry as Registry;
use Ilch\Registry;
use Ilch\Config\File as Config;

/**
Expand All @@ -29,14 +29,12 @@ class TestHelper
*/
public static function setConfigInRegistry(array $configData)
{
if (static::$config === null) {
if (!Registry::has('config') && file_exists(CONFIG_PATH . '/config.php')) {
static::$config = new Config();
static::$config->loadConfigFromFile(CONFIG_PATH . '/config.php');
if ((static::$config === null) && !Registry::has('config') && file_exists(CONFIG_PATH . '/config.php')) {
static::$config = new Config();
static::$config->loadConfigFromFile(CONFIG_PATH . '/config.php');

foreach ($configData as $configKey => $configValue) {
static::$config->set($configKey, $configValue);
}
foreach ($configData as $configKey => $configValue) {
static::$config->set($configKey, $configValue);
}
}

Expand Down
Loading

0 comments on commit 83dfd16

Please sign in to comment.