From fc16e6c87739e417f90d8b90bd4eedb18e670687 Mon Sep 17 00:00:00 2001 From: Joel Male Date: Wed, 27 May 2026 21:42:51 +1000 Subject: [PATCH] feat: add cart:make:coupon command --- src/Commands/MakeCouponCommand.php | 62 +++++++++++++++++++ tests/Unit/Commands/MakeCouponCommandTest.php | 30 +++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Commands/MakeCouponCommand.php create mode 100644 tests/Unit/Commands/MakeCouponCommandTest.php diff --git a/src/Commands/MakeCouponCommand.php b/src/Commands/MakeCouponCommand.php new file mode 100644 index 0000000..d176fa5 --- /dev/null +++ b/src/Commands/MakeCouponCommand.php @@ -0,0 +1,62 @@ +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 <<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)'); + }); +});