search
HomeBackend DevelopmentPHP TutorialBuild PHP's Yii framework and related test environment on Mac OS, osyii_PHP tutorial

YII integrated unit testing and functional testing, implemented with phpunit and selenium. The author encountered a lot of trouble during the configuration process, which is recorded here.

Necessary concepts
selenium
Selenium is a well-known automated testing tool that can launch a local browser to complete testing, so it can be used to automatically test web projects. Selenium is divided into server and client. The server is developed using Java, so it requires a jdk. When the server starts, it will start an http service. The client initiates a test request to the server through http communication with the server. The service The client will automatically launch the browser to complete the test. Testers are responsible for writing client scripts that support most mainstream programming languages. Of course, this is actually due to the powerful power of the open source community, which has developed interface programs for selenium for different languages. The protocol between the server and the client The author did not study it because it is not important.

phpunit
phpunit is a testing framework and tool for the PHP language. Its framework is used when performing unit testing, and its tools are used when performing functional testing. Based on this testing framework, someone made a Selenium PHP interface program, which exists as an extension of PHPunit.

How to integrate YII framework
Yii has made some simple packages for testing based on phpunit. Therefore, when using Yii for testing, you need to rely on the above two.

Environment installation
Firefox
There are not many browsers that selenium-server can recognize. It seems to be IE and Firefox, so install the Firefox browser on OSX first. There is no big difference between installing a browser and general software installation, so I won’t go into details here.

JDK
Since selenium-server is developed using java, we need to install JDK first. Just search JDK on Baidu to download and install it. No more details.

selenium-server
First install the server version of selenium. Under osx, you can use brew to install, which is more convenient:

$ brew install selenium-server-standalone

Since the source of selenium-server is on googleapis, you need to bypass the wall to operate. In fact, if you don’t bypass the wall, other steps will be difficult.

Prompt after installation is complete:

To have launchd start selenium-server-standalone at login:
  ln -sfv /usr/local/opt/selenium-server-standalone/*.plist ~/Library/LaunchAgents
Then to load selenium-server-standalone now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.selenium-server-standalone.plist
Or, if you don't want/need launchctl, you can just run:
  selenium-server -p 4444

Here we are clearly told to start the server through the following command

$ selenium-server -p 4444

As you can see, usually selenium-server listens to port 4444. If you want to modify the port, the corresponding Yii configuration needs to be modified.

phpunit
Detour
Personally understood, phpunit is a collection of tools and frameworks. Tools are tools, and frameworks are frameworks. According to the documentation on the official website, the tool part of phpunit is released in the form of phar package, while the framework part is managed through pear. So let’s record these two concepts first. If you are not interested, you can skip this section.

phar is a PHP packaging solution. That is to say, a PHP program or PHP website can be packaged and distributed together, or even called as a functional module. Therefore, phpunit can completely package tool programs into phar. When executing phar, you usually need to use the php command.

$ wget https://phar.phpunit.de/phpunit.phar
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.

Use the above command to download the executable file of phpunit. You can see that it is a phar package

pear is a PHP extension library system, because it was difficult to reuse PHP in the early days. Compiled languages ​​are easier to reuse due to their compact and rigorous syntax. Because PHP is flexible and changeable, the learning cost for reuse is relatively high, so pear proposed a programming specification and distribution system to realize the reuse of PHP functions. Now it seems that pear has been replaced by composer (discussed below). But since ancient things have gone through a detour, you might as well write them down.

You can install pear like this on mac:

$ wget http://pear.php.net/go-pear.phar
$ sudo php -d detect_unicode=0 go-pear.phar

As you can see, go-pear is also a phar, but it is a php script to install pear, which can be executed using the php command. During the installation process, you will be prompted whether to modify the php.ini file:

WARNING! The include_path defined in the currently used php.ini does not
contain the PEAR PHP directory you just specified:
</usr/share/pear>
If the specified directory is also not in the include_path used by
your scripts, you will have problems getting any PEAR packages working.


Would you like to alter php.ini </etc/php.ini>&#63; [Y/n] : Y

php.ini </etc/php.ini> include_path updated.

Current include path      : .:
Configured directory      : /usr/share/pear
Currently used php.ini (guess) : /etc/php.ini
Press Enter to continue: 

The 'pear' command is now at your service at /usr/bin/pear

** The 'pear' command is not currently in your PATH, so you need to
** use '/usr/bin/pear' until you have added
** '/usr/bin' to your PATH environment variable.

From this tip we can know:

pear’s executable program is installed in /usr/bin/pear
Pear has a working directory which is /usr/share/pear. This working directory needs to be added to php.ini. If you let the installation program add it automatically, it will look like this:

;***** Added by go-pear
include_path=".:/usr/share/pear"
;*****

When we use require and other functions in PHP that include other files, PHP will actually search include_path in addition to the current directory. This configuration shows that the program code installed through pear will be stored in the working directory, and php can be found. By default, there will be a System.php in the working directory, so the following code can work:

<&#63;php 
 require 'System.php';
&#63;>


Install using composer
Originally, phpunit could be installed through pear. However, times have changed. In the era when composer was very popular, phpunit also announced full support for composer and abandoned pear. The original installation method through pear did not work anymore. In the end, I had no choice but to use composer (there are so many package management tools that ten fingers are not enough, I will have the opportunity to do a horizontal comparison in the future).

First install composer, in bypass state:

$ brew update
$ brew tap josegonzalez/homebrew-php
$ brew tap homebrew/versions
$ brew install php55-intl
$ brew install josegonzalez/php/composer

The composer is now installed.

在项目的根目录下,创建一个composer.json,写入:

{
  "require-dev": {
    "phpunit/phpunit": "4.7.*",
    "phpunit/php-invoker": "*",
    "phpunit/dbunit": ">=1.2",
    "phpunit/phpunit-selenium": ">=1.2",
    "phpunit/phpunit-story": "*"
  }
}

上面的phpunit-selenium就是基于phpunit写的selenium客户端库,详见文后的参考资料。

然后在项目根目录下,执行

$ sudo composer install

composer会根据这个composer.json文件在根目录创建一个vendor目录,并将依赖的东西全部下载到这个目录中,其中vendor/bin下面有phpunit的可执行文件。

由于是Yii的项目,所以cd到/protected/tests目录下,执行如下命令即可启动默认的SiteTest.php里面的测试方法: (注意在执行前,保持selenium-server开启状态)

$ ../../vendor/bin/phpunit functional/SiteTest.php

会看到firefox会在执行过程中自动启动,并由如下日志输出:

PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
Warning: Deprecated configuration setting "selenium" used

.

Time: 11.52 seconds, Memory: 6.50Mb

OK (1 test, 1 assertion)

phpunit工具程序会自动找到tests/phpunit.xml这个配置文件并根据此来进行某些配置,而Yii会利用phpunit和phpunit-selenium的框架来与selenium-server端通信,server端会启动浏览器,并将日志和结果等返回给客户端。整个过程大致就是这样的。

测试

测试是软件开发中必不可少的环节.无论我们是否意识到,在开发Web应用的时候,我们始终都是在测试的.例如, 当我们用PHP写了一个类时, 我们可能会用到一些注入 echo 或者 die 语句来显示我们是否正确地实现了某个方法;当我们实现了包含一套复杂的HTML表单的web页面时, 我们可能会试着输入一些测试数据来确认页面是否是按照我们的预期来交互的.更高级的开发者则会写一些代码来自动完成这个测试过程, 这样一来每当我们需要测试一些东西的时候, 我们只需要调用代码, 剩下来的就交给计算机了. 这就是所谓的 自动测试, 也是本章的主要话题.

Yii 提供的测试支持包括 单元测试 和 功能测试.

单元测试检验了代码的一个独立单元是否按照预期工作. 在面向对象编程中, 最基本的代码单元就是类. 因此, 单元测试的主要职责就是校验这个类所实现的每个方法工作都是正常的. 单元测试通常是由开发了这个类的人来编写.

功能测试检验了特性是否按照预期工作(如:在一个博客系统里的提交操作).与单元测试相比, 功能测试通常要高级一些, 因为待测试的特性常常牵涉到多个类. 功能测试通常是由非常了解系统需求的人编写.(这个人既可以是开发者也可以是质量工程师).

测试驱动开发

以下展示的便是所谓的 测试驱动开发 (TDD) 的开发周期:

  • 创建一个涵盖要实现的特性的新的测试. 测试预计将在第一次执行的时候失败, 因为特性尚未实现.
  • 执行所有测试,确保这个新的测试是失败的.
  • 编写代码来使得测试通过.
  • 执行所有测试,确保所有测试通过.
  • 重构新编写的代码并确保这些测试仍然能够通过.
  • 重复步骤1至5推进整体功能的实现.

构建测试环境

Yii 提供的测试支持需要 PHPUnit 3.5+ 和 Selenium Remote Control 1.0+.请参照他们提供的文档来安装 PHPUnit 和 Selenium Remote Control.

当我们使用 yiic webapp 控制台命令来创建一个新的 Yii 应用时, 它将会生成以下文件和目录供我们来编写和完成测试.

testdrive/
   protected/                包含了受保护的应用文件
      tests/                 包含了应用测试
         fixtures/           包含了数据 fixtures
         functional/         包含了功能测试
         unit/               包含了单元测试
         report/             包含了 coverage 报告
         bootstrap.php       这个脚本在一开始执行
         phpunit.xml         PHPUnit 配置文件
         WebTestCase.php     基于 Web 的功能测试基类
如上所示的, 我们的测试代码主要放在 fixtures, functional 和 unit 这三个目录下, report 目录则用于存储生成的代码 coverage 报告.

我们可以在控制台窗口执行以下命令来执行测试(无论是单元测试还是功能测试):

% cd testdrive/protected/tests
% phpunit functional/PostTest.php  // 执行单个测试
% phpunit --verbose functional    // 执行 'functional' 下的所有测试
% phpunit --coverage-html ./report unit

上面的最后一条命令将执行 unit 目录下的所有测试然后在 report 目录下生成出一份 code-coverage 报告. 注意要生成 code-coverage 报告必须安装并开启PHP的 xdebug 扩展 .

测试的引导脚本

让我们来看看 bootstrap.php 文件里会有些什么. 首先这个文件有点特殊,因为它看起来很像是 入口脚本, 而它也正是我们执行一系列测试的入口.

$yiit='path/to/yii/framework/yiit.php';
$config=dirname(__FILE__).'/../config/test.php';
require_once($yiit);
require_once(dirname(__FILE__).'/WebTestCase.php');
Yii::createWebApplication($config);

如上所示, 首先我们包含了来自 Yii 框架的 yiit.php 文件, 它初始化了一些全局常量以及必要的测试基类.然后我们使用 test.php 这个配置文件来创建一个应用实例.如果你查看 test.php 文件, 你会发现它是继承自 main.php 这个配置文件的, 只不过它多加了一个类名为 [CDbFixtureManager] 的 fixture 应用组件.

return CMap::mergeArray(
 require(dirname(__FILE__).'/main.php'),
 array(
 'components'=>array(
  'fixture'=>array(
  'class'=>'system.test.CDbFixtureManager',
  ),
  /* 去除以下注释可为测试提供一个数据库连接.
  'db'=>array(
  'connectionString'=>'DSN for test database',
  ),
  */
 ),
 )
);

当我执行那些涉及到数据库操作的测试时, 我们应该提供一个测试专用的数据库以便测试执行不会干扰到正常的开发或者生产活动. 这样一来, 我们纸需要去除上面 db 配置的注释, 然后填写 connectionString 属性的用以连接到数据库的DSN(数据源名称)即可.

通过这样一个启动脚本, 当我们执行单元测试时, 我们便可以获得一个与服务需求类似的应用实例, 而主要的不同就是测试拥有一个 fixture 管理器以及它专属的测试数据库.

定义特定状态(Fixtures)

自动测试需要被执行很多次.为了确保测试过程是可以重复的, 我们很想要在一些可知的状态下进行测试, 这个状态我们称之为 特定状态. 举个例子,在一个博客应用中测试文章创建特性, 每次当我们进行测试时, 与文章相关的表(例如. Post 表 , Comment 表)应该被恢复到一个特定的状态下. PHPUnit 文档 已经很好的描述了一般的特定状态的构建. 而本节主要介绍怎样像刚才描述的例子那样构建数据库特定状态.

设置构建数据库的特定状态,这恐怕是测试以数据库为后端支持的应用最耗时的部分之一.Yii 引进的 [CBbFixtureManager] 应用组件可以有效的减轻这一问题.当进行一组测试的时候,它基本上会做以下这些事情:

在所有测试运行之前,它重置测试相关数据为可知的状态.
在单个测试运行之前, 它将特定的表重置为可知状态.
在一个测试方法执行过程中, 它提供了供给特定状态的行数据的访问接口.
请按如下使用我们在 应用配置 中配置的 [CDbFixtureManager].

return array(
 'components'=>array(
 'fixture'=>array(
  'class'=>'system.test.CDbFixtureManager',
 ),
 ),
);

然后我们在目录 protected/tests/fixtures下提供一个特定状态数据. 这个目录可以通过配置应用配置文件中的 [CDbFixtureManager::basePath] 属性指定为其他目录.特定状态数据是由多个称之为特定状态文件的PHP文件组合而成.每个特定状态文件返回一个数组, 代表数据的一个特定表的初始行.文件名和表名相同.以下则是将 Post 表的特定状态数据存储于名为 Post.php 文件里的例子.

<&#63;php
return array(
 'sample1'=>array(
 'title'=>'test post 1',
 'content'=>'test post content 1',
 'createTime'=>1230952187,
 'authorId'=>1,
 ),
 'sample2'=>array(
 'title'=>'test post 2',
 'content'=>'test post content 2',
 'createTime'=>1230952287,
 'authorId'=>1,
 ),
);

正如我们所见, 上面返回了两行数据. 每一行都表示一个数组,其键是表的字段名,其值则是对应的字段值.每行的索引都是称之为行别名的字符串(例如: simple1, simple2). 稍后当我们编写测试脚本的时候, 我们可以方便地通过它的别名调用这行数据.你也许注意到了我们并未在上述特定状态中指定 id 字段的值. 这是因为 id 字段已经被定义为自增主键了,它的值也会在我们插入新数据的时候自动生成.

当 [CDbFixtureManager] 第一次被引用时, 它会仔细检查所有的特定状态文件然后使用他们重置对应的表.它通过清空表,重置表主键的自增序列值,然后插入来自特定状态文件的数据行到表中来重置表.

有时候,我们可能不想在一套测试前重置特定状态文件里描述的每一个表, 因为重置太多的特定状态文件可能需要很多时间.这种情况下,我们可以写一个PHP脚本来定制这个初始化过程.这个脚本应该被保存在存放特定状态文件的目录下,并命名为 init.php.当 [CDbFixtureManager] 检测到了这个脚本的存在, 它将执行这个脚本而不是重置每一个表.

不喜欢使用默认方式来重置表也是可以的,例如: 清空表然后插入特定状态数据. 如果是这种情况, 我们可以为指定的特定状态文件编写一个初始化脚本.这个脚本必须名称为表名+.init.php. 例如: Post 表的初始化脚本文件就是 Post.init.php. 当 [CDbFixtureManager] 发现了这个脚本,它将执行这个脚本而不是采用默认的方式去重置该表.

Tip: Too many specific state files greatly extend the test time. Therefore, you should only provide specific state files for those tables whose data will change during the test. Those tables that serve as lookup services will not change, so No specific status file required.

Articles you may be interested in:

  • How to install the php framework Yii in win7
  • Nginx configuration example of rewrite rules for PHP's Yii and CakePHP framework
  • Detailed explanation of the related configuration and use of logs in PHP's Yii framework
  • Explain the steps of running yii2.0 in the php command line with examples
  • Yii connection, modification of MySQL database and phpunit test connection
  • Custom validation rules for rules in models (models) of PHP YII framework development tips
  • Comprehensive list of form validation rules for PHP Yii framework
  • PHP generates ICO icons based on the yii framework
  • List the development advantages of PHP’s Yii 2 framework

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099051.htmlTechArticleBuild PHP’s Yii framework and related test environment on Mac OS. osyii YII integrates unit testing and functional testing. Implemented with phpunit and selenium. The author encountered a lot of trouble during the configuration process...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

如何使用PHP框架Yii开发一个高可用的云备份系统如何使用PHP框架Yii开发一个高可用的云备份系统Jun 27, 2023 am 09:04 AM

随着云计算技术的不断发展,数据的备份已经成为了每个企业必须要做的事情。在这样的背景下,开发一款高可用的云备份系统尤为重要。而PHP框架Yii是一款功能强大的框架,可以帮助开发者快速构建高性能的Web应用程序。下面将介绍如何使用Yii框架开发一款高可用的云备份系统。设计数据库模型在Yii框架中,数据库模型是非常重要的一部分。因为数据备份系统需要用到很多的表和关

Symfony vs Yii2:哪个框架更适合开发大型Web应用?Symfony vs Yii2:哪个框架更适合开发大型Web应用?Jun 19, 2023 am 10:57 AM

随着Web应用需求的不断增长,开发者们在选择开发框架方面也越来越有选择的余地。Symfony和Yii2是两个备受欢迎的PHP框架,它们都具有强大的功能和性能,但在面对需要开发大型Web应用时,哪个框架更适合呢?接下来我们将对Symphony和Yii2进行比较分析,以帮助你更好地进行选择。基本概述Symphony是一个由PHP编写的开源Web应用框架,它是建立

php如何使用Yii3框架?php如何使用Yii3框架?May 31, 2023 pm 10:42 PM

随着互联网的不断发展,Web应用程序开发的需求也越来越高。对于开发人员而言,开发应用程序需要一个稳定、高效、强大的框架,这样可以提高开发效率。Yii是一款领先的高性能PHP框架,它提供了丰富的特性和良好的性能。Yii3是Yii框架的下一代版本,它在Yii2的基础上进一步优化了性能和代码质量。在这篇文章中,我们将介绍如何使用Yii3框架来开发PHP应用程序。

Yii框架中的数据查询:高效地访问数据Yii框架中的数据查询:高效地访问数据Jun 21, 2023 am 11:22 AM

Yii框架是一个开源的PHPWeb应用程序框架,提供了众多的工具和组件,简化了Web应用程序开发的流程,其中数据查询是其中一个重要的组件之一。在Yii框架中,我们可以使用类似SQL的语法来访问数据库,从而高效地查询和操作数据。Yii框架的查询构建器主要包括以下几种类型:ActiveRecord查询、QueryBuilder查询、命令查询和原始SQL查询

Yii2 vs Phalcon:哪个框架更适合开发显卡渲染应用?Yii2 vs Phalcon:哪个框架更适合开发显卡渲染应用?Jun 19, 2023 am 08:09 AM

在当前信息时代,大数据、人工智能、云计算等技术已经成为了各大企业关注的热点。在这些技术中,显卡渲染技术作为一种高性能图形处理技术,受到了越来越多的关注。显卡渲染技术被广泛应用于游戏开发、影视特效、工程建模等领域。而对于开发者来说,选择一个适合自己项目的框架,是一个非常重要的决策。在当前的语言中,PHP是一种颇具活力的语言,一些优秀的PHP框架如Yii2、Ph

Yii2编程指南:运行Cron服务的方法Yii2编程指南:运行Cron服务的方法Sep 01, 2023 pm 11:21 PM

如果您问“Yii是什么?”查看我之前的教程:Yii框架简介,其中回顾了Yii的优点,并概述了2014年10月发布的Yii2.0的新增功能。嗯>在这个使用Yii2编程系列中,我将指导读者使用Yii2PHP框架。在今天的教程中,我将与您分享如何利用Yii的控制台功能来运行cron作业。过去,我在cron作业中使用了wget—可通过Web访问的URL来运行我的后台任务。这引发了安全问题并存在一些性能问题。虽然我在我们的启动系列安全性专题中讨论了一些减轻风险的方法,但我曾希望过渡到控制台驱动的命令

yii如何将对象转化为数组或直接输出为json格式yii如何将对象转化为数组或直接输出为json格式Jan 08, 2021 am 10:13 AM

yii框架:本文为大家介绍了yii将对象转化为数组或直接输出为json格式的方法,具有一定的参考价值,希望能够帮助到大家。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.