概述

在这里,我们可以使用为行为驱动开发构建的工具——官方 php 的 cucumber 实现——behat。我们可以通过运行以下代码来安装它:

$ php composer.phar require –dev behat/behat

增加一个目标到 build.xml(在本文的第一部分中描述了 phing 设置)

<target name="behat">
	<exec executable="bin/behat" passthru="true" checkreturn="true" />
</target>…
<target name="run" depends="phpcs,phpcpd,phan,phpspec,behat" />

然后,你应该为文件 features/price.feature 的测试创建一个规范。

feature: price comparison in order to compare prices as a customer i need to break the currency barrier scenario: compare eur and pln given i use nbp.pl comparator when i compare “100eur” and “100pln” then it should return some result

这个测试场景非常容易阅读,并且应该给你一个关于该特性应该如何工作的良好印象。不幸的是,计算机通常并不真正理解人类语言,所以现在是为每一步编写代码的时候了。

你可以通过运行 ./bin/behat-init 来生成它的代码模板。它应该会创建一个这样的类:

//features/bootstrap/featurecontext.php use behat\behat\context\snippetacceptingcontext;
use behat\gherkin\node\pystringnode;
use behat\gherkin\node\tablenode;
class featurecontext implements snippetacceptingcontext{
	/** * initializes context. */ public function __construct() { }
}

然后你可以执行:

$ bin/behat –dry-run –append-snippets

behat 将自动为场景中定义的每个步骤创建函数。
现在你可以通过填充函数的主体来开始实现真正的检查:

// features/bootstrap/featurecontext.php
<?php
use behat\behat\context\context;
use domain\price;use domain\pricecomparator;
use infrastructure\nbppriceconverter;
/*** defines application features from the specific context.*/
class featurecontext implements context{
	/** @var pricecomparator */ 
	private $pricecomparator;
	/** @var int */ 
	private $result; 
	/** * initializes context. * 
	* every scenario gets its own context instance.
	* you can also pass arbitrary arguments to the* context constructor through behat.yml. */
	public function __construct() { 
		
	} 
	/** * @given i use nbp.pl comparator */ 
	public function iusenbpplcomparator() {
		$this->pricecomparator = new pricecomparator(new nbppriceconverter());
	} 
	/** * @when i compare :price1 and :price2 */
	public function icompareand($price1, $price2) {
		preg_match('/(\d+)([a-z]+)/', $price1, $match1); 
		preg_match('/(\d+)([a-z]+)/', $price2, $match2);
		$price1 = new price($match1[1], $match1[2]);
		$price2 = new price($match2[1], $match2[2]);
		$this->result = $this->pricecomparator->compare($price1, $price2); 
	} 
	/** * @then it should return some result */ 
	public function itshouldreturnsomeresult() {
		if (!is_int($this->result)) {
			throw new \domainexception('returned value is not integer');
		} 
	}
}

最后,使用 ./bin/phing 运行所有的测试。你应该得到以下结果:

buildfile: /home/maciej/workspace/php-testing/build.xmlmyproject > phpcs: myproject > phpcpd: phpcpd 4.0.0 by sebastian bergmann.0.00% duplicated lines out of 103 total lines of code. time: 17 ms, memory: 4.00mb myproject > phan: myproject > phpspec: / skipped: 0% / pending: 0% / passed: 100% / failed: 0% / broken: 0% / 3 examples2 specs3 examples (3 passed)15ms myproject > behat: feature: price comparison in order to compare prices as a customer i need to break the currency barrier scenario: compare eur and pln # features/price.feature:6 given i use nbp.pl comparator # featurecontext::iusenbpplcomparator() when i compare “100eur” and “100pln” # featurecontext::icompareand() then it should return some result # featurecontext::itshouldreturnsomeresult()1 scenario (1 passed)3 steps (3 passed)0m0.01s (9.13mb) myproject > run: build finished total time: 1.1000 second

正如你所看到的,behat 准备了一份很好的报告,说明我们的应用程序做了什么,结果是什么。下一次,当项目经理询问你在测试中涉及到哪些场景时,你可以给他一个 behat 输出!

1、测试的结构

每个测试都包括:

  • 对该场景的一些准备,用“given”部分表示
  • “when”部分所涵盖的一些动作
  • 一些检查被标记为“then”部分

每个部分都可以包含多个与“and”关键字连接的步骤:

scenario: compare eur and pln given nbp.pl comparator is available and i use nbp.pl comparator when i compare “100eur” and “100pln” and i save the result then it should return some result and the first amount should be greater

2、上下文

behat 允许你为你的测试定义多个上下文。这意味着你可以将步骤代码分割成多个类,并从不同的角度去测试你的场景。

你可以例如:为 web 上下文编写代码,它将使用你的应用程序 http 控制器运行你的测试步骤。你还可以创建“domain”上下文,它将只使用 php api 调用来运行你的业务逻辑。通过这种方式,你可以单独地测试业务逻辑集成,从端到端应用程序测试。

关于如何在 behat 建立许多上下文的更多信息,请参考http://behat.org/en/latest/userguide/context.html的文档。

3、如何使用behat 

正如一开始所提到的,你可以使用 behat 进行集成测试。通常情况下,你的代码依赖于一些外部的第三方系统。当我们在第 2 部分中编写单元测试时,我们总是假设外部依赖关系像预期的那样工作。使用 behat,你可以编写测试场景,它将自动运行你的代码,并检查它是否正确地使用真实场景的服务。

最重要的是,behat 对于测试系统使用的复杂的端到端场景非常有用。它允许你隐藏在一个可读性的名字后面运行测试步骤所需的复杂代码,并编写一个人人都能理解的场景。

总结

从以上的文章中,你已经学习了如何在你的项目中设置六个有用的工具:

  • phing 用于运行你的构建
  • phpcs 用于自动检查代码格式
  • phpcpd 用于检测重复代码的
  • phan 用于高级静态代码分析
  • phpspec 用于单元测试
  • behat 用于端到端和集成测试

现在,你可以向 git 提交钩子添加 ./bin/phing,并设置持续集成来运行每个提交的测试。

是不是突然之间,没有什么能阻止你写出高质量的 php 代码!

以上就是浅谈如何提高php代码质量之端到端集成测试的详细内容,更多关于如何提高php代码质量之端到端集成测试的资料请关注www.887551.com其它相关文章!