Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Gaal committed Aug 10, 2022
1 parent c37a919 commit 97b6c99
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 15 deletions.
8 changes: 4 additions & 4 deletions app/Commands/ExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function handle()
try {
$this->getProjectAliases();

if ($alias && !in_array($alias, array_keys($this->aliases))) {
if ($alias && ! in_array($alias, array_keys($this->aliases))) {
$this->components->error("Alias $alias not found.");
}

if (!$alias) {
if (! $alias) {
$alias = $this->choice('Please select alias to execute', array_keys($this->aliases));
}

Expand Down Expand Up @@ -54,8 +54,8 @@ public function handle()
protected function getProjectAliases(): array
{
$file = match (true) {
File::exists($path = getcwd() . '/aliases.dev.json') => File::get($path),
File::exists($path = getcwd() . '/aliases.json') => File::get($path),
File::exists($path = getcwd().'/aliases.dev.json') => File::get($path),
File::exists($path = getcwd().'/aliases.json') => File::get($path),
default => throw new FileNotFoundException('aliases.json or aliases.dev.json not found.')
};

Expand Down
28 changes: 28 additions & 0 deletions app/Commands/SetupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Commands;

use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;

class SetupCommand extends Command
{
protected $signature = 'setup';

protected $description = 'Runs a few commands to setup the project.';

public function handle()
{
if (File::exists('aliases.json')) {
$overwrite = $this->confirm('aliases.json already exists. Do you want to overwrite it?');

if ($overwrite === false) {
return;
}
}

File::copy(base_path().'/stubs/aliases.json.stub', getcwd().'/aliases.json');

$this->components->info('aliases.json created.');
}
}
1 change: 1 addition & 0 deletions box.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app",
"bootstrap",
"config",
"stubs",
"vendor"
],
"files": [
Expand Down
Binary file modified builds/alias
Binary file not shown.
5 changes: 5 additions & 0 deletions stubs/aliases.json.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"about": [
"echo 'A PHP CLI application that helps you organize your aliases.'"
]
}
2 changes: 1 addition & 1 deletion tests/Feature/ExecuteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
->expectsOutputToContain('Executing: about.')
->expectsOutputToContain('A PHP CLI application that helps you organize your aliases.')
->assertExitCode(0);
});
});
26 changes: 26 additions & 0 deletions tests/Feature/SetupCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Support\Facades\File;

test('execute setup creates aliases.json file', function () {
if (File::exists(base_path().'/aliases.json')) {
File::delete(base_path().'/aliases.json');
}

$this->artisan('setup')
->assertExitCode(0);

$this->assertTrue(File::exists('aliases.json'));
});

test('execute setup asks for overwriting aliases.json file', function () {
File::put(
base_path().'/aliases.json',
File::get(base_path().'/stubs/aliases.json.stub')
);

/** @var \Illuminate\Testing\PendingCommand $output */
$this->artisan('setup')
->expectsConfirmation('aliases.json already exists. Do you want to overwrite it?')
->assertExitCode(0);
});
20 changes: 10 additions & 10 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
|
*/

uses(Tests\TestCase::class)->in('Feature');
use Illuminate\Support\Facades\File;

uses(Tests\TestCase::class)
->afterEach(function () {
File::put(
base_path().'/aliases.json',
File::get(base_path().'/stubs/aliases.json.stub')
);
})
->in('Feature');

/*
|--------------------------------------------------------------------------
Expand All @@ -24,10 +33,6 @@
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
Expand All @@ -38,8 +43,3 @@
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}

0 comments on commit 97b6c99

Please sign in to comment.