From 8aa4639b04d2716fc3e46a5475b02618cefaa578 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 27 Aug 2017 19:04:42 +0300 Subject: [PATCH] New Gozer tests, exposed methods for public usage (mainly for testing) --- src/Commands/Gozer.php | 22 +++++++++++++------- tests/GozerTest.php | 46 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php index d66c9a2..18c485b 100644 --- a/src/Commands/Gozer.php +++ b/src/Commands/Gozer.php @@ -60,9 +60,7 @@ class Gozer extends Command '); - $tables = []; - - $this->dbPrefix = $this->getDatabasePrefix(); + $this->setDatabasePrefix($this->getDatabasePrefix()); $confirmationQuestion = 'Delete all of your database tables?'; if (!empty($this->dbPrefix)) { @@ -139,7 +137,7 @@ class Gozer extends Command /** * @return bool|\Doctrine\DBAL\Schema\AbstractSchemaManager */ - private function getConnection() + public function getConnection() { try { /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ @@ -158,7 +156,7 @@ class Gozer extends Command * * @return array|bool */ - private function getTables(\Doctrine\DBAL\Schema\AbstractSchemaManager $connection) + public function getTables(\Doctrine\DBAL\Schema\AbstractSchemaManager $connection) { try { /** @var array $tables */ @@ -172,17 +170,27 @@ class Gozer extends Command return $tables; } - private function getDatabasePrefix() + public function getDatabasePrefix() { return trim(DB::getTablePrefix()); } + /** + * This is mainly for testing purposes + * + * @param string $prefix + */ + public function setDatabasePrefix($prefix = '') + { + $this->dbPrefix = $prefix; + } + /** * @param array $tables * * @return \Illuminate\Support\Collection */ - private function getFilteredTables($tables = []) + public function getFilteredTables($tables = []) { $prefix = $this->dbPrefix; diff --git a/tests/GozerTest.php b/tests/GozerTest.php index b2467d1..449f6a7 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -3,8 +3,6 @@ namespace Superhelio\Commands; use ReflectionClass; use Superhelio\Commands\Commands\Gozer; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\DB; @@ -84,6 +82,50 @@ class GozerTest extends \Orchestra\Testbench\TestCase { $gozer = new ReflectionClass('\\Superhelio\\Commands\\Commands\\Gozer'); $this->assertTrue($gozer->hasMethod('handle')); + $this->assertTrue($gozer->hasProperty('signature')); + $this->assertTrue($gozer->hasProperty('description')); $this->assertTrue($gozer->hasProperty('dbPrefix')); } + + public function test_gozer_finds_database_prefix() + { + $gozer = new Gozer(); + + $this->assertEquals('gozerTest__', $gozer->getDatabasePrefix()); + } + + public function test_gozer_finds_users_table() + { + $gozer = new Gozer(); + + $connection = $gozer->getConnection(); + + $tables = $gozer->getTables($connection); + $this->assertTrue(in_array('gozerTest__users', $tables, false)); + + $gozer->setDatabasePrefix('gozerTest__'); + $filteredTables = $gozer->getFilteredTables($tables); + $this->assertTrue(is_a($filteredTables, \Illuminate\Support\Collection::class)); + $this->assertTrue(in_array('gozerTest__users', $filteredTables->toArray(), false)); + } + + public function test_gozer_table_filtering_works() + { + $gozer = new Gozer(); + $tables = array( + 'gozerTest__users', + 'gozerTest__migrations', + 'this_should_be_filtered', + 'filter_me_too' + ); + + $gozer->setDatabasePrefix('gozerTest__'); + $filtered = $gozer->getFilteredTables($tables); + $array = $filtered->toArray(); + + $this->assertFalse(in_array('this_should_be_filtered', $array, false)); + $this->assertFalse(in_array('filter_me_too', $array, false)); + $this->assertTrue(in_array('gozerTest__users', $array, false)); + $this->assertTrue(in_array('gozerTest__migrations', $array, false)); + } }