PHPUnit 入门案例,phpunit入门案例
了解PHPUnit
本案例是关于创建三角形的一个单元测试入门案例,在netbeans环境中完成,关于在此环境中搭建phpunit这里不再描述,可以参考以下资料完成搭建工作:
http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html
https://phpunit.de/manual/current/zh_cn/installation.html
https://github.com/sebastianbergmann/phpunit-skeleton-generator
原代码类:
<?php class Triangle { /** * 三条边 第一条边 * @var int */ protected $a; /** * 三条边 第二条边 * @var int */ protected $b; /** * 三条边 第三条边 * @var int */ protected $c; /** * 类型 * @var string */ protected $type; /** * 等边 */ const TYPE_EQUILATERAL = 'Equilateral'; /** * 等腰 */ const TYPE_ISOSCELES = 'Isosceles'; /** * 普通 */ const TYPE_ORDINARY = 'Ordinary'; public function __construct($a = 0, $b = 0, $c = 0) { $this->initSide($a, $b, $c); } /** * 初始化三边 * @param int $a * @param int $b * @param int $c */ protected function initSide(&$a = 0, &$b = 0, &$c = 0) { $this->a = intval($a); $this->b = intval($b); $this->c = intval($c); return $this; } /** * 组建 */ public function create($a, $b, $c) { return $this->initSide($a, $b, $c)->verifySideIsValid(); } /** * 获取类型 */ public function getType() { return $this->verifyType()->type; } /** * 验证三边是否有效 * @return boolean */ protected function verifySideIsValid() { if (intval($this->a) <= 0 || intval($this->b) <= 0 || intval($this->c) <= 0) { return false; } if ($this->a + $this->b <= $this->c) { return false; } if ($this->a + $this->c <= $this->b) { return false; } if ($this->b + $this->c <= $this->a) { return false; } if ($this->a - $this->b >= $this->c) { return false; } if ($this->a - $this->c >= $this->b) { return false; } if ($this->b - $this->c >= $this->a) { return false; } return true; } /** * 验证类型 */ protected function verifyType() { if ($this->isEquilateral()) { $this->type = self::TYPE_EQUILATERAL; return $this; } if ($this->isIsosceles()) { $this->type = self::TYPE_ISOSCELES; return $this; } $this->type = self::TYPE_ORDINARY; return $this; } /** * 是否为等边三角形 */ protected function isEquilateral() { return (($this->a == $this->b ) && ($this->b == $this->c)) ? true : false; } /** * 是否为等腰三角形 */ protected function isIsosceles() { return (($this->a == $this->b ) || ($this->b == $this->c) || ($this->a == $this->c)) ? true : false; } }
生成的测试类文件:
<?php /** * Generated by PHPUnit_SkeletonGenerator on 2016-03-13 at 19:49:12. */ class TriangleTest extends PHPUnit_Framework_TestCase { /** * @var Triangle */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new Triangle; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @dataProvider addDataProvider * @covers Triangle::create * @todo Implement testCreate(). */ public function testCreate($a, $b, $c) { // Remove the following lines when you implement this test. /** $this->markTestIncomplete( 'This test has not been implemented yet.' ); * */ /* 实现代码 */ $this->assertTrue($this->object->create($a, $b, $c)); } /** * @covers Triangle::getType * @todo Implement testGetType(). */ public function testGetType() { // Remove the following lines when you implement this test. $this->markTestIncomplete( 'This test has not been implemented yet.' ); } /** * 测试用例 * @return array */ public function addDataProvider() { return [ [3, 4, 5], //yes [2, 2, 2], //yes [8, 10, 8], //yes [2, 3, 4], //yes [1, 2, 3], //no [5, 6, 7], //yes [8, 8, 15], //yes [0, 0, 0], //no [-10, 2, 5], //no [0, 2, 1], //no ]; } }
这里需要注意,在我们执行“创建/更新测试”后生成的测试文件类与上面会有些不同,这里的测试用例是手动加上去的,这里具体实现可以查看手册里的说明!
附执行结果:
"/usr/bin/php" "/usr/local/bin/phpunit" "--colors" "--log-junit" "/tmp/nb-phpunit-log.xml" "--bootstrap" "/var/www/html/phpunit/test/bootstrap.php" "/usr/local/netbeans-8.1/php/phpunit/NetBeansSuite.php" "--" "--run=/var/www/html/phpunit/test/core/triangleTest.php" PHPUnit 5.2.10 by Sebastian Bergmann and contributors. ....F..FFFI 11 / 11 (100%) Time: 105 ms, Memory: 10.50Mb There were 4 failures: 1) TriangleTest::testCreate with data set #4 (1, 2, 3) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 2) TriangleTest::testCreate with data set #7 (0, 0, 0) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 3) TriangleTest::testCreate with data set #8 (-10, 2, 5) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 4) TriangleTest::testCreate with data set #9 (0, 2, 1) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest.php:47 FAILURES! Tests: 11, Assertions: 10, Failures: 4, Incomplete: 1. 完成。

防止会话固定攻击的有效方法包括:1.在用户登录后重新生成会话ID;2.使用安全的会话ID生成算法;3.实施会话超时机制;4.使用HTTPS加密会话数据,这些措施能确保应用在面对会话固定攻击时坚不可摧。

实现无会话身份验证可以通过使用JSONWebTokens(JWT)来实现,这是一种基于令牌的认证系统,所有的必要信息都存储在令牌中,无需服务器端会话存储。1)使用JWT生成和验证令牌,2)确保使用HTTPS防止令牌被截获,3)在客户端安全存储令牌,4)在服务器端验证令牌以防篡改,5)实现令牌撤销机制,如使用短期访问令牌和长期刷新令牌。

PHP会话的安全风险主要包括会话劫持、会话固定、会话预测和会话中毒。1.会话劫持可以通过使用HTTPS和保护cookie来防范。2.会话固定可以通过在用户登录前重新生成会话ID来避免。3.会话预测需要确保会话ID的随机性和不可预测性。4.会话中毒可以通过对会话数据进行验证和过滤来预防。

销毁PHP会话需要先启动会话,然后清除数据并销毁会话文件。1.使用session_start()启动会话。2.用session_unset()清除会话数据。3.最后用session_destroy()销毁会话文件,确保数据安全和资源释放。

如何改变PHP的默认会话保存路径?可以通过以下步骤实现:在PHP脚本中使用session_save_path('/var/www/sessions');session_start();设置会话保存路径。在php.ini文件中设置session.save_path="/var/www/sessions"来全局改变会话保存路径。使用Memcached或Redis存储会话数据,如ini_set('session.save_handler','memcached');ini_set(

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然后使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

在PHP会话中可以存储数组。1.启动会话,使用session_start()。2.创建数组并存储在$_SESSION中。3.通过$_SESSION检索数组。4.优化会话数据以提升性能。

PHP会话垃圾回收通过概率机制触发,清理过期会话数据。1)配置文件中设置触发概率和会话生命周期;2)可使用cron任务优化高负载应用;3)需平衡垃圾回收频率与性能,避免数据丢失。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1
好用且免费的代码编辑器