Skip to content

Commit

Permalink
Add wizard factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Oct 14, 2019
1 parent 893c421 commit cfac1d4
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 58 deletions.
14 changes: 1 addition & 13 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,14 @@ 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.
*
* @return string
*/
protected function getSessionKey()
{
return 'laravel_wizard.' . $this->wizardName;
return 'laravel_wizard.' . $this->wizard->getName();
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Facades/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 5 additions & 5 deletions src/Http/Controllers/WizardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -386,6 +385,7 @@ protected function pushWithViewData(array $data)
protected function getViewPath($view)
{
$viewPath = "wizards.{$this->wizardName}.$view";

if (view()->exists($viewPath)) {
return $viewPath;
}
Expand Down
4 changes: 2 additions & 2 deletions src/StepRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}
Expand Down
47 changes: 25 additions & 22 deletions src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,6 +28,13 @@ class Wizard
*/
protected $stepRepo;

/**
* The wizard name.
*
* @var string
*/
protected $name;

/**
* The wizard options.
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -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()
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -154,7 +147,7 @@ public function setStepRepo($stepRepo = null)
*
* @return array
*/
public function options()
public function getOptions()
{
return $this->options;
}
Expand Down Expand Up @@ -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.
*
Expand Down
46 changes: 46 additions & 0 deletions src/WizardFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Ycs77\LaravelWizard;

use Illuminate\Foundation\Application;

class WizardFactory
{
/**
* The application instance.
*
* @var \Illuminate\Foundation\Application
*/
protected $app;

/**
* Create a new Wizard instance.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
public function __construct(Application $app)
{
$this->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;
}
}
14 changes: 8 additions & 6 deletions src/WizardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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');
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/StepRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 4 additions & 5 deletions tests/Unit/StepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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([
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/WizardFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Ycs77\LaravelWizard\Test\Unit;

use Ycs77\LaravelWizard\Contracts\CacheStore;
use Ycs77\LaravelWizard\StepRepository;
use Ycs77\LaravelWizard\Test\Stubs\UserStepStub;
use Ycs77\LaravelWizard\Test\TestCase;
use Ycs77\LaravelWizard\WizardFactory;

class WizardFactoryTest extends TestCase
{
public function testMakeWizard()
{
$factory = new WizardFactory($this->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());
}
}
2 changes: 1 addition & 1 deletion tests/Unit/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cfac1d4

Please sign in to comment.