From 4c4ee8c3905f5c21a5ad536064a3885cb96471ad Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Sat, 20 Jan 2018 17:27:37 +0100 Subject: [PATCH 1/7] Remake setup --- src/Commands/InstallCommand.php | 10 +- src/Commands/UninstallCommand.php | 10 +- src/Commands/UpdateCommand.php | 19 +- src/Hook.php | 15 +- src/Hooks.php | 296 +++++++- stub/composer.json | 47 +- stub/resources/assets/scripts/alert.js | 1 + stub/resources/database/migrations/.gitkeep | 0 ...TION_DATE_TIME_create_snake_case_table.php | 34 + stub/resources/database/seeders/.gitkeep | 0 .../seeders/StudlyCaseTableSeeder.php | 26 + stub/resources/database/unseeders/.gitkeep | 0 .../unseeders/StudlyCaseTableUnseeder.php | 25 + stub/src/StudlyCase.php | 8 + stub/src/StudlyCaseFacade.php | 18 + tests/HooksTest.php | 658 +++++++++++++++++- tests/TestCase.php | 6 + tests/fixtures/composer.json | 34 + .../resources/assets/scripts/alert.js | 1 + .../resources/database/migrations/.gitkeep | 0 ...20_120000_create_local_test_hook_table.php | 34 + .../resources/database/seeders/.gitkeep | 0 .../seeders/LocalTestHookTableSeeder.php | 26 + .../resources/database/unseeders/.gitkeep | 0 .../unseeders/LocalTestHookTableUnseeder.php | 25 + tests/fixtures/src/LocalTestHook.php | 8 + tests/fixtures/src/LocalTestHookFacade.php | 18 + .../src/LocalTestHookServiceProvider.php | 28 + 28 files changed, 1293 insertions(+), 54 deletions(-) create mode 100644 stub/resources/assets/scripts/alert.js create mode 100644 stub/resources/database/migrations/.gitkeep create mode 100644 stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php create mode 100644 stub/resources/database/seeders/.gitkeep create mode 100644 stub/resources/database/seeders/StudlyCaseTableSeeder.php create mode 100644 stub/resources/database/unseeders/.gitkeep create mode 100644 stub/resources/database/unseeders/StudlyCaseTableUnseeder.php create mode 100644 stub/src/StudlyCase.php create mode 100644 stub/src/StudlyCaseFacade.php create mode 100644 tests/fixtures/composer.json create mode 100644 tests/fixtures/resources/assets/scripts/alert.js create mode 100644 tests/fixtures/resources/database/migrations/.gitkeep create mode 100644 tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php create mode 100644 tests/fixtures/resources/database/seeders/.gitkeep create mode 100644 tests/fixtures/resources/database/seeders/LocalTestHookTableSeeder.php create mode 100644 tests/fixtures/resources/database/unseeders/.gitkeep create mode 100644 tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php create mode 100644 tests/fixtures/src/LocalTestHook.php create mode 100644 tests/fixtures/src/LocalTestHookFacade.php create mode 100644 tests/fixtures/src/LocalTestHookServiceProvider.php diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 112776d..dee5880 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -7,7 +7,7 @@ class InstallCommand extends Command { - protected $signature = 'hook:install {name} {version?} {--enable}'; + protected $signature = 'hook:install {name} {version?} {--enable} {--without-migrating} {--without-seeding} {--without-publishing}'; protected $description = 'Download and install a hook from remote https://larapack.io'; @@ -29,7 +29,13 @@ public function handle() { $name = $this->argument('name'); - $this->hooks->install($name, $this->argument('version')); + $this->hooks->install( + $name, + $this->argument('version'), + !$this->option('without-migrating'), + !$this->option('without-seeding'), + !$this->option('without-publishing') + ); if ($this->option('enable')) { $this->hooks->enable($name); diff --git a/src/Commands/UninstallCommand.php b/src/Commands/UninstallCommand.php index 633a0ef..800fa9a 100644 --- a/src/Commands/UninstallCommand.php +++ b/src/Commands/UninstallCommand.php @@ -7,7 +7,7 @@ class UninstallCommand extends Command { - protected $signature = 'hook:uninstall {name} {--delete}'; + protected $signature = 'hook:uninstall {name} {--delete} {--without-unmigrating} {--without-unseeding} {--without-unpublishing}'; protected $description = 'Uninstall a hook'; @@ -29,7 +29,13 @@ public function handle() { $name = $this->argument('name'); - $this->hooks->uninstall($name, $this->option('delete')); + $this->hooks->uninstall( + $name, + $this->option('delete'), + !$this->option('without-unmigrating'), + !$this->option('without-unseeding'), + !$this->option('without-unpublishing') + ); $this->info("Hook [{$name}] have been uninstalled."); } diff --git a/src/Commands/UpdateCommand.php b/src/Commands/UpdateCommand.php index 2c72250..7082e3e 100644 --- a/src/Commands/UpdateCommand.php +++ b/src/Commands/UpdateCommand.php @@ -7,7 +7,7 @@ class UpdateCommand extends Command { - protected $signature = 'hook:update {name} {version?}'; + protected $signature = 'hook:update {name} {version?} {--without-migrating} {--without-seeding} {--without-publishing} {--force-publish}'; protected $description = 'Update a hook'; @@ -35,10 +35,17 @@ public function handle() $hook = $hooks->where('name', $name)->first(); - if ($this->hooks->update($name, $version)) { - return $this->info("Hook [{$name}] have been updated!"); - } - - return $this->info('Nothing to update.'); + $updated = $this->hooks->update( + $name, + $version, + !$this->option('without-migrating'), + !$this->option('without-seeding'), + !$this->option('without-publishing'), + $this->option('force-publish') + ); + + return $updated + ? $this->info("Hook [{$name}] have been updated!") + : $this->info('Nothing to update.'); } } diff --git a/src/Hook.php b/src/Hook.php index 988cadb..80f3c44 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -67,15 +67,18 @@ public function loadComposerJson() $this->composerJson = json_decode($this->getComposerJsonFile(), true); } - public function getComposerJsonFile() + public function getPath() { - $path = 'vendor/'.$this->name; - if ($this->isLocal()) { - $path = 'hooks/'.$this->name; + return base_path('hooks/'.$this->name); } - return $this->filesystem->get(base_path($path.'/composer.json')); + return base_path('vendor/'.$this->name); + } + + public function getComposerJsonFile() + { + return $this->filesystem->get($this->getPath().'/composer.json'); } public function setLatest($latest) @@ -106,7 +109,7 @@ public function update(array $parameters) public function outdated() { if (is_null($this->latest)) { - $this->latest = app('hooks')->outdated($hook); + $this->latest = app('hooks')->outdated($this->name); } return $this->latest != $this->version; diff --git a/src/Hooks.php b/src/Hooks.php index becfc54..896938d 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -18,6 +18,8 @@ class Hooks protected $composer; protected $composerOutput; + protected $tempDirectories = []; + protected static $memoryLimit = null; protected static $memoryLimitSet = false; @@ -25,6 +27,8 @@ class Hooks protected static $versionWildcard = '*'; protected static $localVersion = 'dev-master'; + protected static $fakeDateTime = false; + public function __construct(Filesystem $filesystem) { $this->filesystem = $filesystem; @@ -64,6 +68,11 @@ public static function getUseVersionWildcardOnUpdate() return static::$useVersionWildcardOnUpdate; } + public static function fakeDateTime(Carbon $dateTime) + { + static::$fakeDateTime = $dateTime; + } + public function readOutdated() { $file = base_path('hooks/outdated.json'); @@ -180,7 +189,7 @@ public function setLastRemoteCheck(Carbon $carbon) * * @throws \Larapack\Hooks\Exceptions\HookAlreadyInstalledException */ - public function install($name, $version = null) + public function install($name, $version = null, $migrate = true, $seed = true, $publish = true) { // Check if already installed if ($this->installed($name)) { @@ -216,6 +225,18 @@ public function install($name, $version = null) $this->readJsonFile([$name]); $this->remakeJson(); + if ($migrate) { + $this->migrateHook($this->hooks[$name]); + } + + if ($seed) { + $this->seedHook($this->hooks[$name]); + } + + if ($publish) { + $this->publishHook($this->hooks[$name]); + } + event(new Events\InstalledHook($this->hooks[$name])); } @@ -237,7 +258,7 @@ public function prepareLocalInstallation($name) * * @throws \Larapack\Hooks\Exceptions\HookNotInstalledException */ - public function uninstall($name, $delete = false) + public function uninstall($name, $delete = false, $unmigrate = true, $unseed = true, $unpublish = true) { // Check if installed if (!$this->installed($name)) { @@ -253,12 +274,22 @@ public function uninstall($name, $delete = false) if ($this->enabled($name)) { event(new Events\DisablingHook($hook)); - //$this->runScripts($name, 'disable'); + // Some logic could later be placed here event(new Events\DisabledHook($hook)); } - // TODO: Run scripts for uninstall + if ($unseed) { + $this->unseedHook($hook); + } + + if ($unmigrate) { + $this->unmigrateHook($hook); + } + + if ($unpublish) { + $this->unpublishHook($hook); + } $this->runComposer([ 'command' => 'remove', @@ -288,7 +319,7 @@ public function uninstall($name, $delete = false) * * @return bool */ - public function update($name, $version = null) + public function update($name, $version = null, $migrate = true, $seed = true, $publish = true, $forcePublishing) { // Check if hook exists if (!$this->downloaded($name)) { @@ -313,6 +344,10 @@ public function update($name, $version = null) } } + if (!$forcePublishing) { + $this->makeTemponaryBackup($this->hooks[$name]); + } + // Require hook if (is_null($version)) { $this->composerRequire([$name]); // TODO: Save Composer output somewhere @@ -331,7 +366,19 @@ public function update($name, $version = null) $this->readJsonFile(); $this->remakeJson(); - // TODO: Run scripts for update + if ($migrate) { + $this->migrateHook($this->hooks[$name]); + } + + if ($seed) { + $this->seedHook($this->hooks[$name]); + } + + if ($publish) { + $this->publishHook($this->hooks[$name], $forcePublishing); + } + + $this->clearTemponaryFiles(); event(new Events\UpdatedHook($this->hooks[$name])); @@ -368,8 +415,6 @@ public function enable($name) event(new Events\EnablingHook($hook)); - // TODO: Run scripts for enable - $this->hooks[$name]->update(['enabled' => true]); $this->remakeJson(); @@ -407,8 +452,6 @@ public function disable($name) event(new Events\DisablingHook($hook)); - // TODO: Run scripts for disable - $this->hooks[$name]->update(['enabled' => false]); $this->remakeJson(); @@ -453,9 +496,10 @@ protected function makeStubFiles($name) { $replaces = [ 'kebab-case' => $name, - 'snake_case' => snake_case($name), - 'camcelCase' => camel_case($name), - 'StudlyCase' => studly_case($name), + 'snake_case' => snake_case(str_replace('-', '_', $name)), + 'camelCase' => camel_case(str_replace('-', '_', $name)), + 'StudlyCase' => studly_case(str_replace('-', '_', $name)), + 'MIGRATION_DATE_TIME' => $this->migrationDateTimeString(), ]; $files = $this->filesystem->allFiles(__DIR__.'/../stub'); @@ -482,6 +526,17 @@ protected function makeStubFiles($name) } } + protected function migrationDateTimeString() + { + $dateTime = Carbon::now(); + + if (static::$fakeDateTime) { + $dateTime = static::$fakeDateTime; + } + + return $dateTime->format('Y_m_d_His'); + } + protected function replace($content, array $replaces) { return str_replace(array_keys($replaces), array_values($replaces), $content); @@ -832,6 +887,221 @@ public function checkForUpdates() return collect($hooks); } + + /** + * Run migrations found for a specific hook. + * + * @param \Larapack\Hooks\Hook $hook + */ + protected function migrateHook(Hook $hook) + { + $migrations = (array) $hook->getComposerHookKey('migrations', []); + + app('migrator')->run($this->realPath($migrations, $hook->getPath().'/')->all()); + } + + /** + * Rollback migrations found for a specific hook. + * + * @param \Larapack\Hooks\Hook $hook + */ + protected function unmigrateHook(Hook $hook) + { + $migrations = (array) $hook->getComposerHookKey('migrations', []); + + app('migrator')->reset($this->realPath($migrations, $hook->getPath().'/')->all()); + } + + /** + * Run seeders found for a specific hook. + * + * @param \Larapack\Hooks\Hook $hook + */ + protected function seedHook(Hook $hook) + { + $folders = (array) $hook->getComposerHookKey('seeders', []); + $basePath = $hook->getPath().'/'; + + $this->runSeeders($folders, $basePath); + } + + /** + * Run unseeders found for a specific hook. + * + * @param \Larapack\Hooks\Hook $hook + */ + protected function unseedHook(Hook $hook) + { + $folders = (array) $hook->getComposerHookKey('unseeders', []); + $basePath = $hook->getPath().'/'; + + $this->runSeeders($folders, $basePath); + } + + /** + * Publish assets found for a specific hook. + * + * @param \Larapack\Hooks\Hook $hook + */ + protected function publishHook(Hook $hook, $force = false) + { + $folders = (array) $hook->getComposerHookKey('assets', []); + $basePath = $hook->getPath().'/'; + + $filesystem = $this->filesystem; + foreach ($folders as $location => $publish) { + $publishPath = base_path($publish); + if (!$realLocation = realpath($basePath.$location)) { + continue; + } + + $allFiles = collect($this->filesystem->allFiles($realLocation)) + ->map(function ($file) use ($realLocation) { + return substr($file->getRealPath(), strlen($realLocation) + 1); + }); + + $newFiles = $allFiles->filter(function ($filename) use ($publishPath, $filesystem) { + return !$filesystem->exists($publishPath.'/'.$filename); + }); + + $updatedFiles = $allFiles->filter(function ($filename) use ($publishPath, $filesystem) { + return $filesystem->exists($publishPath.'/'.$filename); + }); + + if (!$force && isset($this->tempDirectories[$hook->name])) { + $tempLocation = $this->tempDirectories[$hook->name].'/'.$location; + $updatedFiles = $updatedFiles + ->filter(function ($filename) use ($tempLocation, $publishPath, $filesystem) { + if (!$filesystem->exists($tempLocation.'/'.$filename)) { + return true; + } + + return md5_file($tempLocation.'/'.$filename) == + md5_file($publishPath.'/'.$filename); + }); + } + + $newFiles->merge($updatedFiles) + ->each(function ($filename) use ($realLocation, $publishPath, $filesystem) { + $directory = substr($publishPath.'/'.$filename, 0, - strlen(basename($filename))); + + if (!$filesystem->isDirectory($directory)) { + $filesystem->makeDirectory($directory, 0755, true, true); + } + + $filesystem->delete($publishPath.'/'.$filename); + $filesystem->copy( + $realLocation.'/'.$filename, + $publishPath.'/'.$filename + ); + }); + } + } + + /** + * Unpublish assets found for a specific hook. + * + * @param \Larapack\Hooks\Hook $hook + */ + protected function unpublishHook(Hook $hook) + { + $folders = (array) $hook->getComposerHookKey('assets', []); + $basePath = $hook->getPath().'/'; + + $filesystem = $this->filesystem; + foreach ($folders as $location => $publish) { + $publishPath = base_path($publish); + if (!$realLocation = realpath($basePath.$location)) { + continue; + } + + $allFiles = collect($this->filesystem->allFiles($realLocation)) + ->map(function ($file) use ($realLocation) { + return substr($file->getRealPath(), strlen($realLocation) + 1); + }); + + $existingFiles = $allFiles->filter(function ($filename) use ($publishPath, $filesystem) { + return $filesystem->exists($publishPath.'/'.$filename); + }); + + $existingFiles->each(function ($filename) use ($publishPath, $filesystem) { + $filesystem->delete($publishPath.'/'.$filename); + }); + } + } + + /** + * Run seeder files. + * + * @param array $folders + * @param string $basePath + */ + protected function runSeeders($folders, $basePath) + { + $filesystem = $this->filesystem; + + $this->realPath($folders, $basePath) + ->each(function ($folder) use ($filesystem) { + collect($filesystem->files($folder))->filter(function ($file) { + return $file->getExtension() == 'php'; + })->each(function ($file) { + $class = substr($file->getFilename(), 0, -4); + require_once($file->getRealPath()); + + (new $class)->run(); + }); + }); + } + + /** + * Get collection of realpath paths. + * + * @param array $paths + * @param string $basePath + * + * @return \Illuminate\Support\Collection + */ + protected function realPath(array $paths, $basePath = '') + { + return collect($paths)->map(function ($path) use ($basePath) { + return realpath($basePath.$path); + })->filter(function ($path) { + return $path; + }); + } + + /** + * Make temponary backup of hook + * + * @param \Larapack\Hooks\Hook $hook + * + * @return void + */ + protected function makeTemponaryBackup(Hook $hook) + { + $folder = $this->createTempFolder(); + $this->filesystem->copyDirectory($hook->getPath(), $folder); + + $this->tempDirectories[$hook->name] = $folder; + } + + protected function createTempFolder() + { + $path = sys_get_temp_dir().'/hooks-'.uniqid(); + + if ($this->filesystem->exists($path)) { + return $this->createTempFolder(); + } + + return $path; + } + + protected function clearTemponaryFiles() + { + foreach ($this->tempDirectories as $directory) { + $this->filesystem->deleteDirectory($directory); + } + } } // TODO: MOVE! diff --git a/stub/composer.json b/stub/composer.json index 5b836e0..cbc4930 100644 --- a/stub/composer.json +++ b/stub/composer.json @@ -1,19 +1,34 @@ { - "name": "kebab-case", - "description": "This is my first hook.", - "require": { - "larapack/hooks": "~1.0" - }, - "autoload": { - "psr-4": { - "StudlyCase\\": "src/" + "name": "kebab-case", + "description": "This is my first hook.", + "require": { + "larapack/hooks": "^1.0.5" + }, + "autoload": { + "psr-4": { + "StudlyCase\\": "src/" + } + }, + "extra": { + "hook": { + "providers": [ + "StudlyCase\\StudlyCaseServiceProvider" + ], + "aliases": { + "StudlyCase": "StudlyCase\\StudlyCaseFacade" + }, + "migrations": [ + "resources/database/migrations" + ], + "seeders": [ + "resources/database/seeders" + ], + "unseeders": [ + "resources/database/unseeders" + ], + "assets": { + "resources/assets": "public/vendor/kebab-case" + } + } } - }, - "extra": { - "hook": { - "providers": [ - "StudlyCase\\StudlyCaseServiceProvider" - ] - } - } } \ No newline at end of file diff --git a/stub/resources/assets/scripts/alert.js b/stub/resources/assets/scripts/alert.js new file mode 100644 index 0000000..10c5661 --- /dev/null +++ b/stub/resources/assets/scripts/alert.js @@ -0,0 +1 @@ +alert('This is a sample file!'); diff --git a/stub/resources/database/migrations/.gitkeep b/stub/resources/database/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php b/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php new file mode 100644 index 0000000..9097066 --- /dev/null +++ b/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php @@ -0,0 +1,34 @@ +increments('id'); + + $table->string('name'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('snake_case'); + } +} \ No newline at end of file diff --git a/stub/resources/database/seeders/.gitkeep b/stub/resources/database/seeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/stub/resources/database/seeders/StudlyCaseTableSeeder.php b/stub/resources/database/seeders/StudlyCaseTableSeeder.php new file mode 100644 index 0000000..48ca666 --- /dev/null +++ b/stub/resources/database/seeders/StudlyCaseTableSeeder.php @@ -0,0 +1,26 @@ +count() > 0) { + return; + } + + DB::table('snake_case')->insert([ + ['name' => 'foo'], + ['name' => 'bar'], + ['name' => 'baz'], + ]); + } +} \ No newline at end of file diff --git a/stub/resources/database/unseeders/.gitkeep b/stub/resources/database/unseeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php b/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php new file mode 100644 index 0000000..5c7e718 --- /dev/null +++ b/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php @@ -0,0 +1,25 @@ +whereIn('name', ['foo', 'bar', 'baz']) + ->delete(); + } +} diff --git a/stub/src/StudlyCase.php b/stub/src/StudlyCase.php new file mode 100644 index 0000000..89db8e7 --- /dev/null +++ b/stub/src/StudlyCase.php @@ -0,0 +1,8 @@ +format('Y_m_d_His'); // Make hook $this->artisan('hook:make', [ @@ -95,6 +101,61 @@ public function test_making_local_hook() // Check that hook is not yet installed $this->assertCount(0, app('hooks')->hooks()->all()); + + // Check stubs + $this->assertFileExists(base_path('hooks/local-test-hook/composer.json')); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/composer.json'), + $filesystem->get(base_path('hooks/local-test-hook/composer.json')) + ); + + $this->assertFileExists(base_path('hooks/local-test-hook/src/LocalTestHook.php')); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/src/LocalTestHook.php'), + $filesystem->get(base_path('hooks/local-test-hook/src/LocalTestHook.php')) + ); + + $this->assertFileExists(base_path('hooks/local-test-hook/src/LocalTestHookFacade.php')); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/src/LocalTestHookFacade.php'), + $filesystem->get(base_path('hooks/local-test-hook/src/LocalTestHookFacade.php')) + ); + + $this->assertFileExists(base_path('hooks/local-test-hook/src/LocalTestHookServiceProvider.php')); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/src/LocalTestHookServiceProvider.php'), + $filesystem->get(base_path('hooks/local-test-hook/src/LocalTestHookServiceProvider.php')) + ); + + $this->assertFileExists(base_path('hooks/local-test-hook/resources/assets/scripts/alert.js')); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/resources/assets/scripts/alert.js'), + $filesystem->get(base_path('hooks/local-test-hook/resources/assets/scripts/alert.js')) + ); + + $this->assertFileExists(base_path( + 'hooks/local-test-hook/resources/database/migrations/'.$migrationDateTime.'_create_local_test_hook_table.php' + )); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/resources/database/migrations/'.$migrationDateTime.'_create_local_test_hook_table.php'), + $filesystem->get(base_path('hooks/local-test-hook/resources/database/migrations/'.$migrationDateTime.'_create_local_test_hook_table.php')) + ); + + $this->assertFileExists(base_path( + 'hooks/local-test-hook/resources/database/seeders/LocalTestHookTableSeeder.php' + )); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/resources/database/seeders/LocalTestHookTableSeeder.php'), + $filesystem->get(base_path('hooks/local-test-hook/resources/database/seeders/LocalTestHookTableSeeder.php')) + ); + + $this->assertFileExists(base_path( + 'hooks/local-test-hook/resources/database/unseeders/LocalTestHookTableUnseeder.php' + )); + $this->assertEquals( + $filesystem->get(__DIR__.'/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php'), + $filesystem->get(base_path('hooks/local-test-hook/resources/database/unseeders/LocalTestHookTableUnseeder.php')) + ); } public function test_installing_local_hook() @@ -114,7 +175,7 @@ public function test_installing_local_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => false, ]; foreach ($expect as $key => $value) { @@ -153,7 +214,7 @@ public function test_enabling_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => false, ]; foreach ($expect as $key => $value) { @@ -172,7 +233,7 @@ public function test_enabling_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => true, ]; foreach ($expect as $key => $value) { @@ -211,7 +272,7 @@ public function test_disabling_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => false, ]; foreach ($expect as $key => $value) { @@ -230,7 +291,7 @@ public function test_disabling_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => true, ]; foreach ($expect as $key => $value) { @@ -249,7 +310,7 @@ public function test_disabling_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => false, ]; foreach ($expect as $key => $value) { @@ -288,7 +349,7 @@ public function test_uninstall_hook() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => false, ]; foreach ($expect as $key => $value) { @@ -336,7 +397,7 @@ public function test_uninstall_hook_without_delete_parameter() $expect = [ 'name' => 'local-test-hook', 'version' => null, - 'description' => 'This is a hook.', + 'description' => 'This is my first hook.', 'enabled' => false, ]; foreach ($expect as $key => $value) { @@ -588,5 +649,584 @@ public function test_updating_updates_dependencies() $this->assertDirectoryExists(base_path('vendor/marktopper/composer-hook-dependency-2')); } - // TODO: Test that if a hook requires another hook, that hook should be loaded as well + public function test_migrating_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + } + + public function test_installing_without_migrating_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + '--without-migrating' => true, + '--without-seeding' => true, + ]); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + } + + public function test_unmigrating_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + ]); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + } + + public function test_uninstalling_without_unmigrating_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + '--without-unmigrating' => true, + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + $this->assertEquals(0, DB::table('tests')->count()); + } + + public function test_remigrating_hook_on_update() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + $this->assertFalse(Schema::hasTable('another_tests')); + $this->assertMigrationHasNotRan('2018_01_19_100000_create_another_tests_table'); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + + $this->assertFalse(Schema::hasTable('another_tests')); + $this->assertMigrationHasNotRan('2018_01_19_100000_create_another_tests_table'); + + // Install hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + $this->assertTrue(Schema::hasTable('another_tests')); + $this->assertMigrationHasRan('2018_01_19_100000_create_another_tests_table'); + } + + public function test_updating_without_remigrating_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + $this->assertMigrationHasNotRan('2018_01_19_000000_create_tests_table'); + $this->assertFalse(Schema::hasTable('another_tests')); + $this->assertMigrationHasNotRan('2018_01_19_100000_create_another_tests_table'); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + + $this->assertFalse(Schema::hasTable('another_tests')); + $this->assertMigrationHasNotRan('2018_01_19_100000_create_another_tests_table'); + + // Install hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + '--without-migrating' => true, + '--without-seeding' => true, + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertMigrationHasRan('2018_01_19_000000_create_tests_table'); + $this->assertFalse(Schema::hasTable('another_tests')); + $this->assertMigrationHasNotRan('2018_01_19_100000_create_another_tests_table'); + } + + public function test_seeding_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + } + + public function test_installing_without_seeding_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + '--without-seeding' => true, + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(0, DB::table('tests')->count()); + } + + public function test_unseeding_hook() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + '--without-unmigrating' => true, + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(0, DB::table('tests')->count()); + } + + public function test_uninstalling_without_unseeding() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + '--without-unmigrating' => true, + '--without-unseeding' => true, + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + } + + public function test_reseeding_on_update() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + + // Update hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + $this->assertTrue(Schema::hasTable('another_tests')); + $this->assertEquals(3, DB::table('another_tests')->count()); + } + + public function test_updating_without_reseeding() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse(Schema::hasTable('tests')); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + + // Update hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + '--without-seeding' => true, + ]); + + $this->assertTrue(Schema::hasTable('tests')); + $this->assertEquals(3, DB::table('tests')->count()); + $this->assertTrue(Schema::hasTable('another_tests')); + $this->assertEquals(0, DB::table('another_tests')->count()); + } + + public function test_publishing_assets() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + } + + public function test_installing_without_publishing_assets() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + '--without-publishing' => true, + ]); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + } + + public function test_unpublishing_assets() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + ]); + + $this->assertFalse($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + } + + public function test_unpublishing_assets_without_removing_other_files_in_folder() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + $filesystem->put( + base_path('public/vendor/migration-hook/assets/test.js'), + "alert('This is just a test!');\n" + ); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + ]); + + $this->assertFalse($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/test.js'))); + $this->assertEquals("alert('This is just a test!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/test.js') + )); + } + + public function test_uninstalling_without_unpublishing_assets() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + // Uninstall hook + $this->artisan('hook:uninstall', [ + 'name' => 'migrating-hook', + '--without-unpublishing' => true, + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + } + + public function test_republishing_assets_on_update() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + // Update hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/another.js'))); + $this->assertEquals("alert('I am still alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + $this->assertEquals("alert('I am another file!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/another.js') + )); + } + + public function test_updating_without_republishing_assets() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + // Update hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + '--without-publishing' => true, + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertFalse($filesystem->exists(base_path('public/vendor/migration-hook/assets/another.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + } + + public function test_republishing_assets_on_update_with_custom_changed_files() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + $filesystem->put( + base_path('public/vendor/migration-hook/assets/script.js'), + "alert('Am I still alive?');\n" + ); + + // Update hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/another.js'))); + $this->assertEquals("alert('Am I still alive?');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + $this->assertEquals("alert('I am another file!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/another.js') + )); + } + + public function test_force_republishing_assets_on_update_with_custom_changed_files() + { + $filesystem = app(Filesystem::class); + + $this->assertFalse($filesystem->exists(base_path('public/vendor'))); + + // Install hook + $this->artisan('hook:install', [ + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertEquals("alert('I am alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + + $filesystem->put( + base_path('public/vendor/migration-hook/assets/script.js'), + "alert('Am I still alive?');\n" + ); + + // Update hook + $this->artisan('hook:update', [ + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', + '--force-publish' => true, + ]); + + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); + $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/another.js'))); + $this->assertEquals("alert('I am still alive!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/script.js') + )); + $this->assertEquals("alert('I am another file!');\n", $filesystem->get( + base_path('public/vendor/migration-hook/assets/another.js') + )); + } + + protected function assertMigrationHasRan($name) + { + return $this->assertTrue( + DB::table('migrations')->where('migration', $name)->count() == 1 + ); + } + + protected function assertMigrationHasNotRan($name) + { + return $this->assertFalse( + DB::table('migrations')->where('migration', $name)->count() == 1 + ); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index f8da04f..be5a6cb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -25,6 +25,9 @@ public function setUp() // Cleanup old hooks before testing $filesystem->deleteDirectory(base_path('hooks')); + // Cleanup published files + $filesystem->deleteDirectory(base_path('public/vendor')); + // Clear old hooks $hook = app(Hooks::class); $hook->readJsonFile(); @@ -57,6 +60,9 @@ public function setUp() // Reload JSON files app(Hooks::class)->readJsonFile(); + + // Migrate + $this->artisan('migrate'); } public function tearDown() diff --git a/tests/fixtures/composer.json b/tests/fixtures/composer.json new file mode 100644 index 0000000..49c36b5 --- /dev/null +++ b/tests/fixtures/composer.json @@ -0,0 +1,34 @@ +{ + "name": "local-test-hook", + "description": "This is my first hook.", + "require": { + "larapack/hooks": "^1.0.5" + }, + "autoload": { + "psr-4": { + "LocalTestHook\\": "src/" + } + }, + "extra": { + "hook": { + "providers": [ + "LocalTestHook\\LocalTestHookServiceProvider" + ], + "aliases": { + "LocalTestHook": "LocalTestHook\\LocalTestHookFacade" + }, + "migrations": [ + "resources/database/migrations" + ], + "seeders": [ + "resources/database/seeders" + ], + "unseeders": [ + "resources/database/unseeders" + ], + "assets": { + "resources/assets": "public/vendor/local-test-hook" + } + } + } +} \ No newline at end of file diff --git a/tests/fixtures/resources/assets/scripts/alert.js b/tests/fixtures/resources/assets/scripts/alert.js new file mode 100644 index 0000000..10c5661 --- /dev/null +++ b/tests/fixtures/resources/assets/scripts/alert.js @@ -0,0 +1 @@ +alert('This is a sample file!'); diff --git a/tests/fixtures/resources/database/migrations/.gitkeep b/tests/fixtures/resources/database/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php b/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php new file mode 100644 index 0000000..055a104 --- /dev/null +++ b/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php @@ -0,0 +1,34 @@ +increments('id'); + + $table->string('name'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('local_test_hook'); + } +} \ No newline at end of file diff --git a/tests/fixtures/resources/database/seeders/.gitkeep b/tests/fixtures/resources/database/seeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/resources/database/seeders/LocalTestHookTableSeeder.php b/tests/fixtures/resources/database/seeders/LocalTestHookTableSeeder.php new file mode 100644 index 0000000..6546e55 --- /dev/null +++ b/tests/fixtures/resources/database/seeders/LocalTestHookTableSeeder.php @@ -0,0 +1,26 @@ +count() > 0) { + return; + } + + DB::table('local_test_hook')->insert([ + ['name' => 'foo'], + ['name' => 'bar'], + ['name' => 'baz'], + ]); + } +} \ No newline at end of file diff --git a/tests/fixtures/resources/database/unseeders/.gitkeep b/tests/fixtures/resources/database/unseeders/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php b/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php new file mode 100644 index 0000000..730c18b --- /dev/null +++ b/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php @@ -0,0 +1,25 @@ +whereIn('name', ['foo', 'bar', 'baz']) + ->delete(); + } +} diff --git a/tests/fixtures/src/LocalTestHook.php b/tests/fixtures/src/LocalTestHook.php new file mode 100644 index 0000000..8467d53 --- /dev/null +++ b/tests/fixtures/src/LocalTestHook.php @@ -0,0 +1,8 @@ + Date: Sat, 20 Jan 2018 16:27:59 +0000 Subject: [PATCH 2/7] Apply fixes from StyleCI --- src/Hooks.php | 40 ++++++++-------- ...TION_DATE_TIME_create_snake_case_table.php | 6 +-- .../seeders/StudlyCaseTableSeeder.php | 2 +- .../unseeders/StudlyCaseTableUnseeder.php | 12 ++--- tests/HooksTest.php | 46 +++++++++---------- ...20_120000_create_local_test_hook_table.php | 6 +-- .../seeders/LocalTestHookTableSeeder.php | 2 +- .../unseeders/LocalTestHookTableUnseeder.php | 12 ++--- 8 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/Hooks.php b/src/Hooks.php index 896938d..5409dee 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -319,7 +319,7 @@ public function uninstall($name, $delete = false, $unmigrate = true, $unseed = t * * @return bool */ - public function update($name, $version = null, $migrate = true, $seed = true, $publish = true, $forcePublishing) + public function update($name, $version, $migrate, $seed, $publish, $forcePublishing) { // Check if hook exists if (!$this->downloaded($name)) { @@ -495,10 +495,10 @@ public function make($name) protected function makeStubFiles($name) { $replaces = [ - 'kebab-case' => $name, - 'snake_case' => snake_case(str_replace('-', '_', $name)), - 'camelCase' => camel_case(str_replace('-', '_', $name)), - 'StudlyCase' => studly_case(str_replace('-', '_', $name)), + 'kebab-case' => $name, + 'snake_case' => snake_case(str_replace('-', '_', $name)), + 'camelCase' => camel_case(str_replace('-', '_', $name)), + 'StudlyCase' => studly_case(str_replace('-', '_', $name)), 'MIGRATION_DATE_TIME' => $this->migrationDateTimeString(), ]; @@ -891,7 +891,7 @@ public function checkForUpdates() /** * Run migrations found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function migrateHook(Hook $hook) { @@ -903,7 +903,7 @@ protected function migrateHook(Hook $hook) /** * Rollback migrations found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function unmigrateHook(Hook $hook) { @@ -915,7 +915,7 @@ protected function unmigrateHook(Hook $hook) /** * Run seeders found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function seedHook(Hook $hook) { @@ -928,7 +928,7 @@ protected function seedHook(Hook $hook) /** * Run unseeders found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function unseedHook(Hook $hook) { @@ -941,7 +941,7 @@ protected function unseedHook(Hook $hook) /** * Publish assets found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function publishHook(Hook $hook, $force = false) { @@ -983,7 +983,7 @@ protected function publishHook(Hook $hook, $force = false) $newFiles->merge($updatedFiles) ->each(function ($filename) use ($realLocation, $publishPath, $filesystem) { - $directory = substr($publishPath.'/'.$filename, 0, - strlen(basename($filename))); + $directory = substr($publishPath.'/'.$filename, 0, -strlen(basename($filename))); if (!$filesystem->isDirectory($directory)) { $filesystem->makeDirectory($directory, 0755, true, true); @@ -1001,7 +1001,7 @@ protected function publishHook(Hook $hook, $force = false) /** * Unpublish assets found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function unpublishHook(Hook $hook) { @@ -1033,8 +1033,8 @@ protected function unpublishHook(Hook $hook) /** * Run seeder files. * - * @param array $folders - * @param string $basePath + * @param array $folders + * @param string $basePath */ protected function runSeeders($folders, $basePath) { @@ -1046,9 +1046,9 @@ protected function runSeeders($folders, $basePath) return $file->getExtension() == 'php'; })->each(function ($file) { $class = substr($file->getFilename(), 0, -4); - require_once($file->getRealPath()); + require_once $file->getRealPath(); - (new $class)->run(); + (new $class())->run(); }); }); } @@ -1056,8 +1056,8 @@ protected function runSeeders($folders, $basePath) /** * Get collection of realpath paths. * - * @param array $paths - * @param string $basePath + * @param array $paths + * @param string $basePath * * @return \Illuminate\Support\Collection */ @@ -1071,9 +1071,9 @@ protected function realPath(array $paths, $basePath = '') } /** - * Make temponary backup of hook + * Make temponary backup of hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook * * @return void */ diff --git a/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php b/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php index 9097066..8071914 100644 --- a/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php +++ b/stub/resources/database/migrations/MIGRATION_DATE_TIME_create_snake_case_table.php @@ -1,8 +1,8 @@ 'baz'], ]); } -} \ No newline at end of file +} diff --git a/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php b/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php index 5c7e718..417e348 100644 --- a/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php +++ b/stub/resources/database/unseeders/StudlyCaseTableUnseeder.php @@ -13,13 +13,13 @@ class StudlyCaseTableUnseeder extends Seeder */ public function run() { - // Skip if table does not exists. - if (!Schema::hasTable('snake_case')) { - return; - } + // Skip if table does not exists. + if (!Schema::hasTable('snake_case')) { + return; + } DB::table('snake_case') - ->whereIn('name', ['foo', 'bar', 'baz']) - ->delete(); + ->whereIn('name', ['foo', 'bar', 'baz']) + ->delete(); } } diff --git a/tests/HooksTest.php b/tests/HooksTest.php index dfaacf7..4b23baf 100644 --- a/tests/HooksTest.php +++ b/tests/HooksTest.php @@ -675,10 +675,10 @@ public function test_installing_without_migrating_hook() // Install hook $this->artisan('hook:install', [ - 'name' => 'migrating-hook', - 'version' => 'v1.0.0', + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', '--without-migrating' => true, - '--without-seeding' => true, + '--without-seeding' => true, ]); $this->assertFalse(Schema::hasTable('tests')); @@ -728,7 +728,7 @@ public function test_uninstalling_without_unmigrating_hook() // Uninstall hook $this->artisan('hook:uninstall', [ - 'name' => 'migrating-hook', + 'name' => 'migrating-hook', '--without-unmigrating' => true, ]); @@ -793,10 +793,10 @@ public function test_updating_without_remigrating_hook() // Install hook $this->artisan('hook:update', [ - 'name' => 'migrating-hook', - 'version' => 'v2.0.0', + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', '--without-migrating' => true, - '--without-seeding' => true, + '--without-seeding' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -829,8 +829,8 @@ public function test_installing_without_seeding_hook() // Install hook $this->artisan('hook:install', [ - 'name' => 'migrating-hook', - 'version' => 'v1.0.0', + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', '--without-seeding' => true, ]); @@ -855,7 +855,7 @@ public function test_unseeding_hook() // Uninstall hook $this->artisan('hook:uninstall', [ - 'name' => 'migrating-hook', + 'name' => 'migrating-hook', '--without-unmigrating' => true, ]); @@ -880,9 +880,9 @@ public function test_uninstalling_without_unseeding() // Uninstall hook $this->artisan('hook:uninstall', [ - 'name' => 'migrating-hook', + 'name' => 'migrating-hook', '--without-unmigrating' => true, - '--without-unseeding' => true, + '--without-unseeding' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -933,8 +933,8 @@ public function test_updating_without_reseeding() // Update hook $this->artisan('hook:update', [ - 'name' => 'migrating-hook', - 'version' => 'v2.0.0', + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', '--without-seeding' => true, ]); @@ -971,8 +971,8 @@ public function test_installing_without_publishing_assets() // Install hook $this->artisan('hook:install', [ - 'name' => 'migrating-hook', - 'version' => 'v1.0.0', + 'name' => 'migrating-hook', + 'version' => 'v1.0.0', '--without-publishing' => true, ]); @@ -1060,7 +1060,7 @@ public function test_uninstalling_without_unpublishing_assets() // Uninstall hook $this->artisan('hook:uninstall', [ - 'name' => 'migrating-hook', + 'name' => 'migrating-hook', '--without-unpublishing' => true, ]); @@ -1091,7 +1091,7 @@ public function test_republishing_assets_on_update() // Update hook $this->artisan('hook:update', [ - 'name' => 'migrating-hook', + 'name' => 'migrating-hook', 'version' => 'v2.0.0', ]); @@ -1125,8 +1125,8 @@ public function test_updating_without_republishing_assets() // Update hook $this->artisan('hook:update', [ - 'name' => 'migrating-hook', - 'version' => 'v2.0.0', + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', '--without-publishing' => true, ]); @@ -1162,7 +1162,7 @@ public function test_republishing_assets_on_update_with_custom_changed_files() // Update hook $this->artisan('hook:update', [ - 'name' => 'migrating-hook', + 'name' => 'migrating-hook', 'version' => 'v2.0.0', ]); @@ -1201,8 +1201,8 @@ public function test_force_republishing_assets_on_update_with_custom_changed_fil // Update hook $this->artisan('hook:update', [ - 'name' => 'migrating-hook', - 'version' => 'v2.0.0', + 'name' => 'migrating-hook', + 'version' => 'v2.0.0', '--force-publish' => true, ]); diff --git a/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php b/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php index 055a104..04c296a 100644 --- a/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php +++ b/tests/fixtures/resources/database/migrations/2018_01_20_120000_create_local_test_hook_table.php @@ -1,8 +1,8 @@ 'baz'], ]); } -} \ No newline at end of file +} diff --git a/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php b/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php index 730c18b..d0fe7dc 100644 --- a/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php +++ b/tests/fixtures/resources/database/unseeders/LocalTestHookTableUnseeder.php @@ -13,13 +13,13 @@ class LocalTestHookTableUnseeder extends Seeder */ public function run() { - // Skip if table does not exists. - if (!Schema::hasTable('local_test_hook')) { - return; - } + // Skip if table does not exists. + if (!Schema::hasTable('local_test_hook')) { + return; + } DB::table('local_test_hook') - ->whereIn('name', ['foo', 'bar', 'baz']) - ->delete(); + ->whereIn('name', ['foo', 'bar', 'baz']) + ->delete(); } } From 29e101fb2dcc6c832e21650faeb9b5fff858b1f5 Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Sat, 20 Jan 2018 18:17:15 +0100 Subject: [PATCH 3/7] Dont stop on failure --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c42dfcf..bb3ad53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,4 +19,4 @@ before_script: - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist script: - - vendor/bin/phpunit --stop-on-failure + - vendor/bin/phpunit From bf14c3c2605cb54ec911ca5785430b1cc703e65f Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Sun, 21 Jan 2018 13:23:24 +0100 Subject: [PATCH 4/7] Fix "Class migrator does not exist" --- src/Hooks.php | 50 +++++++++++++++++++----------------- src/HooksServiceProvider.php | 8 +++++- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/Hooks.php b/src/Hooks.php index 896938d..2a2cf97 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Illuminate\Filesystem\Filesystem; +use Illuminate\Database\Migrations\Migrator; use Symfony\Component\Console\Input\ArrayInput; class Hooks @@ -11,6 +12,7 @@ class Hooks protected static $remote = 'https://larapack.io'; protected $filesystem; + protected $migrator; protected $hooks; protected $lastRemoteCheck; protected $outdated = []; @@ -29,9 +31,11 @@ class Hooks protected static $fakeDateTime = false; - public function __construct(Filesystem $filesystem) + public function __construct(Filesystem $filesystem, Migrator $migrator) { $this->filesystem = $filesystem; + $this->migrator = $migrator; + $this->prepareComposer(); $this->readOutdated(); $this->readJsonFile(); @@ -319,7 +323,7 @@ public function uninstall($name, $delete = false, $unmigrate = true, $unseed = t * * @return bool */ - public function update($name, $version = null, $migrate = true, $seed = true, $publish = true, $forcePublishing) + public function update($name, $version, $migrate, $seed, $publish, $forcePublishing) { // Check if hook exists if (!$this->downloaded($name)) { @@ -495,10 +499,10 @@ public function make($name) protected function makeStubFiles($name) { $replaces = [ - 'kebab-case' => $name, - 'snake_case' => snake_case(str_replace('-', '_', $name)), - 'camelCase' => camel_case(str_replace('-', '_', $name)), - 'StudlyCase' => studly_case(str_replace('-', '_', $name)), + 'kebab-case' => $name, + 'snake_case' => snake_case(str_replace('-', '_', $name)), + 'camelCase' => camel_case(str_replace('-', '_', $name)), + 'StudlyCase' => studly_case(str_replace('-', '_', $name)), 'MIGRATION_DATE_TIME' => $this->migrationDateTimeString(), ]; @@ -891,31 +895,31 @@ public function checkForUpdates() /** * Run migrations found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function migrateHook(Hook $hook) { $migrations = (array) $hook->getComposerHookKey('migrations', []); - app('migrator')->run($this->realPath($migrations, $hook->getPath().'/')->all()); + $this->migrator->run($this->realPath($migrations, $hook->getPath().'/')->all()); } /** * Rollback migrations found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function unmigrateHook(Hook $hook) { $migrations = (array) $hook->getComposerHookKey('migrations', []); - app('migrator')->reset($this->realPath($migrations, $hook->getPath().'/')->all()); + $this->migrator->reset($this->realPath($migrations, $hook->getPath().'/')->all()); } /** * Run seeders found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function seedHook(Hook $hook) { @@ -928,7 +932,7 @@ protected function seedHook(Hook $hook) /** * Run unseeders found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function unseedHook(Hook $hook) { @@ -941,7 +945,7 @@ protected function unseedHook(Hook $hook) /** * Publish assets found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function publishHook(Hook $hook, $force = false) { @@ -983,7 +987,7 @@ protected function publishHook(Hook $hook, $force = false) $newFiles->merge($updatedFiles) ->each(function ($filename) use ($realLocation, $publishPath, $filesystem) { - $directory = substr($publishPath.'/'.$filename, 0, - strlen(basename($filename))); + $directory = substr($publishPath.'/'.$filename, 0, -strlen(basename($filename))); if (!$filesystem->isDirectory($directory)) { $filesystem->makeDirectory($directory, 0755, true, true); @@ -1001,7 +1005,7 @@ protected function publishHook(Hook $hook, $force = false) /** * Unpublish assets found for a specific hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook */ protected function unpublishHook(Hook $hook) { @@ -1033,8 +1037,8 @@ protected function unpublishHook(Hook $hook) /** * Run seeder files. * - * @param array $folders - * @param string $basePath + * @param array $folders + * @param string $basePath */ protected function runSeeders($folders, $basePath) { @@ -1046,9 +1050,9 @@ protected function runSeeders($folders, $basePath) return $file->getExtension() == 'php'; })->each(function ($file) { $class = substr($file->getFilename(), 0, -4); - require_once($file->getRealPath()); + require_once $file->getRealPath(); - (new $class)->run(); + (new $class())->run(); }); }); } @@ -1056,8 +1060,8 @@ protected function runSeeders($folders, $basePath) /** * Get collection of realpath paths. * - * @param array $paths - * @param string $basePath + * @param array $paths + * @param string $basePath * * @return \Illuminate\Support\Collection */ @@ -1071,9 +1075,9 @@ protected function realPath(array $paths, $basePath = '') } /** - * Make temponary backup of hook + * Make temponary backup of hook. * - * @param \Larapack\Hooks\Hook $hook + * @param \Larapack\Hooks\Hook $hook * * @return void */ diff --git a/src/HooksServiceProvider.php b/src/HooksServiceProvider.php index 48d408f..73304d4 100644 --- a/src/HooksServiceProvider.php +++ b/src/HooksServiceProvider.php @@ -2,6 +2,7 @@ namespace Larapack\Hooks; +use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; @@ -31,7 +32,12 @@ public function register() } // Register Hooks system and aliases - $this->app->singleton(Hooks::class, Hooks::class); + $this->app->singleton(Hooks::class, function ($app) { + $filesystem = $app[Filesystem::class]; + $migrator = $app['migrator']; + + return new Hooks($filesystem, $migrator); + }); $this->app->alias(Hooks::class, 'hooks'); } From c1ef1bd05c028dbff6b3700917d1026de0129cb6 Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Sun, 21 Jan 2018 12:23:52 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI --- src/Hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hooks.php b/src/Hooks.php index 2a2cf97..2cfce56 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -3,8 +3,8 @@ namespace Larapack\Hooks; use Carbon\Carbon; -use Illuminate\Filesystem\Filesystem; use Illuminate\Database\Migrations\Migrator; +use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Input\ArrayInput; class Hooks From b7d27b3cb3d3d32b355dbed9d0b81af284e11d42 Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Sun, 21 Jan 2018 14:10:06 +0100 Subject: [PATCH 6/7] Fix installing local version --- src/Hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Hooks.php b/src/Hooks.php index 2a2cf97..f5759a1 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -27,7 +27,7 @@ class Hooks protected static $useVersionWildcardOnUpdate = false; protected static $versionWildcard = '*'; - protected static $localVersion = 'dev-master'; + protected static $localVersion = '*'; protected static $fakeDateTime = false; From 018bd65d736644c40a5c6573dc40c9dc6957ed1c Mon Sep 17 00:00:00 2001 From: Mark Topper Date: Sun, 21 Jan 2018 14:54:38 +0100 Subject: [PATCH 7/7] Rename options --- src/Commands/InstallCommand.php | 8 ++++---- src/Commands/UninstallCommand.php | 8 ++++---- src/Commands/UpdateCommand.php | 10 +++++----- src/Hooks.php | 10 +++++++--- tests/HooksTest.php | 28 ++++++++++++++-------------- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index dee5880..b671f0e 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -7,7 +7,7 @@ class InstallCommand extends Command { - protected $signature = 'hook:install {name} {version?} {--enable} {--without-migrating} {--without-seeding} {--without-publishing}'; + protected $signature = 'hook:install {name} {version?} {--enable} {--no-migrate} {--no-seed} {--no-publish}'; protected $description = 'Download and install a hook from remote https://larapack.io'; @@ -32,9 +32,9 @@ public function handle() $this->hooks->install( $name, $this->argument('version'), - !$this->option('without-migrating'), - !$this->option('without-seeding'), - !$this->option('without-publishing') + !$this->option('no-migrate'), + !$this->option('no-seed'), + !$this->option('no-publish') ); if ($this->option('enable')) { diff --git a/src/Commands/UninstallCommand.php b/src/Commands/UninstallCommand.php index 800fa9a..8db6449 100644 --- a/src/Commands/UninstallCommand.php +++ b/src/Commands/UninstallCommand.php @@ -7,7 +7,7 @@ class UninstallCommand extends Command { - protected $signature = 'hook:uninstall {name} {--delete} {--without-unmigrating} {--without-unseeding} {--without-unpublishing}'; + protected $signature = 'hook:uninstall {name} {--delete} {--no-unmigrate} {--no-unseed} {--no-unpublish}'; protected $description = 'Uninstall a hook'; @@ -32,9 +32,9 @@ public function handle() $this->hooks->uninstall( $name, $this->option('delete'), - !$this->option('without-unmigrating'), - !$this->option('without-unseeding'), - !$this->option('without-unpublishing') + !$this->option('no-unmigrate'), + !$this->option('no-unseed'), + !$this->option('no-unpublish') ); $this->info("Hook [{$name}] have been uninstalled."); diff --git a/src/Commands/UpdateCommand.php b/src/Commands/UpdateCommand.php index 7082e3e..1dd73e2 100644 --- a/src/Commands/UpdateCommand.php +++ b/src/Commands/UpdateCommand.php @@ -7,7 +7,7 @@ class UpdateCommand extends Command { - protected $signature = 'hook:update {name} {version?} {--without-migrating} {--without-seeding} {--without-publishing} {--force-publish}'; + protected $signature = 'hook:update {name} {version?} {--no-migrate} {--no-seed} {--no-publish} {--force}'; protected $description = 'Update a hook'; @@ -38,10 +38,10 @@ public function handle() $updated = $this->hooks->update( $name, $version, - !$this->option('without-migrating'), - !$this->option('without-seeding'), - !$this->option('without-publishing'), - $this->option('force-publish') + !$this->option('no-migrate'), + !$this->option('no-seed'), + !$this->option('no-publish'), + $this->option('force') ); return $updated diff --git a/src/Hooks.php b/src/Hooks.php index 2087cbf..da11e21 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -317,13 +317,17 @@ public function uninstall($name, $delete = false, $unmigrate = true, $unseed = t * * @param $name * @param string|null $version + * @param bool $migrate + * @param bool $seed + * @param bool $publish + * @param bool $force * * @throws \Larapack\Hooks\Exceptions\HookNotFoundException * @throws \Larapack\Hooks\Exceptions\HookNotInstalledException * * @return bool */ - public function update($name, $version, $migrate, $seed, $publish, $forcePublishing) + public function update($name, $version, $migrate = true, $seed = true, $publish = true, $force = false) { // Check if hook exists if (!$this->downloaded($name)) { @@ -348,7 +352,7 @@ public function update($name, $version, $migrate, $seed, $publish, $forcePublish } } - if (!$forcePublishing) { + if (!$force) { $this->makeTemponaryBackup($this->hooks[$name]); } @@ -379,7 +383,7 @@ public function update($name, $version, $migrate, $seed, $publish, $forcePublish } if ($publish) { - $this->publishHook($this->hooks[$name], $forcePublishing); + $this->publishHook($this->hooks[$name], $force); } $this->clearTemponaryFiles(); diff --git a/tests/HooksTest.php b/tests/HooksTest.php index 4b23baf..2f08137 100644 --- a/tests/HooksTest.php +++ b/tests/HooksTest.php @@ -677,8 +677,8 @@ public function test_installing_without_migrating_hook() $this->artisan('hook:install', [ 'name' => 'migrating-hook', 'version' => 'v1.0.0', - '--without-migrating' => true, - '--without-seeding' => true, + '--no-migrate' => true, + '--no-seed' => true, ]); $this->assertFalse(Schema::hasTable('tests')); @@ -729,7 +729,7 @@ public function test_uninstalling_without_unmigrating_hook() // Uninstall hook $this->artisan('hook:uninstall', [ 'name' => 'migrating-hook', - '--without-unmigrating' => true, + '--no-unmigrate' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -795,8 +795,8 @@ public function test_updating_without_remigrating_hook() $this->artisan('hook:update', [ 'name' => 'migrating-hook', 'version' => 'v2.0.0', - '--without-migrating' => true, - '--without-seeding' => true, + '--no-migrate' => true, + '--no-seed' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -831,7 +831,7 @@ public function test_installing_without_seeding_hook() $this->artisan('hook:install', [ 'name' => 'migrating-hook', 'version' => 'v1.0.0', - '--without-seeding' => true, + '--no-seed' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -856,7 +856,7 @@ public function test_unseeding_hook() // Uninstall hook $this->artisan('hook:uninstall', [ 'name' => 'migrating-hook', - '--without-unmigrating' => true, + '--no-unmigrate' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -881,8 +881,8 @@ public function test_uninstalling_without_unseeding() // Uninstall hook $this->artisan('hook:uninstall', [ 'name' => 'migrating-hook', - '--without-unmigrating' => true, - '--without-unseeding' => true, + '--no-unmigrate' => true, + '--no-unseed' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -935,7 +935,7 @@ public function test_updating_without_reseeding() $this->artisan('hook:update', [ 'name' => 'migrating-hook', 'version' => 'v2.0.0', - '--without-seeding' => true, + '--no-seed' => true, ]); $this->assertTrue(Schema::hasTable('tests')); @@ -973,7 +973,7 @@ public function test_installing_without_publishing_assets() $this->artisan('hook:install', [ 'name' => 'migrating-hook', 'version' => 'v1.0.0', - '--without-publishing' => true, + '--no-publish' => true, ]); $this->assertFalse($filesystem->exists(base_path('public/vendor'))); @@ -1061,7 +1061,7 @@ public function test_uninstalling_without_unpublishing_assets() // Uninstall hook $this->artisan('hook:uninstall', [ 'name' => 'migrating-hook', - '--without-unpublishing' => true, + '--no-unpublish' => true, ]); $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets'))); @@ -1127,7 +1127,7 @@ public function test_updating_without_republishing_assets() $this->artisan('hook:update', [ 'name' => 'migrating-hook', 'version' => 'v2.0.0', - '--without-publishing' => true, + '--no-publish' => true, ]); $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js'))); @@ -1203,7 +1203,7 @@ public function test_force_republishing_assets_on_update_with_custom_changed_fil $this->artisan('hook:update', [ 'name' => 'migrating-hook', 'version' => 'v2.0.0', - '--force-publish' => true, + '--force' => true, ]); $this->assertTrue($filesystem->exists(base_path('public/vendor/migration-hook/assets/script.js')));