Skip to content
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
49 changes: 49 additions & 0 deletions src/Validation/Console/MakeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Phenix\Validation\Console;

use Phenix\Console\Maker;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeRule extends Maker
{
/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultName = 'make:rule';

/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultDescription = 'Creates a new validation rule.';

protected function configure(): void
{
$this->setHelp('This command allows you to create a new validation rule.');

$this->addArgument('name', InputArgument::REQUIRED, 'The rule class name');
$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to create rule');
}

protected function outputDirectory(): string
{
return 'app' . DIRECTORY_SEPARATOR . 'Validation' . DIRECTORY_SEPARATOR . 'Rules';
}

protected function stub(): string
{
return 'rule.stub';
}

protected function commonName(): string
{
return 'Rule';
}
}
49 changes: 49 additions & 0 deletions src/Validation/Console/MakeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Phenix\Validation\Console;

use Phenix\Console\Maker;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeType extends Maker
{
/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultName = 'make:type';

/**
* @var string
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint
*/
protected static $defaultDescription = 'Creates a new data type for validation.';

protected function configure(): void
{
$this->addArgument('name', InputArgument::REQUIRED, 'The type class name');
$this->addOption('force', 'f', InputOption::VALUE_NONE, 'Force to create type');

$this->setHelp('This command allows you to create a new data type for validation.');
}

protected function commonName(): string
{
return 'Type';
}

protected function stub(): string
{
return 'type.stub';
}

protected function outputDirectory(): string
{
return 'app' . DIRECTORY_SEPARATOR . 'Validation' . DIRECTORY_SEPARATOR . 'Types';
}
}
20 changes: 20 additions & 0 deletions src/Validation/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Phenix\Validation;

use Phenix\Providers\ServiceProvider;
use Phenix\Validation\Console\MakeRule;
use Phenix\Validation\Console\MakeType;

class ValidationServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->commands([
MakeRule::class,
MakeType::class,
]);
}
}
20 changes: 20 additions & 0 deletions src/stubs/rule.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace {namespace};

use Phenix\Validation\Rules\Rule;

class {name} extends Rule
{
public function passes(): bool
{
return true;
}

public function message(): string|null
{
return trans('validation.custom', ['field' => $this->getFieldForHumans()]);
}
}
17 changes: 17 additions & 0 deletions src/stubs/type.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace {namespace};

use Phenix\Validation\Rules\IsString;
use Phenix\Validation\Rules\TypeRule;
use Phenix\Validation\Types\Scalar;

class {name} extends Scalar
{
protected function defineType(): TypeRule
{
return IsString::new();
}
}
110 changes: 110 additions & 0 deletions tests/Unit/Validation/Console/MakeRuleCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

use Phenix\Filesystem\Contracts\File;
use Phenix\Testing\Mock;

it('creates rule successfully', function () {
$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => false,
get: fn (string $path) => '',
put: function (string $path) {
expect($path)->toBe(base_path('app/Validation/Rules/AwesomeRule.php'));

return true;
},
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:rule', [
'name' => 'AwesomeRule',
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Rule [app/Validation/Rules/AwesomeRule.php] successfully generated!');
});

it('does not create the rule because it already exists', function () {
$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => true,
);

$this->app->swap(File::class, $mock);

$this->phenix('make:rule', [
'name' => 'TestRule',
]);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:rule', [
'name' => 'TestRule',
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Rule already exists!');
});

it('creates rule successfully with force option', function () {
$tempDir = sys_get_temp_dir();
$tempPath = $tempDir . DIRECTORY_SEPARATOR . 'TestRule.php';

file_put_contents($tempPath, 'old content');

$this->assertEquals('old content', file_get_contents($tempPath));

$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => false,
get: fn (string $path) => 'new content',
put: fn (string $path, string $content) => file_put_contents($tempPath, $content),
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:rule', [
'name' => 'TestRule',
'--force' => true,
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Rule [app/Validation/Rules/TestRule.php] successfully generated!');
expect('new content')->toBe(file_get_contents($tempPath));
});

it('creates rule successfully in nested namespace', function () {
$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => false,
get: fn (string $path) => '',
put: function (string $path) {
expect($path)->toBe(base_path('app/Validation/Rules/Admin/TestRule.php'));

return true;
},
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:rule', [
'name' => 'Admin/TestRule',
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Rule [app/Validation/Rules/Admin/TestRule.php] successfully generated!');
});
110 changes: 110 additions & 0 deletions tests/Unit/Validation/Console/MakeTypeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

use Phenix\Filesystem\Contracts\File;
use Phenix\Testing\Mock;

it('creates type successfully', function () {
$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => false,
get: fn (string $path) => '',
put: function (string $path) {
expect($path)->toBe(base_path('app/Validation/Types/AwesomeType.php'));

return true;
},
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:type', [
'name' => 'AwesomeType',
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Type [app/Validation/Types/AwesomeType.php] successfully generated!');
});

it('does not create the type because it already exists', function () {
$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => true,
);

$this->app->swap(File::class, $mock);

$this->phenix('make:type', [
'name' => 'TestType',
]);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:type', [
'name' => 'TestType',
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Type already exists!');
});

it('creates type successfully with force option', function () {
$tempDir = sys_get_temp_dir();
$tempPath = $tempDir . DIRECTORY_SEPARATOR . 'TestType.php';

file_put_contents($tempPath, 'old content');

$this->assertEquals('old content', file_get_contents($tempPath));

$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => false,
get: fn (string $path) => 'new content',
put: fn (string $path, string $content) => file_put_contents($tempPath, $content),
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:type', [
'name' => 'TestType',
'--force' => true,
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Type [app/Validation/Types/TestType.php] successfully generated!');
expect('new content')->toBe(file_get_contents($tempPath));
});

it('creates type successfully in nested namespace', function () {
$mock = Mock::of(File::class)->expect(
exists: fn (string $path) => false,
get: fn (string $path) => '',
put: function (string $path) {
expect($path)->toBe(base_path('app/Validation/Types/Admin/TestType.php'));

return true;
},
createDirectory: function (string $path): void {
// ..
}
);

$this->app->swap(File::class, $mock);

/** @var \Symfony\Component\Console\Tester\CommandTester $command */
$command = $this->phenix('make:type', [
'name' => 'Admin/TestType',
]);

$command->assertCommandIsSuccessful();

expect($command->getDisplay())->toContain('Type [app/Validation/Types/Admin/TestType.php] successfully generated!');
});
1 change: 1 addition & 0 deletions tests/fixtures/application/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
\Phenix\Queue\QueueServiceProvider::class,
\Phenix\Events\EventServiceProvider::class,
\Phenix\Translation\TranslationServiceProvider::class,
\Phenix\Validation\ValidationServiceProvider::class,
],
];