From cfac1d49f3dce018baecc1cea6a2f4e567adc7ed Mon Sep 17 00:00:00 2001 From: Lucas Yang Date: Mon, 14 Oct 2019 14:50:20 +0800 Subject: [PATCH] Add wizard factory --- src/CacheManager.php | 14 +------ src/Facades/Wizard.php | 13 +++++++ src/Http/Controllers/WizardController.php | 10 ++--- src/StepRepository.php | 4 +- src/Wizard.php | 47 ++++++++++++----------- src/WizardFactory.php | 46 ++++++++++++++++++++++ src/WizardServiceProvider.php | 14 ++++--- tests/Unit/StepRepositoryTest.php | 6 +-- tests/Unit/StepTest.php | 9 ++--- tests/Unit/WizardFactoryTest.php | 25 ++++++++++++ tests/Unit/WizardTest.php | 2 +- 11 files changed, 132 insertions(+), 58 deletions(-) create mode 100644 src/WizardFactory.php create mode 100644 tests/Unit/WizardFactoryTest.php diff --git a/src/CacheManager.php b/src/CacheManager.php index 2a88b08..3b31a8f 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -70,18 +70,6 @@ protected function createDatabaseDriver() return new DatabaseStore($this->getDatabaseConnection(), $table, $this->app); } - /** - * Set the wizard key. - * - * @param string $wizardName - * @return self - */ - public function setWizardName(string $wizardName) - { - $this->wizardName = $wizardName; - return $this; - } - /** * Get session key. * @@ -89,7 +77,7 @@ public function setWizardName(string $wizardName) */ protected function getSessionKey() { - return 'laravel_wizard.' . $this->wizardName; + return 'laravel_wizard.' . $this->wizard->getName(); } /** diff --git a/src/Facades/Wizard.php b/src/Facades/Wizard.php index 30192b3..06f80a7 100644 --- a/src/Facades/Wizard.php +++ b/src/Facades/Wizard.php @@ -4,8 +4,21 @@ use Illuminate\Support\Facades\Facade; +/** + * @see \Ycs77\LaravelWizard\WizardFactory + */ class Wizard extends Facade { + /** + * Get the registered name of the component. + * + * @return string + */ + protected static function getFacadeAccessor() + { + return 'wizard'; + } + /** * Undocumented function * diff --git a/src/Http/Controllers/WizardController.php b/src/Http/Controllers/WizardController.php index 315ab81..107ccc6 100644 --- a/src/Http/Controllers/WizardController.php +++ b/src/Http/Controllers/WizardController.php @@ -11,7 +11,7 @@ use Ycs77\LaravelWizard\Exceptions\StepNotFoundException; use Ycs77\LaravelWizard\Http\Controllers\Traits\WizardControllerEvents; use Ycs77\LaravelWizard\Step; -use Ycs77\LaravelWizard\Wizard; +use Ycs77\LaravelWizard\WizardFactory; class WizardController extends Controller { @@ -71,13 +71,12 @@ class WizardController extends Controller /** * Create new wizard controller. * - * @param \Ycs77\LaravelWizard\Wizard $wizard + * @param \Ycs77\LaravelWizard\WizardFactory $factory * @return void */ - public function __construct(Wizard $wizard) + public function __construct(WizardFactory $factory) { - $this->wizard = $wizard; - $this->wizard->load($this->wizardName, $this->steps, $this->wizardOptions); + $this->wizard = $factory->make($this->wizardName, $this->steps, $this->wizardOptions); } /** @@ -386,6 +385,7 @@ protected function pushWithViewData(array $data) protected function getViewPath($view) { $viewPath = "wizards.{$this->wizardName}.$view"; + if (view()->exists($viewPath)) { return $viewPath; } diff --git a/src/StepRepository.php b/src/StepRepository.php index 651479e..26dbb78 100644 --- a/src/StepRepository.php +++ b/src/StepRepository.php @@ -80,7 +80,7 @@ public function set($steps) /** * Make new step instance. * - * @param mixed $stepClass + * @param array|string $stepClass * @param int|null $index * @return self */ @@ -91,7 +91,7 @@ public function make($stepClass, int $index = null) $step = new $_stepClass($this->wizard, $_index); $this->steps->push($step); } - } else { + } elseif (is_string($stepClass)) { $step = new $stepClass($this->wizard, $index); $this->steps->push($step); } diff --git a/src/Wizard.php b/src/Wizard.php index 70365c4..71d0ec3 100644 --- a/src/Wizard.php +++ b/src/Wizard.php @@ -17,7 +17,7 @@ class Wizard /** * The wizard cache manager instance. * - * @var \Ycs77\LaravelWizard\Contracts\CacheStore|\Ycs77\LaravelWizard\CacheManager + * @var \Ycs77\LaravelWizard\Contracts\CacheStore */ protected $cache; @@ -28,6 +28,13 @@ class Wizard */ protected $stepRepo; + /** + * The wizard name. + * + * @var string + */ + protected $name; + /** * The wizard options. * @@ -51,28 +58,14 @@ class Wizard * Create a new Wizard instance. * * @param \Illuminate\Foundation\Application $app - * @return void - */ - public function __construct(Application $app) - { - $this->app = $app; - } - - /** - * Load the wizard dependencies. - * * @param string $name - * @param mixed $steps * @param array $options - * @return self + * @return void */ - public function load($name, $steps, $options = []) + public function __construct(Application $app, string $name, $options = []) { - $this->setCache(); - $this->setStepRepo(); - - $this->cache->setWizardName($name); - $this->stepRepo->make($steps); + $this->app = $app; + $this->name = $name; $this->setOptions($options); } @@ -108,7 +101,7 @@ public function nextStepIndex() /** * Get the wizard cache instance. * - * @return \Ycs77\LaravelWizard\Contracts\CacheStore|\Ycs77\LaravelWizard\CacheManager + * @return \Ycs77\LaravelWizard\Contracts\CacheStore */ public function cache() { @@ -123,7 +116,7 @@ public function cache() */ public function setCache($cache = null) { - $this->cache = $cache ?? new CacheManager($this, $this->app); + $this->cache = $cache ?? (new CacheManager($this, $this->app))->driver(); return $this; } @@ -154,7 +147,7 @@ public function setStepRepo($stepRepo = null) * * @return array */ - public function options() + public function getOptions() { return $this->options; } @@ -198,6 +191,16 @@ public function getApp() return $this->app; } + /** + * Get the wizard name. + * + * @return string + */ + public function getName() + { + return $this->name; + } + /** * Handle dynamic method calls into the wizard. * diff --git a/src/WizardFactory.php b/src/WizardFactory.php new file mode 100644 index 0000000..534a50d --- /dev/null +++ b/src/WizardFactory.php @@ -0,0 +1,46 @@ +app = $app; + } + + /** + * Make the new wizard. + * + * @param string $name + * @param array|string $steps + * @param array $options + * @return \Ycs77\LaravelWizard\Wizard + */ + public function make(string $name, $steps, $options = []) + { + $wizard = new Wizard($this->app, $name, $options); + + $wizard->setCache(); + $wizard->setStepRepo(); + + $wizard->stepRepo()->make($steps); + + return $wizard; + } +} diff --git a/src/WizardServiceProvider.php b/src/WizardServiceProvider.php index 761433d..a2b570d 100644 --- a/src/WizardServiceProvider.php +++ b/src/WizardServiceProvider.php @@ -18,10 +18,10 @@ class WizardServiceProvider extends ServiceProvider public function register() { $this->app->singleton('wizard', function ($app) { - return new Wizard($app); + return new WizardFactory($app); }); - $this->app->alias('wizard', Wizard::class); + $this->app->alias('wizard', WizardFactory::class); $this->mergeConfigFrom(__DIR__ . '/../config/wizard.php', 'wizard'); } @@ -33,10 +33,12 @@ public function register() */ public function boot() { - $this->commands(WizardMakeCommand::class); - $this->commands(WizardControllerMakeCommand::class); - $this->commands(StepMakeCommand::class); - $this->commands(TableCommand::class); + $this->commands([ + WizardMakeCommand::class, + WizardControllerMakeCommand::class, + StepMakeCommand::class, + TableCommand::class, + ]); $this->loadViewsFrom(__DIR__ . '/../resources/views', 'wizard'); $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'wizard'); diff --git a/tests/Unit/StepRepositoryTest.php b/tests/Unit/StepRepositoryTest.php index 492c087..f3a1ff9 100644 --- a/tests/Unit/StepRepositoryTest.php +++ b/tests/Unit/StepRepositoryTest.php @@ -35,10 +35,8 @@ public function setUp() { parent::setUp(); - $this->wizard = $this->mock(Wizard:: class, [$this->app])->makePartial(); - $this->step = $this->app->makeWith(StepRepository::class, [ - 'wizard' => $this->wizard, - ]); + $this->wizard = $this->mock(Wizard:: class)->makePartial(); + $this->step = $this->app->makeWith(StepRepository::class, [$this->wizard]); $this->stepsStub = [ new UserStepStub($this->wizard, 0), diff --git a/tests/Unit/StepTest.php b/tests/Unit/StepTest.php index 7bd8802..d28e9f8 100644 --- a/tests/Unit/StepTest.php +++ b/tests/Unit/StepTest.php @@ -4,7 +4,6 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\Request; -use Ycs77\LaravelWizard\CacheManager; use Ycs77\LaravelWizard\Test\Stubs\UserStepStub; use Ycs77\LaravelWizard\Test\Stubs\PostStepStub; use Ycs77\LaravelWizard\Test\TestCase; @@ -32,7 +31,7 @@ public function setUp() { parent::setUp(); - $this->wizard = $this->mock(Wizard::class, [$this->app])->makePartial(); + $this->wizard = $this->mock(Wizard::class)->makePartial(); $this->step = $this->mock(UserStepStub::class, [$this->wizard, 0])->makePartial(); } @@ -67,7 +66,7 @@ public function testGetData() ->once() ->andReturn('user-step-stub'); /** @param \Mockery\MockInterface $mock */ - $cache = $this->mock(CacheManager::class, function ($mock) { + $cache = $this->mock(CacheStore::class, function ($mock) { $mock->shouldReceive('get')->once()->andReturn(['field' => 'data']); }); $this->wizard->shouldReceive('cache')->once()->andReturn($cache); @@ -101,7 +100,7 @@ public function testCacheProgress() ->andReturn(['name' => 'Lucas Yang']); /** @param \Mockery\MockInterface $mock */ - $cache = $this->mock(CacheManager::class, function ($mock) use ($expected) { + $cache = $this->mock(CacheStore::class, function ($mock) use ($expected) { $mock->shouldReceive('get') ->twice() ->andReturn([], $expected); @@ -143,7 +142,7 @@ public function testSecondStepCacheProgress() ->andReturn(['phone' => '12345678']); /** @param \Mockery\MockInterface $mock */ - $cache = $this->mock(CacheManager::class, function ($mock) use ($expected) { + $cache = $this->mock(CacheStore::class, function ($mock) use ($expected) { $mock->shouldReceive('get') ->twice() ->andReturn([ diff --git a/tests/Unit/WizardFactoryTest.php b/tests/Unit/WizardFactoryTest.php new file mode 100644 index 0000000..54c6924 --- /dev/null +++ b/tests/Unit/WizardFactoryTest.php @@ -0,0 +1,25 @@ +app); + + $wizard = $factory->make('test-wizard', [UserStepStub::class]); + + $this->assertEquals('test-wizard', $wizard->getName()); + $this->assertInstanceOf(CacheStore::class, $wizard->cache()); + $this->assertInstanceOf(StepRepository::class, $wizard->stepRepo()); + $this->assertCount(1, $wizard->stepRepo()->original()); + $this->assertInstanceOf(UserStepStub::class, $wizard->stepRepo()->original()->first()); + } +} diff --git a/tests/Unit/WizardTest.php b/tests/Unit/WizardTest.php index ea2a671..d72b0b3 100644 --- a/tests/Unit/WizardTest.php +++ b/tests/Unit/WizardTest.php @@ -20,7 +20,7 @@ public function setUp() { parent::setUp(); - $this->wizard = $this->mock(Wizard::class, [$this->app])->makePartial(); + $this->wizard = new Wizard($this->app, 'test-wizard'); } protected function tearDown()