Skip to content

Commit 6d5f5b3

Browse files
Merge pull request #49 from Relaticle/2x/tests
2x tests
2 parents 23b8fdc + eecf38f commit 6d5f5b3

File tree

18 files changed

+138
-78
lines changed

18 files changed

+138
-78
lines changed

.github/workflows/run-tests.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: run-tests
2+
3+
on:
4+
push:
5+
branches: [2.x]
6+
pull_request:
7+
branches: [2.x]
8+
9+
jobs:
10+
test:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
fail-fast: true
14+
matrix:
15+
os: [ubuntu-latest]
16+
php: [8.4]
17+
laravel: [12.*]
18+
stability: [prefer-stable]
19+
include:
20+
- laravel: 12.*
21+
testbench: 9.*
22+
carbon: 2.*
23+
24+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
25+
26+
steps:
27+
- name: Checkout code
28+
uses: actions/checkout@v5
29+
30+
- name: Setup PHP
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php }}
34+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
35+
coverage: xdebug
36+
37+
- name: Setup problem matchers
38+
run: |
39+
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
40+
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
41+
42+
- name: Install dependencies
43+
run: |
44+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update
45+
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
46+
47+
- name: List Installed Dependencies
48+
run: composer show -D
49+
50+
- name: Execute tests
51+
run: vendor/bin/pest --ci

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<a href="https://laravel.com/docs/12.x"><img src="https://img.shields.io/badge/Laravel-12.x-FF2D20?style=for-the-badge&logo=laravel" alt="Laravel 12"></a>
77
<a href="https://php.net"><img src="https://img.shields.io/badge/PHP-8.3-777BB4?style=for-the-badge&logo=php" alt="PHP 8.3"></a>
88
<a href="https://github.com/Relaticle/custom-fields/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-AGPL--3.0-blue.svg?style=for-the-badge" alt="License"></a>
9+
<a href="https://github.com/relaticle/custom-fields/actions"><img src="https://img.shields.io/github/actions/workflow/status/relaticle/custom-fields/run-tests.yml?branch=2.x&style=for-the-badge&label=tests)" alt="License"></a>
910
</p>
1011

1112
A powerful Laravel/Filament plugin for adding dynamic custom fields to any Eloquent model without database migrations.

src/FieldTypeSystem/FieldManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ public function getFieldTypes(): array
124124
return $this->cachedFieldTypes;
125125
}
126126

127-
public function getFieldType(string $fieldType): ?FieldTypeData
127+
public function getFieldType(?string $fieldType): ?FieldTypeData
128128
{
129+
if ($fieldType === null) {
130+
return null;
131+
}
132+
129133
return $this->toCollection()->firstWhere('key', $fieldType);
130134
}
131135

src/Filament/Integration/Base/AbstractFormComponent.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ protected function configure(
6161
)
6262
)
6363
->dehydrated(
64-
fn (mixed $state): bool => FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CONDITIONAL_VISIBILITY) &&
65-
($this->coreVisibilityLogic->shouldAlwaysSave($customField) || filled($state))
64+
fn (mixed $state): bool => ! FeatureManager::isEnabled(CustomFieldsFeature::FIELD_CONDITIONAL_VISIBILITY) ||
65+
$this->coreVisibilityLogic->shouldAlwaysSave($customField) ||
66+
filled($state)
6667
)
6768
->required($this->validationService->isRequired($customField))
6869
->rules($this->validationService->getValidationRules($customField))

src/Filament/Integration/Builders/BaseBuilder.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ public function only(array $fieldCodes): static
8282
*/
8383
protected function getFilteredSections(): Collection
8484
{
85-
// Use a static cache within the request to prevent duplicate queries
86-
static $sectionsCache = [];
87-
88-
$cacheKey = get_class($this).':'.$this->model::class.':'.
89-
hash('xxh128', serialize($this->only).serialize($this->except));
90-
91-
if (isset($sectionsCache[$cacheKey])) {
92-
return $sectionsCache[$cacheKey];
93-
}
94-
9585
/** @var Collection<int, CustomFieldSection> $sections */
9686
$sections = $this->sections
9787
->with(['fields' => function (mixed $query): mixed {
@@ -105,16 +95,12 @@ protected function getFilteredSections(): Collection
10595
}])
10696
->get();
10797

108-
$filteredSections = $sections
98+
return $sections
10999
->map(function (CustomFieldSection $section): CustomFieldSection {
110100
$section->setRelation('fields', $section->fields->filter(fn (CustomField $field): bool => $field->typeData !== null));
111101

112102
return $section;
113103
})
114104
->filter(fn (CustomFieldSection $section) => $section->fields->isNotEmpty());
115-
116-
$sectionsCache[$cacheKey] = $filteredSections;
117-
118-
return $filteredSections;
119105
}
120106
}

src/Filament/Integration/Builders/FormBuilder.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
use Filament\Schemas\Components\Grid;
99
use Illuminate\Support\Collection;
1010
use Relaticle\CustomFields\Filament\Integration\Factories\FieldComponentFactory;
11-
use Relaticle\CustomFields\Filament\Integration\Factories\SectionComponentFactory;
1211
use Relaticle\CustomFields\Models\CustomField;
13-
use Relaticle\CustomFields\Models\CustomFieldSection;
1412

1513
class FormBuilder extends BaseBuilder
1614
{
@@ -42,14 +40,21 @@ private function getDependentFieldCodes(Collection $fields): array
4240
public function values(): Collection
4341
{
4442
$fieldComponentFactory = app(FieldComponentFactory::class);
45-
$sectionComponentFactory = app(SectionComponentFactory::class);
4643

4744
$allFields = $this->getFilteredSections()->flatMap(fn (mixed $section) => $section->fields);
4845
$dependentFieldCodes = $this->getDependentFieldCodes($allFields);
4946

50-
return $this->getFilteredSections()
51-
->map(fn (CustomFieldSection $section) => $sectionComponentFactory->create($section)->schema(
52-
fn () => $section->fields->map(fn (CustomField $customField) => $fieldComponentFactory->create($customField, $dependentFieldCodes, $allFields))->toArray()
53-
));
47+
// Return fields directly without Section/Fieldset wrappers
48+
// This ensures the flat structure: custom_fields.{field_code}
49+
// Note: We skip section grouping to avoid nested paths like custom_fields.{section_code}.{field_code}
50+
// which causes issues with Filament v4's child schema nesting behavior.
51+
// Visual grouping can be added later using alternative methods if needed.
52+
return $allFields->map(
53+
fn (CustomField $customField) => $fieldComponentFactory->create(
54+
$customField,
55+
$dependentFieldCodes,
56+
$allFields
57+
)
58+
);
5459
}
5560
}

src/Filament/Integration/Concerns/Forms/ConfiguresColorOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function hasColorOptionsEnabled(CustomField $customField): bool
3838
protected function getColoredOptions(CustomField $customField): array
3939
{
4040
return $customField->options
41-
->filter(fn (mixed $option): bool => $option->settings->color ?? false)
41+
->filter(fn (mixed $option): bool => filled($option->settings->color ?? null))
4242
->mapWithKeys(fn (mixed $option): array => [$option->id => $option->name])
4343
->all();
4444
}
@@ -51,7 +51,7 @@ protected function getColoredOptions(CustomField $customField): array
5151
protected function getColorMapping(CustomField $customField): array
5252
{
5353
return $customField->options
54-
->filter(fn (mixed $option): bool => $option->settings->color ?? false)
54+
->filter(fn (mixed $option): bool => filled($option->settings->color ?? null))
5555
->mapWithKeys(fn (mixed $option): array => [$option->id => $option->settings->color])
5656
->all();
5757
}

src/Filament/Management/Pages/CustomFieldsManagementPage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use BackedEnum;
88
use Filament\Actions\Action;
9-
use Filament\Facades\Filament;
109
use Filament\Pages\Page;
1110
use Filament\Panel;
1211
use Filament\Support\Enums\Size;
@@ -25,6 +24,7 @@
2524
use Relaticle\CustomFields\FeatureSystem\FeatureManager;
2625
use Relaticle\CustomFields\Filament\Management\Schemas\SectionForm;
2726
use Relaticle\CustomFields\Models\CustomFieldSection;
27+
use Relaticle\CustomFields\Services\TenantContextService;
2828
use Relaticle\CustomFields\Support\Utils;
2929

3030
class CustomFieldsManagementPage extends Page
@@ -136,7 +136,7 @@ public function updateSectionsOrder(array $sections): void
136136
private function storeSection(array $data): CustomFieldSection
137137
{
138138
if (FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY)) {
139-
$data[config('custom-fields.database.column_names.tenant_foreign_key')] = Filament::getTenant()?->getKey();
139+
$data[config('custom-fields.database.column_names.tenant_foreign_key')] = TenantContextService::getCurrentTenantId();
140140
}
141141

142142
$data['type'] ??= CustomFieldSectionType::SECTION->value;

src/Filament/Management/Schemas/FieldForm.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Relaticle\CustomFields\Filament\Management\Schemas;
66

77
use Exception;
8-
use Filament\Facades\Filament;
98
use Filament\Forms\Components\ColorPicker;
109
use Filament\Forms\Components\Hidden;
1110
use Filament\Forms\Components\Repeater;
@@ -324,7 +323,7 @@ public static function schema(bool $withOptionsRelationship = true): array
324323
->visible(
325324
fn (
326325
Get $get
327-
): bool => CustomFieldsType::getFieldType($get('type'))->searchable
326+
): bool => CustomFieldsType::getFieldType($get('type'))->searchable ?? false
328327
)
329328
->disabled(
330329
fn (Get $get): bool => $get(

src/Livewire/ManageCustomFieldSection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Filament\Actions\ActionGroup;
99
use Filament\Actions\Concerns\InteractsWithActions;
1010
use Filament\Actions\Contracts\HasActions;
11-
use Filament\Facades\Filament;
1211
use Filament\Forms\Concerns\InteractsWithForms;
1312
use Filament\Forms\Contracts\HasForms;
1413
use Filament\Support\Enums\Size;
@@ -24,6 +23,7 @@
2423
use Relaticle\CustomFields\Filament\Management\Schemas\FieldForm;
2524
use Relaticle\CustomFields\Filament\Management\Schemas\SectionForm;
2625
use Relaticle\CustomFields\Models\CustomFieldSection;
26+
use Relaticle\CustomFields\Services\TenantContextService;
2727

2828
final class ManageCustomFieldSection extends Component implements HasActions, HasForms
2929
{
@@ -167,7 +167,7 @@ public function createFieldAction(): Action
167167
])
168168
->mutateDataUsing(function (array $data): array {
169169
if (FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY)) {
170-
$data[config('custom-fields.database.column_names.tenant_foreign_key')] = Filament::getTenant()?->getKey();
170+
$data[config('custom-fields.database.column_names.tenant_foreign_key')] = TenantContextService::getCurrentTenantId();
171171
}
172172

173173
return [
@@ -181,7 +181,7 @@ public function createFieldAction(): Action
181181
->filter()
182182
->map(function (array $option): array {
183183
if (FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_MULTI_TENANCY)) {
184-
$option[config('custom-fields.database.column_names.tenant_foreign_key')] = Filament::getTenant()?->getKey();
184+
$option[config('custom-fields.database.column_names.tenant_foreign_key')] = TenantContextService::getCurrentTenantId();
185185
}
186186

187187
return $option;

0 commit comments

Comments
 (0)