Skip to content

Clean up incomplete tests and fix factory patterns#470

Merged
nielsdrost7 merged 7 commits into
developfrom
claude/payments-foreign-key-error-gQk5p
Jun 7, 2026
Merged

Clean up incomplete tests and fix factory patterns#470
nielsdrost7 merged 7 commits into
developfrom
claude/payments-foreign-key-error-gQk5p

Conversation

@nielsdrost7

@nielsdrost7 nielsdrost7 commented Jun 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR removes incomplete/skipped tests, fixes factory patterns to properly handle company resolution, and adds missing module configuration. The changes improve test reliability and factory consistency across the codebase.

Key Changes

  • Removed incomplete tests from ProjectsTest.php:

    • Deleted duplicate it_fails_to_create_project_without_required_project_name() test
    • Deleted incomplete it_updates_a_project() test marked with markTestIncomplete() and markTestSkipped()
  • Fixed factory patterns to properly handle company resolution:

    • Updated RelationFactory to use lazy factory pattern (Company::factory()) instead of eagerly creating companies when companyId is null
    • Updated EmailTemplateFactory to use lazy factory pattern for company resolution
    • Modified AbstractFactory::resolveForeignKey() to only query existing records when $companyId is explicitly provided, otherwise return a lazy factory instance
  • Added missing module configuration:

    • Created Modules/Invoices/module.json with proper module metadata and service provider configuration
  • Fixed test method signatures in FormatHandlersTest.php:

    • Added optional $format = null parameter to all data provider test methods to match the provider's data structure
  • Fixed syntax error in CustomersTest.php:

    • Added missing closing brace for it_only_lists_customers_for_the_current_tenant() method

Implementation Details

The factory pattern changes follow Laravel's lazy factory approach, deferring factory instantiation until needed. This prevents unnecessary database operations during test setup and allows factories to be properly resolved within the context where they're used.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W

Summary by CodeRabbit

  • Bug Fixes

    • Fixed data factory behavior to consistently handle null values and prevent unintended factory instantiation in certain scenarios.
  • Tests

    • Enhanced test coverage and fixed test method definitions to align with data provider expectations.
    • Improved project management test implementations with comprehensive CRUD validation.
  • Chores

    • Added module metadata configuration for better system organization.

claude added 7 commits June 6, 2026 06:52
Without module.json, nwidart/laravel-modules never discovers the Invoices
module, so InvoicesServiceProvider is never registered, its migrations are
never loaded, and the `invoices` table does not exist when the `payments`
migration tries to add a FK to it — causing errno 150.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
When a factory is created with ->state(['company_id' => ...]), the state
values are not yet visible inside definition() — so resolveCompanyId()
always returns null. With APP_ENV=testing, the runningUnitTests() guard
was true and resolveForeignKey executed User::where('company_id', null)
which fails because the users table has no company_id column (user-company
is a pivot relationship).

Fix: skip the DB lookup when companyId is null and fall back to
$relatedClass::factory() so standalone factory usage still works. When
the caller provides an explicit id via state (as all seeders do), Laravel
ignores the factory reference and uses the state value instead.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
Company::factory()->create() in definition() fired on every Relation
factory call because resolveCompanyId() always returns null when
company_id is supplied via ->state() (state runs after definition).
With 11 companies × 25 relations each, 275 extra companies were
created, blowing the DatabaseSeeder company-count assertion.

Replace with a lazy Company::factory() reference so the company is
only created if the state does not override company_id.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
…rrent_tenant

The method body was never closed before the #endregion comment, causing
PHPUnit to report "Unclosed '{' on line 21" and fail to load the test suite.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
Two stale/incomplete methods were duplicated causing PHP fatal errors:
- it_fails_to_create_project_without_required_project_name (markTestIncomplete)
- it_updates_a_project (markTestIncomplete + old field names)

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
resolveCompany() in definition() returns null before state() values are
applied, causing a crash on ->id. Use lazy factory reference instead.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
…r methods

PHPUnit exits with code 1 when a data provider supplies more arguments
than the test method accepts. Add optional $format parameter to the 8
affected methods so the provider matches the signature.

https://claude.ai/code/session_015zna7fenkZhhJgqWcpt53W
@coderabbitai

coderabbitai Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0b6adbc0-93c7-48e6-aaa5-5c92c5f6613a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/payments-foreign-key-error-gQk5p

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
Modules/Clients/Tests/Feature/CustomersTest.php (1)

439-458: ⚡ Quick win

Remove duplicated fixture creation in tenant-listing test.

$customerA/$customerB are each created twice, and the first pair is overwritten. This adds unnecessary DB writes and makes the test intent harder to follow.

Proposed cleanup
-        $customerA = Relation::factory()->for($this->company)->customer()->create(['company_name' => 'Visible Customer']);
-        $customerB = Relation::factory()->for($companyB)->customer()->create(['company_name' => 'Hidden Customer']);
-        $customerA = Relation::factory()->for($this->company)->customer()->create(['company_name' => 'Visible Customer']);
-        $customerB = Relation::factory()->for($companyB)->customer()->create(['company_name' => 'Hidden Customer']);
+        $customerA = Relation::factory()->for($this->company)->customer()->create(['company_name' => 'Visible Customer']);
+        $customerB = Relation::factory()->for($companyB)->customer()->create(['company_name' => 'Hidden Customer']);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Modules/Clients/Tests/Feature/CustomersTest.php` around lines 439 - 458, In
the it_only_lists_customers_for_the_current_tenant test remove the duplicate
fixture creation lines so each customer is created only once: keep a single
Relation::factory()->for($this->company)->customer()->create([...]) for
$customerA and a single
Relation::factory()->for($companyB)->customer()->create([...]) for $customerB,
then leave the Livewire::actingAs(...)->test(ListRelations::class, ...) and
assertions unchanged (ensure $customerB->id still references the created hidden
customer).
Modules/Invoices/Tests/Unit/Peppol/FormatHandlers/FormatHandlersTest.php (1)

44-44: ⚡ Quick win

Avoid nullable unused provider args in these test signatures.

These methods now accept $format = null, but they never use it (matching the PHPMD unused-parameter warnings). Either use $format in assertions or split to a provider that yields only $handlerClass for these tests.

Also applies to: 55-55, 66-66, 80-80, 94-94, 115-115, 130-130, 145-145

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Modules/Invoices/Tests/Unit/Peppol/FormatHandlers/FormatHandlersTest.php` at
line 44, The test method it_returns_correct_mime_type($handlerClass, $format =
null) (and the other similar test methods flagged) declare an unused nullable
$format parameter which triggers PHPMD warnings; remove the unused parameter
from the test signatures (e.g., change
it_returns_correct_mime_type($handlerClass): void) and update the data
provider(s) to yield only $handlerClass, or alternately use $format in the
body/assertions if that value is intended; apply the same change to the other
affected test methods in FormatHandlersTest.php so signatures and providers
align and the unused-parameter warnings are eliminated.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@Modules/Clients/Tests/Feature/CustomersTest.php`:
- Around line 439-458: In the it_only_lists_customers_for_the_current_tenant
test remove the duplicate fixture creation lines so each customer is created
only once: keep a single
Relation::factory()->for($this->company)->customer()->create([...]) for
$customerA and a single
Relation::factory()->for($companyB)->customer()->create([...]) for $customerB,
then leave the Livewire::actingAs(...)->test(ListRelations::class, ...) and
assertions unchanged (ensure $customerB->id still references the created hidden
customer).

In `@Modules/Invoices/Tests/Unit/Peppol/FormatHandlers/FormatHandlersTest.php`:
- Line 44: The test method it_returns_correct_mime_type($handlerClass, $format =
null) (and the other similar test methods flagged) declare an unused nullable
$format parameter which triggers PHPMD warnings; remove the unused parameter
from the test signatures (e.g., change
it_returns_correct_mime_type($handlerClass): void) and update the data
provider(s) to yield only $handlerClass, or alternately use $format in the
body/assertions if that value is intended; apply the same change to the other
affected test methods in FormatHandlersTest.php so signatures and providers
align and the unused-parameter warnings are eliminated.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 55507f9a-2cde-488b-94fb-848c72973515

📥 Commits

Reviewing files that changed from the base of the PR and between 2a45660 and 108f80f.

📒 Files selected for processing (7)
  • Modules/Clients/Database/Factories/RelationFactory.php
  • Modules/Clients/Tests/Feature/CustomersTest.php
  • Modules/Core/Database/Factories/AbstractFactory.php
  • Modules/Core/Database/Factories/EmailTemplateFactory.php
  • Modules/Invoices/Tests/Unit/Peppol/FormatHandlers/FormatHandlersTest.php
  • Modules/Invoices/module.json
  • Modules/Projects/Tests/Feature/ProjectsTest.php
💤 Files with no reviewable changes (1)
  • Modules/Projects/Tests/Feature/ProjectsTest.php

@nielsdrost7

Copy link
Copy Markdown
Collaborator Author

@coderabbitai autofix

@coderabbitai

coderabbitai Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

Autofix skipped. No unresolved CodeRabbit review comments with fix instructions found.

@nielsdrost7 nielsdrost7 marked this pull request as ready for review June 7, 2026 04:44
@nielsdrost7 nielsdrost7 merged commit 8469f3b into develop Jun 7, 2026
8 checks passed
@nielsdrost7 nielsdrost7 deleted the claude/payments-foreign-key-error-gQk5p branch June 7, 2026 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants