Skip to content

Commit

Permalink
Merge pull request #1 from caneco/release-1.1.0
Browse files Browse the repository at this point in the history
[1.1.0] added a couple of features and tests
  • Loading branch information
caneco authored Jan 29, 2019
2 parents 98a804a + 367bc1c commit 13ceec6
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ build
composer.lock
vendor
tests/temp
.idea
.idea
.phpintel
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<p align="center"><img src="https://raw.githubusercontent.com/caneco/artisan-aliases/master/art/logo.png" width="400"/></p>
<p align="center"><img src="https://raw.githubusercontent.com/caneco/artisan-aliases/master/art/logo.png" width="400"/>
</p><br>

<p align="center"><img src="https://raw.githubusercontent.com/caneco/artisan-aliases/master/art/preview.png"/></p>
<p align="center"><img src="https://raw.githubusercontent.com/caneco/artisan-aliases/master/art/preview.png" width="560"/></p>

<p align="center">
<a href="https://packagist.org/packages/caneco/artisan-aliases"><img src="https://poser.pugx.org/caneco/artisan-aliases/d/total.svg" alt="Total Downloads"></a>
Expand Down Expand Up @@ -100,7 +101,7 @@ To list the current alias available you can run the following command:

```
>_ php artisan alias --list
Laravel `Artisan Aliases` 1.0.0
Laravel `Artisan Aliases` 1.1.0
Usage:
alias [-g|--global] [--] [<as>]
Expand Down Expand Up @@ -206,16 +207,16 @@ Laravel Framework | Artisan Alias

## Road map

Artisan Alias was released so you can use it normally, but there's still some things that I would like to see in the package.
Artisan Alias is stable but there is still some things that I would like to add in the package.

Here's the plan for what's coming:

- [ ] Remove an existing alias using the option `--d|delete`
- [ ] Allow to replace an existing alias using the option `--force`
- [ ] Firing a `@handle` method if
- [ ] Firing a `@handle` method if alias has a `::class` reference
- [ ] Alert the user try to add an alias with `sudo` in the command **(usefull?)**
- [ ] Add comments
- [ ] Add tests
- [x] Allow to replace an existing alias using the option `--force`
- [x] Add tests



Expand Down
15 changes: 10 additions & 5 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter">
<testsuites>
<testsuite name="ArtisanAliases Test Suite">
<directory>tests</directory>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Functional">
<directory suffix="Test.php">./tests/Functional</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>
16 changes: 10 additions & 6 deletions src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

class Alias
{
public const VERSION = '1.0.0';
public const VERSION = '1.1.0';

public static function store(string $name, string $line, bool $global)
public static function store(string $name, string $line, ?bool $global = false, ?bool $force = false)
{
$manager = new AliasManager(
$global ? AliasFile::global() : AliasFile::local()
);

return $manager->save(new AliasModel($name, $line));
return self::manager($global)->save(new AliasModel($name, $line), $force);
}

public static function load(?string $location = null): array
Expand Down Expand Up @@ -43,4 +40,11 @@ public static function global(): array
AliasFile::global()
))->load();
}

public static function manager(bool $global): AliasManager
{
return new AliasManager(
$global ? AliasFile::global() : AliasFile::local()
);
}
}
4 changes: 1 addition & 3 deletions src/AliasFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Caneco\ArtisanAliases;

use Illuminate\Support\Facades\Config;

class AliasFile
{
public static function local(): string
Expand All @@ -15,4 +13,4 @@ public static function global(): string
{
return $_SERVER['HOME'] . '/.laravel_aliases';
}
}
}
13 changes: 8 additions & 5 deletions src/AliasManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Caneco\ArtisanAliases;

use Caneco\ArtisanAliases\Alias;
use Caneco\ArtisanAliases\AliasDirectory;
use Caneco\ArtisanAliases\AliasModel;
use Caneco\ArtisanAliases\Exceptions\AliasException;
use Caneco\ArtisanAliases\Helpers\Art;
use Illuminate\Support\Facades\Artisan;
use sixlive\DotenvEditor\DotenvEditor;
use sixlive\DotenvEditor\EnvFile;

class AliasManager
{
Expand Down Expand Up @@ -50,13 +53,13 @@ public function safeLoadFile(): array
}
}

public function save(AliasModel $command)
public function save(AliasModel $command, ?bool $force = false)
{
if (! file_exists($this->filePath)) {
throw new AliasException("Expected file '{$this->filePath}' does not exists!");
}

if (array_key_exists($command->getName(), Artisan::all())) {
if (array_key_exists($command->getName(), Artisan::all()) && !$force) {
throw new AliasException("Artisan command/alias already exists!");
}

Expand All @@ -67,13 +70,13 @@ public function save(AliasModel $command)
return $file->save();
}

public function looksValid(string $string): bool
private function looksValid(string $string): bool
{
return strpos($string, '=') !== false;
}

public function isNotComment(string $string): bool
private function isNotComment(string $string): bool
{
return isset($string[0]) && $string[0] !== '#';
}
}
}
4 changes: 3 additions & 1 deletion src/AliasModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Caneco\ArtisanAliases;

use Caneco\ArtisanAliases\Exceptions\AliasException;

class AliasModel
{
private $name;
Expand Down Expand Up @@ -72,4 +74,4 @@ public function sanitizeCommand(string $string): string

return trim($string);
}
}
}
4 changes: 2 additions & 2 deletions src/ArtisanAliasesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private function registerPublishing()
], 'config');

$this->publishes([
__DIR__.'/../stubs/.laravel_aliases' => base_path('.laravel_aliases'),
__DIR__.'/../stubs/laravel_aliases' => base_path('.laravel_aliases'),
], 'aliases');
}

Expand All @@ -52,4 +52,4 @@ function () use ($alias, $command) {
$this->commands($list ?? []);
}
}
}
}
41 changes: 26 additions & 15 deletions src/Commands/AliasCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@ class AliasCommand extends Command
{name? : The name of the alias}
{line? : The command line to be aliased}
{--list : list all alias defined}
{--g|global : Add alias globally [`~/.laravel_alias`]}
{--force : Force alias to be replaced}';
{--force : Force alias to be replaced}
{--g|global : Add alias globally [`~/.laravel_alias`]}';

protected $description = 'Create an alias of another command.';

public function handle()
{
if (empty($this->argument('name'))
or empty($this->argument('line'))
or $this->option('list')
) {
$this->handleList();
exit;
if (empty($this->argument('name')) or $this->option('list')) {
return $this->listAliases();
}

Alias::store(
trim($this->argument('name')),
trim($this->argument('line')),
$this->option('global')
);
if (! empty($this->argument('line'))) {
return $this->storeAlias();
}

return $this->listAliases();
}

private function handleList()
private function listAliases()
{
$list = Alias::load();

Expand All @@ -59,4 +55,19 @@ private function handleList()
$this->getOutput()->block('All aliases are currently disabled, to enable set `enabled = true` in your config file to use them.', 'Notice', 'fg=black;bg=yellow', '! ', true);
}
}
}

private function storeAlias()
{
Alias::store(
trim($this->argument('name')),
trim($this->argument('line')),
$this->option('global'),
$this->option('force')
);
}

// private function deleteAlias()
// {
// // SOON
// }
}
2 changes: 1 addition & 1 deletion src/Commands/AliasedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ private function process(string $command)
$this->error($e->getMessage());
}
}
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/AliasException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
class AliasException extends RuntimeException
{
//
}
}
File renamed without changes.
89 changes: 89 additions & 0 deletions tests/Feature/AliasManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Caneco\ArtisanAliases\Tests\Feature;

use Caneco\ArtisanAliases\AliasManager;
use Caneco\ArtisanAliases\AliasModel;
use Caneco\ArtisanAliases\Exceptions\AliasException;
use Illuminate\Support\Facades\Artisan;
use PHPUnit\Framework\TestCase;

class AliasManagerTest extends TestCase
{
protected $path = __DIR__.'/../tmp/laravel_aliases';

public function setUp()
{
copy(__DIR__.'/../../stubs/laravel_aliases', $this->path);

Artisan::shouldReceive('all')
->andReturn(['laravel' => 'inspire']);
}

public function tearDown()
{
array_map('unlink', glob(__DIR__.'/tmp/*'));
}

/** @test */
public function an_exception_is_not_thrown_if_the_file_does_not_exist()
{
$manager = new AliasManager('MISSING_' . $this->path);

$this->assertEmpty($manager->safeLoadFile());
}

/** @test */
public function stub_file_contains_the_initial_aliases()
{
$manager = new AliasManager($this->path);

$this->assertEquals(
'laravel=inspire,# cc=clear-compiled',
implode(',', $manager->loadFile())
);
}

/** @test */
public function initial_aliases_from_stub_are_imported_on_load()
{
$manager = new AliasManager($this->path);

$this->assertEquals(
'{"laravel":"inspire"}',
json_encode($manager->load())
);
}

/** @test */
public function an_alias_can_be_saved()
{
$manager = new AliasManager($this->path);
$manager->save(AliasModel::parse('example=alias'));
$alias = $manager->load();

$this->assertArrayHasKey('example', $alias);
$this->assertContains('alias', array_values($alias));
}

/** @test */
public function an_exception_is_thrown_storing_an_alias_that_already_exists()
{
$this->expectException(AliasException::class);
$this->expectExceptionMessage("Artisan command/alias already exists!");

$manager = new AliasManager($this->path);
$manager->save(AliasModel::parse('laravel=expires'));
}

/** @test */
public function force_storing_an_alias_that_already_exists()
{
$manager = new AliasManager($this->path);
$manager->save(AliasModel::parse('laravel=expires'), $force = true);
$alias = $manager->load();

$this->assertArrayHasKey('laravel', $alias);
$this->assertContains('expires', array_values($alias));
}
}
Loading

0 comments on commit 13ceec6

Please sign in to comment.