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

Enable PHPStan in tests #3203

Closed
wants to merge 1 commit into from
Closed

Enable PHPStan in tests #3203

wants to merge 1 commit into from

Conversation

carusogabriel
Copy link
Contributor

@carusogabriel carusogabriel commented Jul 1, 2018

Q A
Type improvement
BC Break no
Fixed issues -

Summary

Enable PHPStan on test at level 3.

@carusogabriel
Copy link
Contributor Author

@Majkl578 @morozov There're a few errors left. Can you help me with them? 😇

Copy link
Member

@morozov morozov left a comment

Choose a reason for hiding this comment

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

@carusogabriel as for the remaining

 ------ ---------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/Functional/Driver/PDOSqlsrv/DriverTest.php  
 ------ ---------------------------------------------------------------------- 
  72     Call to an undefined method                                           
         Doctrine\DBAL\Driver\Connection::getAttribute().                      
 ------ ---------------------------------------------------------------------- 

You can add a type hint like @var \PDO $connection.

@@ -138,9 +138,11 @@ public function testGetEventManager()

public function testConnectDispatchEvent()
{
/** @var \PHPUnit_Framework_MockObject_MockObject $listenerMock */
Copy link
Member

Choose a reason for hiding this comment

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

Is there a problem detecting the type here? The code looks straightforward. AFAIK, PHPUnit will use namespaced class names if future versions, so it makes sense to use \PHPUnit\Framework\... instead.

Copy link
Contributor Author

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.

@@ -26,6 +26,7 @@ protected function setUp()

public function testExecutionErrorsAreNotSuppressed()
{
/** @var \Doctrine\DBAL\Statement $stmt */
Copy link
Member

Choose a reason for hiding this comment

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

This looks redundant. The @return annotation on the method declares the type.

@@ -34,7 +34,7 @@
class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
{
/**
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
* @var mixed
Copy link
Member

Choose a reason for hiding this comment

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

Is it supposed to be anything else than a schema manager?

@@ -342,10 +342,12 @@ public function testListTableColumnsDispatchEvent()

$this->_sm->dropAndCreateTable($table);

/** @var \PHPUnit_Framework_MockObject_MockObject $listenerMock */
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to remove, but, no success 😢

Copy link
Member

Choose a reason for hiding this comment

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

It still doesn't look right. PhpStorm unambiguously detects the $listenerMockType as PHPUnit\Framework\MockObject\MockObject based on the @return annotation of PHPUnit\Framework\MockObject\MockBuilder::getMock(). So should do PHPStan. Instead of annotating every call like this, we need to find what the underlying problem is and fix it.

@@ -370,10 +372,12 @@ public function testListTableIndexesDispatchEvent()

$this->_sm->dropAndCreateTable($table);

/** @var \PHPUnit_Framework_MockObject_MockObject $listenerMock */
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't be needed.

@@ -44,6 +44,7 @@ public function testReturnsBindingType()

public function testConvertsDateTimeImmutableInstanceToDatabaseValue()
{
/** @var \DateTimeImmutable|\Prophecy\Prophecy\ObjectProphecy $date */
Copy link
Member

Choose a reason for hiding this comment

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

Does phpstan/phpstan-phpunit handle prophecy mocks? Or there's a separate extension for them? In any event, it's better to have this type detection automated rather than manually entered every time.

@@ -16,7 +16,7 @@
private $platform;

/**
* @var \Doctrine\DBAL\Types\DateIntervalType
* @var \Doctrine\DBAL\Types\Type
Copy link
Member

Choose a reason for hiding this comment

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

Please revert.

@@ -17,7 +17,7 @@ class DateTimeImmutableTypeTest extends \PHPUnit\Framework\TestCase
private $platform;

/**
* @var DateTimeImmutableType
* @var Type
Copy link
Member

Choose a reason for hiding this comment

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

Please revert.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this parent/child issue be reported to upstream?

 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/Types/DateTimeImmutableTypeTest.php                                                                                              
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------- 
  26     Property Doctrine\Tests\DBAL\Types\DateTimeImmutableTypeTest::$type (Doctrine\DBAL\Types\DateTimeImmutableType) does not accept Doctrine\DBAL\Types\Type.  
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------- 

Copy link
Member

Choose a reason for hiding this comment

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

No. Please try using new DateTimeImmutableType() instead of the factory.

*/
private static $_sharedConn;

/**
* @var \Doctrine\DBAL\Connection
* @var mixed
Copy link
Member

Choose a reason for hiding this comment

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

Please revert.

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 breaks a lot of places:

 ------ ------------------------------------------------------------------------------------ 
  Line   tests/Doctrine/Tests/DBAL/Functional/Driver/IBMDB2/DB2StatementTest.php             
 ------ ------------------------------------------------------------------------------------ 
  32     Call to an undefined method Doctrine\DBAL\Driver\Statement::getWrappedStatement().  
 ------ ------------------------------------------------------------------------------------ 

 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/Functional/Driver/OCI8/OCI8ConnectionTest.php                                                                                                                
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  29     Property Doctrine\Tests\DBAL\Functional\Driver\OCI8\OCI8ConnectionTest::$driverConnection (Doctrine\DBAL\Driver\OCI8\OCI8Connection) does not accept Doctrine\DBAL\Driver\Connection.  
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/Functional/Driver/PDOConnectionTest.php                                                                                                          
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  27     Property Doctrine\Tests\DBAL\Functional\Driver\PDOConnectionTest::$driverConnection (Doctrine\DBAL\Driver\PDOConnection) does not accept Doctrine\DBAL\Driver\Connection.  
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
 ------ ------------------------------------------------------------------------------ 
  Line   tests/Doctrine/Tests/DBAL/Functional/Ticket/DBAL630Test.php                   
 ------ ------------------------------------------------------------------------------ 
  41     Call to an undefined method Doctrine\DBAL\Driver\Connection::setAttribute().  
  75     Call to an undefined method Doctrine\DBAL\Driver\Connection::setAttribute().  
  99     Call to an undefined method Doctrine\DBAL\Driver\Connection::setAttribute().  
  123    Call to an undefined method Doctrine\DBAL\Driver\Connection::setAttribute().  
 ------ ------------------------------------------------------------------------------ 

Copy link
Contributor

Choose a reason for hiding this comment

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

Those breakages look valid though, looks PDO related. Explicitly mixed only disables some checks (so better to avoid mixed).

use Doctrine\DBAL\Schema\AbstractSchemaManager;

class DriverMock implements \Doctrine\DBAL\Driver
{
/**
* @var DatabasePlatformMock
* @var AbstractPlatform|DatabasePlatformMock
Copy link
Member

Choose a reason for hiding this comment

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

DatabasePlatformMock extends AbstractPlatform. The former is enough.

@carusogabriel carusogabriel changed the title [WIP] Enable PHPStan in tests Enable PHPStan in tests Jul 9, 2018
@carusogabriel
Copy link
Contributor Author

phpcbf f*cked us in this review 🤦‍♂️

@carusogabriel
Copy link
Contributor Author

@morozov Some comments that now are folded but I'd like your reply to continue to fix:

@Majkl578
Copy link
Contributor

Majkl578 commented Aug 2, 2018

@carusogabriel Hi, any progress?

@carusogabriel
Copy link
Contributor Author

carusogabriel commented Aug 2, 2018

Closing as I won't be able to finish it. Anyone is encouraged to finish it 😊

@carusogabriel carusogabriel deleted the phpstan-tests branch August 10, 2018 00:21
@Majkl578
Copy link
Contributor

Great job.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants