diff --git a/codeception.yml b/codeception.yml index 95b32aa..2f2c202 100644 --- a/codeception.yml +++ b/codeception.yml @@ -10,6 +10,7 @@ settings: colors: true memory_limit: 1024M strict_xml: true + backup_globals: false extensions: enabled: - Codeception\Extension\RunFailed diff --git a/composer.json b/composer.json index d94468b..357fc3f 100644 --- a/composer.json +++ b/composer.json @@ -69,7 +69,8 @@ "tests/Extensions/" ], "psr-4": { - "Test\\Model\\": "tests/_support/Helper" + "Test\\Model\\": "tests/_support/Helper", + "Test\\Traits\\": "tests/Traits" } }, "suggest": { diff --git a/framework/Providers/DatabaseServiceProvider.php b/framework/Providers/DatabaseServiceProvider.php index 33ead8e..8f5503a 100644 --- a/framework/Providers/DatabaseServiceProvider.php +++ b/framework/Providers/DatabaseServiceProvider.php @@ -1,4 +1,5 @@ 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 () { @@ -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'; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..bf77072 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,32 @@ + + + + + ./tests/unit/ + + + + + + + + + + + + + + + + + diff --git a/tests/Extensions/MigrationExtension.php b/tests/Extensions/MigrationExtension.php index 824436f..aacbcfe 100644 --- a/tests/Extensions/MigrationExtension.php +++ b/tests/Extensions/MigrationExtension.php @@ -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() diff --git a/tests/Traits/RunsMigrations.php b/tests/Traits/RunsMigrations.php new file mode 100644 index 0000000..aec4b60 --- /dev/null +++ b/tests/Traits/RunsMigrations.php @@ -0,0 +1,98 @@ +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'); + } +} diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index 8a88555..fd11a15 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -1,2 +1,3 @@