Skip to content

Commit

Permalink
rewrite tests, move create new step to separate function & minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
smajti1 committed Sep 23, 2017
1 parent 81db9c0 commit 82a9a91
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 27 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.1.1] - 2017-09-23
### Added
- `Smajit1\Laravel\Wizard::createStepClass` function has been added
### Changed
- rewrite test for `Smajti1\Laravel\Wizrad` class
- `Smajti1\Laravel\Step::$label`, `Smajti1\Laravel\Step::$slug` and `Smajti1\Laravel\Step::$view` has been deprecated.
And will be changed to non-static
### Fixed
- Type declaration of `Smajti1\Laravel\Step::$key` has been changed to mixed

## [1.1.0] - 2017-09-21
### Added
- this CHANGELOG file
Expand All @@ -22,5 +32,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- abstract `Smajti1\Laravel\Step` class to keep main information/validation rules about one step
- `Smajti1\Laravel\Wizard` class to manage steps

[Unreleased]: https://github.com/smajti1/laravel-wizard/compare/v1.1.0...HEAD
[Unreleased]: https://github.com/smajti1/laravel-wizard/compare/v1.1.1...HEAD
[1.1.1]: https://github.com/smajti1/laravel-wizard/compare/v1.1.0...v1.1.1
[1.1.0]: https://github.com/smajti1/laravel-wizard/compare/v1.0.0...v1.1.0
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@
"psr-4": {
"Smajti1\\Laravel\\": "src/"
}
},
"minimum-stability": "stable"
}
}
13 changes: 12 additions & 1 deletion src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@
abstract class Step
{

/**
* @deprecated since 1.1.0 $label will be no more static
*/
public static $label;

/**
* @deprecated from 1.1.0 $slug will be no more static
*/
public static $slug;

/**
* @deprecated from 1.1.0 $view will be no more static
*/
public static $view;
public $number;
public $key;
public $index;
protected $wizard;

public function __construct(int $number, string $key, int $index, Wizard $wizard)
public function __construct(int $number, $key, int $index, Wizard $wizard)
{
$this->number = $number;
$this->key = $key;
Expand Down
24 changes: 15 additions & 9 deletions src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function __construct(array $steps, string $sessionKeyName = '')
}

$this->currentIndex = $index = 0;
$natural_number = 1;
foreach ($steps as $key => $step) {
$newStep = new $step($natural_number, $key, $index, $this);
$naturalNumber = 1;
foreach ($steps as $key => $stepClassName) {
$newStep = $this->createStepClass($stepClassName, $naturalNumber, $key, $index);
$this->steps[$index] = $newStep;
$index++;
$natural_number++;
$naturalNumber++;
}

$this->sessionKeyName = self::SESSION_NAME . '.' . $sessionKeyName;
Expand All @@ -36,6 +36,12 @@ public function __construct(array $steps, string $sessionKeyName = '')
}
}

protected function createStepClass($stepClassName, int $naturalNumber, $key, int $index): Step
{
$step = new $stepClassName($naturalNumber, $key, $index, $this);
return $step;
}

/**
* @return Step|null
*/
Expand Down Expand Up @@ -151,13 +157,13 @@ public function lastProcessed()
}

/**
* @return bool|null
* @return int|null
*/
public function lastProcessedIndex()
{
$data = $this->data();
if ($data) {
$lastProcessed = isset($data['lastProcessed']) ? $data['lastProcessed'] : false;
$lastProcessed = isset($data['lastProcessed']) ? $data['lastProcessed'] : null;
return $lastProcessed;
}
return null;
Expand All @@ -176,19 +182,19 @@ public function data($data = null): array
return session($this->sessionKeyName, $default);
}

public function dataHas(string $key): bool
public function dataHas($key): bool
{
$data = $this->data();
return isset($data[$key]);
}

public function dataGet(string $key)
public function dataGet($key)
{
$data = $this->data();
return $data[$key];
}

public function dataStep(Step $step, string $key): array
public function dataStep(Step $step, $key): array
{
$data = $this->data();
$stepData = $data[$step::$slug][$key] ?? [];
Expand Down
169 changes: 155 additions & 14 deletions tests/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,187 @@ class WizardTest extends PHPUnit\Framework\TestCase
protected $wizard;
protected $firstTestStepClass;
protected $secondTestStepClass;
protected $wizard_reflection;
protected $thirdTestStepClass;
protected $wizardThirdStepKey;

public function __construct()
{
parent::__construct();
$this->firstTestStepClass = $this->createMock(Step::class);
$this->firstTestStepClass = $this->getMockBuilder(Step::class)
->disableOriginalConstructor()
->setMethods([
'process',
])
->getMock();
$this->firstTestStepClass::$label = 'First step label';
$this->firstTestStepClass::$slug = 'first-step';
$this->firstTestStepClass::$view = '';

$this->secondTestStepClass = $this->createMock(Step::class);
$this->secondTestStepClass = $this->getMockBuilder(Step::class)
->disableOriginalConstructor()
->setMethods([
'process',
])
->getMock();
$this->secondTestStepClass::$label = 'Second step label';
$this->secondTestStepClass::$slug = 'second-step';
$this->secondTestStepClass::$view = '';

$this->thirdTestStepClass = $this->getMockBuilder(Step::class)
->disableOriginalConstructor()
->setMethods([
'process',
])
->getMock();
$this->secondTestStepClass::$label = 'Third step label';
$this->secondTestStepClass::$slug = 'third-step';
$this->secondTestStepClass::$view = '';

$this->wizardFirstStepKey = 'first_step_key';
$this->wizardThirdStepKey = 'step_key_third';
$this->steps = [
$this->wizardFirstStepKey => get_class($this->firstTestStepClass),
get_class($this->secondTestStepClass),
$this->wizardThirdStepKey => get_class($this->thirdTestStepClass),
];
$this->sessionKeyName = 'test';
$this->wizard = $this->getMockBuilder(Wizard::class)->setConstructorArgs([$this->steps, $this->sessionKeyName])->setMethods(null)->getMock();
$this->wizard = $this->getMockBuilder(Wizard::class)
->disableOriginalConstructor()
->setMethods(['createStepClass', 'lastProcessedIndex'])
->getMock();

$this->wizard_reflection = new \ReflectionClass(Wizard::class);
}

public function testConstructor()
{
$this->wizard->expects($this->exactly(3))
->method('createStepClass');
$this->wizard->__construct($this->steps);
}

public function testConstructorEmptySteps()
{
$this->expectException(StepNotFoundException::class);
$this->wizard->__construct([]);
}

public function testWizardTestBasicFunctions()
public function testCreateStepClass()
{
$this->assertTrue($this->wizard->limit() == count($this->steps));
$this->assertTrue($this->wizard->hasNext());
$testStepClassName = 'TestStepClassName';
$this->getMockForAbstractClass(Step::class, [], $testStepClassName, false);

$method = $this->wizard_reflection->getMethod('createStepClass');
$method->setAccessible(true);

$result = $method->invoke($this->wizard, $testStepClassName, 1, 'test_key', 2);
$this->assertInstanceOf($testStepClassName, $result);
$this->assertInstanceOf(Step::class, $result);
}

public function testPrevStep()
{
$this->wizard->__construct($this->steps);
$this->assertNull($this->wizard->prevStep());
$this->wizard->nextStep();
$this->assertInstanceOf(Step::class, $this->wizard->prevStep());
}

public function testHasStep()
{
$this->wizard->__construct($this->steps);
$this->assertFalse($this->wizard->hasPrev());
$this->assertTrue(count($this->steps) == count($this->wizard->all()));
$this->assertTrue($this->wizard->first()->key == $this->wizardFirstStepKey);
$this->assertTrue($this->wizard->nextSlug() == $this->secondTestStepClass::$slug);
$this->wizard->nextStep();
$this->assertTrue($this->wizard->hasPrev());
}

public function testGetNotExistingStep()
{
$this->expectException(StepNotFoundException::class);

$method = $this->wizard_reflection->getMethod('get');
$method->setAccessible(true);

$method->invoke($this->wizard, -1);
}

public function testPrevSlug()
{
$this->wizard->__construct($this->steps);
$this->assertNull($this->wizard->prevSlug());
$this->wizard->nextStep();
$this->assertEquals($this->firstTestStepClass::$slug, $this->wizard->prevSlug());
}

public function testNextStep()
{
$this->wizard->__construct($this->steps);
$this->assertInstanceOf(Step::class, $this->wizard->nextStep());
$this->wizard->nextStep();
$this->assertNull($this->wizard->nextStep());
}

public function testNextSlug()
{
$this->wizard->__construct($this->steps);
$this->assertEquals($this->secondTestStepClass::$slug, $this->wizard->nextSlug());
$this->wizard->nextStep();
$this->wizard->nextStep();
$this->assertNull($this->wizard->nextSlug());
}

public function testGetBySlugNotExistingStep()
{
$this->expectException(StepNotFoundException::class);
$this->wizard->getBySlug('wrong_slug');
}

public function testFirst()
{
$this->wizard->__construct($this->steps);
$result = $this->wizard->first();
$this->assertEquals($this->firstTestStepClass::$slug, $result::$slug);
}

public function testFirstOrLastProcessed()
{
$this->wizard->__construct($this->steps);
$this->wizard->expects($this->once())
->method('lastProcessedIndex')
->willReturn(1);
$allSteps = $this->wizard->all();
$result = $this->wizard->firstOrLastProcessed();
$this->assertEquals($allSteps[1], $result);
}

public function testLastProcessedIndex()
{
$wizard = $this->getMockBuilder(Wizard::class)
->disableOriginalConstructor()
->setMethods(['data'])
->getMock();
$wizard->expects($this->once())
->method('data')
->willReturn(['lastProcessed' => 1]);
$this->assertEquals($wizard->lastProcessedIndex(), 1);
}

public function testLastProcessedIndexWithoutData()
{
$wizard = $this->getMockBuilder(Wizard::class)
->disableOriginalConstructor()
->setMethods(['data'])
->getMock();
$wizard->expects($this->once())
->method('data')
->willReturn([]);
$this->assertNull($wizard->lastProcessedIndex());
}

public function testWizardTestSteps()
{
$this->wizard->__construct($this->steps);
$nextStep = $this->wizard->nextStep();
$this->assertTrue($nextStep::$slug == $this->secondTestStepClass::$slug);

Expand All @@ -57,9 +203,4 @@ public function testWizardTestSteps()
$this->assertTrue($stepBySlug::$slug == $this->secondTestStepClass::$slug);
}

public function testWizardGetNotExistingStep()
{
$this->expectException(StepNotFoundException::class);
$this->wizard->getBySlug('wrong_slug');
}
}

0 comments on commit 82a9a91

Please sign in to comment.