diff --git a/src/Commands/MakeDriverCommand.php b/src/Commands/MakeDriverCommand.php new file mode 100644 index 0000000..e4a1b71 --- /dev/null +++ b/src/Commands/MakeDriverCommand.php @@ -0,0 +1,111 @@ +argument('name'); + $directory = app_path('Cart/Drivers'); + $path = "{$directory}/{$name}.php"; + + if (file_exists($path)) { + $this->error("Driver already exists: app/Cart/Drivers/{$name}.php"); + return self::FAILURE; + } + + if (! is_dir($directory)) { + mkdir($directory, 0755, true); + } + + file_put_contents($path, static::stub($name)); + $this->info("Driver created: app/Cart/Drivers/{$name}.php"); + $this->line(" Register it in config/cart.php under drivers."); + + return self::SUCCESS; + } + + public static function stub(string $className): string + { + return <<sessionKey = \$sessionKey; + } + + public function getSessionKey(): string + { + return \$this->sessionKey; + } + + public function setSessionKey(string \$sessionKey): void + { + \$this->sessionKey = \$sessionKey; + } + + public function getDriverName(): string + { + return 'custom'; + } + + public function getItems(): array + { + // TODO: retrieve items from your storage backend + return []; + } + + public function putItems(array \$items): void + { + // TODO: persist items to your storage backend + } + + public function getConditions(): array + { + // TODO: retrieve conditions from your storage backend + return []; + } + + public function putConditions(array \$conditions): void + { + // TODO: persist conditions to your storage backend + } + + public function clearItems(): void + { + // TODO: remove only items from your storage backend + } + + public function flush(): void + { + // TODO: remove both items and conditions from your storage backend + } + + public function getSessionModel(): mixed + { + return null; + } +} +PHP; + } +} diff --git a/tests/Unit/Commands/MakeDriverCommandTest.php b/tests/Unit/Commands/MakeDriverCommandTest.php new file mode 100644 index 0000000..2b4d623 --- /dev/null +++ b/tests/Unit/Commands/MakeDriverCommandTest.php @@ -0,0 +1,34 @@ +toContain('class RedisDriver implements CartDriver'); + }); + + test('stub uses the App\\Cart\\Drivers namespace', function () { + $stub = MakeDriverCommand::stub('RedisDriver'); + expect($stub)->toContain('namespace App\\Cart\\Drivers'); + }); + + test('stub imports the CartDriver interface', function () { + $stub = MakeDriverCommand::stub('RedisDriver'); + expect($stub)->toContain('use Wearepixel\\Cart\\Drivers\\Contracts\\CartDriver'); + }); + + test('stub declares all CartDriver interface methods', function () { + $stub = MakeDriverCommand::stub('RedisDriver'); + expect($stub)->toContain('public function getSessionKey(): string'); + expect($stub)->toContain('public function setSessionKey(string $sessionKey): void'); + expect($stub)->toContain('public function getDriverName(): string'); + expect($stub)->toContain('public function getItems(): array'); + expect($stub)->toContain('public function putItems(array $items): void'); + expect($stub)->toContain('public function getConditions(): array'); + expect($stub)->toContain('public function putConditions(array $conditions): void'); + expect($stub)->toContain('public function clearItems(): void'); + expect($stub)->toContain('public function flush(): void'); + expect($stub)->toContain('public function getSessionModel(): mixed'); + }); +});