2024-02-28 17:55:55 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\BillingPortal;
|
|
|
|
|
|
|
|
use App\Livewire\BillingPortal\Authentication\RegisterOrLogin;
|
|
|
|
use App\Livewire\BillingPortal\Cart\Cart;
|
2024-02-28 18:48:52 +01:00
|
|
|
use App\Livewire\BillingPortal\Payments\Methods;
|
2024-02-28 17:55:55 +01:00
|
|
|
use App\Livewire\BillingPortal\Purchase;
|
|
|
|
use App\Livewire\BillingPortal\RFF;
|
|
|
|
use App\Livewire\BillingPortal\Setup;
|
|
|
|
use App\Livewire\BillingPortal\Submit;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class DependencyTest extends TestCase
|
|
|
|
{
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDependencyOrder()
|
|
|
|
{
|
|
|
|
$results = $this->checkDependencies([
|
|
|
|
RFF::class,
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
Cart::class,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertCount(1, $results);
|
|
|
|
|
|
|
|
$results = $this->checkDependencies([
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
Cart::class,
|
|
|
|
RFF::class,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertCount(0, $results);
|
|
|
|
|
|
|
|
$results = $this->checkDependencies([
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
RFF::class,
|
|
|
|
Cart::class,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertCount(0, $results);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSorting()
|
|
|
|
{
|
|
|
|
$results = $this->sort([
|
|
|
|
RFF::class,
|
2024-02-28 18:48:52 +01:00
|
|
|
Methods::class,
|
2024-02-28 17:55:55 +01:00
|
|
|
RegisterOrLogin::class,
|
|
|
|
Cart::class,
|
|
|
|
]);
|
2024-02-28 18:48:52 +01:00
|
|
|
|
2024-02-29 17:36:52 +01:00
|
|
|
$this->assertEquals(Purchase::defaultSteps(), $results);
|
2024-02-28 18:48:52 +01:00
|
|
|
|
2024-02-28 17:55:55 +01:00
|
|
|
$results = $this->sort([
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
RFF::class,
|
2024-02-28 18:48:52 +01:00
|
|
|
Methods::class,
|
|
|
|
Cart::class,
|
2024-02-28 17:55:55 +01:00
|
|
|
]);
|
2024-02-28 18:48:52 +01:00
|
|
|
|
2024-02-28 17:55:55 +01:00
|
|
|
$this->assertEquals([
|
|
|
|
Setup::class,
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
RFF::class,
|
2024-02-28 18:48:52 +01:00
|
|
|
Methods::class,
|
|
|
|
Cart::class,
|
2024-02-28 17:55:55 +01:00
|
|
|
Submit::class,
|
|
|
|
], $results);
|
|
|
|
|
|
|
|
$results = $this->sort([
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
RFF::class,
|
|
|
|
Cart::class,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
Setup::class,
|
|
|
|
RegisterOrLogin::class,
|
|
|
|
RFF::class,
|
|
|
|
Cart::class,
|
|
|
|
Submit::class,
|
|
|
|
], $results);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkDependencies(array $steps): array
|
|
|
|
{
|
|
|
|
$dependencies = Purchase::$dependencies;
|
2024-02-29 17:36:52 +01:00
|
|
|
$stepOrder = array_flip($steps);
|
2024-02-28 17:55:55 +01:00
|
|
|
$errors = [];
|
2024-02-29 17:36:52 +01:00
|
|
|
|
2024-02-28 17:55:55 +01:00
|
|
|
foreach ($steps as $step) {
|
2024-02-28 19:02:12 +01:00
|
|
|
$dependent = $dependencies[$step]['dependencies'] ?? [];
|
2024-02-29 17:36:52 +01:00
|
|
|
|
|
|
|
if (!empty($dependent) && !array_intersect($dependent, $steps)) {
|
|
|
|
$errors[] = "Dependency error: [$step] requires at least one of its dependencies [" . implode(', ', $dependent) . "] in the list.";
|
|
|
|
}
|
|
|
|
|
2024-02-28 17:59:27 +01:00
|
|
|
foreach ($dependent as $dependency) {
|
2024-02-29 17:36:52 +01:00
|
|
|
if (in_array($dependency, $steps) && $stepOrder[$dependency] > $stepOrder[$step]) {
|
2024-02-28 17:55:55 +01:00
|
|
|
$errors[] = "Dependency error: $step depends on $dependency";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-29 17:36:52 +01:00
|
|
|
|
2024-02-28 17:55:55 +01:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sort(array $dependencies): array
|
|
|
|
{
|
|
|
|
$errors = $this->checkDependencies($dependencies);
|
|
|
|
|
|
|
|
if (count($errors)) {
|
2024-02-29 17:36:52 +01:00
|
|
|
return Purchase::defaultSteps();
|
2024-02-28 17:55:55 +01:00
|
|
|
}
|
|
|
|
|
2024-02-28 17:56:41 +01:00
|
|
|
return [Setup::class, ...$dependencies, Submit::class]; // Note: Re-index if you're doing any index-based checking/comparision.
|
2024-02-28 17:55:55 +01:00
|
|
|
}
|
|
|
|
}
|