Skip to content

Commit

Permalink
creating models within a domain command
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Apr 14, 2023
1 parent 447c4f4 commit 7e7d630
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-domain-expert` will be documented in this file

## v1.3.2 - 2023-04-13

- creating models within a domain command

## v1.3.1 - 2023-04-13

- creating observers within a domain command
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ You can do the same routine to make:
- observers

```bash
php artisan make:observers ObserverName --domain
php artisan make:observer ObserverName --domain
```
- models

```bash
php artisan make:model ModelName --domain
```


## Auto-loading Routes and Views

Expand Down
33 changes: 33 additions & 0 deletions src/Console/ExtendedModelMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Salehhashemi\LaravelDomainExpert\Console;

use Illuminate\Foundation\Console\ModelMakeCommand;

class ExtendedModelMakeCommand extends ModelMakeCommand
{
use HandlesDomainOption;

protected string $folder = 'Models';

/**
* {@inheritdoc}
*/
public function handle(): bool|null
{
$this->handleDomainOption();

return parent::handle();
}

/**
* {@inheritdoc}
*/
protected function getOptions(): array
{
$options = parent::getOptions();
$options[] = $this->getDomainOption();

return $options;
}
}
2 changes: 2 additions & 0 deletions src/LaravelDomainExpertServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\ServiceProvider;
use Salehhashemi\LaravelDomainExpert\Console\DomainMakeCommand;
use Salehhashemi\LaravelDomainExpert\Console\ExtendedControllerMakeCommand;
use Salehhashemi\LaravelDomainExpert\Console\ExtendedModelMakeCommand;
use Salehhashemi\LaravelDomainExpert\Console\ExtendedObserverMakeCommand;

class LaravelDomainExpertServiceProvider extends ServiceProvider
Expand All @@ -31,6 +32,7 @@ public function boot()
DomainMakeCommand::class,
ExtendedControllerMakeCommand::class,
ExtendedObserverMakeCommand::class,
ExtendedModelMakeCommand::class,
]);
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/ExtendedModelMakeCommandConsoleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Salehhashemi\LaravelDomainExpert\Tests;

use Illuminate\Support\Facades\File;
use Salehhashemi\LaravelDomainExpert\Console\ExtendedModelMakeCommand;
use Salehhashemi\LaravelDomainExpert\LaravelDomainExpertServiceProvider;

class ExtendedModelMakeCommandConsoleTest extends ConsoleTestCase
{
/**
* {@inheritdoc}
*/
protected function getPackageProviders($app): array
{
return [LaravelDomainExpertServiceProvider::class];
}

/** @test */
public function test_creates_a_model_in_the_selected_domain()
{
// Set the expected domain
$expectedDomain = 'SampleDomain';

$tester = $this->runCommand(ExtendedModelMakeCommand::class, [
'name' => 'SampleModel',
'--domain' => $expectedDomain,
], [0]);

$tester->assertCommandIsSuccessful();

// Check if the model file has been created
$this->assertTrue(File::exists(app_path("Domains/{$expectedDomain}/Models/SampleModel.php")));
}
}

0 comments on commit 7e7d630

Please sign in to comment.