Skip to content

Commit

Permalink
Add cleanUpWizard hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Sep 16, 2024
1 parent 6c4cb08 commit b7b766f
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Cache/SessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ public function has(string $key)
*/
public function clear()
{
$this->serializer->clearTmpFiles($this->get('_files'));
if (is_array($files = $this->get('_files'))) {
$this->serializer->clearTmpFiles($files);
}

$this->session->forget($this->wizardKey);
}
Expand Down
23 changes: 23 additions & 0 deletions src/Wizardable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ trait Wizardable
*/
public function create(Request $request, $step = null)
{
// Reset the wizard cache when passing the `_reset` query parameter.
if ($request->query('_reset') === '1') {
$this->wizard()->cache()->clear();

// Clean up the wizard event.
$this->cleanUpWizard($request);

return $this->redirectToLastProcessedStep($request, 0);
}

// Before wizard step create event.
if ($redirectTo = $this->beforeWizardStepCreate($request)) {
return $redirectTo;
Expand Down Expand Up @@ -153,6 +163,9 @@ public function store(Request $request, string $step)
// Wizard ended event.
$this->wizardEnded($request, $data);

// Clean the wizard event.
$this->cleanUpWizard($request);

return $this->redirectToDone($data);
}

Expand Down Expand Up @@ -570,4 +583,14 @@ protected function wizardEnded(Request $request, $data)
{
//
}

/**
* Clean up the wizard event.
*
* @return void
*/
protected function cleanUpWizard(Request $request)
{
//
}
}
49 changes: 49 additions & 0 deletions tests/Feature/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Ycs77\LaravelWizard\Cache\CachedFile;
use Ycs77\LaravelWizard\Facades\Wizard;
use Ycs77\LaravelWizard\Test\Stubs\WizardControllerBeforeBackStepStub;
use Ycs77\LaravelWizard\Test\Stubs\WizardControllerCleanUpStub;
use Ycs77\LaravelWizard\Test\Stubs\WizardControllerOptionsStub;
use Ycs77\LaravelWizard\Test\Stubs\WizardControllerSkipStub;
use Ycs77\LaravelWizard\Test\Stubs\WizardControllerStub;
Expand Down Expand Up @@ -348,4 +350,51 @@ public function testWizardSetNoCacheFromControllerNowRunStepSaveData()
'name' => 'John',
]);
}

public function testWizardCalledCleanUpEventWhenDone()
{
$this->setWizardRoutes('/wizard/clean-up', WizardControllerCleanUpStub::class, 'wizard.clean-up');

$this->assertFalse(Cache::has('wizard.clean-up.cached'));

$response = $this->post('/wizard/clean-up/user-step-stub', [
'name' => 'John',
]);
$response->assertRedirect('/wizard/clean-up/set-cache-step-stub');

$response = $this->post('/wizard/clean-up/set-cache-step-stub');
$response->assertRedirect('/wizard/clean-up/post-step-stub');

$this->assertTrue(Cache::has('wizard.clean-up.cached'));

$response = $this->post('/wizard/clean-up/post-step-stub', [
'title' => 'Title',
'content' => 'Content.',
]);
$response->assertRedirect('/wizard/clean-up/done');

$this->assertFalse(Cache::has('wizard.clean-up.cached'));
}

public function testWizardCalledCleanUpEventWhenTriggerReset()
{
$this->setWizardRoutes('/wizard/clean-up', WizardControllerCleanUpStub::class, 'wizard.clean-up');

$this->assertFalse(Cache::has('wizard.clean-up.cached'));

$response = $this->post('/wizard/clean-up/user-step-stub', [
'name' => 'John',
]);
$response->assertRedirect('/wizard/clean-up/set-cache-step-stub');

$response = $this->post('/wizard/clean-up/set-cache-step-stub');
$response->assertRedirect('/wizard/clean-up/post-step-stub');

$this->assertTrue(Cache::has('wizard.clean-up.cached'));

$response = $this->get('/wizard/clean-up?_reset=1');
$response->assertRedirect('/wizard/clean-up/user-step-stub');

$this->assertFalse(Cache::has('wizard.clean-up.cached'));
}
}
89 changes: 89 additions & 0 deletions tests/Stubs/SetCacheToCleanUpStepStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Ycs77\LaravelWizard\Test\Stubs;

use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Http\Request;
use Ycs77\LaravelWizard\Step;
use Ycs77\LaravelWizard\Wizard;

class SetCacheToCleanUpStepStub extends Step
{
/**
* The step slug.
*
* @var string
*/
protected $slug = 'set-cache-step-stub';

/**
* The step show label text.
*
* @var string
*/
protected $label = 'Set cache step stub';

/**
* The cache store instance.
*/
protected Cache $cache;

/**
* Create a new step instance.
*
* @return void
*/
public function __construct(Wizard $wizard, int $index, Cache $cache)
{
parent::__construct($wizard, $index);

$this->cache = $cache;
}

/**
* Set the step model instance or the relationships instance.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null
*/
public function model(Request $request)
{
//
}

/**
* Save this step form data.
*
* @param \Illuminate\Http\Request $request
* @param array|null $data
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null $model
* @return void
*/
public function saveData(Request $request, $data = null, $model = null)
{
//
}

/**
* Validation rules.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function rules(Request $request)
{
return [
//
];
}

/**
* On step saved event.
*
* @return \Illuminate\Http\RedirectResponse|null
*/
public function onStepSaved(Request $request)
{
$this->cache->put('wizard.clean-up.cached', true);
}
}
44 changes: 44 additions & 0 deletions tests/Stubs/WizardControllerCleanUpStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ycs77\LaravelWizard\Test\Stubs;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Ycs77\LaravelWizard\Step;

class WizardControllerCleanUpStub extends WizardControllerStub
{
/**
* The wizard steps instance.
*
* @var array
*/
protected $steps = [
UserStepStub::class,
SetCacheToCleanUpStepStub::class,
PostStepStub::class,
];

/**
* On wizard step saved event.
*
* @return \Illuminate\Http\RedirectResponse|null
*/
protected function wizardStepSaved(Request $request, Step $step)
{
/** @var mixed $step */
if (method_exists($step, 'onStepSaved')) {
return $step->onStepSaved($request);
}
}

/**
* Clean up the wizard event.
*
* @return void
*/
protected function cleanUpWizard(Request $request)
{
Cache::forget('wizard.clean-up.cached');
}
}

0 comments on commit b7b766f

Please sign in to comment.