Skip to content

Commit

Permalink
Merge pull request #17 from binafy/feat/add-convenience-methods
Browse files Browse the repository at this point in the history
[1.x] Feat/add convenience methods
  • Loading branch information
milwad-dev authored Dec 6, 2024
2 parents 797d25a + 50258ce commit 2ad57dd
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
- [`download`](#download)
- [`generate`](#generate)
- [`generateForce`](#generate-force)
- [`generateIf`](#generate-if)
- [`generateUnless`](#generate-unless)
- [Contributors](#contributors)
- [Security](#security)
- [Changelog](#changelog)
Expand Down Expand Up @@ -292,6 +294,40 @@ LaravelStub::from(__DIR__ . 'model.stub')
->generateForce();
```

<a name="generate-if"></a>
### `generateIf`

If you want to generate a stub file if given boolean expression evaluates to `true`, you can use the `generateIf` method:

```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateIf(true);
```

<a name="generate-unless"></a>
### `generateUnless`

If you want to generate a stub file if given boolean expression evaluates to `false`, you can use the `generateIf` method:

```php
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateUnless(true);
```

<a name="contributors"></a>
## Contributors

Expand Down
24 changes: 24 additions & 0 deletions src/LaravelStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ public function generateForce(): bool
return $this->generate(true);
}

/**
* Generate stub if condition pass.
*/
public function generateIf(bool $condition): bool
{
if ($condition) {
return $this->generate();
}

return false;
}

/**
* Generate stub if condition pass (reversed).
*/
public function generateUnless(bool $condition): bool
{
if (! $condition) {
return $this->generate();
}

return false;
}

/**
* Generate stub file.
*/
Expand Down
70 changes: 70 additions & 0 deletions tests/Feature/LaravelStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,73 @@
assertFileExists(__DIR__ . '/../App/new-test.php');
assertFileDoesNotExist(__DIR__ . '/../App/test.stub');
});

test('generate stub successfully with `generateIf` method', function () {
$stub = __DIR__ . '/test.stub';

$generate = LaravelStub::from($stub)
->to(__DIR__ . '/../App')
->replaces([
'CLASS' => 'Milwad',
'NAMESPACE' => 'App\Models'
])
->name('new-test')
->ext('php')
->moveStub()
->generateIf(true);

assertTrue($generate);
assertFileExists(__DIR__ . '/../App/new-test.php');
assertFileDoesNotExist(__DIR__ . '/../App/test.stub');
});

test('generate stub unsuccessfully with `generateIf` method', function () {
$stub = __DIR__ . '/test.stub';

$generate = LaravelStub::from($stub)
->to(__DIR__ . '/../App')
->replaces([
'CLASS' => 'Milwad',
'NAMESPACE' => 'App\Models'
])
->name('new-test')
->ext('php')
->generateIf(false);

\PHPUnit\Framework\assertFalse($generate);
});

test('generate stub successfully with `generateUnless` method', function () {
$stub = __DIR__ . '/test.stub';

$generate = LaravelStub::from($stub)
->to(__DIR__ . '/../App')
->replaces([
'CLASS' => 'Milwad',
'NAMESPACE' => 'App\Models'
])
->name('new-test')
->ext('php')
->moveStub()
->generateUnless(false);

assertTrue($generate);
assertFileExists(__DIR__ . '/../App/new-test.php');
assertFileDoesNotExist(__DIR__ . '/../App/test.stub');
});

test('generate stub unsuccessfully with `generateUnless` method', function () {
$stub = __DIR__ . '/test.stub';

$generate = LaravelStub::from($stub)
->to(__DIR__ . '/../App')
->replaces([
'CLASS' => 'Milwad',
'NAMESPACE' => 'App\Models'
])
->name('new-test')
->ext('php')
->generateUnless(true);

\PHPUnit\Framework\assertFalse($generate);
});

0 comments on commit 2ad57dd

Please sign in to comment.