Skip to content

🐛 has control scope#35

Merged
GautierDele merged 2 commits intomainfrom
fix/has-control-scope
Jul 27, 2025
Merged

🐛 has control scope#35
GautierDele merged 2 commits intomainfrom
fix/has-control-scope

Conversation

@GautierDele
Copy link
Member

@GautierDele GautierDele commented Jul 27, 2025

closes #34

Summary by CodeRabbit

  • Tests
    • Added new tests to verify the behavior of model query scopes related to control filtering.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 27, 2025

Walkthrough

The visibility of the newControl() method in the HasControl trait was changed from protected to public. Additionally, a new feature test class, ControlsScopeTest, was introduced to verify the behavior of model query scopes related to control filtering.

Changes

Cohort Files Change Summary
Method Visibility Update src/Controls/HasControl.php Changed newControl() method visibility from protected to public in the HasControl trait.
New Feature Tests tests/Feature/ControlsScopeTest.php Added ControlsScopeTest class with tests for "controlled" and "uncontrolled" model query scopes.

Sequence Diagram(s)

sequenceDiagram
    participant Test as ControlsScopeTest
    participant Model as Model Instance
    participant Scope as Control Scope

    Test->>Model: Create 50 model instances
    Test->>Model: Apply "controlled" scope
    Model->>Scope: Evaluate control scope logic
    Scope-->>Test: Return filtered results

    Test->>Model: Apply "uncontrolled" scope
    Model->>Scope: Remove control-related scope
    Scope-->>Test: Return results without control filtering
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

A rabbit hopped in code’s domain,
Made newControl public, clear and plain.
With tests that check the query’s might,
Controlled or not, the scopes are right.
Now bunnies cheer, their work complete—
Another hop, another feat! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/has-control-scope

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
tests/Feature/ControlsScopeTest.php (3)

7-16: Improve test method naming and add setup context.

The test creates 50 models but expects 0 results from the controlled scope without any setup context. Consider:

  1. More descriptive test name: test_controlled_scope_returns_empty_when_no_access_granted
  2. Add comments explaining why 0 results are expected
  3. Consider if 50 models are necessary or if fewer would suffice
-    public function test_control_controlled_scope(): void
+    public function test_controlled_scope_returns_empty_when_no_access_granted(): void
     {
+        // Create test models - controlled scope should filter all out without proper access
         Model::factory()
-            ->count(50)
+            ->count(10) // Reduced count for faster test execution
             ->create();

         $query = Model::controlled()->get();

-        $this->assertEquals(0, $query->count());
+        $this->assertEmpty($query, 'Controlled scope should return no models without proper access configuration');
     }

18-27: Enhance the uncontrolled scope test with better assertions.

The test logic is sound but could be more robust and descriptive.

-    public function test_control_uncontrolled_scope(): void
+    public function test_uncontrolled_scope_removes_control_scope(): void
     {
         Model::factory()
-            ->count(50)
+            ->count(10) // Reduced count for faster test execution
             ->create();

         $query = Model::uncontrolled();

-        $this->assertContains(\Lomkit\Access\Controls\HasControlScope::class, $query->removedScopes());
+        $this->assertContains(
+            \Lomkit\Access\Controls\HasControlScope::class, 
+            $query->removedScopes(),
+            'Uncontrolled scope should remove HasControlScope from applied scopes'
+        );
     }

1-28: Consider adding more comprehensive test coverage.

The current tests cover basic functionality but could be expanded to include edge cases and error conditions:

  1. Test behavior when no models exist
  2. Test with different model states/configurations
  3. Test error handling scenarios
  4. Test integration with actual access control setup

Consider adding additional test methods:

public function test_controlled_scope_with_no_models(): void
{
    $query = Model::controlled()->get();
    $this->assertEmpty($query);
}

public function test_scopes_work_with_model_relationships(): void
{
    // Test scope behavior with related models
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8e3dd1 and 847c61f.

📒 Files selected for processing (2)
  • src/Controls/HasControl.php (1 hunks)
  • tests/Feature/ControlsScopeTest.php (1 hunks)

* @return Control|null The newly created control instance, or null if creation was unsuccessful.
*/
protected function newControl(): ?Control
public function newControl(): ?Control
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider alternative approaches to exposing this method for testing.

While the method functionality is correct, changing visibility from protected to public exposes internal implementation as part of the public API. This could lead to unintended usage and maintenance burden.

Consider these alternatives:

  • Use reflection in tests to access the protected method
  • Create a dedicated testing trait or helper method
  • Add a @internal annotation if this must remain public

If keeping it public, add documentation clarifying its intended use:

 /**
  * Attempts to create a new control instance.
  *
+ * @internal This method is primarily intended for internal use and testing.
+ *           External usage is not recommended and may change without notice.
  * @return Control|null The newly created control instance, or null if creation was unsuccessful.
  */
 public function newControl(): ?Control
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function newControl(): ?Control
/**
* Attempts to create a new control instance.
*
* @internal This method is primarily intended for internal use and testing.
* External usage is not recommended and may change without notice.
* @return Control|null The newly created control instance, or null if creation was unsuccessful.
*/
public function newControl(): ?Control
🤖 Prompt for AI Agents
In src/Controls/HasControl.php at line 24, the method newControl() was changed
from protected to public, exposing internal implementation unnecessarily. To fix
this, revert the method visibility back to protected and access it in tests
using reflection or a dedicated testing trait/helper. If it must remain public,
add a @internal annotation and documentation clarifying it is not part of the
public API and intended only for internal/testing use.

@GautierDele GautierDele merged commit b796493 into main Jul 27, 2025
21 checks passed
@GautierDele GautierDele deleted the fix/has-control-scope branch July 27, 2025 18:59
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.

Protected method newControl() not accessible from HasControlScope (undefined method error)

2 participants