Project

General

Profile

Actions

Task #103880

closed

Enable PHPStan ruleset for PHPunit tests - Missing call to parent::tearDown; use assertCount instead of assertSame

Added by Ayke Halder 24 days ago. Updated 19 days ago.

Status:
Resolved
Priority:
Should have
Assignee:
-
Category:
Code Cleanup
Target version:
-
Start date:
2024-05-22
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
13
PHP Version:
Tags:
Complexity:
Sprint Focus:

Description

We can enable vendor/phpstan/phpstan-phpunit/rules.neon for PHPunit tests:

rules:
  - PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
  - PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
  - PHPStan\Rules\PHPUnit\AssertSameWithCountRule
  - PHPStan\Rules\PHPUnit\MockMethodCallRule
  - PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule

ShouldCallParentMethodsRule

Missing call to parent::tearDown() method.
This is especially nice to fix.

AssertSameWithCountRule

You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).
This is easily fixable.

MockMethodCallRule

Trying to mock an undefined method executeUpdate() on class TYPO3\CMS\Core\Database\Connection.
This is just temporary --> add to phpstan-baseline.neon

See core/Tests/Unit/Database/ConnectionTest.php:

// @todo drop else branch and condition once doctrine/dbal is requried in version 2.11.0 minimum


PHPStan ruleset for PHPunit: without fixes

$ Build/Scripts/runTests.sh -s phpstan
 ------ ---------------------------------------------------------------------------------------------------------------- 
  Line   core/Tests/Functional/Cache/Backend/ApcuBackendTest.php                                                         
 ------ ---------------------------------------------------------------------------------------------------------------- 
  273    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
 ------ ---------------------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------------------------- 
  Line   core/Tests/Functional/Database/Query/QueryBuilder/PositionPlaceholderPreparedStatementTest.php                  
 ------ ---------------------------------------------------------------------------------------------------------------- 
  79     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
  87     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
  123    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
  134    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
 ------ ---------------------------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------- 
  Line   core/Tests/Functional/Security/ContentSecurityPolicy/ModelServiceTest.php  
 ------ --------------------------------------------------------------------------- 
  42     Missing call to parent::tearDown() method.                                 
 ------ --------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------- 
  Line   core/Tests/Unit/Database/ConnectionTest.php                                                      
 ------ ------------------------------------------------------------------------------------------------- 
  246    Trying to mock an undefined method executeUpdate() on class TYPO3\CMS\Core\Database\Connection.  
  296    Trying to mock an undefined method executeUpdate() on class TYPO3\CMS\Core\Database\Connection.  
 ------ ------------------------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------------------------------------- 
  Line   core/Tests/Unit/Utility/String/StringFragmentCollectionTest.php                                                 
 ------ ---------------------------------------------------------------------------------------------------------------- 
  34     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
 ------ ---------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   extbase/Tests/Functional/Persistence/CountTest.php                                                                
 ------ ------------------------------------------------------------------------------------------------------------------ 
  141    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
 ------ ------------------------------------------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   extbase/Tests/Functional/Persistence/WorkspaceTest.php                                                            
 ------ ------------------------------------------------------------------------------------------------------------------ 
  132    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
 ------ ------------------------------------------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   extensionmanager/Tests/Unit/Domain/Model/ExtensionTest.php                                                        
 ------ ------------------------------------------------------------------------------------------------------------------ 
  197    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
 ------ ------------------------------------------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   form/Tests/Functional/Domain/Model/Renderable/AbstractRenderableTest.php                                          
 ------ ------------------------------------------------------------------------------------------------------------------ 
  54     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
  58     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
 ------ ------------------------------------------------------------------------------------------------------------------ 

 ------ ---------------------------------------------------------------------------------------------------------------- 
  Line   frontend/Tests/Functional/SiteHandling/SlugLinkGeneratorTest.php                                                
 ------ ---------------------------------------------------------------------------------------------------------------- 
  918    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).  
 ------ ---------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   redirects/Tests/Functional/EventListener/AddPageTypeZeroSourceTest.php                                            
 ------ ------------------------------------------------------------------------------------------------------------------ 
  65     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
  112    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
  218    You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
 ------ ------------------------------------------------------------------------------------------------------------------ 

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   redirects/Tests/Unit/RedirectUpdate/RedirectSourceCollectionTest.php                                              
 ------ ------------------------------------------------------------------------------------------------------------------ 
  39     You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).  
 ------ ------------------------------------------------------------------------------------------------------------------ 

 [ERROR] Found 19 errors                                                                                                

###########################################################################
Result of phpstan
Container runtime: docker
PHP: 8.2
FAILURE
###########################################################################

Related issues 1 (0 open1 closed)

Blocked by TYPO3 Core - Task #103929: Remove `doctrine/dbal` version aware method mocking in testsResolvedStefan Bürk2024-05-26

Actions
Actions #1

Updated by Ayke Halder 24 days ago

  • Description updated (diff)
Actions #2

Updated by Ayke Halder 24 days ago

  • Description updated (diff)
Actions #3

Updated by Gerrit Code Review 24 days ago

  • Status changed from New to Under Review

Patch set 1 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/84351

Actions #4

Updated by Ayke Halder 20 days ago

  • Blocked by Task #103929: Remove `doctrine/dbal` version aware method mocking in tests added
Actions #5

Updated by Gerrit Code Review 20 days ago

Patch set 2 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/84351

Actions #6

Updated by Gerrit Code Review 19 days ago

Patch set 3 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/84351

Actions #7

Updated by Gerrit Code Review 19 days ago

Patch set 4 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/84351

Actions #8

Updated by Gerrit Code Review 19 days ago

Patch set 5 for branch main of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/84351

Actions #9

Updated by Ayke Halder 19 days ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
Actions

Also available in: Atom PDF