wp-browser icon indicating copy to clipboard operation
wp-browser copied to clipboard

Fix "Undefined method" in IDE for WPTestCase [FEATURE REQUEST]

Open jerclarke opened this issue 4 years ago • 6 comments

Is your feature request related to a problem? Please describe.

In both VS Code and PHPStorm, when I look at a test file for the wpunit suite, the actual tests work fine, but the IDEs are full of Undefined Method/Method X not found errors. I can work around them/ignore them, but they kind of drive me crazy as normally I'd resolve errors like that immediately.

The full file in VS Code+Intelephense:

Screen Shot 2021-07-08 at 3 43 04 PM

The specific error in VS Code:

Screen Shot 2021-07-08 at 3 43 08 PM

The same error in PHPStorm:

Screen Shot 2021-07-08 at 3 43 50 PM

Is this happening to others as well?

Guessing at the cause:

It seems the problem is that the \tad\WPBrowser\Compat\Codeception\Unit class isn't being picked up by the IDE.

In the actual test file, extending WPTestCase doesn't seem to have a problem, but as you can see below, the definition of WPTestCase itself in the wp-browser source can't seem to find \tad\WPBrowser\Compat\Codeception\Unit:

Screen Shot 2021-07-08 at 4 03 16 PM

This matches the actual behavior in the editor, where the methods defined in WPTestCase work fine, while it's the ones sourced from the underlying PHPUnit test class that are broken:

Screen Shot 2021-07-08 at 4 13 36 PM

Describe the solution you'd like

I don't know how to fix it, I'm a noob at namespaces and my attempts to dig around in the source files are making my head spin.

Ultimately I of course just want the autocomplete to work and not see errors when the actual code will run fine when I run it :)

Describe alternatives you've considered

I've solved things like this in the past by symlinking a directory or something like that. I'd be happy to do a hack if it made this problem go away.

Additional context

Thanks for this amazing app!

jerclarke avatar Jul 08 '21 21:07 jerclarke

I can report that out of the box, using composer install on a project, PHP storm picks up the the asserts specific to the WP modules, but NOT the basic asserts from Codeception. $this->assert... contains assertEqualsFields [wherever that comes from] a bunch of other stuff... contains assertWPError and other WordPress specific asserts but does NOT contain basis asserts like assertTrue, assertEquals etc. [those functions work fine, just not in the IDE automatically]

On Thu, Jul 8, 2021 at 2:25 PM jerclarke @.***> wrote:

Is your feature request related to a problem? Please describe.

In both VS Code and PHPStorm, when I look at a test file for the wpunit suite, the actual tests work fine, but the IDEs are full of Undefined Method/Method X not found errors. I can work around them/ignore them, but they kind of drive me crazy as normally I'd resolve errors like that immediately.

The full file in VS Code+Intelephense:

[image: Screen Shot 2021-07-08 at 3 43 04 PM] https://user-images.githubusercontent.com/139995/124991063-cbe2dc00-e006-11eb-9168-875acc677033.png

The specific error in VS Code:

[image: Screen Shot 2021-07-08 at 3 43 08 PM] https://user-images.githubusercontent.com/139995/124991117-dd2be880-e006-11eb-8b80-aa7e7efdb4c8.png

The same error in PHPStorm:

[image: Screen Shot 2021-07-08 at 3 43 50 PM] https://user-images.githubusercontent.com/139995/124991145-e61cba00-e006-11eb-8fba-3ebaaef4f12c.png

Is this happening to others as well? Guessing at the cause:

It seems the problem is that the \tad\WPBrowser\Compat\Codeception\Unit class isn't being picked up by the IDE.

In the actual test file, extending WPTestCase doesn't seem to have a problem, but as you can see below, the definition of WPTestCase itself in the wp-browser source can't seem to find \tad\WPBrowser\Compat\Codeception\Unit:

[image: Screen Shot 2021-07-08 at 4 03 16 PM] https://user-images.githubusercontent.com/139995/124991266-09e00000-e007-11eb-80cd-85426db1da66.png

This matches the actual behavior in the editor, where the methods defined in WPTestCase work fine, while it's the ones sourced from the underlying PHPUnit test class that are broken:

[image: Screen Shot 2021-07-08 at 4 13 36 PM] https://user-images.githubusercontent.com/139995/124991573-7824c280-e007-11eb-9c3c-c453e5ec9c13.png Describe the solution you'd like

I don't know how to fix it, I'm a noob at namespaces and my attempts to dig around in the source files are making my head spin.

Ultimately I of course just want the autocomplete to work and not see errors when the actual code will run fine when I run it :)

Describe alternatives you've considered

I've solved things like this in the past by symlinking a directory or something like that. I'd be happy to do a hack if it made this problem go away.

Additional context

Thanks for this amazing app!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lucatume/wp-browser/issues/513, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH33P3VBYYXYBFB2DBV5NTLTWYJTVANCNFSM5ABRAPOQ .

JohnDeeBDD avatar Jul 08 '21 23:07 JohnDeeBDD

Thanks @jerclarke for reporting the issue. I've got mixed success finding a solution for this that seems to be related to the inheritance tree. I use PHPStorm as my daily driver and have projects where it works, and projects where it does not.

Having it work reliably would be a quality-of-life improvement and I will look into it. The first mitigation that comes to mind would be to use @method notations on the class(es) to "force" the IDEs to pick those up. Codeception own code does that in some instances and it works, so that might prove the first approach.

Will post updates here.

lucatume avatar Jul 09 '21 06:07 lucatume

I've just rolled out a fresh project using the latest version of wp-browser and PHPStorm; auto-completion works correctly on test cases extending the WPTestCase class: see here. The same is not true, though, for VSCode: see here. The reason for this might be the indexing engine of PHPStorm will correctly parse and ingest the alias used in the inheritance tree of the WPTestCase class.

This premise made, the issue stands for those that are not using PHPStorm.

There are some solutions for this and I'm exploring a trait based one. The trait would need to be compiled at run-time to keep up with the base PHPUnit Test Case, so the solution is not a 5' copy-and-paste job. Achievable, though.

lucatume avatar Jul 09 '21 07:07 lucatume

Thanks for looking into it! Your expertise is invaluable, and thank you so much for trying to make it work in VSC as well 🙏🏻

jerclarke avatar Jul 09 '21 17:07 jerclarke

A workaround is posted by swashata @ https://github.com/bmewburn/vscode-intelephense/issues/600#issuecomment-639525222 . Class aliasing is at the root of the problem. So at this moment I have a helper class:

<?php 


namespace tad\WPBrowser\Compat\Codeception;

class Unit extends \tad\WPBrowser\Compat\Codeception\Version2\Unit{}

tkvw avatar Aug 22 '21 19:08 tkvw

Thank you for sharing!

For anyone trying to implement this (hi future Jer), here's exactly what I did that seems to work:

  • Create a file at /tests/vscode-class_alias-fixer.php
  • Insert the code above

That's it. VS Code will scan the file even though it's not included anywhere, and it fixes intellisense for core PHPUnit assertions etc.

Luca maybe you could add this somewhere harmless in the package for the sake of other VS Code users? I have no idea what the downsides might be but if there are none, this would be a nice QoL improvement.

jerclarke avatar Nov 11 '21 00:11 jerclarke

Closing this due to lack of activity, thank to all for sharing the knowledge.

lucatume avatar Sep 11 '23 12:09 lucatume