Skip to content

AHN-2700: find charge by type, object and quantity#108

Merged
SilverFire merged 2 commits into
hiqdev:masterfrom
bladeroot:AHN-2700
Sep 12, 2025
Merged

AHN-2700: find charge by type, object and quantity#108
SilverFire merged 2 commits into
hiqdev:masterfrom
bladeroot:AHN-2700

Conversation

@bladeroot

@bladeroot bladeroot commented Sep 12, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Tests
    • Enhanced billing test scenarios to support quantity-aware charge matching when multiple charges share the same type/target.
    • Improved selection logic in test utilities to pick the correct charge based on usage quantity, reducing false positives in validations.
    • Added the ability to retrieve all matching charges for more comprehensive assertions.
    • Overall, increases test reliability and clarity for billing-related behaviors without impacting runtime application functionality.

@coderabbitai

coderabbitai Bot commented Sep 12, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces quantity-aware charge lookup in BillingContext. Updates findCharge to accept an optional quantity, adds findCharges to collect matching charges by type/target, and modifies chargeWithTarget to pass quantity. Logic iterates collected charges to find one matching the requested quantity or returns the last iterated charge when no exact quantity match exists.

Changes

Cohort / File(s) Summary
BillingContext charge lookup updates
tests/behat/bootstrap/BillingContext.php
- chargeWithTarget now passes quantity to findCharge.
- findCharge($type, $target, $quantity = null): ?ChargeInterface adds optional quantity, delegates to findCharges, returns quantity-matched charge or fallback.
- New findCharges($type, $target): ?array collects all charges matching type and target.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Test as Behat Test
  participant BC as BillingContext
  participant Coll as Charges Collection

  Test->>BC: chargeWithTarget(type, target, quantity)
  activate BC
  BC->>BC: findCharge(type, target, quantity)
  activate BC
  BC->>BC: findCharges(type, target)
  note right of BC: Filter charges by type and target
  BC->>Coll: iterate charges
  Coll-->>BC: matching charges[]
  deactivate BC

  alt charges found
    loop for each charge in charges[]
      BC->>BC: compare usage quantity
    end
    note right of BC: Return exact quantity match\nelse fallback to last iterated
    BC-->>Test: ChargeInterface|null
  else none
    BC-->>Test: null
  end
  deactivate BC
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks (2 passed, 1 warning)

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "AHN-2700: find charge by type, object and quantity" accurately and concisely describes the primary change in the PR — introducing quantity-aware lookup for charges by type and associated entity. It is specific, directly related to the changes in the diff, and short enough for a reviewer scanning history to understand the main intent.

Poem

A bunny counts each nibble’s weight,
Two carrots? Three? I’ll validate! 🥕
I sniff the pile, by type and lot,
Then pick the one that hits the spot.
If none fit quite the tally true—
I hop to last and send to you.

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.

✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@bladeroot bladeroot changed the title Ahn 2700 AHN-2700: find charge by type, object and quantity Sep 12, 2025

@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.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/behat/bootstrap/BillingContext.php (1)

405-419: Regression: null target can never match; also initialize accumulator.

findCharges() filters by target unconditionally; when $target is null (e.g., charge() step), no charge can match. Also, pre-initialize $charges for clarity and return []-emptiness consistently.

-    public function findCharges($type, $target): ?array
-    {
-        foreach ($this->charges as $charge) {
-            if ($charge->getType()->getName() !== $type) {
-                continue;
-            }
-            if ($charge->getTarget()->getFullName() !== $target) {
-                continue;
-            }
-
-            $charges[] = $charge;
-        }
-
-        return $charges ?? null;
-    }
+    public function findCharges($type, $target): ?array
+    {
+        $charges = [];
+        foreach ($this->charges as $charge) {
+            if ($charge->getType()->getName() !== $type) {
+                continue;
+            }
+            if ($target !== null && $charge->getTarget()->getFullName() !== $target) {
+                continue;
+            }
+            $charges[] = $charge;
+        }
+
+        return $charges ?: null;
+    }
🧹 Nitpick comments (1)
tests/behat/bootstrap/BillingContext.php (1)

389-403: Avoid ambiguous fallback; return null when quantity doesn’t match; compare numerically.

Current code returns the last iterated charge if no exact quantity match, which can produce misleading assertions. Prefer returning null when a quantity was provided but not found, and compare as floats to avoid string-format mismatches.

-    public function findCharge($type, $target, $quantity = null): ?ChargeInterface
-    {
-        $charges = $this->findCharges($type, $target);
-        if ($charges === null) {
-            return null;
-        }
-
-        foreach ($charges as $charge) {
-            if ((string) $quantity === (string) $charge->getUsage()->getQuantity()) {
-                return $charge;
-            }
-        }
-
-        return $charge;
-    }
+    public function findCharge($type, $target, $quantity = null): ?ChargeInterface
+    {
+        $charges = $this->findCharges($type, $target);
+        if (empty($charges)) {
+            return null;
+        }
+
+        if ($quantity !== null) {
+            foreach ($charges as $candidate) {
+                if ((float) $quantity === (float) $candidate->getUsage()->getQuantity()) {
+                    return $candidate;
+                }
+            }
+            return null;
+        }
+
+        return end($charges) ?: null;
+    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e28267a and fa5f110.

📒 Files selected for processing (1)
  • tests/behat/bootstrap/BillingContext.php (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/behat/bootstrap/BillingContext.php (4)
tests/behat/bootstrap/FactoryBasedBuilder.php (1)
  • findCharges (226-234)
src/charge/Charge.php (1)
  • getUsage (132-135)
src/charge/ChargeInterface.php (1)
  • getUsage (47-47)
src/charge/derivative/ChargeDerivativeQueryInterface.php (1)
  • getUsage (60-60)
🔇 Additional comments (1)
tests/behat/bootstrap/BillingContext.php (1)

379-387: Quantity propagation looks correct; mind BC when target is null.

Passing $quantity into findCharge is good. However, the new findCharges() requires a non-null target to match; calls from charge() pass null, which will now return no matches. See fix below on findCharges().

@SilverFire SilverFire merged commit 2f5715a into hiqdev:master Sep 12, 2025
5 checks passed
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