Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow auto-import of db
- Loading branch information
Showing
11 changed files
with
211 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
/** | ||
* Setup Class. | ||
* | ||
* @author Vijay Mahrra <vijay@yoyo.org> | ||
* @copyright (c) Copyright 2016 Vijay Mahrra | ||
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) | ||
*/ | ||
class Setup | ||
{ | ||
|
||
/** | ||
* setup database | ||
* | ||
* @param \Dice\Dice dependency injector | ||
* @return void | ||
*/ | ||
public static function database(&$dice) | ||
{ | ||
$f3 = \Base::instance(); | ||
$cache = \Cache::instance(); | ||
// cli mode will not use cache on cli and will check db every time if in dev mode | ||
if ($f3->get('db.create') && (!$cache->exists('tables', $tables) || PHP_SAPI == 'cli' || 'dev' == $f3->get('app.env'))) { | ||
$db = $dice->create('DB\\SQL'); | ||
$tables = $db->exec('SHOW TABLES'); | ||
if (empty($tables)) { | ||
$sql = $f3->get('HOMEDIR') . '/data/db/sql/create.sql'; | ||
$db->exec(file_get_contents($sql)); | ||
$tables = $db->exec('SHOW TABLES'); | ||
} | ||
$cache->set('tables', $tables, 600); | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Test Environment | ||
|
||
## F3 Unit Tests | ||
|
||
Require the file `tests/setup.php` which contains the function `setup()` which will do the following: | ||
|
||
- Setup the base application environment | ||
- Read the `dsn_test` database DSN value from the config.ini | ||
- Delete all existing tables in the test database | ||
- Execute Setup::database from `app/lib/App/Setup.php` which will import the sql dump from `data/db/sql/create.sql` and create a new root user and api app for that user. | ||
- It then returns an instance of $f3 which can be used by the test suite | ||
|
||
This environment can then be used for safe testing which doesn't interfere with the running website. | ||
|
||
### Run the test database setup and checks | ||
|
||
``` | ||
cd tests | ||
php setuptest.php | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
namespace App; | ||
|
||
/** | ||
* Load, configure, test environment, return database handle to it | ||
* | ||
* @return \Base $f3 instance | ||
*/ | ||
function &setup() | ||
{ | ||
if (session_status() == PHP_SESSION_NONE) { | ||
ini_set('session.auto_start', 'Off'); | ||
} else { | ||
ini_set('session.lazy_write', 'On'); | ||
} | ||
|
||
chdir(__DIR__ . '/../app'); | ||
require_once '../lib/autoload.php'; // composer autoloader | ||
|
||
// bootstrap initial environment | ||
$f3 = \Base::instance(); | ||
|
||
\FFMVC\App::start(); | ||
|
||
// override db params for test | ||
// setup database connection params | ||
// @see http://fatfreeframework.com/databases | ||
$httpDSN = $f3->get('db.dsn_test'); | ||
if (!empty($httpDSN)) { | ||
$dbParams = $f3->get('db'); | ||
$params = \FFMVC\Helpers\DB::instance()->parseHttpDsn($httpDSN); | ||
$params['dsn'] = \FFMVC\Helpers\DB::instance()->createDbDsn($params); | ||
$dbParams = array_merge($dbParams, $params); | ||
$f3->set('db', $dbParams); | ||
} | ||
|
||
// load dependency injection container | ||
$dice = new \Dice\Dice; | ||
|
||
// logging for application | ||
$logfile = $f3->get('log.file'); | ||
$dice->addRule('Log', ['shared' => true, 'constructParams' => [$logfile]]); | ||
|
||
// database connection used by app | ||
$dbConfig = $f3->get('db'); | ||
|
||
$dice->addRule('DB\\SQL', ['shared' => true, 'constructParams' => [ | ||
\FFMVC\Helpers\DB::createDbDsn($dbConfig), | ||
$dbConfig['user'], | ||
$dbConfig['pass'], | ||
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION] | ||
]]); | ||
|
||
// auto-create database if options set | ||
try { | ||
$db = $dice->create('DB\\SQL'); | ||
\Registry::set('db', $db); | ||
} catch (\PDOException $e) { | ||
switch ($e->getCode()) { | ||
case 1049: // db doesn't exist | ||
die($e->getMessage()); | ||
break; | ||
default: | ||
throw($e); | ||
return; | ||
} | ||
} | ||
|
||
// drop existing tables, re-create db | ||
try { | ||
$results = $db->exec('SHOW TABLES'); | ||
$queries = []; | ||
if (!empty($results)) { | ||
$queries[] = 'SET FOREIGN_KEY_CHECKS = 0'; | ||
foreach ($results as $result) { | ||
foreach ($result as $table) { | ||
$queries[] = sprintf('DROP TABLE IF EXISTS %s', $db->quotekey($table)); | ||
} | ||
} | ||
$queries[] = 'SET FOREIGN_KEY_CHECKS = 1'; | ||
$db->exec($queries); | ||
} | ||
\App\Setup::database($dice); | ||
\Registry::set('db', $db); | ||
} catch (\PDOException $e) { | ||
throw($e); | ||
} | ||
|
||
return $f3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
declare (strict_types=1); | ||
|
||
namespace App; | ||
|
||
if (PHP_SAPI !== 'cli') { | ||
die("This can only be executed in CLI mode."); | ||
} | ||
|
||
require_once 'setup.php'; | ||
|
||
// initialise test database | ||
try { | ||
$f3 = setup(); | ||
$db = \Registry::get('db'); | ||
} catch (\Exception $e) { | ||
// fatal, can't continue | ||
throw($e); | ||
} | ||
|
||
$test = new \Test; | ||
|
||
// insert tests here from f3 | ||
// https://fatfreeframework.com/test | ||
// https://fatfreeframework.com/unit-testing | ||
|
||
// This is where the tests begin | ||
$test->expect( | ||
count($db->exec('SHOW TABLES')), | ||
'Tables exist?'' | ||
); | ||
|
||
// Display the results; not MVC but let's keep it simple | ||
foreach ($test->results() as $result) { | ||
echo $result['text'].'<br>'; | ||
if ($result['status']) { | ||
echo 'Pass'; | ||
} else { | ||
echo 'Fail ('.$result['source'].')'; | ||
} | ||
echo '<br>'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters