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

[3.x] Add method to run a callback on the central application #512

Closed
wants to merge 5 commits into from

Conversation

morloderex
Copy link
Contributor

Closes: #487

I found some free time to create a helper for when a tenant has been identified and you want to run a callback on the central application.

I'm not quite sure about the naming tho:

Other naming possiblities considered:

  • runAsCentral($callback)
  • onCentral($callback)
  • runCentral($callback)

Signed-off-by: michael lundbøl <[email protected]>
@codecov
Copy link

codecov bot commented Oct 24, 2020

Codecov Report

Merging #512 into 3.x will increase coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##                3.x     #512      +/-   ##
============================================
+ Coverage     87.01%   87.10%   +0.09%     
- Complexity      370      373       +3     
============================================
  Files           103      103              
  Lines          1078     1086       +8     
============================================
+ Hits            938      946       +8     
  Misses          140      140              
Impacted Files Coverage Δ Complexity Δ
src/Tenancy.php 92.72% <100.00%> (+1.23%) 24.00 <3.00> (+3.00)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 411d486...0c3fe61. Read the comment docs.

Copy link
Member

@stancl stancl left a comment

Choose a reason for hiding this comment

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

Hey. Thanks for submitting this.

I suggested a few changes.

$this->assertSame('acme', app('tenancy_initialized_for_tenant'));

tenancy()->runGlobal(function () {
GlobalRun::$count = 1;
Copy link
Member

Choose a reason for hiding this comment

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

I think you could just assert that the connection was tenant before this call and is not tenant inside it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to make sure that the callback is indeed being called.

Copy link
Member

Choose a reason for hiding this comment

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

Count is not really an intuitive way of testing things. I'd make assertions that:

  1. The connection was tenant but is not anymore, testing that we're in the central context
  2. Setting a value in app(), testing that the callback is being called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you take a look at the changes to the tests and se if you like this better?

src/Tenancy.php Show resolved Hide resolved
src/Tenancy.php Outdated Show resolved Hide resolved
Signed-off-by: michael lundbøl <[email protected]>
Signed-off-by: michael lundbøl <[email protected]>
@morloderex morloderex requested a review from stancl October 24, 2020 14:54
@morloderex
Copy link
Contributor Author

Hey @stancl I believe your comments have been addressed now.

Can you take a second look?

@morloderex morloderex changed the title Add runGlobal helper [3.x] Add method to run a callback on the central application Oct 24, 2020
src/Tenancy.php Outdated Show resolved Hide resolved
Comment on lines +105 to +116
$runned = 0;

$this->assertFalse(tenancy()->initialized);
$this->assertFalse(app()->bound('tenancy_ended'));
$this->assertFalse(app()->bound('tenancy_initialized_for_tenant'));

tenancy()->central(function () use (&$runned) {
$this->assertFalse(app()->bound('tenancy_initialized_for_tenant'));
$this->assertFalse(app()->bound('tenancy_ended'));
$this->assertFalse(tenancy()->initialized);

$runned = 1;
Copy link
Member

Choose a reason for hiding this comment

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

What I meant is to:

  1. Check that tenancy was initialized by seeing that config('database.default') was tenant and is not anymore.
  2. Do app()->instance('central_callback_executed', true) inside this closure.

I can just rewrite the whole thing if you want.

It should

  • set it to false at the beginning of the test
  • check that it's false right before the central() call
  • set it to true inside the central call
  • check that it's true after the central call

Copy link
Contributor Author

@morloderex morloderex Oct 24, 2020

Choose a reason for hiding this comment

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

@stancl We have 2 different tests here which tests different scenarios seperatly.

One where the tenant is already initialized and one where a tenant is not already initialized.

The test you are commenting on is the use case where a tenant has not been initialized so i want to make sure that the TenancyManager does not call the end method before running the callback.

However, am not quite sure why you don't see this logic as sufficient? I am not quite sure why we would need to go through the entire Laravel container class to check that a veriable is changed within a callback? Using a variable passed in by reference does the same thing here and the assertions called within the callback tests wether or not the bootstrapper provided has been executed. Can you ellabrorate on throughs here?

Co-authored-by: Samuel Štancl <[email protected]>
@stancl
Copy link
Member

stancl commented Nov 12, 2020

there are some other changes that I'd want here, but this is a small amount of code so it's literally easier to just write it than to explain the changes

appreciate the effort, closing in favor of #526

@stancl stancl closed this Nov 12, 2020
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.

Helper for global context
2 participants