mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
feat: add cart:make:shipping command
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Wearepixel\Cart\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class MakeShippingCommand extends Command
|
||||
{
|
||||
protected $signature = 'cart:make:shipping {name : The class name of the shipping rate}';
|
||||
|
||||
protected $description = 'Create a new ShippingRate class in app/Cart/Shipping.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$directory = app_path('Cart/Shipping');
|
||||
$path = "{$directory}/{$name}.php";
|
||||
|
||||
if (file_exists($path)) {
|
||||
$this->error("Shipping rate already exists: app/Cart/Shipping/{$name}.php");
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if (! is_dir($directory)) {
|
||||
mkdir($directory, 0755, true);
|
||||
}
|
||||
|
||||
file_put_contents($path, static::stub($name));
|
||||
$this->info("Shipping rate created: app/Cart/Shipping/{$name}.php");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
public static function stub(string $className): string
|
||||
{
|
||||
return <<<PHP
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Cart\Shipping;
|
||||
|
||||
use Wearepixel\Cart\Shipping\ShippingRate;
|
||||
|
||||
class {$className} extends ShippingRate
|
||||
{
|
||||
protected string \$name = 'Standard Shipping';
|
||||
|
||||
protected string \$value = '+10';
|
||||
|
||||
protected string \$target = 'total';
|
||||
|
||||
public function isApplicable(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
PHP;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Wearepixel\Cart\Commands\MakeShippingCommand;
|
||||
|
||||
describe('MakeShippingCommand', function () {
|
||||
test('stub contains the given class name', function () {
|
||||
$stub = MakeShippingCommand::stub('FlatRateShipping');
|
||||
expect($stub)->toContain('class FlatRateShipping extends ShippingRate');
|
||||
});
|
||||
|
||||
test('stub uses the App\\Cart\\Shipping namespace', function () {
|
||||
$stub = MakeShippingCommand::stub('FlatRateShipping');
|
||||
expect($stub)->toContain('namespace App\\Cart\\Shipping');
|
||||
});
|
||||
|
||||
test('stub imports the ShippingRate base class', function () {
|
||||
$stub = MakeShippingCommand::stub('FlatRateShipping');
|
||||
expect($stub)->toContain('use Wearepixel\\Cart\\Shipping\\ShippingRate');
|
||||
});
|
||||
|
||||
test('stub declares isApplicable() method', function () {
|
||||
$stub = MakeShippingCommand::stub('FlatRateShipping');
|
||||
expect($stub)->toContain('public function isApplicable(): bool');
|
||||
});
|
||||
|
||||
test('stub defaults target to total', function () {
|
||||
$stub = MakeShippingCommand::stub('FlatRateShipping');
|
||||
expect($stub)->toContain("'total'");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user