-
Notifications
You must be signed in to change notification settings - Fork 11
[IP-390]: filter products by category and unit #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nielsdrost7
wants to merge
1
commit into
InvoicePlane:develop
Choose a base branch
from
underdogg-forks:feature/390-product-category-filter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Modules/Products/Tests/Feature/ProductCategoryFilterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| <?php | ||
|
|
||
| namespace Modules\Products\Tests\Feature; | ||
|
|
||
| use Illuminate\Support\Str; | ||
| use Livewire\Livewire; | ||
| use Modules\Core\Tests\AbstractCompanyPanelTestCase; | ||
| use Modules\Products\Filament\Company\Resources\Products\Pages\ListProducts; | ||
| use Modules\Products\Filament\Company\Resources\Products\ProductResource; | ||
| use Modules\Products\Models\Product; | ||
| use Modules\Products\Models\ProductCategory; | ||
| use Modules\Products\Models\ProductUnit; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
|
|
||
| #[CoversClass(ProductResource::class)] | ||
| class ProductCategoryFilterTest extends AbstractCompanyPanelTestCase | ||
| { | ||
| #[Test] | ||
| #[Group('crud')] | ||
| public function it_filters_products_by_category(): void | ||
| { | ||
| /* Arrange */ | ||
| $hardware = ProductCategory::factory()->for($this->company)->create([ | ||
| 'category_name' => 'Hardware', | ||
| ]); | ||
| $software = ProductCategory::factory()->for($this->company)->create([ | ||
| 'category_name' => 'Software', | ||
| ]); | ||
|
|
||
| $hardwareProduct = Product::factory()->for($this->company)->create([ | ||
| 'category_id' => $hardware->id, | ||
| 'product_name' => 'Hardware Product', | ||
| ]); | ||
| $softwareProduct = Product::factory()->for($this->company)->create([ | ||
| 'category_id' => $software->id, | ||
| 'product_name' => 'Software Product', | ||
| ]); | ||
|
|
||
| /* Act + Assert */ | ||
| Livewire::actingAs($this->user) | ||
| ->test(ListProducts::class, ['tenant' => Str::lower($this->company->search_code)]) | ||
| ->assertSuccessful() | ||
| ->assertCanSeeTableRecords([$hardwareProduct, $softwareProduct]) | ||
| ->filterTable('category_id', $hardware->id) | ||
| ->assertCanSeeTableRecords([$hardwareProduct]) | ||
| ->assertCanNotSeeTableRecords([$softwareProduct]); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[Group('crud')] | ||
| public function it_filters_products_by_unit(): void | ||
| { | ||
| /* Arrange */ | ||
| $piece = ProductUnit::factory()->for($this->company)->create([ | ||
| 'unit_name' => 'Piece', | ||
| ]); | ||
| $hour = ProductUnit::factory()->for($this->company)->create([ | ||
| 'unit_name' => 'Hour', | ||
| ]); | ||
|
|
||
| $pieceProduct = Product::factory()->for($this->company)->create([ | ||
| 'unit_id' => $piece->id, | ||
| 'product_name' => 'Piece Product', | ||
| ]); | ||
| $hourProduct = Product::factory()->for($this->company)->create([ | ||
| 'unit_id' => $hour->id, | ||
| 'product_name' => 'Hour Product', | ||
| ]); | ||
|
|
||
| /* Act + Assert */ | ||
| Livewire::actingAs($this->user) | ||
| ->test(ListProducts::class, ['tenant' => Str::lower($this->company->search_code)]) | ||
| ->assertSuccessful() | ||
| ->assertCanSeeTableRecords([$pieceProduct, $hourProduct]) | ||
| ->filterTable('unit_id', $piece->id) | ||
| ->assertCanSeeTableRecords([$pieceProduct]) | ||
| ->assertCanNotSeeTableRecords([$hourProduct]); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[Group('multi-tenancy')] | ||
| public function it_only_offers_categories_of_the_current_tenant_as_filter_options(): void | ||
| { | ||
| /* Arrange */ | ||
| $ownCategory = ProductCategory::factory()->for($this->company)->create([ | ||
| 'category_name' => 'Own Category', | ||
| ]); | ||
| $companyB = \Modules\Core\Models\Company::factory()->create(); | ||
| $foreignCategory = ProductCategory::factory()->for($companyB)->create([ | ||
| 'category_name' => 'Foreign Category', | ||
| ]); | ||
|
|
||
| /* Act */ | ||
| $component = Livewire::actingAs($this->user) | ||
| ->test(ListProducts::class, ['tenant' => Str::lower($this->company->search_code)]) | ||
| ->assertSuccessful(); | ||
|
|
||
| /* Assert — filter options are tenant-scoped by the BelongsToCompany global scope */ | ||
| $options = $component->instance() | ||
| ->getTable() | ||
| ->getFilter('category_id') | ||
| ->getOptions(); | ||
|
|
||
| $this->assertContains('Own Category', $options); | ||
| $this->assertNotContains('Foreign Category', $options); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Unused variables
$ownCategory/$foreignCategoryflagged by static analysis; use them instead of hard-coded strings.Static analysis reports both variables as unused since the assertions reference literal strings rather than the created models' attributes, which also weakens the test's coupling to the actual seeded data.
🧹 Suggested fix
Also applies to: 106-107
🧰 Tools
🪛 PHPMD (2.15.0)
[warning] 87-87: Avoid unused local variables such as '$ownCategory'. (undefined)
(UnusedLocalVariable)
[warning] 91-91: Avoid unused local variables such as '$foreignCategory'. (undefined)
(UnusedLocalVariable)
🤖 Prompt for AI Agents
Source: Linters/SAST tools