Laravel 项目:使用 TDD 构建论坛 Chapter 7

0.写在前面

  • 本系列文章为laracasts.com 的系列视频教程——Let's Build A Forum with Laravel and TDD 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版
  • 视频源码地址:https://github.com/laracasts/Lets-Build-a-Forum-in-Laravel
  • *本项目为一个 forum(论坛)项目,与本站的第二本实战教程 Laravel 教程 - Web 开发实战进阶 ( Laravel 5.5 ) 类似,可互相参照
  • 项目开发模式为TDD开发,教程简介为:

    A forum is a deceptively complex thing. Sure, it's made up of threads and replies, but what else might exist as part of a forum? What about profiles, or thread subscriptions, or filtering, or real-time notifications? As it turns out, a forum is the perfect project to stretch your programming muscles. In this series, we'll work together to build one with tests from A to Z.

  • 项目版本为laravel 5.4,教程后面会进行升级到laravel 5.5的教学
  • 视频教程共计 102 个小节,笔记章节与视频教程一一对应

1.本节说明

  • 对应视频第 7 小节:Let's Make Some Testing Helpers

2.本节内容

本节让我们来建立一些测试时用到的辅助函数,方便我们进行编写测试:
修改composer.json

.
.
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    },
    "files":["tests/utilities/functions.php"]  -->这里增加一行
},
.
.

新建tests/utilities/functions.php文件,执行命令将该文件加载进来:

$ composer dump-autoload

我们将获取模型实例这样的方法,例如create()make()抽离出来,放到functions.php中:
\tests\utilities\functions.php

<?php

function create($class,$attributes = [])
{
    return factory($class)->create($attributes);
}

function make($class,$attributes = [])
{
    return factory($class)->make($attributes);
}

function raw($class,$attributes = [])
{
    return factory($class)->raw($attributes);
}

在很多测试中,我们需要测试用户是否登录。在之前的测试当中,我们使用了be()actingAs()方法来得到一个已登录用户。现在我们在TestCase.php新建signIn()方法,将用户登录的逻辑放在基类文件中:
\tests\TestCase.php

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function signIn($user = null)
    {
        $user = $user ?: create('App\User');

        $this->actingAs($user);

        return $this;
    }
}

现在我们需要重构之前的代码,将像factory('App\Reply')->create()这样的代码片段更改为create('App\Reply')be()actingAs()方法更改为signIn()方法。
对于使用PHP Storm的 programmer,我们有建立测试更加便捷的方法:

  1. 新建一个template:
    file
  2. 在相对应的位置填充完模板,在点击Edit variables
    file
  3. 定义应用的位置everywhere
    file
  4. 勾上Reformat according to style
    file
  5. 最后点击Apply应用设置,再点击OK
    file
    建完模板之后,再新建一个 PHP 文件,输入testclass后使用Tab键即可补齐模板,十分便捷。

3.写在后面

  • 如有建议或意见,欢迎指出~
  • 如果觉得文章写的不错,请点赞鼓励下哈,你的鼓励将是我的动力!
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!