GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — getMockBuilder() should also work with non-existent classes by Majkl578 · Pull Request #28 · phpstan/phpstan-phpunit

Conversation

@Majkl578

@Majkl578 Majkl578 commented Aug 18, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

TestCase::getMockBuilder() can be used with undefined classes as well. In this case, the mock system creates dummy empty class of the given name.

This is used in Doctrine on some places, i.e. for testing event listeners that are based on naming, not interface.

Without this patch, we were getting the following errors:

 ------ ---------------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/ConnectionTest.php                                
 ------ ---------------------------------------------------------------------------- 
  144    Call to method expects() on an unknown class ConnectDispatchEventListener.  
 ------ ---------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php      
 ------ ------------------------------------------------------------------------------------- 
  349    Call to method expects() on an unknown class ListTableColumnsDispatchEventListener.  
  377    Call to method expects() on an unknown class ListTableIndexesDispatchEventListener.  
 ------ ------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------------- 
  Line   tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php                     
 ------ ------------------------------------------------------------------------------------- 
  372    Call to method expects() on an unknown class GetCreateTableSqlDispatchEvenListener.  
  375    Call to method expects() on an unknown class GetCreateTableSqlDispatchEvenListener.  
  396    Call to method expects() on an unknown class GetDropTableSqlDispatchEventListener.   
  421    Call to method expects() on an unknown class GetAlterTableSqlDispatchEvenListener.   
  424    Call to method expects() on an unknown class GetAlterTableSqlDispatchEvenListener.   
  427    Call to method expects() on an unknown class GetAlterTableSqlDispatchEvenListener.   
  430    Call to method expects() on an unknown class GetAlterTableSqlDispatchEvenListener.   
  433    Call to method expects() on an unknown class GetAlterTableSqlDispatchEvenListener.   
 ------ -------------------------------------------------------------------------------------

No tests for Type\PHPUnit - how to add some?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Majkl578
getMockBuilder() should also work with non-existent classes
3c06d97
@Majkl578 Majkl578 mentioned this pull request Aug 18, 2018
Enable PHPStan in tests doctrine/dbal#3260
Closed
@Majkl578

Majkl578 commented Aug 20, 2018

Copy link
Copy Markdown
Contributor Author

@lookyman @ondrejmirtes Can you please review this as a bugfix for 0.10.x? Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ondrejmirtes

ondrejmirtes commented Aug 20, 2018

Copy link
Copy Markdown
Member

Hi, what about createMock? That one needs the same fix probably: https://github.com/phpstan/phpstan-phpunit/blob/master/src/Type/PHPUnit/CreateMockDynamicReturnTypeExtension.php

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Majkl578

Majkl578 commented Aug 20, 2018

Copy link
Copy Markdown
Contributor Author

According to this comment from Sebastian Bergmann, non-existent classes cannot be used with createMock() API, but can be used with Mock Builder API:

Classes that do not exist cannot be doubled using the new createMock() API.

and

Classes that do not exist can still be doubled using the Mock Builder API.

It's 2 years old commit though, it may've changed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ondrejmirtes

ondrejmirtes commented Aug 20, 2018

Copy link
Copy Markdown
Member

Can you verify that with a quick test?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Majkl578

Majkl578 commented Aug 20, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Seems to still be valid:

<?php

class NonExistentTest extends PHPUnit\Framework\TestCase
{
    public function testCreateMock()
    {
        $mock = $this->createMock('NonExistent');
        self::assertInstanceOf(PHPUnit\Framework\MockObject\MockObject::class, $mock);
    }

    public function testGetMockBuilder()
    {
        $mock = $this->getMockBuilder('NonExistent')->getMock();
        self::assertInstanceOf(PHPUnit\Framework\MockObject\MockObject::class, $mock);
    }
}
$ vendor/bin/phpunit NonExistentTest.php
PHPUnit 7.3.1 by Sebastian Bergmann and contributors.

W.                                                                  2 / 2 (100%)

Time: 56 ms, Memory: 4.00MB

There was 1 warning:

1) NonExistentTest::testCreateMock
Cannot stub or mock class or interface "NonExistent" which does not exist

WARNINGS!
Tests: 2, Assertions: 1, Warnings: 1.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Majkl578

Majkl578 commented Aug 20, 2018

Copy link
Copy Markdown
Contributor Author

This is because it's explicitly disallowed in createMock():
https://github.com/sebastianbergmann/phpunit/blob/7.3.1/src/Framework/TestCase.php#L1311

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ondrejmirtes

ondrejmirtes commented Aug 20, 2018

Copy link
Copy Markdown
Member

Cool. Maybe we should modify https://github.com/phpstan/phpstan-phpunit/blob/master/src/Type/PHPUnit/MockBuilderDynamicReturnTypeExtension.php too to return a little bit different instance of MockBuilderType after calling disallowMockingUnknownTypes that would still complain about unknown class.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Majkl578 @ondrejmirtes
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.