tyler36/phpunit-helper-traits

此包最新版本(v1.0)没有可用的许可信息。

补充Laravel测试的PHPUnit特性

v1.0 2016-07-11 06:35 UTC

This package is auto-updated.

Last update: 2024-08-29 03:48:13 UTC


README

``# PHPUnit Traits

安装

1. 使用composer安装包。

   composer require tyler36/phpunit-helper-traits

简介

这些特性是为与Laravel 5.2和Laravel-elixir 5一起设计和测试的。使用laravel-elixir的'visit'命令可以测试页面是否可用并生成用于某些测试的爬虫对象。查看测试目录以获取更多示例和用法。

CheckAssetExistsTrait

检查项目是否可用,并出现在页面上。如果字符串以'http'开头,此特性将向网络发送HTTP请求并检查资产是否可用。对于检查CDN非常有用。其他路径将在项目的'public'目录中开始查找("app()->publicPath()")。如果测试有一个crawler对象,此特性还将检查字符串是否显示在页面上。

*返回TEST案例以允许链式调用。

使用方法:在类中包含特性 - use tyler36\phpunitTraits\CheckAssetExistsTrait;

通过文件名或数组调用特性 - $this->checkAssetExists($filename);

例如。

$this->checkAssetExists("https://code.jqueryjs.cn/jquery-3.1.0.min.js");

$this->checkAssetExists("/robots.txt");

$this->checkAssetExists(["js/jquery.js", "css/main.css"]);

$this->visit('/home')->checkAssetExists("/images/logo.jpg")

CountElementsTrait

检查页面并计算指定CSS选择器的出现次数。

使用方法:在类中包含特性 - use tyler36\phpunitTraits\CountElementsTrait;

通过CSS选择器和期望的计数调用特性 - $this->countElements($selector, $count);

例如 $this->countElements('.card', 3);

DisableExceptionHandlerTrait

此特性覆盖了默认异常处理器,允许您使用断言检查错误消息。

使用方法:在类中包含特性 - use tyler36\phpunitTraits\DisableExceptionHandlerTrait;

通过CSS选择器调用特性以禁用异常处理 - $this->disableExceptionHandling()

在测试中使用TRY / CATCH。

ImpersonateTrait

设置认证状态的辅助工具。

asGuest

确保当前状态为访客(未登录)。

使用方法:在类中包含特性 - use tyler36\phpunitTraits\CountElementsTrait;

例如。

$this->asGuest();

asMember

确保当前状态为成员(未登录)。如果传递了用户对象,此特性将以该用户登录。如果没有传递用户对象,此特性将使用'App\User'工厂创建一个随机的用户对象并登录。

使用方法:在类中包含特性 - use tyler36\phpunitTraits\CountElementsTrait;

例如。

$this->asMember();

MailTrackingTrait

phpunit-testing-in-laravel启发。通过拦截发送的邮件来检查邮件选项。您可能希望使用日志驱动程序阻止laravel发送邮件,在测试或setUp()函数中。

config()->set('mail.driver', 'log');

使用方法:在类中包含特性 - use tyler36\phpunitTraits\MailTrackerTrait;

seeEmailWasNotSent

断言:未发送邮件

$this->seeEmailWasNotSent();

seeEmailWasSent

断言:已发送邮件

$this->seeEmailWasSent();

seeEmailsSent($count)

断言:发送了$count封邮件

$this->seeEmailsSent(3)

seeEmailTo($recipient)

断言:收件人

$this->seeEmailTo($recipient);

seeEmailNotTo($recipient)

断言:非收件人

$this->seeEmailNotTo($recipient);

seeEmailFrom($sender)

断言:发送者

$this->seeEmailFrom($sender);

seeEmailNotFrom($sender)

断言:非发送者

$this->seeEmailNotFrom($sender);

seeEmailEquals($body)

断言:正文匹配

$this->seeEmailEquals($body);

seeEmailNotEquals($body)

断言:正文不匹配

$this->seeEmailNotEquals($body);

seeEmailContains($excerpt)

断言:正文包含片段

$this->seeEmailContains($excerpt);

seeEmailNotContains($excerpt)

断言:正文不包含片段

$this->seeEmailNotContains($excerpt);

seeEmailSubjectEquals($subject)

断言:主题匹配

$this->seeEmailSubjectEquals($subject);

seeEmailSubjectNotEquals($subject)

断言:主题不匹配

$this->seeEmailSubjectNotEquals($subject);

seeEmailSubjectContains($excerpt)

断言:主题包含

$this->seeEmailSubjectContains($fragment);

seeEmailSubjectNotContains($excerpt)

断言:主题不包含

$this->seeEmailSubjectNotContains($fragment);

PrepareFileUploadTrait

模拟文件上传

在类中包含特性 - use tyler36\phpunitTraits\PrepareFileUploadTrait;

使用文件名或数组调用特性 - $this->prepareUpload($file)

例如:$this->prepareUpload('./avatar.jpg');