-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Connection is not lazy anymore, since platform detection was introduced - exposing the issue via broken test #784
Connection is not lazy anymore, since platform detection was introduced - exposing the issue via broken test #784
Conversation
Hello, thank you for creating this pull request. I have automatically opened an issue http://www.doctrine-project.org/jira/browse/DBAL-1130 We use Jira to track the state of pull requests and the versions they got |
Here's the proof that the test is causing an exception, but only because the password is incorrect - not because there's some sort of "Connection is Closed" exception: https://travis-ci.org/doctrine/dbal/jobs/48084218#L298 |
@@ -405,7 +405,7 @@ public function testConnectionIsClosed() | |||
{ | |||
$this->_conn->close(); | |||
|
|||
$this->setExpectedException('Doctrine\\DBAL\\Exception\\DriverException'); | |||
//$this->setExpectedException('Doctrine\\DBAL\\Exception\\DriverException'); | |||
|
|||
$this->_conn->quoteIdentifier('Bug'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the platform cannot be fetched? This code will likely cause a method call on null
, according to #783.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this should still throw an exception if the $platform
is null (connection is re-opened, platform is re-detected, platform is null
due to failed automatic detection)
The exception type may change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, $this->platform
will never fail being detected: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php#L386.
But more importantly, this test was added to test this change: https://github.com/doctrine/dbal/pull/688/files#diff-7b1e94fb7dd408985f7b7973330dc2edL600 (you can see this referenced from #691 where this change came from).
Basically, the test was to check to make sure that if the connection is closed, we don't end up getting any "property doesn't exist" type errors. I'm not sure that's something that can be tested, and I can't see how this test was ever valid. So, either this test should be removed (because it's totally not testing what it was intended to test) or I'm completely missing the purpose of this test. Both are possible :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the only way to properly test this without side effects is by using reflection. @Ocramius what do you think? Any other idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay seems there are no other suggestions so I would accept using reflection here to do the assertion. @weaverryan can you do that?
Agree that the test should be rewritten somehow. The test is very risky and highly depends on implementation detail. I guess either Reflection has to be used to test the internal state of |
@deeky666 I agree on all points! Now that we're connecting when the platform is being determined, we do now try to connect in some cases, so we have to be careful (since, as you said, it was never the real intention). I'd really like your thoughts on #783 (other than the removal of this test in that PR - I can rewrite it with reflection). Thanks! |
…perty has be nullified
Done! Summary:
The only issue is that this test should technically test that the property was nullified, and not unset. But, I'm not actually sure if that's testable - even with reflection. Even when I unset the property temporarily, Reflection sees it just fine (since it's in the class declaration). My opinion (fwiw) is that we merge this, or do-away with the test entirely. But I think we should keep it. Thanks! |
@weaverryan thanks for your investigation! IMO if not even reflection can detect whether a property was unset, this test is rather useless. As you stated this test does not fail if the property was unset so it is not a failing test case for the original issue. class DBAL1003Connection extends \Doctrine\DBAL\Connection
{
public function getWrappedConnection()
{
return $this->_conn;
}
}
class ConnectionTest extends \Doctrine\Tests\DbalTestCase
{
$connection = new DBAL1003Connection(); // params to be inserted
$connection->close();
$this->assertNull($connection->getWrappedConnection());
} I think this should raise the notice and reveal the bug. |
Or we simply mock |
Yep that reveals the issue: public function testConnectionIsClosed()
{
$connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
->disableOriginalConstructor()
->setMethods(array('connect'))
->getMock();
$connection->close();
$this->assertNull($connection->getWrappedConnection());
} |
… close (not unset) This mocks connect(), sets the internal _conn to an object, then calls close, which should re-set _conn back to null. The getWrappedConnection() call gives us access to _conn, but does NOT trigger a re-connect, since we mocked connect() to do nothing. Ultimately, if _conn is unset, calling getWrappedConnection() will error. If _conn is not set to null, the test will fail. This tests all the cases.
@deeky666 excellent suggestion - I've updated the test (I'm still using reflection to set I've verified that Thanks! |
…flawed "Breaking" a test temporarily to show that it's not doing what is expect...
@weaverryan finally merging, thanks again! |
Hi guys!
This is not meant to be merged, at least not yet. The test added in #691 is flawed. It's failing NOT because there is an exception when trying to use a closed connection (in fact, if the connection is closed, it simply re-opens), but instead, the exception is:
The tests should show this. The reason is that we're using connection parameters at the top of this class (https://github.com/doctrine/dbal/blob/master/tests/Doctrine/Tests/DBAL/ConnectionTest.php#L19) that, until now, were never used to actually connect to the database. But now, they are being used to connect to the database, but they're incorrect - they should be pulling from
TestUtil
.So, this test needs to be fixed or removed.
Thanks!