Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX]: add missing tests for nested service provider #1

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
<env name="SEND_VIEWS_TO_RAY" value="(false)"/>
<env name="SEND_EXCEPTIONS_TO_RAY" value="(true)"/>
</php>
</phpunit>
</phpunit>
2 changes: 1 addition & 1 deletion src/Package/Concerns/HasConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getConfigPath(string $path = ''): string

private function shouldLoadDefaultConfigFolder(): bool
{
return ! $this->preventLoadDefault && $this->shouldLoadDefault;
return ! $this->preventLoadDefault && $this->shouldIncludeConfigFromFolder;
}

private function loadConfigFilesFromFolder(): array
Expand Down
6 changes: 3 additions & 3 deletions src/Package/Concerns/HasMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait HasMigrations

protected bool $preventLoadDefault = false;

protected bool $shouldLoadDefault = false;
protected bool $shouldIncludeMigrationsFromFolder = false;

public function hasMigrations(): bool
{
Expand All @@ -30,7 +30,7 @@ public function withMigrations(string|array $migration = [], bool $publish = tru
->all();
}

$this->shouldLoadDefault = true;
$this->shouldIncludeMigrationsFromFolder = true;

return $this;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public function getMigrationPath(string $path = ''): string

private function shouldLoadDefaultMigrationsFolder(): bool
{
return ! $this->preventLoadDefault && $this->shouldLoadDefault;
return ! $this->preventLoadDefault && $this->shouldIncludeMigrationsFromFolder;
}

private function loadDefaultFolder(): array
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/NestedServiceProvider/RegisterCommandsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Asciito\LaravelPackage\Package\Package;

use function Pest\Laravel\artisan;

trait NestedServiceProviderWithCommands
{
protected function configureNestedService(Package $package): void
{
$package
->setName('nested-service')
->withCommands();
}
}

uses(NestedServiceProviderWithCommands::class);

it('register commands', function () {
artisan('list')
->expectsOutputToContain('project:one')
->expectsOutputToContain('project:two')
->assertSuccessful();
});

it('run commands', function () {
artisan('project:one')
->expectsOutput('Project command one')
->assertSuccessful();

artisan('project:two')
->expectsOutput('Project command two')
->assertSuccessful();
});
41 changes: 41 additions & 0 deletions tests/Feature/NestedServiceProvider/RegisterConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Asciito\LaravelPackage\Package\Package;

use function Pest\Laravel\{ artisan };
use function PHPUnit\Framework\assertFileExists;

trait NestedServiceProviderWithConfig
{
protected function configureNestedService(Package $package): void
{
$package
->setName('nested-service')
->setConfigPath($package->getBasePath('../../config/nested'))
->withConfig();

expect($package)
->getConfigPath()
->toBe($package->getBasePath('../../config/nested'))
->getPublishableConfig()
->not()->toBeEmpty()
->each
->toMatch('/nested-\w+.php$/');
}
}

uses(NestedServiceProviderWithConfig::class);

it('register config', function () {
expect(config('nested-one.key'))
->toBe('nested-one')
->and(config('nested-two.key'))
->toBe('nested-two');
});

it('publish config files', function () {
artisan('vendor:publish', ['--tag' => 'nested-service-config'])->run();

assertFileExists(config_path('nested-one.php'));
assertFileExists(config_path('nested-two.php'));
});
45 changes: 45 additions & 0 deletions tests/Feature/NestedServiceProvider/RegisterMigrationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Asciito\LaravelPackage\Package\Package;

use function Pest\Laravel\artisan;
use function Pest\Laravel\assertDatabaseCount;
use function PHPUnit\Framework\assertFileExists;

trait NestedServiceProviderWithMigrations
{
protected function configureNestedService(Package $package): void
{
$package
->setName('nested-service')
->setMigrationPath($package->getBasePath('../../database/migrations/nested'))
->withMigrations();

expect($package)
->getMigrationPath()
->toBe($package->getBasePath('../../database/migrations/nested'))
->getPublishableMigrations()
->not->toBeEmpty()
->each
->toStartWith($package->getMigrationPath())
->toMatch('/\/\w+.php$/');
}
}

uses(NestedServiceProviderWithMigrations::class);

it('publish migrations', function () {
artisan('vendor:publish', ['--tag' => 'nested-service-migrations'])->run();

assertFileExists(database_path('migrations/2023_01_01_000000_create_nested_test_one_table.php'));
assertFileExists(database_path('migrations/2023_01_01_000001_create_nested_test_two_table.php'));
});

it('run published migrations', function () {
artisan('vendor:publish', ['--tag' => 'nested-service-migrations'])->run();

artisan('migrate')->assertSuccessful();

assertdatabasecount('nested_test_one', 0);
assertdatabasecount('nested_test_one', 0);
});
32 changes: 32 additions & 0 deletions workbench/app/Nested/Console/Commands/ProjectTestCommandOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Workbench\App\Nested\Console\Commands;

use Illuminate\Console\Command;

class ProjectTestCommandOne extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project:one';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Project Command Test One';

/**
* Execute the console command.
*/
public function handle(): int
{
$this->info('Project command one');

return self::SUCCESS;
}
}
32 changes: 32 additions & 0 deletions workbench/app/Nested/Console/Commands/ProjectTestCommandTwo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Workbench\App\Nested\Console\Commands;

use Illuminate\Console\Command;

class ProjectTestCommandTwo extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project:two';

/**
* The console command description.
*
* @var string
*/
protected $description = 'project Command Test Two';

/**
* Execute the console command.
*/
public function handle(): int
{
$this->info('Project command two');

return self::SUCCESS;
}
}
23 changes: 23 additions & 0 deletions workbench/app/Nested/NestedServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Workbench\App\Nested;

use Asciito\LaravelPackage\Package\Package;
use Asciito\LaravelPackage\Package\PackageServiceProvider;

class NestedServiceProvider extends PackageServiceProvider
{
public static ?\Closure $configureNestedUsing = null;

protected function configurePackage(Package $package): void
{
$configClosure = self::$configureNestedUsing ?? fn (Package $package) => null;

$configClosure($package);
}

protected function getNamespace(): string
{
return __NAMESPACE__;
}
}
5 changes: 5 additions & 0 deletions workbench/config/nested/nested-one.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'key' => 'nested-one',
];
5 changes: 5 additions & 0 deletions workbench/config/nested/nested-two.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'key' => 'nested-two',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('nested_test_one', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('nested_tests');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('nested_test_two', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('nested_tests');
}
};
14 changes: 13 additions & 1 deletion workbench/tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Asciito\LaravelPackage\Package\Package;
use Asciito\LaravelPackage\Tests\TestCase;
use Illuminate\Support\Facades\File;
use Workbench\App\Nested\NestedServiceProvider;
use Workbench\App\ServiceProvider;

use function Spatie\PestPluginTestTime\testTime;
Expand All @@ -21,18 +22,29 @@ protected function setUp(): void
{
ServiceProvider::$configurePackageUsing = fn (Package $package) => $this->configurePackage($package);

NestedServiceProvider::$configureNestedUsing = fn (Package $package) => $this->configureNestedService($package);

testTime()->freeze('2023-01-01 00:00:00');

parent::setUp();

$this->deletePublishable();
}

abstract protected function configurePackage(Package $package);
protected function configurePackage(Package $package): void
{
//
}

protected function configureNestedService(Package $package): void
{
//
}

protected function getPackageProviders($app): array
{
return [
NestedServiceProvider::class,
ServiceProvider::class,
];
}
Expand Down