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

Introduced improved way to interact with settings in tests #12834

Merged
merged 9 commits into from
Apr 16, 2023

Conversation

marcusmoore
Copy link
Collaborator

Description

This PR improves the way settings are interacted with in tests. Previously, in most feature tests we would have to set up app settings by adding the following to the top of each test:

Setting::factory()->create()

// or something like
Setting::factory()->withMultipleFullCompanySupport()->create()

Another approach could be to put it in the universal setUp() method but that would nullify our usage of LazilyRefreshDatabase since each test would be refreshing the database regardless of the actual test code needing it.

In addition, subsequent changes to settings had the possibility of not talking practical effect if Setting::getSettings() was called prior to the changes since that method caches settings in a static variable.

This PR introduces a trait and fluent interface for interacting with settings in a similar manner as Laravel's WithFaker trait.

Tests that need to interact with the application's settings should add the InteractsWithSettings trait to the class. Our base TestCase's setUp method checks for this trait and will initialize the app's settings using the Setting factory. It will also allow tests to use the following syntax for updating application settings within tests:

$this->settings->enableMultipleFullCompanySupport();

$this->settings->set(['some' => 'thing']);

Using these methods will clear the settings cache that may have been stored if Setting::getSettings() was called.

Additional helper methods like enableMultipleFullCompanySupport() can be added to the Settings class as they are needed.

Note: I'm avoiding updating the testing docs to document this because I want to use it in other pieces of work a bit to make sure the API is solid.

@what-the-diff
Copy link

what-the-diff bot commented Apr 14, 2023

PR Summary

  • Introduced a new trait for test cases
    Added a new trait to make handling settings in tests more efficient.

  • Refactored tests using settings
    Updated all relevant tests to use the new trait instead of directly interacting with the database model.

  • Simplified settings creation with factory method
    Created a factory method to enable multiple full company support by default, enhancing readability and test setup efficiency.

@snipe snipe merged commit 8c09226 into snipe:develop Apr 16, 2023
3 checks passed
@marcusmoore marcusmoore deleted the mxm/improve-test-setup branch April 17, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants