Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Remove side effects from data provider #7478

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

pulsovi
Copy link
Contributor

@pulsovi pulsovi commented Aug 5, 2024

Issue

Tests_MISC_Functions->test_give_meta_helpers's data provider had side effects, which caused unexpected failures when using the --filter option of PHPUnit.

Solution

  • Removal of data provider side effects

Impact

This change improves test reliability and isolation, allowing more predictable execution, especially when using --filter.

Good practice

Data providers should never have side effects to guarantee the consistency and independence of the tests.

@jonwaldstein
Copy link
Contributor

@pulsovi thanks for the contribution! It appears our older automated tests are failing due to this change probably due to implementation update.

https://github.com/impress-org/givewp/actions/runs/10254681539/job/29133510148?pr=7478

David GABISON added 5 commits September 5, 2024 17:46
- Eliminated side effects in test_give_meta_helpers data provider
- Ensures consistent test environment regardless of --filter usage

This change prevents unexpected failures when running specific tests
with PHPUnit's --filter option.
double declaration of `add_filter('..._post_meta')` breaks the return
value. See test: Tests_MISC_Functions::test_give_meta_helpers
MyIsam engine does not support transactions
Copy link
Contributor

@jonwaldstein jonwaldstein left a comment

Choose a reason for hiding this comment

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

@pulsovi it looks like this PR has expanded from fixing our legacy test suite side effects to modifying code in production. Please understand this introduces risk to this PR and is something we have to be very careful about as it could impact our customers.

What exactly are you hoping to solve it here?

@@ -49,7 +49,7 @@ public function run() {
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (id)
) $charset";
) $charset ENGINE=InnoDB;";
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of adding this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On my machine where the default engine is MyISAM, many of the tests crash because of their interactions.

Indeed, the teardown cleanup function of legacy tests uses SQL transactions with ROLLBACK to undo any modification of the DB by the test.

But MyISAM does not manage transactions...

This is why I suggest explicit use of the InnoDB engine.

Comment on lines +518 to +525
'donors_db' => give()->get(Give_DB_Donors::class),
'donor_meta_db' => give()->get(Give_DB_Donor_Meta::class),
'comment_db' => give()->get(Give_DB_Comments::class),
'comment_db_meta' => give()->get(Give_DB_Comment_Meta::class),
'give_session' => give()->get(Give_DB_Sessions::class),
'formmeta_db' => give()->get(Give_DB_Form_Meta::class),
'sequential_db' => give()->get(Give_DB_Sequential_Ordering::class),
'donation_meta' => give()->get(Give_DB_Payment_Meta::class),
Copy link
Contributor

Choose a reason for hiding this comment

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

What's happening here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of these classes are extensions of Give_DB_Meta.

However, this class, during its instantiation, will override all post modification methods via hooks such as:

if (in_array('delete_post_metadata', $this->supports)) {
    add_filter('delete_post_metadata', [$this, '__delete_meta'], 0, 5);
}

This will trigger a bugged behavior if one of these classes is instantiated more than once.

In the specific case of deletion, the return value indicates the number of rows deleted from the database.

But if the hook is called by several instances of the class, this is what will happen:

  1. The first hook will do the deletion work and return the number of rows deleted
  2. The second hook will look for lines to delete, find none (they were all deleted by the first hook) and therefore return the value 0
  3. The hook ends by systematically returning 0, the return value is wrong.

For this reason, I made the minimum possible modifications to approach singleton pattern behavior.

This is the code you present here.

@pulsovi
Copy link
Contributor Author

pulsovi commented Sep 22, 2024

@jonwaldstein Hi,

I'm not very familiar with the PR treatment process.
Even less for change requests.

What should I do here next?

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