1. 云栖社区>
  2. PHP教程>
  3. 正文

[PHP][Laravel Test]单元化测试:工作流篇

作者:用户 来源:互联网 时间:2017-12-01 09:13:26

php测试laravel工作流test单元化

[PHP][Laravel Test]单元化测试:工作流篇 - 摘要: 本文讲的是[PHP][Laravel Test]单元化测试:工作流篇, 本文适用于对 PHP 和 laravel 框架有一定了解并已经看完laravel入门视频:Laravel 5 Fundamentals 的初学者。本文内容主要讲解如何使用单元化测试工具测试应用。 相关视频 laravel test

本文适用于对 PHP 和 laravel 框架有一定了解并已经看完laravel入门视频:Laravel 5 Fundamentals 的初学者。本文内容主要讲解如何使用单元化测试工具测试应用。

相关视频 laravel test 已经下载到网盘如果还没看请先下载观看:

链接: http://pan.baidu.com/s/1sjXeLQH 提取密码:jjb5

1.本篇以一个队伍为例进行一系列测试操作。首先创建 TeamTest.php 运行 phpunit进行测试。

测试代码意义明确,和文档格式相同,仔细阅读均可理解。其中 factory() 为作者编写的实例化 user 方法,为其设置各项参数。

<?php use App/Team;use App/User;use Illuminate/Foundation/Testing/WithoutMiddleware;use Illuminate/Foundation/Testing/DatabaseMigrations;use Illuminate/Foundation/Testing/DatabaseTransaction; class TeamTestextends TestCase;{    use DatabaseTransactions;     /** @test */    public function a_team_has_a_name()    {        $team = new Team(['name' => 'Acme']);         $this->assertEquals('Acme',$team->name);    }     /** @test */    public function a_team_can_add_members()    {        $team = factory(Team::class)->create();         $user = factory(User::class)->create();        $userTwo = factory(User::class)->create();         $team->add($user);        $team->add($userTwo);         $this->assertEquals(2,$team->count());    }     /** @test */    public function a_team_has_a_maximum_size()    {        $team = factory(Team::class)->create(['size' => 2]);         $userOne = factory(User::class)->create();        $userTwo = factory(User::class)->create();         $team->add($userOne);        $team->add($userTwo);         $this->assertEquals(2,$team->count());         $this->setExpectedException('Exception');         $userThree = factory(User::class)->create();        $team->add($userThree);     }      /** @test */    public function a_team_can_add_multiple_members_at_once()    {        $team = factory(Team::class)->create();         $users = factory(User::class,2)->create();         $team->add($users);         $this->assertEquals(2,$team->count());    }     /** @test */    public function a_team_can_remove_a_member()    {     }     /** @test */    public function a_team_can_remove_all_members_at_once()    {     }} 

Team.php 创建 Team 类

<?php namespace App; use Illuminate/Database/Eloquent/Model; class Team extends Model{    protected $fillable = ['name','size'];     public function add($user)    {        $this->members()->save($user);         //gurd        if($this->count() >= $this->size) {            throw new /Exception;        }         if($userinstanceof User) {            return $this->members()->save($user);        }         $this->members()->saveMany($user);    }     public function members()    {        return $this->hasMany(User::class);    }     public function count()    {        return $this->members()->count();    }}  总结

本篇从新开始测试了一项新工程。命名规范思路清晰,如有问题可详细阅读相关 文档,或者之前的入门篇。

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , 测试 , laravel , 工作流 , test 单元化 ,以便于您获取更多的相关知识。