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

Use Maintenance::getDB in setupStore.php, refs 2963 #2968

Merged
merged 1 commit into from Jan 28, 2018
Merged

Conversation

mwjames
Copy link
Contributor

@mwjames mwjames commented Jan 27, 2018

This PR is made in reference to: #2963

This PR addresses or contains:

This PR includes:

  • Tests (unit/integration)
  • CI build passed

Fixes #2963

@mwjames mwjames added this to the SMW 3.0.0 milestone Jan 27, 2018
@mwjames
Copy link
Contributor Author

mwjames commented Jan 27, 2018

@kghbln This passes the test suite which means it should be safe to use whether it addresses the $wgDBadminuser issue or not but it would be good to confirm the assumption somehow.

$store->setConnectionManager(
$connectionManager
);

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this should be a single function. No need to have these two new variables in scope longer, or to access the ones already in scope. Plus this function is already very long, with lots of random details (thus mixed levels of abstractions) in it.

/**
* @since 3.0
*
* @return mixed
Copy link
Member

Choose a reason for hiding this comment

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

Should this not be a connection thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it is a generic callback therefore you type. The invoker is responsible to make the right choice.

Copy link
Member

Choose a reason for hiding this comment

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

I understand that this function cannot guarantee the type that is returned. Though I think this does not really matter for the return type. The return type is about indicating what it should return. Of course this should match what the callback is supposed to return. Unless I am misunderstanding the code, in both cases this should be a connection, whatever a connection is. Weirdly enough the ConnectionProvider interface does not mention a specific type. Is that understanding wrong?

*
* @param Closure $callback
*/
public function __construct( Closure $callback ) {
Copy link
Member

Choose a reason for hiding this comment

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

callable is less restrictive and has no downsides as far as I know

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but nothing is wrong with Closure and it doesn't impose a functional impairment.

Copy link
Member

Choose a reason for hiding this comment

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

It does: you can only provide Closures like this. You can't pass in 'globalFunctionName', 'SomeClass::staticFunction' or $instanceOfClassThatIsCallable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, yes that is the only valid functional argument I have seen in this thread that should be reconsidered to allow for something like [ $this, '...' ]. [0] requires PHP 5.4 which is fine given that min req. is PHP 5.5.

[0] http://php.net/manual/de/language.types.callable.php


$connection = $this->getMockBuilder( '\stdClass' )
->disableOriginalConstructor()
->getMock();
Copy link
Member

Choose a reason for hiding this comment

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

huh?! This is the same as $connection = new \stdClass() no?

Copy link
Member

Choose a reason for hiding this comment

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

Also beware that this PHPUnit syntax is not compatible with the latest versions of PHPUnit. I don't know if it is easy to switch to using a more recent version of PHPUnit (with SMW being a MW extension and all...), though if possible, doing this sooner rather than later will save migration work.

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 should work in any version, or? and if not what changed?

to switch to using a more recent version of PHPUnit (with SMW being a MW extension and all...)

2713 has the details!

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Oh... actually this might indeed work in the latest PHPUnit. I'm not sure as I have not used this format recently. Which is because it does not make sense to use this form there. You'd just do $connection = $this->createMock( '\stdClass' )

At least you would if there was reason to use the mock API. Why not just create the instance like in my first comment?

CallbackConnectionProvider::class,
new CallbackConnectionProvider( $callback )
);
}
Copy link
Member

Choose a reason for hiding this comment

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

IMO this does not add value and is thus harmful clutter. (Yes I realize I introduced the first tests like this in the codebase, this was a mistake)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It works for me!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

harmful clutter

Disagree, this allows to check whether the constructor does work or not.

);
}

public function testGetConnection() {
Copy link
Member

Choose a reason for hiding this comment

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

This is not a descriptive name. I suggest doing something like testGetConnectionReturnsTheCallbacksReturnValue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feel free to changed it, it works for me.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not set on having this particular bit of code look one way or the other. What I am doing here with my time is giving you some advice, which you are of course free to ignore. Naming your test methods badly indeed "will work". Naming them more descriptive will work better, both for yourself, and to a bigger degree for other people. Not writing any tests also "will work". I recall giving advice to this one specific contributor several years ago that writing tests is a good idea, and at first getting a bunch of outraged "oh, so now I need to write tests for everything?!". I was happy to see this person changed their mind, wrote a lot of tests, and now encourages other people to do the same. If they had stayed at "it works for me", that'd have been a shame.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let not mix functional relevancy (which is to use a unit test to verify that what is expected can actually be asserted) with cosmetic preference which the later is about. We do agree that descriptive names may help others to understand the code in question but for a class with one single responsibility we don't have go in circles here and keep discussing a dead horse.

Copy link
Member

Choose a reason for hiding this comment

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

So you are saying that in classes with a single responsibility good naming is not important as long as the classes do their job? And also that I am wasting my time even bringing this up?

Copy link
Member

@JeroenDeDauw JeroenDeDauw left a comment

Choose a reason for hiding this comment

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

Code quality issues

Also beware that you did not test most of the new code (the new test only covers arguably the simplest addition)

@mwjames
Copy link
Contributor Author

mwjames commented Jan 27, 2018

Also beware that you did not test most of the new code (the new test only covers arguably the simplest addition)

That is not true,it is covered by the integration test.

@mwjames mwjames merged commit 6e2059c into master Jan 28, 2018
@mwjames mwjames deleted the setup-db-handle branch January 28, 2018 07:56
@mwjames
Copy link
Contributor Author

mwjames commented Jan 28, 2018

@kghbln Would be nice if you could test this with the wgDBadminuser user thing as I'd like to finish this up.

@JeroenDeDauw
Copy link
Member

That is not true,it is covered by the integration test.

What integration test? One that was already there? That verify the correctness of the new code if it was already there and did not fail before the new code was added. In such a case you could refactor the code, accidentally break the new behavior, and still have the test passing.

@kghbln
Copy link
Member

kghbln commented May 2, 2018

Would be nice if you could test this with the wgDBadminuser user thing as I'd like to finish this up.

I will switch sandbox today. Will it be possible to back-port this to 2.5.x once I confirmed it is working on master?

@kghbln
Copy link
Member

kghbln commented May 2, 2018

@mwjames The field test just failed. "setupStore.php" still uses user and password defined with $wgDBuser and $wgDBpassword respectively. $wgDBadminuser and $wgDBadminpassword also defined is being ignored.

Privileges assigned:

  • user: SELECT, INSERT, UPDATE, DELETE
  • admin: SELECT, INSERT, UPDATE ,DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, TRIGGER, LOCK TABLES, REFERENCES

@mwjames
Copy link
Contributor Author

mwjames commented May 12, 2018

The field test just failed. "setupStore.php" still uses user and password defined

Well. after browsing the Maintenance class it became clear that we need to set \Maintenance::DB_ADMIN to force a preference for the admin related user.

@kghbln
Copy link
Member

kghbln commented May 12, 2018

Well. after browsing the Maintenance class it became clear that we need to set \Maintenance::DB_ADMIN to force a preference for the admin related user.

No worries and thanks for the fix. I will do another field test after the next update on sandbox.

@kghbln
Copy link
Member

kghbln commented May 13, 2018

I will do another field test after the next update on sandbox.

Works perfect now. Thanks a lot!

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.

setupStore.php: Use wgDBadminuser instead wgDBuser
3 participants