Skip to content

Commit

Permalink
Update StepNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Oct 14, 2019
1 parent c56fb17 commit ea37e43
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 48 deletions.
25 changes: 25 additions & 0 deletions src/Exceptions/InternalException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Ycs77\LaravelWizard\Exceptions;

use Exception;

class InternalException extends Exception
{
/**
* Undocumented function
*
* @param string $message
* @param int $code
* @param Throwable|null $previous
* @return mixed
*/
public function __construct(string $message = '', int $code = 0, \Throwable $previous = null)
{
if (!config('app.debug')) {
abort($code);
}

return parent::__construct($message, $code, $previous);
}
}
24 changes: 22 additions & 2 deletions src/Exceptions/StepNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,27 @@

namespace Ycs77\LaravelWizard\Exceptions;

class StepNotFoundException extends \Exception
class StepNotFoundException extends InternalException
{
//
/**
* The step slug.
*
* @var string
*/
protected $slug;

/**
* The wizard title.
*
* @var string
*/
protected $wizardTitle;

public function __construct(string $wizardTitle, string $slug)
{
parent::__construct("Step [$slug] is not found to wizard $wizardTitle.", 404);

$this->slug = $slug;
$this->wizardTitle = $wizardTitle;
}
}
26 changes: 11 additions & 15 deletions src/Http/Controllers/WizardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,24 +326,20 @@ protected function getLastProcessedStepIndex(Request $request)
protected function getWizardStep(Request $request, $slug)
{
/** @var \Ycs77\LaravelWizard\Step|null $step */
try {
if (isset($slug)) {
$step = $this->wizard()->stepRepo()->find($slug);
} else {
$lastProcessedStepIndex = $this->getLastProcessedStepIndex($request);
$step = $this->wizard()->stepRepo()->get($lastProcessedStepIndex);
}
if (isset($slug)) {
$step = $this->wizard()->stepRepo()->find($slug);
} else {
$lastProcessedStepIndex = $this->getLastProcessedStepIndex($request);
$step = $this->wizard()->stepRepo()->get($lastProcessedStepIndex);
}

if (is_null($step)) {
throw new StepNotFoundException();
}
if (is_null($step)) {
throw new StepNotFoundException($this->wizardTitle, $slug);
}

$this->wizard()->stepRepo()->setCurrentIndex($step->index());
$this->wizard()->stepRepo()->setCurrentIndex($step->index());

$step->setModel($request);
} catch (StepNotFoundException $e) {
abort(404);
}
$step->setModel($request);

return $step;
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Feature/HttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,21 @@ public function testRunAllWizardSteps()
]);
}

public function testWizardFormThrowStepNotFoundException()
public function testThrowStepNotFoundException()
{
$this->app['config']->set('app.debug', false);

$response = $this->get('/wizard/test/step-not-found');
$response->assertStatus(404);
}

public function testThrowStepNotFoundExceptionFromDebugMode()
{
$response = $this->get('/wizard/test/step-not-found');
$response->assertStatus(500);
$response->assertSee('Step [step-not-found] is not found to wizard Test.');
}

public function testWizardStepNotEquialToLastProcessedStep()
{
$this->session([
Expand Down
31 changes: 1 addition & 30 deletions tests/Stubs/WizardControllerOptionsStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,8 @@

namespace Ycs77\LaravelWizard\Test\Stubs;

use Ycs77\LaravelWizard\Http\Controllers\WizardController;

class WizardControllerOptionsStub extends WizardController
class WizardControllerOptionsStub extends WizardControllerStub
{
/**
* The wizard name.
*
* @var string
*/
protected $wizardName = 'test';

/**
* The wizard options.
*
Expand All @@ -23,24 +14,4 @@ class WizardControllerOptionsStub extends WizardController
protected $wizardOptions = [
'cache' => false,
];

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

/**
* Get controller main class name.
*
* @return string
*/
public function getControllerClass()
{
return static::class;
}
}
7 changes: 7 additions & 0 deletions tests/Stubs/WizardControllerStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class WizardControllerStub extends WizardController
*/
protected $wizardName = 'test';

/**
* The wizard title.
*
* @var string
*/
protected $wizardTitle = 'Test';

/**
* The wizard steps instance.
*
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function setUp()
protected function getEnvironmentSetUp($app)
{
// App
$app['config']->set('app.debug', true);
$app['config']->set('app.key', 'base64:tqASP1YzC4hhdT1nMEc+DFGMRq6WQmfMzYFW522Ce8g=');

// Database
Expand Down

0 comments on commit ea37e43

Please sign in to comment.