Skip to content

Commit

Permalink
Add: tests for install command
Browse files Browse the repository at this point in the history
  • Loading branch information
jordyvanderhaegen committed Nov 20, 2023
1 parent 65f9101 commit 5d83f36
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Installers/FileInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function appendFileAfterSearchResultInFile(string $path, string $target,
);
}

public function fileContainsString(string $path, string $search): bool
public function fileContainsString(string $path, ?string $search): bool
{
return str_contains(
haystack: $this->filesystem->get($path),
needle: $search
needle: $search ?? ''
);
}
}
54 changes: 52 additions & 2 deletions tests/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,61 @@

namespace Esign\InstallCommand\Tests;

use Esign\InstallCommand\Exceptions\CouldNotInstallNodePackagesException;
use Esign\InstallCommand\Tests\Support\InstallCommand;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;

class InstallCommandTest extends TestCase
{
/** @test */
public function test()
public function it_can_publish_files()
{
$this->artisan(InstallCommand::class);

$this->assertFileExists(app_path('Services/UserService.php'));
}

/** @test */
public function it_can_append_after_the_search_value_in_a_file()
{
$this->artisan(InstallCommand::class);

$this->assertTrue(str_contains(
haystack: File::get(app_path('Models/User.php')),
needle: <<<PHP
public function isAdmin(): bool
{
return false;
}
PHP,
));
}

/** @test */
public function it_can_install_composer_packages()
{
$this->assertTrue(true);
$this->artisan(InstallCommand::class);

Process::assertRan('composer require my/composer-package my/specific-composer-package:^1.0');
Process::assertRan('composer require --dev my/dev-composer-package');
}

/** @test */
public function it_can_throw_an_exception_when_no_package_json_file_is_present()
{
File::delete(base_path('package.json'));
$command = $this->artisan(InstallCommand::class);

$command->expectsOutput("Could not find package.json file in the root of your project. Please create one using `npm init`");
}

/** @test */
public function it_can_install_node_packages()
{
$this->artisan(InstallCommand::class);

Process::assertRan('npm install my/node-package my/specific-node-package@^1.0');
Process::assertRan('npm install --save-dev my/dev-node-package');
}
}
47 changes: 47 additions & 0 deletions tests/Support/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Esign\InstallCommand\Tests\Support;

use Esign\InstallCommand\InstallCommand as BaseInstallCommand;
use Esign\InstallCommand\ValueObjects\AppendableFile;
use Esign\InstallCommand\ValueObjects\ComposerPackage;
use Esign\InstallCommand\ValueObjects\NodePackage;
use Esign\InstallCommand\ValueObjects\PublishableFile;

class InstallCommand extends BaseInstallCommand
{
protected $signature = 'app:install-command';

protected function publishableFiles(): array
{
return [
new PublishableFile(
path: __DIR__ . '/stubs/app/Services/UserService.php',
target: base_path('app/Services/UserService.php'),
),
new AppendableFile(
path: __DIR__ . '/stubs/app/Models/User.php',
target: base_path('app/Models/User.php'),
search: 'use HasFactory;',
),
];
}

protected function composerPackages(): array
{
return [
new ComposerPackage(name: 'my/composer-package'),
new ComposerPackage(name: 'my/specific-composer-package', version: '^1.0'),
new ComposerPackage(name: 'my/dev-composer-package', dev: true),
];
}

protected function nodePackages(): array
{
return [
new NodePackage(name: 'my/node-package'),
new NodePackage(name: 'my/specific-node-package', version: '^1.0'),
new NodePackage(name: 'my/dev-node-package', dev: true),
];
}
}
5 changes: 5 additions & 0 deletions tests/Support/stubs/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

public function isAdmin(): bool
{
return false;
}
8 changes: 8 additions & 0 deletions tests/Support/stubs/app/Services/UserService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Services;

class UserService
{

}
30 changes: 29 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,36 @@

namespace Esign\InstallCommand\Tests;

use Esign\InstallCommand\Tests\Support\InstallCommand;
use Illuminate\Console\Application;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Orchestra\Canvas\Console\ModelMakeCommand;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
}
protected function setUp(): void
{
parent::setUp();

File::put(base_path('package.json'), '{}');

Application::starting(function (Application $artisan) {
$artisan->add(app(ModelMakeCommand::class));
$artisan->call(ModelMakeCommand::class, ['name' => 'User']);
$artisan->add(app(InstallCommand::class));
});

Process::fake();
}

protected function tearDown(): void
{
File::delete(app_path('Models/User.php'));
File::delete(base_path('package.json'));

parent::tearDown();
}
}

0 comments on commit 5d83f36

Please sign in to comment.