php代码质量检测工具_8个必须具备PHP质量保证工具

php代码质量检测工具

This popular post has been updated on June 30th 2017 to include newest technologies and tools.

该热门帖子已于2017年6月30日更新,其中包括最新技术和工具。



For shipping quality code, we must have testing in mind while coding (if not doing TDD). However, with the wide range of PHP testing tools out there, it’s hard to make a choice! Exploring PHP is a fun adventure (premium course on that here!) but it’s hard to assemble a toolbelt that’s not too heavy to wear to work!

对于运输质量代码,编码时必须牢记测试(如果不执行TDD)。 但是,有了各种各样PHP测试工具,很难做出选择! 探索PHP是一个有趣的冒险( 这里是高级课程!),但是要组装一个不太重的工具带很难工作!

This popular article will highlight the most popular testing tools and has been updated to reflect the state of QA tools in 2017.

这篇热门文章将重点介绍最受欢迎的测试工具,并已更新以反映2017年质量检查工具的状态。

Untested code is broken code.

未经测试的代码是损坏的代码。

PHPUnit (PHPUnit)

PHPUnit is the go to testing framework for PHP. It was created by Sebastian Bergmann in 2004 and current in version 6 that requires PHP 7.

PHPUnit是用于PHP的测试框架。 它由Sebastian Bergmann于2004年创建,当前版本为6,需要PHP 7。

We have plenty of tutorials coming up about it.

我们有很多关于它的教程。

Cucumber (Cucumber)

Cucumber is a framework for creating acceptance tests from specifications. It’s known for it descriptive generated texts that can be read as just plain English. The official PHP implementation for Cucumber is Behat.

Cucumber是用于根据规范创建验收测试的框架。 它以描述性的生成文本而著称,可以将其理解为纯英语。 Cucumber的官方PHP实现是Behat

We have a getting started tutorial about it here on SitePoint. The below example taken from the documentation is a good example on how expressive those expectations are.

我们在SitePoint上有关于它的入门教程 。 以下文档中的示例是这些期望表达能力的一个很好的例子。

 
  1. Feature: Listing command

  2. In order to change the structure of the folder I am currently in

  3. As a UNIX user

  4. I need to be able see the currently available files and folders there

  5. Scenario: Listing two files in a directory

  6. Given I am in a directory "test"

  7. And I have a file named "foo"

  8. And I have a file named "bar"

  9. When I run "ls"

  10. Then I should get:

  11. """

  12. bar

  13. foo

  14. """

原子 (Atoum)

Atoum is another unit testing framework for PHP. It’s a standalone package that you can install via GitHub, Composer or via a PHAR executable file.

Atoum是另一个用于PHP的单元测试框架。 它是一个独立的软件包,您可以通过GitHub,Composer或PHAR可执行文件进行安装。

Atoum tests are very readable with expressive method names and chaining.

Atoum测试使用表达性方法名称和链接非常可读。

 
  1. $this->integer($classInstance->myMethod())

  2. ->isEqualTo(10);

  3. $this->string($classInstance->myMethod())

  4. ->contains("Something heppened");

You want to learn more about PHP unit testing with Atoum, you can follow this tutorial.

您想了解有关使用Atoum进行PHP单元测试的更多信息,可以按照本教程进行操作

Selenium (Selenium)

Selenium is a tool for automated browser testing (Integration and acceptance testing). It transforms the tests to browser API commands and it asserts the expected results. It supports most of the available browsers out there.

Selenium是用于自动浏览器测试(集成和验收测试)的工具。 它将测试转换为浏览器API命令,并声明预期结果。 它支持大多数可用的浏览器。

We can use Selenium with PHPUnit using an extension.

我们可以将Selenium与PHPUnit一起使用扩展。

 
  1. composer require --dev phpunit/phpunit

  2. composer require --dev phpunit/phpunit-selenium

Here’s a simple example:

这是一个简单的例子:

 
  1. class UserSubscriptionTest extends PHPUnit_Extensions_Selenium2TestCase

  2. {

  3. public function testFormSubmissionWithUsername()

  4. {

  5. $this->byName('username')->value('name');

  6. $this->byId('subscriptionForm')->submit();

  7. }

  8. }

You can follow this series if you want to learn more about Testing with PHPUnit and Selenium.

如果您想了解有关使用PHPUnit和Selenium进行测试的更多信息,可以阅读本系列文章。

黄昏 (Dusk)

Dusk from Laravel is another browser automation tool. It can be used standalone (with chromedriver) or with Selenium. It has an easy to use API and covers all the testing possibilities like waiting for elements, file upload, mouse control, etc. Here’s a simple example:

Laravel的Dusk是另一个浏览器自动化工具。 它可以单独使用(与chromedriver一起使用)或与Selenium一起使用。 它具有易于使用的API,涵盖了所有测试可能性,例如等待元素,文件上传,鼠标控制等。这是一个简单的示例:

 
  1. class LanguagesControllerTest extends DuskTestCase

  2. {

  3. public function testCreate()

  4. {

  5. $this->browse(function (Browser $browser) {

  6. $user = $this->getAdminUser();

  7. $browser->loginAs($user)

  8. ->visit('/panel/core/languages')

  9. ->click('#add')

  10. ->assertPathIs('/panel/core/languages/create')

  11. ->type('name', 'Arabic')

  12. ->select('direction', 'rtl')

  13. ->press('Submit')

  14. ->assertSee('Language: Arabic')

  15. ->assertSee('ar')

  16. ->assertSee('rtl')

  17. ->assertSee('Language created');

  18. });

  19. }

  20. }

You can check this tutorial to get started on testing with Dusk.

您可以查看本教程以开始使用Dusk进行测试。

卡兰 (Kahlan)

Kahlan is a full-featured Unit & BDD test framework which uses a describe-it syntax.

Kahlan是功能齐全的Unit&BDD测试框架,它使用describe-it语法。

 
  1. describe("Positive Expectation", function() {

  2. it("expects that 5 > 4", function() {

  3. expect(5)->toBeGreaterThan(4);

  4. });

  5. });

You can see from the syntax above that it’s similar to Behat tests. Kahlan supports stubbing and mocking out of the box with no dependencies, code coverage, reporting, etc.

您可以从上面的语法中看到它类似于Behat测试。 Kahlan支持开箱即用和模拟,无需依赖项,代码覆盖率,报告等。

 
  1. it("makes a instance double with a parent class", function() {

  2. $double = Double::instance(['extends' => 'Kahlan\Util\Text']);

  3. expect(is_object($double))->toBe(true);

  4. expect(get_parent_class($double))->toBe('Kahlan\Util\Text');

  5. });

php_testability (php_testability)

The last package I want mention here is PHP Testability. It’s a static analysis tool that tells you about testability issues in your program and generates a detailed report.

我要在这里提到的最后一个软件包是PHP Testability 。 这是一个静态分析工具,可以告诉您程序中的可测试性问题并生成详细的报告。

The package doesn’t currently have a tagged release that you can rely on, but you can safely use it in development. You can install it via Composer:

该软件包当前没有可依赖的带标记的发行版,但是您可以在开发中安全地使用它。 您可以通过Composer安装它:

composer require edsonmedina/php_testability "dev-master"

Then run it like this:

然后像这样运行它:

vendor/bin/testability . -x vendor

持续集成(CI)服务 (Continuous integration (CI) Services)

An important part in delivering code when working with teams is the ability to automatically check code before it’s merged to the official repo of the project. Most available CI services/tools provide the ability to test code on different platforms and configurations to make sure your code is safe to merge.

与团队合作时交付代码的重要部分是能够在代码合并到项目的正式仓库之前自动检查代码。 大多数可用的CI服务/工具都可以在不同的平台和配置上测试代码,以确保您的代码可以安全合并。

There are a lot of services out there which offer good pricing tiers, but you can use open source tools as well:

有很多提供良好定价层的服务,但是您也可以使用开源工具:

结论 (Conclusion)

Adopting the testing culture is hard, but it grows slowly with practice. If you care about your code, you should test it! The above tools and resources will help you get started quickly.

很难采用测试文化,但随着实践的发展,它会慢慢发展。 如果您关心自己的代码,则应该对其进行测试! 以上工具和资源将帮助您快速入门。

What is your experience with the tools mentioned above? Did we miss something? Let us know and we’ll do our best to expand the list with essential tools!

您对上述工具有何经验? 我们错过了什么吗? 让我们知道,我们将尽力使用必要的工具来扩大清单!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值