From 82a9a91ea7dd330771650cb6aaf92e3312695614 Mon Sep 17 00:00:00 2001 From: hd Date: Sat, 23 Sep 2017 15:42:51 +0200 Subject: [PATCH] rewrite tests, move create new step to separate function & minor fixes --- CHANGELOG.md | 13 +++- composer.json | 3 +- src/Step.php | 13 +++- src/Wizard.php | 24 +++--- tests/WizardTest.php | 169 +++++++++++++++++++++++++++++++++++++++---- 5 files changed, 195 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5c924d..76e361c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/composer.json b/composer.json index 68653ed..1f56fe8 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,5 @@ "psr-4": { "Smajti1\\Laravel\\": "src/" } - }, - "minimum-stability": "stable" + } } \ No newline at end of file diff --git a/src/Step.php b/src/Step.php index b10e306..c17f671 100644 --- a/src/Step.php +++ b/src/Step.php @@ -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; diff --git a/src/Wizard.php b/src/Wizard.php index f0eb4b9..12ef7c3 100644 --- a/src/Wizard.php +++ b/src/Wizard.php @@ -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; @@ -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 */ @@ -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; @@ -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] ?? []; diff --git a/tests/WizardTest.php b/tests/WizardTest.php index a2a2b2d..8e0bff1 100644 --- a/tests/WizardTest.php +++ b/tests/WizardTest.php @@ -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); @@ -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'); - } }