Skip to content

Try always using assertSame()#2947

Merged
Crabcyborg merged 5 commits into
masterfrom
try_always_using_assert_same
Feb 6, 2026
Merged

Try always using assertSame()#2947
Crabcyborg merged 5 commits into
masterfrom
try_always_using_assert_same

Conversation

@Crabcyborg
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg commented Feb 6, 2026

This update changes most uses of assertEquals to assertSame, but not all (where it is failing).

I updated some tests but left several for now.

Summary by CodeRabbit

Release Notes

  • Tests

    • Updated test assertions to use stricter equality checks across the test suite, ensuring both type and value must match in test comparisons.
  • Chores

    • Enabled stricter PHP code quality standards by activating additional PHPUnit-related fixes in the code fixer configuration.

@Crabcyborg Crabcyborg added this to the 6.28 milestone Feb 6, 2026
@deepsource-io
Copy link
Copy Markdown

deepsource-io Bot commented Feb 6, 2026

Here's the code health analysis summary for commits 8056528..bc5ea92. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource PHP LogoPHP✅ SuccessView Check ↗
DeepSource JavaScript LogoJavaScript✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 6, 2026

Warning

Rate limit exceeded

@Crabcyborg has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 42 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Configuration file updated to enforce strict PHPUnit assertion comparisons, followed by systematic replacement of loose equality assertions (assertEquals) with strict identity assertions (assertSame) across 50+ test files, ensuring type-and-value exactness in test comparisons.

Changes

Cohort / File(s) Summary
PHP CS Fixer Configuration
.php-cs-fixer.php
Added new rule 'php_unit_strict' => true to enable automatic PHPUnit strict assertion enforcement in the fixer configuration.
Core Test Classes
tests/phpunit/base/FrmUnitTest.php, tests/phpunit/database/test_FrmMigrate.php
Replaced assertEquals with assertSame across multiple assertions for stricter type-and-value comparisons in base unit and database migration tests.
Email-Related Tests
tests/phpunit/emails/test_FrmEmail.php, tests/phpunit/emails/test_FrmEmailStylesController.php, tests/phpunit/emails/test_FrmEmailSummaryHelper.php
Converted loose equality assertions to strict identity checks; one test also added a new 'last_license' option key to test scenarios.
Entry Tests
tests/phpunit/entries/test_FrmEntriesController.php, tests/phpunit/entries/test_FrmEntry.php, tests/phpunit/entries/test_FrmEntryMeta.php, tests/phpunit/entries/test_FrmEntryValidate.php, tests/phpunit/entries/test_FrmShortodeHelper.php, tests/phpunit/entries/test_FrmTableHTMLGenerator.php
Systematically replaced assertEquals with assertSame across entry validation, metadata, and rendering tests to enforce strict equality.
Field Tests
tests/phpunit/fields/test_FrmFieldCombo.php, tests/phpunit/fields/test_FrmFieldGridHelper.php.php, tests/phpunit/fields/test_FrmFieldName.php, tests/phpunit/fields/test_FrmFieldNumber.php, tests/phpunit/fields/test_FrmFieldType.php, tests/phpunit/fields/test_FrmFieldValidate.php, tests/phpunit/fields/test_FrmFieldsAjax.php, tests/phpunit/fields/test_FrmFieldsController.php, tests/phpunit/fields/test_FrmFieldsHelper.php, tests/phpunit/fields/test_FrmSubmitHelper.php
Converted loose equality checks to strict identity checks across all field-related tests, including combo, grid, validation, and submission helpers.
Form Template Tests
tests/phpunit/form-templates/test_FrmFormTemplatesController.php, tests/phpunit/form-templates/test_FrmFormTemplatesControllerAjax.php
Replaced assertEquals with assertSame for strict comparisons in form template initialization and AJAX operations.
Form Tests
tests/phpunit/forms/test_FrmForm.php, tests/phpunit/forms/test_FrmFormAction.php, tests/phpunit/forms/test_FrmFormsController.php, tests/phpunit/forms/test_FrmFormsControllerAjax.php, tests/phpunit/forms/test_FrmFormsHelper.php, tests/phpunit/forms/test_FrmOnSubmitAction.php
Updated multiple assertions across form handling, actions, and helper tests from loose to strict equality checks.
Helper Tests
tests/phpunit/helpers/test_FrmCssScopeHelper.php, tests/phpunit/misc/test_FrmAddon.php, tests/phpunit/misc/test_FrmAppController.php, tests/phpunit/misc/test_FrmAppHelper.php, tests/phpunit/misc/test_FrmCurrencyHelper.php, tests/phpunit/misc/test_FrmFormApi.php, tests/phpunit/misc/test_FrmInbox.php, tests/phpunit/misc/test_FrmSpamCheckDenylist.php, tests/phpunit/misc/test_FrmSpamCheckWPDisallowedWords.php
Converted assertEquals to assertSame across CSS, addon, app, currency, API, inbox, and spam-check helper tests for stricter type matching.
Settings Tests
tests/phpunit/settings/test_FrmSettings.php
Updated settings test assertions from loose to strict equality.
Stripe Tests
tests/phpunit/stripe/test_FrmStripeLiteAuth.php, tests/phpunit/stripe/test_FrmTransLiteActionsController.php
Replaced assertEquals with assertSame in Stripe authentication and transaction controller tests.
Style Tests
tests/phpunit/styles/test_FrmStyle.php, tests/phpunit/styles/test_FrmStylesCardHelper.php, tests/phpunit/styles/test_FrmStylesController.php, tests/phpunit/styles/test_FrmStylesHelper.php
Converted loose equality checks to strict identity checks across style sanitization, card layout, and styling utilities tests.
XML Tests
tests/phpunit/xml/test_FrmXMLHelper.php
Updated XML helper test assertions from loose to strict equality comparisons.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~25 minutes

Possibly related PRs

  • Add some extra php cs fixer rules #2946: Updates PHPUnit test assertions (replacing assertEquals with assertSame/using assertCount and correcting expected/actual parameter order) and modifies php-cs-fixer configuration to enforce stricter assertion styles.

Poem

🐰 Assertions now strict with identity's care,
No loose type coercion in the testing air!
From assertEquals to assertSame we leap,
Fifty test files now promise runs deep.
The fixer's new rule keeps strictness in place,
Type and value matching in every test case!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 70.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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 'Try always using assertSame()' is directly related to the main change in the PR, which replaces assertEquals with assertSame across all test files for stricter equality checks.

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

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch try_always_using_assert_same

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.

Copy link
Copy Markdown
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: 9

Caution

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

⚠️ Outside diff range comments (4)
tests/phpunit/misc/test_FrmSpamCheckDenylist.php (1)

114-146: ⚠️ Potential issue | 🔴 Critical

Pipeline failure: field IDs are integers in test data but strings from DB queries.

The pipeline confirms this at Line 116: Expected 460 as int vs 460 as string. $this->factory->field->create() returns int, but get_field_ids_to_check() returns field IDs from database queries which are strings in PHP/MySQL. This affects all four assertSame calls in this method (Lines 114, 126, 140, 146).

Cast the factory-created IDs to strings, or use array_map('intval', ...) on the result:

🐛 Proposed fix: cast field IDs to string for comparison
 		$this->assertSame(
 			array(
-				$this->text_field_id,
-				$this->name_field_id,
+				(string) $this->text_field_id,
+				(string) $this->name_field_id,
 			),
 			$field_ids_to_check
 		);

Apply the same (string) cast pattern to all field ID references in Lines 126–134, 140, and 146.

tests/phpunit/database/test_FrmMigrate.php (1)

99-131: ⚠️ Potential issue | 🔴 Critical

Pipeline failure: type mismatch between expected and actual field size values.

The pipeline confirms failure at Line 129: Failed asserting that 10 is identical to '10'. After unserialization, field_options['size'] can be returned as int when the value is purely numeric, while $expected ($sizes values) are strings or the result of round().

Two issues:

  1. '10' => '10' — DB unserialization may yield int 10 instead of string '10'.
  2. '1024px' => round(1024 / 9)round() returns float/int, but the DB will store and return a string.
🐛 Proposed fix: ensure consistent types
 		$sizes     = array(
 			'10px'   => '10px',
-			'10'     => '10',
-			'1024'   => '1024',
-			'1024px' => round( 1024 / 9 ),
+			'10'     => 10,
+			'1024'   => 1024,
+			'1024px' => (int) round( 1024 / 9 ),
 		);

Alternatively, cast $new_size to string before assertion if the expected values should remain strings, or cast to (string) on round():

-			'1024px' => round( 1024 / 9 ),
+			'1024px' => (string) round( 1024 / 9 ),

and investigate why '10' comes back as int from unserialization to pick the right fix direction.

tests/phpunit/fields/test_FrmFieldType.php (1)

119-138: ⚠️ Potential issue | 🔴 Critical

Pipeline failure: user_id sanitization returns integers, but expected values are strings.

The pipeline confirms at Line 137: expected ['6','2','0'] (strings) but got [6,2,0] (ints). The user_id field type's sanitize_value applies absint/intval, converting values to integers, while the test's expected array uses string literals.

🐛 Proposed fix: update expected values to match sanitized types
 		array(
 			'type'     => 'user_id',
 			'value'    => array(
 				'6',
 				'2a',
 				'a1',
 			),
 			'expected' => array(
-				'6',
-				'2',
-				'0',
+				6,
+				2,
+				0,
 			),
 		),
tests/phpunit/emails/test_FrmEmail.php (1)

764-769: ⚠️ Potential issue | 🔴 Critical

Only include_user_info has a type mismatch; is_plain_text is handled correctly.

test_set_include_user_info expects boolean values (false/true), but FrmEmail::set_include_user_info() at line 302 directly assigns $this->settings['inc_user_info'] without converting the string values '0'/'1' from the test. The property stores '0' and '1' (strings), causing assertSame(false, '0') to fail.

In contrast, test_set_is_plain_text should pass because FrmEmail::set_is_plain_text() correctly converts to boolean via ! empty() at line 285: ! empty('0') evaluates to false and ! empty('1') evaluates to true.

🐛 Fix for `test_set_include_user_info`

Either cast expected values to strings or change to assertEquals:

Option A: Update expected values to match actual stored types:

 public function test_set_include_user_info() {
     $settings = array(
-        '0' => false,
-        '1' => true,
+        '0' => '0',
+        '1' => '1',
     );
     $this->check_private_properties( $settings, 'inc_user_info', 'include_user_info' );
 }

Option B: Use assertEquals for loose type comparison.

🤖 Fix all issues with AI agents
In `@tests/phpunit/database/test_FrmMigrate.php`:
- Around line 68-93: The size assertions in test_migrate_to_17 risk failing due
to type differences (string vs int) for $field->field_options['size']; change
the assertions to use a type-flexible comparison or normalize types before
asserting: for example, in test_migrate_to_17 convert both $expected_size and
$field->field_options['size'] to the same type (cast to string or int) or
replace assertSame with assertEquals when checking $field->field_options['size']
after calls to FrmField::update and $frmdb->upgrade so the test no longer fails
if unserialization returns int 10 instead of string '10'.

In `@tests/phpunit/emails/test_FrmEmail.php`:
- Line 749: The test assertion uses PHPUnit's assertSame with arguments
reversed; change the call to assertSame so the expected value is first and the
actual value second — replace $this->assertSame( $actual, $expected ) with
$this->assertSame( $expected, $actual ) in the test (look for the assertSame
invocation using the $actual and $expected variables in test_FrmEmail.php).

In `@tests/phpunit/entries/test_FrmEntriesController.php`:
- Line 64: The assertion fails due to type mismatch between $entry->post_id
(string from DB) and $new_post->ID (int); update the test assertion in
test_FrmEntriesController.php to cast the DB value to int (compare
(int)$entry->post_id to $new_post->ID) so types match and the assertSame passes.

In `@tests/phpunit/fields/test_FrmFieldNumber.php`:
- Around line 16-19: The test is failing because check_value_is_valid_with_step
returns float values but the assertions use integer literals; update the
expected arrays in the assertions to use float values so types match (e.g., 0.0,
3.0 and 8.0, 10.0) for the calls to
$number_field->check_value_is_valid_with_step (including the string-input case
to assert it still returns floats); keep using assertSame to ensure type
equality with the returned float array.

In `@tests/phpunit/fields/test_FrmFieldsController.php`:
- Line 87: The failing assertion uses assertSame comparing $form_id (int) to
$new_field['form_id'] (string); update the test in test_FrmFieldsController.php
to avoid the strict type mismatch by either casting $new_field['form_id'] to int
before the assertSame or replace this single assertion with
$this->assertEquals($form_id, $new_field['form_id']); locate the assertion using
the symbols $form_id, $new_field['form_id'], and assertSame and apply one of
these two fixes so the types match.

In `@tests/phpunit/fields/test_FrmFieldsHelper.php`:
- Around line 216-227: The tests fail due to strict type mismatch between
integer IDs returned by $this->factory->field->create() (e.g., $draft_field_id,
$draft_field_id2) and the string IDs returned by wp_list_pluck($results, 'id');
fix by normalizing types before the assertions: convert the plucked $ids to
integers (e.g., $ids = array_map('intval', $ids)) prior to the assertSame calls
that compare to $draft_field_id/$draft_field_id2 (or alternatively cast
$draft_field_id/$draft_field_id2 to strings), ensuring
FrmFieldsHelper::get_draft_field_results test assertions compare identical
types.

In `@tests/phpunit/form-templates/test_FrmFormTemplatesController.php`:
- Line 139: The test uses $this->assertSame($expected, $favorites, ...) which
fails due to associative array key order; change the assertion to
$this->assertEquals($expected, $favorites, ...) so the comparison ignores key
order, or alternatively normalize both arrays before asserting (e.g.,
ksort($expected); ksort($favorites);) — update the assertion in
test_FrmFormTemplatesController (the line with $this->assertSame and variables
$expected and $favorites) accordingly.

In `@tests/phpunit/misc/test_FrmAppHelper.php`:
- Line 410: The test is using strict type comparison: replace the assertion that
uses assertSame comparing $new_post_id (from go_to_new_post()) and $get_post_id
(from get_query_var()) with assertEquals so the numeric string vs integer
comparison succeeds; update the assertion call from $this->assertSame(
$new_post_id, $get_post_id ); to use $this->assertEquals( $new_post_id,
$get_post_id ); ensuring the test still verifies equality but not strict type
identity.

In `@tests/phpunit/stripe/test_FrmTransLiteActionsController.php`:
- Line 38: The test fails due to a type mismatch between $field_id (int returned
by $this->factory->field->create()) and $field (string returned by
get_fields_for_price()); to fix, make the types match in the test by casting the
expected value to string before asserting (change the assertion around
$this->assertSame( $field_id, $field ) to use a string-cast of $field_id), or
alternatively update get_fields_for_price() to return ints—prefer the quick
test-side fix by casting $field_id to (string) so $field and $field_id have the
same type.
🧹 Nitpick comments (2)
tests/phpunit/entries/test_FrmTableHTMLGenerator.php (1)

57-60: Nit: assertSame argument order is reversed (pre-existing).

PHPUnit convention is assertSame($expected, $actual). On lines 57 and 60, the method call result (actual) is the first argument and $html (expected) is second. This won't affect correctness for assertSame with strings, but it will produce a confusing diff message on failure. This was pre-existing from the assertEquals version, so low priority.

Suggested fix
-		$this->assertSame( $table_generator->remove_border( $html, 'bottom' ), $html );
+		$this->assertSame( $html, $table_generator->remove_border( $html, 'bottom' ) );

		$html = '<div style="border-top:1px solid `#eee`;"></div>';
-		$this->assertSame( $table_generator->remove_border( $html ), $html );
+		$this->assertSame( $html, $table_generator->remove_border( $html ) );
tests/phpunit/emails/test_FrmEmailStylesController.php (1)

9-33: Arguments are in reversed order across all three assertions.

PHPUnit's assertSame($expected, $actual) expects the expected value first. Here, the method result (actual) is passed as the first argument and $new_content (expected) is the second. This won't cause failures but will produce confusing messages if a test fails.

Proposed fix: swap argument order
 		$this->assertSame(
-			$this->run_private_method(
-				array( 'FrmEmailStylesController', 'add_inline_css' ),
-				array( 'div', $css, $content )
-			),
-			$new_content
+			$new_content,
+			$this->run_private_method(
+				array( 'FrmEmailStylesController', 'add_inline_css' ),
+				array( 'div', $css, $content )
+			)
 		);

Same for the 'p' and 'a' assertions below.

Comment thread tests/phpunit/database/test_FrmMigrate.php
Comment thread tests/phpunit/emails/test_FrmEmail.php Outdated
Comment thread tests/phpunit/entries/test_FrmEntriesController.php Outdated
Comment thread tests/phpunit/fields/test_FrmFieldNumber.php Outdated
Comment thread tests/phpunit/fields/test_FrmFieldsController.php Outdated
Comment thread tests/phpunit/fields/test_FrmFieldsHelper.php Outdated
Comment thread tests/phpunit/form-templates/test_FrmFormTemplatesController.php Outdated
Comment thread tests/phpunit/misc/test_FrmAppHelper.php
Comment thread tests/phpunit/stripe/test_FrmTransLiteActionsController.php Outdated
@Crabcyborg Crabcyborg merged commit cf4606a into master Feb 6, 2026
19 of 20 checks passed
@Crabcyborg Crabcyborg deleted the try_always_using_assert_same branch February 6, 2026 14:53
stephywells pushed a commit that referenced this pull request Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant