当前位置: 首页 > news >正文

Symfony学习笔记 - Symfony Documentation - The Basics(2)

3、Tests

Symfony集成了PHPUnit作为测试框架。可以包含unit test、integration test和application test。

  1. 按照惯例,test目录下,应该复制一份与你应用目录相同的目录。
  2. 为了避免环境污染,每次的测试可以通过bootKernel()的方式,reboot kernel。
  3. 可以通过getContainer()来获取到ServiceContainer。
  4. DAMADoctrineTestBundle可以每次交互时,用的是未被修改的database
  5. application test一般在Controller目录下,作为request/response的处理
  6. 可以模拟客户端,$crawler = $client->request('GET', '/post/hello-world');,来进行测试
  7. Client可以可以执行其他的操作,比如:
    $crawler = $client->followRedirect(); $client->followRedirects(); $client->followRedirects(false); $client->loginUser($testUser, 'my_firewall') //登录 $client->xmlHttpRequest('POST', '/submit', ['name' => 'Fabien']); //ajax请求 $client->catchExceptions(false); //异常处理 $client->request('GET', '/post/hello-world'); $client->clickLink('Click here'); //模拟点击链接 $client->request('GET', '/post/hello-world'); $crawler = $client->submitForm('Add comment', [ 'comment_form[content]' => '...', ]); //模拟提交Form ...
  8. 测试Response,验证输出符合预期。包括一系列的Assertion函数,比如:
    assertResponseIsSuccessful(string $message = '', bool $verbose = true) Asserts that the response was successful (HTTP status is 2xx). assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true) Asserts a specific HTTP status code.
  9. 测试Request,验证输入符合预期。包括一系列的Assertion函数,比如
    assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = '') Asserts the given request attribute is set to the expected value.
  10. 验证Browser,符合预期:
    assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = '')/assertBrowserNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = '')
  11. Crawler Assertions
    assertSelectorExists(string $selector, string $message = '')/assertSelectorNotExists(string $selector, string $message = '') Asserts that the given selector does (not) match at least one element in the response.
  12. Mailer Assertions
    assertEmailCount(int $count, ?string $transport = null, string $message = '') Asserts that the expected number of emails was sent.
  13. Notifier Assertions
    assertNotificationCount(int $count, ?string $transportName = null, string $message = '') Asserts that the given number of notifications has been created (in total or for the given transport).
http://www.rkmt.cn/news/752.html

相关文章:

  • 【分享+1】HarmonyOS官方模板优秀案例(第6期:商务办公 笔记应用)
  • TypeScript 队列实战:从零实现简单、循环、双端、优先队列,附完整测试代码
  • 半导体行业CRM就用八骏CRM
  • c++开发大模型mcp服务(七)使用cpp-mcp的例子MCP-ExcelAutoCpp
  • 北京市科学技术奖励揭示创新风向标:信息技术与产学研协同成亮点
  • 如何去除AI生成文章中的AI成分:一份指南
  • 2025年9月份实时最新获取地图边界数据方法,省市区县街道多级联动【文末附实时geoJson数据下载】
  • os.Signal信号量
  • 国产化替代加速:Gitee Git自建平台如何破解企业代码管理困局
  • [豪の学习笔记] 软考中级备考 基础复习#4
  • 【源码解读之 Mybatis】【基础篇】-- 第1篇:MyBatis 整体架构设计
  • 《ESP32-S3使用指南—IDF版 V1.6》第三十七章 SPI_SDCARD实验
  • 1、Windows 注册表定义
  • Gitee DevOps:中国开发者效率革命的幕后推手
  • Gitee领跑中国开发者生态:本土化优势与技术创新双轮驱动
  • Windows 注册表定义
  • windows 查询端口占用
  • CTDB 脚本配置文件指南
  • canal同步mysql到kafka
  • pb9新建“工具”选项卡中文说明
  • EasyWeChat报错Failed to cache access token.
  • 16 - Metatheory of subtyping
  • 国产项目管理工具崛起:Gitee如何以本土优势赋能企业研发效能
  • 项目调度管理系统(源码+文档+讲解+演示)
  • OB-Oracle百亿级数据存储方案
  • ZeroGPU Spaces 加速实践:PyTorch 提前编译全解析
  • 基于yolo12对目标物体进行自动裁剪和模糊打码
  • 2025.9.9数学课
  • 【Git】在更新项目时“将传入更改合并到当前分支“和”在传入更改上变基当前分支“有什么区别
  • 国内开发者如何选择代码管理平台?Gitee、GitHub等主流工具深度解析