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