简体   繁体   English

如何在测试中忽略`静态方法称为动态`检查

[英]How to ignore `static method called as dynamic` inspection in tests only

PHPUnit has all assertions defined as static functions, but you are supposed to call them with object context 1 . PHPUnit将所有断言定义为静态函数,但您应该使用对象上下文1来调用它们。 Apart from if this is a good practice or not, if you want to follow this practice you will get warnings in PhpStorm: static method called as dynamic 除非这是一个好的做法,如果你想遵循这种做法,你将在PhpStorm中收到警告: static method called as dynamic

I like this inspection and do not want to turn it off for all files. 我喜欢这个检查,不想为所有文件关闭它。 I do want to turn it off for phpunit tests, but adding a noinspection comment to all my tests is annoying and repetitive. 确实希望将其关闭以进行phpunit测试,但在我的所有测试中添加无noinspection注释都很烦人且重复。

How can I ignore this in tests, but have this inspection in my normal code? 我如何在测试中忽略这一点,但是在我的普通代码中进行了检查?
I'm using PhpStorm 2016.3 我正在使用PhpStorm 2016.3


1 : see Sebastian's comment on this github issue . 1 :看看塞巴斯蒂安对这个github问题的评论。

A regular user of PHPUnit should use $this-> instead of self:: PHPUnit的普通用户应该使用$ this->而不是self ::

Also, all the examples in the manual use this method. 此外,手册中的所有示例都使用此方法。

To ignore the "static method called as dynamic" errors in your tests, you can define a scope, and then change the inspections for that scope. 要忽略测试中的“静态方法称为动态”错误,可以定义范围,然后更改该范围的检查。

The setting-names etc are for PhpStorm 2016.3. 设置名称等适用于PhpStorm 2016.3。

scope 范围

You can use the default Test scope, by setting your tests directory as "test sources root". 通过将tests目录设置为“test sources root”,可以使用默认的Test范围。 You can also create your own scope (see below) 您也可以创建自己的范围(见下文)

inspection 检查

Now make your inspection work only for this scope 现在让您的检查仅适用于此范围

  1. Go to settings -> editor -> inspections (or search for inspections) 转到设置 - >编辑器 - >检查(或搜索检查)
  2. Search (in inspections, not in all settings) for the 'static method called as dynamic' inspection 搜索(在检查中,而不是在所有设置中)搜索“静态方法称为动态”检查
  3. Expand the scopes (default it say 'in all scopes'), and select your new scope "all but phpunit tests" 扩展范围(默认为'在所有范围内'),并选择新范围“除了phpunit测试之外的所有”
  4. Check the tests scope 检查测试范围
  5. unselect the warning, but keep the 'all scopes' selected. 取消选择警告,但保持选中“所有范围”。

If you have created a specific scope for your project with exclusions, you could also select only that scope here. 如果已使用排除项为项目创建了特定范围,则还可以在此处仅选择该范围。

You might need to restart phpstorm or re-run inspections. 您可能需要重新启动phpstorm或重新运行检查。

It looks like so: 它看起来像这样:

phpstorm的设置示例

Creating your own scope 创建自己的范围

  1. Go to settings -> appearance & behavior -> scopes (you can also search for 'scopes' in the setttings-search). 转到设置 - >外观和行为 - >范围(您还可以在设置搜索中搜索“范围”)。
  2. Press '+' to add a new scope 按'+'添加新范围
  3. Select your directories 选择你的目录
  4. Apply / OK 应用/确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在测试中实例化之前设置静态方法mock - Set static method mock before instantiation in tests 如何检查mockobject的方法只是用一个特定的参数调用? - How to check that method of a mockobject was not called only with a specific parameter? php:使用“Mockery”来模拟在另一个静态方法中调用的静态方法 - php:use “Mockery” to mock a static method called in another static method php 重置 static 测试之间的方法范围变量 - php reset static method-scope variable between tests 如何在 PHPUnit 测试中显示底层测试方法? - How to display underlying tested method in PHPUnit tests? 在单元测试中忽略.env文件 - Ignore .env file on unit tests 总是在感染测试期间完成phpunit-tests:失败后不调用拆卸方法 - Finish phpunit-tests always during infection testing: teardown method not called after a failure 使用PHPunit和Selenium2运行测试时如何忽略html5验证 - How ignore html5 validation when running tests with PHPunit and Selenium2 如何获取PHPUnit中确定的被调用方法的数量 - How to get number of a determined called to method in PHPUnit 收到错误:非静态方法Phactory \\ Sql \\ Phactory :: reset()不应静态调用 - Getting error: Non-static method Phactory\Sql\Phactory::reset() should not be called statically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM