Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on the deprecated DbUnit #527

Merged
merged 3 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
104 changes: 13 additions & 91 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,34 +49,15 @@ 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;

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

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

/**
Expand All @@ -109,14 +88,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 @@ -127,58 +104,22 @@ protected function setUp()
parent::setUp();
}

/**
* Creates the db connection to the test database.
*
* @return \PHPUnit_Extensions_Database_DB_IDatabaseConnection|null Returns null if the necessary config was not set
*/
final public function getConnection()
{
$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;
}

/**
* Returns database schema sql statements to initialize database
*
* @return string
*/
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 +132,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)
{
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)
{
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)
{
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)
{
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