Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Add unit test bootstrap and config
Browse files Browse the repository at this point in the history
  • Loading branch information
spekkionu committed Apr 16, 2017
1 parent 640a64e commit 373405b
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 3 deletions.
1 change: 1 addition & 0 deletions codeception.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ settings:
colors: true
memory_limit: 1024M
strict_xml: true
backup_globals: false
extensions:
enabled:
- Codeception\Extension\RunFailed
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"tests/Extensions/"
],
"psr-4": {
"Test\\Model\\": "tests/_support/Helper"
"Test\\Model\\": "tests/_support/Helper",
"Test\\Traits\\": "tests/Traits"
}
},
"suggest": {
Expand Down
10 changes: 9 additions & 1 deletion framework/Providers/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Framework\Providers;

use Illuminate\Config\Repository;
Expand Down Expand Up @@ -29,6 +30,7 @@ class DatabaseServiceProvider extends AbstractServiceProvider implements Bootabl
protected $provides
= [
'database',
'PDO',
'Illuminate\Database\Capsule\Manager',
'Illuminate\Database\Eloquent\Factory',
];
Expand Down Expand Up @@ -58,6 +60,12 @@ function () {
return $this->getContainer()->get('Illuminate\Database\Capsule\Manager');
}
);
$this->getContainer()->share(
'PDO',
function () {
return $this->getContainer()->get('Illuminate\Database\Capsule\Manager')->connection()->getPdo();
}
);
$this->getContainer()->share(
'Illuminate\Database\Eloquent\Factory',
function () {
Expand Down Expand Up @@ -86,7 +94,7 @@ protected function getCapsule()
$driver = $config->get("database.{$name}.driver", 'mysql');
if ($driver === 'sqlite') {
$dbname = $config->get("database.{$name}.dbname");
if ($dbname === 'memory') {
if (in_array($dbname, ['memory', ':memory:'])) {
$database = ':memory:';
} else {
$filename = pathinfo($dbname, PATHINFO_FILENAME) . '.sqlite';
Expand Down
32 changes: 32 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/unit/_bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Unit Test Suite">
<directory suffix=".php">./tests/unit/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_NAME" value="Test"/>
<env name="APP_URL" value="http://localhost:8723"/>
<env name="CRYPT_KEY" value="base64:ePDCvhWAK3qZcj35rsvk6tRPmkgkyZK3odZdiPbha7M="/>
<env name="DATABASE_DRIVER" value="sqlite"/>
<env name="DATABASE_DBNAME" value="testing"/>
<env name="SESSION_DRIVER" value="test"/>
<env name="QUEUE_ENABLED" value="false"/>
<env name="MAIL_DRIVER" value="fake"/>
<env name="MAIL_FROM_ADDRESS" value="[email protected]"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="FILESYSTEM_DEFAULT" value="test"/>
</php>
</phpunit>
7 changes: 6 additions & 1 deletion tests/Extensions/MigrationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public function beforeTest(\Codeception\Event\TestEvent $e)

public function afterSuite(\Codeception\Event\SuiteEvent $e)
{

if ($this->config['type'] === 'sqlite') {
$this->dropSqliteTables();
}
if ($this->config['type'] === 'mysql') {
$this->dropMysqlTables();
}
}

protected function hasDbConnection()
Expand Down
98 changes: 98 additions & 0 deletions tests/Traits/RunsMigrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Test\Traits;

use Illuminate\Database\Capsule\Manager;
use PDO;
use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Tester\CommandTester;

trait RunsMigrations
{
/**
* @var PDO
*/
protected $dbh;

/**
* Connects to the database and runs migrations
*
* @before
*/
public function createDatabase()
{
$this->dbh = app('PDO');
$this->dropTables();
$this->runMigrations();
}

/**
* Drops tables and closes connection
*
* @after
*/
public function closeDatabase()
{
$this->dropTables();
$this->dbh = null;
}

/**
* Drops database tables
*/
public function dropTables()
{
if (getenv('DATABASE_DRIVER') === 'mysql') {
$this->dropMysqlTables();
}
if (getenv('DATABASE_DRIVER') === 'sqlite' && getenv('DATABASE_DBNAME') !== ':memory:') {
$this->dropSqliteTables();
}
}

/**
* Runs database migrations
*/
public function runMigrations()
{
$phinx = new PhinxApplication();
$command = $phinx->find('migrate');
$commandTester = new CommandTester($command);

$commandTester->execute([
'command' => $command->getName(),
'--environment' => 'unit',
]);
}

/**
* Drops all tables
*/
protected function dropSqliteTables()
{
$sth = $this->dbh->query("SELECT `name` FROM sqlite_master WHERE `type`='table'");
$tables = $sth->fetchAll(PDO::FETCH_COLUMN);
foreach ($tables as $table) {
$this->dbh->exec("DELETE FROM `{$table}`");
}
}

/**
* Drops all tables
*/
protected function dropMysqlTables()
{
$this->dbh->exec('SET foreign_key_checks = 0');
$sth = $this->dbh->query('SHOW TABLES');
$tables = [];
while ($table = $sth->fetchColumn()) {
$tables[] = "`{$table}`";
}
if ($tables) {
$tables = implode(',', $tables);
$this->dbh->exec("DROP TABLE IF EXISTS {$tables}");
}

$this->dbh->exec('SET foreign_key_checks = 1');
}
}
1 change: 1 addition & 0 deletions tests/unit/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<?php
// Here you can initialize variables that will be available to your tests
require_once dirname(dirname(__DIR__)) . '/framework/bootstrap.php';

0 comments on commit 373405b

Please sign in to comment.