From e9b1beaf573936acf1b9bacc0c37cdf9ed06c76e Mon Sep 17 00:00:00 2001 From: Wtyd Date: Tue, 9 Jul 2024 22:21:27 +0000 Subject: [PATCH] gh-26 refactor Created Storage for avoid php-mock and simplify filesystem access --- app/Commands/CleanHookCommand.php | 12 ++--- app/Commands/CreateHookCommand.php | 35 ++++++------- composer.json | 1 - qa/phpstan.neon | 1 + src/ConfigurationFile/FileReader.php | 1 - src/ConfigurationFile/FileReaderFake.php | 3 ++ src/Utils/Storage.php | 52 +++++++++++++++++++ .../System/Commands/CleanHookCommandTest.php | 40 ++------------ .../System/Commands/CreateHookCommandTest.php | 52 +++---------------- .../Unit/ConfigurationFile/FileReaderTest.php | 3 -- tests/Utils/Traits/FileSystemTrait.php | 3 ++ 11 files changed, 90 insertions(+), 113 deletions(-) diff --git a/app/Commands/CleanHookCommand.php b/app/Commands/CleanHookCommand.php index e22deed7..8e7e760a 100644 --- a/app/Commands/CleanHookCommand.php +++ b/app/Commands/CleanHookCommand.php @@ -5,6 +5,7 @@ use LaravelZero\Framework\Commands\Command; use Wtyd\GitHooks\Hooks; use Wtyd\GitHooks\Utils\Printer; +use Wtyd\GitHooks\Utils\Storage; class CleanHookCommand extends Command { @@ -39,13 +40,13 @@ public function handle() return 1; } - $file = $this->getHooksPath() . "/$hook"; - if (!file_exists($file)) { + $file = ".git/hooks/$hook"; + if (!Storage::exists($file)) { $this->printer->warning("The hook $hook cannot be deleted because it cannot be found"); return 1; } - if (unlink($file)) { + if (Storage::delete($file)) { $this->printer->success("Hook $hook has been deleted"); return 0; } else { @@ -53,9 +54,4 @@ public function handle() return 1; } } - - public function getHooksPath(): string - { - return getcwd() . "/.git/hooks"; - } } diff --git a/app/Commands/CreateHookCommand.php b/app/Commands/CreateHookCommand.php index baf9028d..a4e68ffc 100644 --- a/app/Commands/CreateHookCommand.php +++ b/app/Commands/CreateHookCommand.php @@ -3,9 +3,10 @@ namespace Wtyd\GitHooks\App\Commands; use Exception; +use LaravelZero\Framework\Commands\Command; use Wtyd\GitHooks\Hooks; use Wtyd\GitHooks\Utils\Printer; -use LaravelZero\Framework\Commands\Command; +use Wtyd\GitHooks\Utils\Storage; class CreateHookCommand extends Command { @@ -25,13 +26,6 @@ class CreateHookCommand extends Command */ protected $printer; - /** - * Path to the root project (getcwd()) - * - * @var string|false - */ - protected $root; - /** * First argument. The hook that will be setted. * @@ -54,7 +48,6 @@ public function __construct(Printer $printer) public function handle() { - $this->root = getcwd(); $this->hook = strval($this->argument('hook')); $this->scriptFile = strval($this->argument('scriptFile')) ?? ''; @@ -66,13 +59,15 @@ public function handle() $origin = $this->path2OriginFile(); try { - $destiny = "{$this->root}/.git/hooks/{$this->hook}"; + $destiny = ".git/hooks/{$this->hook}"; - if (file_exists($destiny)) { - unlink($destiny); + if (Storage::exists($destiny)) { + Storage::delete($destiny); } - copy($origin, $destiny); - chmod($destiny, 0755); + + Storage::copy($origin, $destiny); + Storage::chmod($destiny, 0755); + $this->printer->success("Hook {$this->hook} created"); } catch (\Throwable $th) { $this->printer->error("Error copying $origin in {$this->hook}"); @@ -91,7 +86,7 @@ public function path2OriginFile(): string if (empty($this->scriptFile)) { $origin = $this->defaultPrecommit(); } else { - if (!file_exists($this->scriptFile)) { + if (!Storage::exists($this->scriptFile)) { throw new Exception("{$this->scriptFile} file not found"); } $origin = $this->scriptFile; @@ -109,16 +104,16 @@ public function path2OriginFile(): string public function defaultPrecommit(): string { $origin = ''; - if (file_exists($this->root . '/vendor/wtyd/githooks/hooks/default.php')) { - $origin = $this->root . '/vendor/wtyd/githooks/hooks/default.php'; + if (Storage::exists('vendor/wtyd/githooks/hooks/default.php')) { + $origin = 'vendor/wtyd/githooks/hooks/default.php'; } - if (file_exists($this->root . '/hooks/default.php')) { - $origin = $this->root . '/hooks/default.php'; + if (Storage::exists('hooks/default.php')) { + $origin = 'hooks/default.php'; } if (empty($origin)) { - throw new Exception("Error: the file default.php not found"); + throw new Exception("The file default.php not found"); } return $origin; diff --git a/composer.json b/composer.json index 7d2c1105..595f42cc 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,6 @@ "laravel-zero/phar-updater": "^1.0.6", "mikey179/vfsstream": "^1.6", "mockery/mockery": "^1.3", - "php-mock/php-mock": "^2.2", "php-parallel-lint/php-parallel-lint": "^1.2", "phpmd/phpmd": "^2.9", "phpstan/phpstan": "^1.4", diff --git a/qa/phpstan.neon b/qa/phpstan.neon index 0d6fa27d..df16e606 100644 --- a/qa/phpstan.neon +++ b/qa/phpstan.neon @@ -10,6 +10,7 @@ checkMissingIterableValueType: false ignoreErrors: [ '#Call to an undefined static method Illuminate\\Support\\Facades\\Storage::[a-zA-Z\\_]+\(\)#', # Needed for php 7.1 + '#Call to an undefined method Illuminate\\Contracts\\Filesystem\\Filesystem::path\(\).#' ] excludePaths: [ %currentWorkingDirectory%/src/Tools/Process/, # inherits from Symfony diff --git a/src/ConfigurationFile/FileReader.php b/src/ConfigurationFile/FileReader.php index 23d309c9..705a20b8 100644 --- a/src/ConfigurationFile/FileReader.php +++ b/src/ConfigurationFile/FileReader.php @@ -47,7 +47,6 @@ public function readFile(): array */ public function findConfigurationFile(): string { - // dd(file_exists("$this->rootPath/githooks.yml"), file_exists("$this->rootPath/qa/githooks.yml")); if (file_exists("$this->rootPath/githooks.yml")) { $configFile = "$this->rootPath/githooks.yml"; } elseif (file_exists("$this->rootPath/qa/githooks.yml")) { diff --git a/src/ConfigurationFile/FileReaderFake.php b/src/ConfigurationFile/FileReaderFake.php index 7d7090e0..f7b10f11 100644 --- a/src/ConfigurationFile/FileReaderFake.php +++ b/src/ConfigurationFile/FileReaderFake.php @@ -30,6 +30,9 @@ public function readFile(): array } } + /** + * @param array $configurationFile Configuration file in associative array format for testing + */ public function mockConfigurationFile(array $configurationFile): void { $this->mockConfigurationFile = $configurationFile; diff --git a/src/Utils/Storage.php b/src/Utils/Storage.php index 2d2ba1e8..8d6c13f1 100644 --- a/src/Utils/Storage.php +++ b/src/Utils/Storage.php @@ -10,6 +10,7 @@ class Storage { /** @var string */ public static $disk = 'local'; + /** * Determine if a file or directory exists. * @@ -46,4 +47,55 @@ public static function put($path, $contents, $lock = false) { return FacadesStorage::disk(self::$disk)->put($path, $contents, $lock = false); } + + /** + * Get the contents of a file. + * + * @param string $path + * @return string + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + public static function get($path) + { + return FacadesStorage::disk(self::$disk)->get($path); + } + + /** + * Delete the file at a given path. + * + * @param string|array $paths + * @return bool + */ + public static function delete($paths) + { + return FacadesStorage::disk(self::$disk)->delete($paths); + } + + /** + * Create a directory. + * + * @param string $path + * @return bool + */ + public static function makeDirectory($path) + { + return FacadesStorage::disk(self::$disk)->makeDirectory($path); + } + + /** + * Get or set UNIX mode of a file or directory. + * + * @param string $path + * @param int|null $mode + * @return mixed + */ + public static function chmod($path, $mode = null) + { + if ($mode) { + return chmod(FacadesStorage::disk(self::$disk)->path($path), $mode); + } + + return substr(sprintf('%o', fileperms($path)), -4); + } } diff --git a/tests/System/Commands/CleanHookCommandTest.php b/tests/System/Commands/CleanHookCommandTest.php index 4f855215..4ea75a0a 100644 --- a/tests/System/Commands/CleanHookCommandTest.php +++ b/tests/System/Commands/CleanHookCommandTest.php @@ -2,54 +2,22 @@ namespace Tests\System\Commands; -use phpmock\MockBuilder; -use phpmock\Mock as PhpmockMock; use Tests\Utils\TestCase\SystemTestCase; +use Wtyd\GitHooks\Utils\Storage; class CleanHookCommandTest extends SystemTestCase { - - protected $configurationFile; - - protected $mock; - protected function setUp(): void { parent::setUp(); - mkdir($this->path . '/.git/hooks', 0777, true); - - $this->mock = $this->getMockRootDirectory(); - $this->mock->enable(); - } - - protected function tearDown(): void - { - $this->mock->disable(); - parent::tearDown(); - } - - /** - * @return PhpmockMock - */ - public function getMockRootDirectory(): PhpmockMock - { - $builder = new MockBuilder(); - $builder->setNamespace('Wtyd\GitHooks\App\Commands') - ->setName('getcwd') - ->setFunction( - function () { - return $this->getPath(); - } - ); - - return $builder->build(); + Storage::makeDirectory('.git/hooks', 0777, true); } /** @test */ function it_deletes_the_precommit_hook_as_default() { - file_put_contents($this->getPath() . '/.git/hooks/pre-commit', ''); + Storage::put('/.git/hooks/pre-commit', ''); $this->artisan('hook:clean') ->containsStringInOutput('Hook pre-commit has been deleted') @@ -96,7 +64,7 @@ public function hooksProvider() */ function it_deletes_the_hook_passed_as_argument($hook) { - file_put_contents($this->getPath() . '/.git/hooks/' . $hook, ''); + Storage::put("/.git/hooks/$hook", ''); $this->artisan("hook:clean $hook") ->containsStringInOutput("Hook $hook has been deleted") diff --git a/tests/System/Commands/CreateHookCommandTest.php b/tests/System/Commands/CreateHookCommandTest.php index e0b1fd33..0195aa0b 100644 --- a/tests/System/Commands/CreateHookCommandTest.php +++ b/tests/System/Commands/CreateHookCommandTest.php @@ -2,37 +2,20 @@ namespace Tests\System\Commands; -use phpmock\MockBuilder; -use phpmock\Mock as PhpmockMock; use Tests\Utils\TestCase\SystemTestCase; +use Wtyd\GitHooks\Utils\Storage; /** * Testing Wtyd\GitHooks\App\Commands\CreateHookCommand; */ class CreateHookCommandTest extends SystemTestCase { - protected $mock; - - /** - * Creates the temporal filesystem structure for the tests and mocks the 'getcwd' method for return this path. - * - * @return void - */ protected function setUp(): void { parent::setUp(); $this->copyDefaultPrecommitToTestDirectory(); - mkdir($this->path . '/.git/hooks', 0777, true); - - $this->mock = $this->getMockRootDirectory(); - $this->mock->enable(); - } - - protected function tearDown(): void - { - $this->mock->disable(); - parent::tearDown(); + Storage::makeDirectory('.git/hooks', 0777, true); } /** @@ -43,29 +26,10 @@ protected function tearDown(): void */ protected function copyDefaultPrecommitToTestDirectory() { - mkdir($this->path . '/hooks', 0777, true); + Storage::makeDirectory('/hooks', 0777, true); shell_exec('cp -r hooks ' . $this->path); } - /** - * Mocks the 'getcwd' method for return the root of the filesystem for this tests. - * - * @return PhpmockMock - */ - public function getMockRootDirectory(): PhpmockMock - { - $builder = new MockBuilder(); - $builder->setNamespace('Wtyd\GitHooks\App\Commands') - ->setName('getcwd') - ->setFunction( - function () { - return $this->path; - } - ); - - return $builder->build(); - } - /** @test */ function it_creates_default_script_for_precommit_when_is_called_without_arguments() { @@ -73,7 +37,7 @@ function it_creates_default_script_for_precommit_when_is_called_without_argument ->containsStringInOutput('Hook pre-commit created') ->assertExitCode(0); - $this->assertFileExists($this->path . '/.git/hooks/pre-commit', file_get_contents('hooks/default.php')); + $this->assertFileExists($this->path . '/.git/hooks/pre-commit', Storage::get('hooks/default.php')); } public function hooksProvider() @@ -130,14 +94,14 @@ function it_creates_default_script_in_the_hook_passed_as_argument($hook) function it_sets_a_custom_script_as_some_hook() { $hookContent = 'my custom script'; - $scriptFilePath = $this->path . '/MyScript.php'; - file_put_contents($scriptFilePath, $hookContent); + $scriptFile = 'MyScript.php'; + Storage::put($scriptFile, $hookContent); - $this->artisan("hook pre-push $scriptFilePath") + $this->artisan("hook pre-push $scriptFile") ->containsStringInOutput("Hook pre-push created") ->assertExitCode(0); - $this->assertFileExists($this->path . "/.git/hooks/pre-push", $scriptFilePath); + $this->assertFileExists($this->path . "/.git/hooks/pre-push", $scriptFile); } /** @test */ diff --git a/tests/Unit/ConfigurationFile/FileReaderTest.php b/tests/Unit/ConfigurationFile/FileReaderTest.php index cd00164a..b8b6fd6d 100644 --- a/tests/Unit/ConfigurationFile/FileReaderTest.php +++ b/tests/Unit/ConfigurationFile/FileReaderTest.php @@ -26,9 +26,6 @@ class FileReaderTest extends UnitTestCase /** \Tests\Utils\ConfigurationFileBuilder */ private $configurationFileBuilder; - /** \phpmock\PhpmockMock */ - private $mockRootDirectory; - protected function setUp(): void { $this->fileReader = new FileReaderFake($this->getUrl('')); diff --git a/tests/Utils/Traits/FileSystemTrait.php b/tests/Utils/Traits/FileSystemTrait.php index 06f03d3f..c6597c1f 100644 --- a/tests/Utils/Traits/FileSystemTrait.php +++ b/tests/Utils/Traits/FileSystemTrait.php @@ -27,6 +27,9 @@ public function getPath(): string */ public function createDirStructure(string $token = ''): void { + if (!empty($token)) { + dd($token); + } mkdir("$this->path/$token/src", 0777, true); mkdir("$this->path/$token/vendor"); }