From 504034d6abd7fba456af59ed192ebaa6c760b0fa Mon Sep 17 00:00:00 2001 From: Fede Isas Date: Mon, 30 Oct 2023 21:54:29 -0300 Subject: [PATCH 1/2] Alternative way to add onboarding steps I think this could be useful to design each step on it's own class (extending OnboardingStep). --- src/OnboardingSteps.php | 5 +++++ tests/OnboardTest.php | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/OnboardingSteps.php b/src/OnboardingSteps.php index 09369c0..b0b964a 100644 --- a/src/OnboardingSteps.php +++ b/src/OnboardingSteps.php @@ -14,6 +14,11 @@ public function addStep(string $title, string $model = null): OnboardingStep { $step = new OnboardingStep($title); + return $this->addOnboardingStep($step, $model); + } + + public function addOnboardingStep(OnboardingStep $step, string $model = null): OnboardingStep + { if ($model && new $model() instanceof Onboardable) { return $this->steps[$model][] = $step; } diff --git a/tests/OnboardTest.php b/tests/OnboardTest.php index 934a013..7df2fd2 100644 --- a/tests/OnboardTest.php +++ b/tests/OnboardTest.php @@ -2,6 +2,7 @@ use Spatie\Onboard\OnboardingManager; +use Spatie\Onboard\OnboardingStep; use Spatie\Onboard\OnboardingSteps; use Spatie\Onboard\Tests\Team; use Spatie\Onboard\Tests\User; @@ -294,3 +295,50 @@ ->user_name->toBe($this->user->name) ->title->tobe('Step 1'); }); + +test('can add step objects', function () { + $onboardingSteps = new OnboardingSteps(); + $firstStep = (new OnboardingStep('Step 1')) + ->link('/some/url') + ->cta('Test This!'); + $secondStep = (new class ('Step 2') extends OnboardingStep {}) + ->link('/another/url') + ->cta('Test That!'); + $onboardingSteps->addOnboardingStep($firstStep); + $onboardingSteps->addOnboardingStep($secondStep); + + $onboarding = new OnboardingManager($this->user, $onboardingSteps); + + $step = $onboarding->steps()->first(); + + expect($step) + ->title->toBe('Step 1') + ->cta->toBe('Test This!'); + + expect($onboarding->steps()->last()) + ->title->toBe('Step 2') + ->cta->toBe('Test That!'); + + $firstStep->title = 'Step 11'; + $secondStep->title = 'Step 22'; + + expect($step) + ->title->toBe('Step 11') + ->toArray()->toBe([ + 'title' => 'Step 11', + 'link' => '/some/url', + 'cta' => 'Test This!', + 'complete' => false, + 'excluded' => false, + ]); + + expect($onboarding->steps()->last()) + ->title->toBe('Step 22') + ->toArray()->toBe([ + 'title' => 'Step 22', + 'link' => '/another/url', + 'cta' => 'Test That!', + 'complete' => false, + 'excluded' => false, + ]); +}); From a62e542eda1ff2708353013659bd75b60edebbc9 Mon Sep 17 00:00:00 2001 From: Fede Isas Date: Tue, 31 Oct 2023 10:32:47 -0300 Subject: [PATCH 2/2] Method rename: addOnboardingStep to addingStep --- src/OnboardingSteps.php | 4 ++-- tests/OnboardTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/OnboardingSteps.php b/src/OnboardingSteps.php index b0b964a..cf7e974 100644 --- a/src/OnboardingSteps.php +++ b/src/OnboardingSteps.php @@ -14,10 +14,10 @@ public function addStep(string $title, string $model = null): OnboardingStep { $step = new OnboardingStep($title); - return $this->addOnboardingStep($step, $model); + return $this->addingStep($step, $model); } - public function addOnboardingStep(OnboardingStep $step, string $model = null): OnboardingStep + public function addingStep(OnboardingStep $step, string $model = null): OnboardingStep { if ($model && new $model() instanceof Onboardable) { return $this->steps[$model][] = $step; diff --git a/tests/OnboardTest.php b/tests/OnboardTest.php index 7df2fd2..9129bef 100644 --- a/tests/OnboardTest.php +++ b/tests/OnboardTest.php @@ -304,8 +304,8 @@ $secondStep = (new class ('Step 2') extends OnboardingStep {}) ->link('/another/url') ->cta('Test That!'); - $onboardingSteps->addOnboardingStep($firstStep); - $onboardingSteps->addOnboardingStep($secondStep); + $onboardingSteps->addingStep($firstStep); + $onboardingSteps->addingStep($secondStep); $onboarding = new OnboardingManager($this->user, $onboardingSteps);