Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Description ----------- Should be released as v1.6 Commits ------- c8130ac Added DatabaseTrait 8ec4825 Change ext-pdo to be optional b27c362 Throw an exception if the SQL file does not exist
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\TestCase; | ||
|
||
trait ContaoDatabaseTrait | ||
{ | ||
private static $pdo; | ||
|
||
protected static function loadFileIntoDatabase(string $sqlFile): void | ||
{ | ||
if (!file_exists($sqlFile)) { | ||
throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $sqlFile)); | ||
} | ||
|
||
$pdo = static::getConnection(); | ||
$pdo->exec(file_get_contents($sqlFile)); | ||
} | ||
|
||
protected static function getConnection(): \PDO | ||
{ | ||
if (null === self::$pdo) { | ||
self::$pdo = new \PDO( | ||
sprintf('mysql:host=%s;port=%s;dbname=%s;', getenv('DB_HOST'), getenv('DB_PORT'), getenv('DB_NAME')), | ||
getenv('DB_USER'), | ||
getenv('DB_PASS'), | ||
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION] | ||
); | ||
} | ||
|
||
return self::$pdo; | ||
} | ||
} |