Skip to content

Commit

Permalink
gh-26 refactor Created Storage for avoid php-mock and simplify filesy…
Browse files Browse the repository at this point in the history
…stem access
  • Loading branch information
Wtyd committed Jul 9, 2024
1 parent 869373f commit e9b1bea
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 113 deletions.
12 changes: 4 additions & 8 deletions app/Commands/CleanHookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -39,23 +40,18 @@ 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 {
$this->printer->error("Could not delete $hook hook");
return 1;
}
}

public function getHooksPath(): string
{
return getcwd() . "/.git/hooks";
}
}
35 changes: 15 additions & 20 deletions app/Commands/CreateHookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
*
Expand All @@ -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')) ?? '';

Expand All @@ -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}");
Expand All @@ -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;
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions qa/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/ConfigurationFile/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
3 changes: 3 additions & 0 deletions src/ConfigurationFile/FileReaderFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions src/Utils/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Storage
{
/** @var string */
public static $disk = 'local';

/**
* Determine if a file or directory exists.
*
Expand Down Expand Up @@ -46,4 +47,55 @@ public static function put($path, $contents, $lock = false)
{
return FacadesStorage::disk(self::$disk)->put($path, $contents, $lock = false);

Check warning on line 48 in src/Utils/Storage.php

View workflow job for this annotation

GitHub Actions / Infection (ubuntu-latest, 8.3)

Escaped Mutant for Mutator "FalseValue": @@ @@ */ public static function put($path, $contents, $lock = false) { - return FacadesStorage::disk(self::$disk)->put($path, $contents, $lock = false); + return FacadesStorage::disk(self::$disk)->put($path, $contents, $lock = true); } /** * Get the contents of a file.

Check warning on line 48 in src/Utils/Storage.php

View workflow job for this annotation

GitHub Actions / Infection (ubuntu-latest, 8.3)

Escaped Mutant for Mutator "FalseValue": @@ @@ */ public static function put($path, $contents, $lock = false) { - return FacadesStorage::disk(self::$disk)->put($path, $contents, $lock = false); + return FacadesStorage::disk(self::$disk)->put($path, $contents, $lock = true); } /** * Get the contents of a file.
}

/**
* 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);
}
}
40 changes: 4 additions & 36 deletions tests/System/Commands/CleanHookCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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")
Expand Down
52 changes: 8 additions & 44 deletions tests/System/Commands/CreateHookCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -43,37 +26,18 @@ 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()
{
$this->artisan('hook')
->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()
Expand Down Expand Up @@ -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 */
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/ConfigurationFile/FileReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(''));
Expand Down
Loading

0 comments on commit e9b1bea

Please sign in to comment.