|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Code16\Sharp\Utils\Testing\Dashboard; |
| 4 | + |
| 5 | +use Code16\Sharp\Dashboard\SharpDashboard; |
| 6 | +use Code16\Sharp\Http\Context\SharpBreadcrumb; |
| 7 | +use Code16\Sharp\Show\Fields\SharpShowDashboardField; |
| 8 | +use Code16\Sharp\Show\Fields\SharpShowField; |
| 9 | +use Code16\Sharp\Utils\Entities\SharpEntityManager; |
| 10 | +use Code16\Sharp\Utils\Testing\Commands\FormatsDataForCommand; |
| 11 | +use Code16\Sharp\Utils\Testing\Commands\PendingCommand; |
| 12 | +use Code16\Sharp\Utils\Testing\IsPendingComponent; |
| 13 | +use Code16\Sharp\Utils\Testing\Show\PendingShow; |
| 14 | +use Illuminate\Foundation\Testing\TestCase; |
| 15 | + |
| 16 | +class PendingDashboard |
| 17 | +{ |
| 18 | + use FormatsDataForCommand; |
| 19 | + use IsPendingComponent; |
| 20 | + |
| 21 | + protected SharpDashboard $dashboard; |
| 22 | + public string $entityKey; |
| 23 | + protected array $filterValues = []; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + /** @var TestCase $test */ |
| 27 | + protected object $test, |
| 28 | + string $entityKey, |
| 29 | + public ?PendingShow $parent = null, |
| 30 | + ) { |
| 31 | + $this->entityKey = app(SharpEntityManager::class)->entityKeyFor($entityKey); |
| 32 | + $this->dashboard = app(SharpEntityManager::class)->entityFor($this->entityKey)->getViewOrFail(); |
| 33 | + } |
| 34 | + |
| 35 | + public function withFilter(string $filterKey, mixed $value): static |
| 36 | + { |
| 37 | + $key = $this->dashboard->filterContainer()->findFilterHandler($filterKey)->getKey(); |
| 38 | + $this->filterValues[$key] = $value; |
| 39 | + |
| 40 | + return $this; |
| 41 | + } |
| 42 | + |
| 43 | + public function get(): AssertableDashboard |
| 44 | + { |
| 45 | + $this->setGlobalFilterUrlDefault(); |
| 46 | + |
| 47 | + return new AssertableDashboard( |
| 48 | + $this->parent instanceof PendingShow |
| 49 | + ? $this->test |
| 50 | + ->getJson( |
| 51 | + route('code16.sharp.api.dashboard', [ |
| 52 | + 'dashboardKey' => $this->entityKey, |
| 53 | + ...$this->dashboard |
| 54 | + ->filterContainer() |
| 55 | + ->getQueryParamsFromFilterValues($this->dashboardShowField()->toArray()['hiddenFilters'] ?? []), |
| 56 | + ...$this->dashboardQueryParams(), |
| 57 | + ]), |
| 58 | + headers: [ |
| 59 | + SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(), |
| 60 | + ] |
| 61 | + ) |
| 62 | + : $this->test->get(route('code16.sharp.dashboard', [ |
| 63 | + 'dashboardKey' => $this->entityKey, |
| 64 | + ...$this->dashboardQueryParams(), |
| 65 | + ])), |
| 66 | + $this, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public function dashboardCommand(string $commandKeyOrClassName): PendingCommand |
| 71 | + { |
| 72 | + $this->setGlobalFilterUrlDefault(); |
| 73 | + |
| 74 | + $commandKey = class_exists($commandKeyOrClassName) |
| 75 | + ? class_basename($commandKeyOrClassName) |
| 76 | + : $commandKeyOrClassName; |
| 77 | + |
| 78 | + return new PendingCommand( |
| 79 | + getForm: fn (?string $step = null) => $this |
| 80 | + ->test |
| 81 | + ->getJson( |
| 82 | + route( |
| 83 | + 'code16.sharp.api.dashboard.command.form', |
| 84 | + [ |
| 85 | + 'entityKey' => $this->entityKey, |
| 86 | + 'commandKey' => $commandKey, |
| 87 | + 'command_step' => $step, |
| 88 | + ...$this->dashboardQueryParams(), |
| 89 | + ] |
| 90 | + ), |
| 91 | + headers: [ |
| 92 | + SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(), |
| 93 | + ] |
| 94 | + ), |
| 95 | + post: fn (array $data, ?string $step = null, ?array $baseData = null) => $this |
| 96 | + ->test |
| 97 | + ->postJson( |
| 98 | + route( |
| 99 | + 'code16.sharp.api.dashboard.command', |
| 100 | + ['entityKey' => $this->entityKey, 'commandKey' => $commandKey] |
| 101 | + ), |
| 102 | + [ |
| 103 | + 'data' => $this->formatDataForCommand( |
| 104 | + $this->dashboard->findDashboardCommandHandler($commandKey), |
| 105 | + $data, |
| 106 | + $baseData |
| 107 | + ), |
| 108 | + 'query' => $this->dashboardQueryParams(), |
| 109 | + 'command_step' => $step, |
| 110 | + ], |
| 111 | + headers: [ |
| 112 | + SharpBreadcrumb::CURRENT_PAGE_URL_HEADER => $this->getCurrentPageUrlFromParents(), |
| 113 | + ] |
| 114 | + ), |
| 115 | + commandContainer: $this->dashboard, |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + protected function dashboardShowField(): ?SharpShowDashboardField |
| 120 | + { |
| 121 | + /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 122 | + return $this->parent instanceof PendingShow |
| 123 | + ? $this->parent->show->getBuiltFields() |
| 124 | + ->first(fn (SharpShowField $field) => $field instanceof SharpShowDashboardField |
| 125 | + && $field->toArray()['dashboardKey'] === $this->entityKey |
| 126 | + ) |
| 127 | + : null; |
| 128 | + } |
| 129 | + |
| 130 | + protected function dashboardQueryParams(): array |
| 131 | + { |
| 132 | + return $this->dashboard |
| 133 | + ->filterContainer() |
| 134 | + ->getQueryParamsFromFilterValues($this->filterValues) |
| 135 | + ->all(); |
| 136 | + } |
| 137 | +} |
0 commit comments