Skip to content

Add unit tests for mutex classes#20786

Open
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
WarLikeLaux:add-mutex-tests
Open

Add unit tests for mutex classes#20786
WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
WarLikeLaux:add-mutex-tests

Conversation

@WarLikeLaux
Copy link
Copy Markdown
Contributor

@WarLikeLaux WarLikeLaux commented Mar 7, 2026

Q A
Is bugfix?
New feature?
Tests added? ✔️
Breaks BC?
Fixed issues

What does this PR do?

Adds unit tests for Mutex, DbMutex, and OracleMutex.

Coverage

File Tests Lines MSI
framework/mutex/Mutex.php 7 15/17 (88%) 86% (20/23)
framework/mutex/DbMutex.php 5 2/2 (100%) 0% (0/1)
framework/mutex/OracleMutex.php 8 3/28 (11%) 61% (8/13)

Changelog not required, because no source files changed.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.17%. Comparing base (9265980) to head (1325585).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #20786      +/-   ##
============================================
+ Coverage     80.15%   80.17%   +0.01%     
  Complexity    11536    11536              
============================================
  Files           374      374              
  Lines         30213    30213              
============================================
+ Hits          24218    24222       +4     
+ Misses         5995     5991       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WarLikeLaux WarLikeLaux marked this pull request as ready for review March 7, 2026 17:46
@WarLikeLaux WarLikeLaux changed the title Add tests for mutex classes Add unit tests for mutex classes Mar 7, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds PHPUnit coverage for Yii2’s mutex layer to validate core behaviors in Mutex, DB-backed mutex initialization in DbMutex, and driver validation / configuration defaults in OracleMutex.

Changes:

  • Add unit tests for base Mutex acquire/release behavior and autoRelease handling using a lightweight mock mutex.
  • Add unit tests for DbMutex to verify $db resolution via app component, config arrays, and invalid configuration handling.
  • Add unit tests for OracleMutex to verify supported drivers, default/custom properties, and invalid driver exceptions.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/framework/mutex/MutexTest.php Covers core Mutex acquire/release semantics and ensures acquire short-circuits when already held.
tests/framework/mutex/DbMutexTest.php Verifies DbMutex::$db initialization via Instance::ensure() across supported input forms and error cases.
tests/framework/mutex/OracleMutexTest.php Validates OracleMutex::init() driver checks plus default/custom property configuration and exception behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 9, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Tests
    • Comprehensive new test coverage for mutex operations, including database connection configuration and validation, lock acquisition and release behavior with auto-release functionality, and database-specific mutex initialization and error handling.

Walkthrough

Three new test suites were added to validate mutex implementations: DbMutexTest verifies database mutex configuration and property handling, MutexTest covers DumbMutex lock acquisition and release operations, and OracleMutexTest validates Oracle-specific mutex initialization and driver constraints.

Changes

Cohort / File(s) Summary
New Mutex Test Suites
tests/framework/mutex/DbMutexTest.php, tests/framework/mutex/MutexTest.php, tests/framework/mutex/OracleMutexTest.php
Added three comprehensive test classes covering DbMutex property behavior and default resolution, DumbMutex lock acquisition/release and auto-release functionality, and OracleMutex initialization with driver validation and configurable properties.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Three test suites hop into place,
DbMutex, DumbMutex, Oracle's grace,
Locks acquired and released with care,
Configuration validated everywhere! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title clearly and concisely describes the main change: adding unit tests for mutex classes, which matches the changeset content.
Description check ✅ Passed The pull request description is directly related to the changeset, providing context about the tests added, coverage metrics, and clarifying that no source files were changed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Copy link
Copy Markdown

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

🧹 Nitpick comments (1)
tests/framework/mutex/OracleMutexTest.php (1)

23-97: Optional: extract repeated DB mock setup into a helper.

The Connection mock + driverName mapping pattern is repeated in several tests; a private helper would make this file easier to maintain.

♻️ Suggested refactor
 class OracleMutexTest extends TestCase
 {
+    private function createDbMock(string $driverName): Connection
+    {
+        $db = $this->createMock(Connection::class);
+        $db->method('__get')->willReturnMap([
+            ['driverName', $driverName],
+        ]);
+        return $db;
+    }
+
     public function testInitSucceedsWithOciDriver(): void
     {
-        $db = $this->createMock(Connection::class);
-        $db->method('__get')->willReturnMap([
-            ['driverName', 'oci'],
-        ]);
+        $db = $this->createDbMock('oci');
         ...
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/framework/mutex/OracleMutexTest.php` around lines 23 - 97, Multiple
tests in OracleMutexTest repeat creating a Connection mock with a driverName
mapping; extract that into a private helper (e.g., private function
createConnectionMock(string $driverName): Connection) and use it from tests like
testInitSucceedsWithOciDriver, testInitSucceedsWithOdbcDriver,
testDefaultProperties, testCustomProperties, and
testThrowsExceptionForInvalidDrivers to reduce duplication; the helper should
configure the __get()->willReturnMap([...]) for 'driverName' and return the mock
so tests can pass it into mockApplication() and OracleMutex instantiation.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/framework/mutex/OracleMutexTest.php`:
- Around line 23-97: Multiple tests in OracleMutexTest repeat creating a
Connection mock with a driverName mapping; extract that into a private helper
(e.g., private function createConnectionMock(string $driverName): Connection)
and use it from tests like testInitSucceedsWithOciDriver,
testInitSucceedsWithOdbcDriver, testDefaultProperties, testCustomProperties, and
testThrowsExceptionForInvalidDrivers to reduce duplication; the helper should
configure the __get()->willReturnMap([...]) for 'driverName' and return the mock
so tests can pass it into mockApplication() and OracleMutex instantiation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f016babe-ad09-4a88-bcc5-2a7217663ff3

📥 Commits

Reviewing files that changed from the base of the PR and between 9265980 and 1325585.

📒 Files selected for processing (3)
  • tests/framework/mutex/DbMutexTest.php
  • tests/framework/mutex/MutexTest.php
  • tests/framework/mutex/OracleMutexTest.php

@terabytesoftw terabytesoftw added the status:code review The pull request needs review. label Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:code review The pull request needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants