[IP-135]: add unit tests for InvoiceCalculator and QuoteCalculator#535
[IP-135]: add unit tests for InvoiceCalculator and QuoteCalculator#535nielsdrost7 wants to merge 26 commits into
Conversation
InvoiceCalculatorTest (8 tests, no DB required): - subtotal from quantity × price - single and dual tax rate application - item-level discount reducing the taxable base before tax - grand total with taxes, document-level fixed and percentage discounts - multi-item aggregation - zero totals for empty item list QuoteCalculatorTest (7 tests, no DB required): - same scenarios but exercises the Eloquent object path (taxRate/taxRate2 relationship objects) and quote-specific discount field names Fixes #135 https://claude.ai/code/session_01L9apN3AW7b5h7ypmBA5pUg
…alculator tests Add Arrange/Act/Assert phase comments to all existing tests. Add edge case coverage: zero items, zero quantity, zero price, zero tax, 100% discount, single item, floating-point precision, multiple tax rates. Add failing-path tests documenting that the calculator passes through negative values without throwing (noted inline for future validation). https://claude.ai/code/session_01L9apN3AW7b5h7ypmBA5pUg
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughTwo new PHPUnit test classes are added: ChangesInvoiceCalculator Unit Tests
QuoteCalculator Unit Tests
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
Modules/Invoices/Tests/Unit/InvoiceCalculatorTest.php (1)
100-103: ⚡ Quick winStrengthen this assertion to exact total math.
assertGreaterThan(200.00, $totals['total'])is too permissive for a deterministic case and can let incorrect totals pass. Assert the exact expected value (with delta) to protect regression detection.Suggested change
- $this->assertGreaterThan(200.00, $totals['total']); + $this->assertEqualsWithDelta(284.00, $totals['total'], 0.001);🤖 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/InvoiceCalculatorTest.php` around lines 100 - 103, The assertion at the end of the test method uses assertGreaterThan to only verify that the total is greater than 200.00, which is too lenient for a deterministic calculation and allows incorrect totals to pass. Replace the assertGreaterThan call with assertEquals to assert the exact expected grand total value (200 + 42 + 42 = 284.00) using an appropriate delta parameter to handle floating point precision, ensuring that regressions in the calculation logic are properly detected.
🤖 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.
Inline comments:
In `@Modules/Invoices/Tests/Unit/InvoiceCalculatorTest.php`:
- Around line 21-393: Add the #[Group(...)] attribute to each test method in the
InvoiceCalculatorTest class to comply with test organization guidelines. For
each test method (it_calculates_subtotal_from_quantity_and_price,
it_applies_item_level_tax, it_applies_two_tax_rates,
it_applies_item_discount_before_tax, it_calculates_grand_total_with_taxes,
it_applies_document_level_discount, it_applies_percentage_discount,
it_aggregates_multiple_items, it_returns_zero_totals_for_empty_items, and all
edge case and failing path tests), place the #[Group('...')] attribute on the
line immediately before the #[Test] attribute using an appropriate group label
such as 'invoice_calculation', 'edge_cases', or 'failing_path' to categorize the
test's purpose and scope.
In `@Modules/Quotes/Tests/Unit/QuoteCalculatorTest.php`:
- Around line 21-380: All test methods in the QuoteCalculatorTest.php file are
missing the required `#[Group(...)]` attribute per repository standards. Add a
`#[Group(...)]` attribute directly before each `#[Test]` attribute for all test
methods (it_calculates_subtotal_from_quantity_and_price,
it_applies_tax_rate_from_relationship_object,
it_applies_two_tax_rates_from_relationship_objects,
it_applies_percentage_discount_before_tax,
it_applies_document_level_percentage_discount,
it_aggregates_totals_across_multiple_items,
it_returns_zero_totals_for_empty_item_list,
it_returns_zero_total_when_item_quantity_is_zero,
it_returns_zero_total_when_unit_price_is_zero,
it_returns_zero_tax_total_when_tax_rate_is_zero,
it_clamps_tax_base_to_zero_when_item_discount_exceeds_subtotal,
it_applies_100_percent_document_discount_resulting_in_zero_total,
it_handles_single_item_correctly,
it_sums_taxes_from_multiple_items_with_relationship_objects,
it_handles_floating_point_precision_across_multiple_items,
it_applies_flat_document_discount,
it_produces_negative_subtotal_for_negative_quantity_without_throwing, and
it_produces_negative_subtotal_for_negative_price_without_throwing). Select an
appropriate group annotation based on each test's intent, such as 'crud' for
core business logic tests.
---
Nitpick comments:
In `@Modules/Invoices/Tests/Unit/InvoiceCalculatorTest.php`:
- Around line 100-103: The assertion at the end of the test method uses
assertGreaterThan to only verify that the total is greater than 200.00, which is
too lenient for a deterministic calculation and allows incorrect totals to pass.
Replace the assertGreaterThan call with assertEquals to assert the exact
expected grand total value (200 + 42 + 42 = 284.00) using an appropriate delta
parameter to handle floating point precision, ensuring that regressions in the
calculation logic are properly detected.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0837f97a-0cf3-4d69-ac4e-9361f3ac5128
📒 Files selected for processing (2)
Modules/Invoices/Tests/Unit/InvoiceCalculatorTest.phpModules/Quotes/Tests/Unit/QuoteCalculatorTest.php
…r tests AAA phase labels must use block comments, not line comments. Documents the rule in CLAUDE.md. https://claude.ai/code/session_01L9apN3AW7b5h7ypmBA5pUg
Resource pages gate on Spatie permissions since the role-permissions feature (#500), but the test base classes never seeded them, so every company/admin panel Livewire test aborted with 403. Seed permissions and roles in setUp and assign client_admin / super_admin to the test users. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
array-access null-coalescing on stdClass throws a TypeError in PHP 8, so quantity/price/discount now branch on is_array — matching the documented array|object contract and the object tax-rate handling. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Closes #135
What was missing
No tests existed for
InvoiceCalculatororQuoteCalculator— the core tax calculation logic had zero coverage.Changes
Modules/Invoices/Tests/Unit/InvoiceCalculatorTest.php(8 tests, no DB required):it_calculates_subtotal_from_quantity_and_priceit_applies_item_level_taxit_applies_two_tax_ratesit_applies_item_discount_before_tax— verifies discount reduces the taxable base, not the display subtotalit_calculates_grand_total_with_taxesit_applies_document_level_discountit_applies_percentage_discountit_aggregates_multiple_itemsit_returns_zero_totals_for_empty_itemsModules/Quotes/Tests/Unit/QuoteCalculatorTest.php(7 tests, no DB required):it_calculates_subtotal_from_quantity_and_priceit_applies_tax_rate_from_relationship_object— exercises the Eloquent object path (item asstdClasswith->taxRateproperty)it_applies_two_tax_rates_from_relationship_objectsit_applies_percentage_discount_before_taxit_applies_document_level_percentage_discount— usesquote_discount_percentfield nameit_aggregates_totals_across_multiple_itemsit_returns_zero_totals_for_empty_item_listBoth test classes extend
AbstractTestCase(noRefreshDatabase). They test the real, existing calculator logic — no mocking, all tests should pass immediately.Run
Generated by Claude Code
Summary by CodeRabbit