phpunit 相关问题

PHPUnit是面向程序员的PHP测试框架。它是单元测试框架的xUnit体系结构的一个实例。

如何在Linux上使用selenium和php?

我想在centos 6.5上安装selenium,然后通过PHP脚本中的API使用它。 Selenium 测试脚本将记录在另一个系统上,这些 Selenium 测试脚本将作为输入给出......

回答 2 投票 0

在 PHPUnit 中,如何在连续调用模拟方法时指示不同的 with() ?

我想使用不同的预期参数调用我的模拟方法两次。这不起作用,因为 Expects($this->once()) 将在第二次调用时失败。 $mock->期望($this->once()) ...

回答 4 投票 0

如何使用约束属性对 symfony dto 进行单元测试

我有一个新创建的 Symfony 7 应用程序,并且有一个名为:AuthCallbackDto 的 DTO 我有一个新创建的 Symfony 7 应用程序,并且有一个名为:AuthCallbackDto 的 DTO <?php declare(strict_types=1); namespace App\Application\Model\Auth; use Symfony\Component\Validator\Constraints as Assert; readonly class AuthCallbackDto { public function __construct( #[Assert\NotBlank(message: 'The `code` value cannot be blank')] #[Assert\Length(min: 5)] private string $code, #[Assert\NotBlank(message: 'The `session_state` value cannot be blank')] #[Assert\Length(min: 5)] private string $session_state, ) { } public function getCode(): string { return $this->code; } public function getSessionState(): string { return $this->session_state; } } 我在控制器中使用它,如下所示: <?php declare(strict_types=1); namespace App\Application\Controller\Auth; use App\Application\Model\Auth\AuthCallbackDto; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\MapQueryString; use Symfony\Component\Routing\Attribute\Route; #[AsController] class Callback { #[Route('/auth/callback', name: 'auth/callback', methods: ['GET'])] public function test( #[MapQueryString] AuthCallbackDto $authCallbackDto, ): Response { return new JsonResponse([ 'code' => $authCallbackDto->getCode(), 'session_state' => $authCallbackDto->getSessionState() ]); } } 如果代码或 session_state 值不存在或 < 5 characters then an error is thrown. This is good! http://localhost:8000/auth/callback?code=oihiohoih&session_state=34f34f34f - 有效(好) http://localhost:8000/auth/callback?code=&session_state= - 失败(好) 'http://localhost:8000/auth/callback?code=123&session_state=123 - 失败(好) 由于过于自信,我决定为 DTO 类编写一个非常小的单元测试。然而,我随后注意到当我从控制台运行 PHP Unit 时,约束属性没有被应用。测试中的异常永远不会抛出。 任何人都可以告诉我我做错了什么还是我只是假设太多? 这是我的单元测试: <?php declare(strict_types=1); namespace App\Tests\Unit\Application\Model\Auh; use App\Application\Model\Auth\AuthCallbackDto; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; class AuhCallbackDtoTest extends TestCase { /** * @return array<array<int, string|bool>> */ public static function dtoData(): array { return [ [ '', '', true, ], [ 'someCodeABC12344', 'someSessionState', false, ], ]; } #[DataProvider('dtoData')] public function testDtoCanBeCreated(string $code, string $sessionState, bool $exception): void { if ($exception) { $this->expectException(\Exception::class); } $dto = new AuthCallbackDto($code, $sessionState); self::assertEquals($dto->getCode(), $code); self::assertEquals($dto->getSessionState(), $sessionState); } } 我尝试使用控制台调试验证规则,看起来不错: bin/console debug:validator 'App\Application\Model\Auth\AuthCallbackDto' App\Application\Model\Auth\AuthCallbackDto ------------------------------------------ +---------------+--------------------------------------------------+--------------------------+---------------------------------------------------------------------------------+ | Property | Name | Groups | Options | +---------------+--------------------------------------------------+--------------------------+---------------------------------------------------------------------------------+ | code | property options | | [ | | | | | "cascadeStrategy" => | | | | | "None", | | | | | "autoMappingStrategy" => | | | | | "None", | | | | | "traversalStrategy" => | | | | | "None" | | | | | ] | | code | Symfony\Component\Validator\Constraints\NotBlank | Default, AuthCallbackDto | [ | | | | | "allowNull" => | | | | | false, | | | | | "message" => "The `code` | | | | | value cannot be blank", | | | | | "normalizer" => | | | | | null, | | | | | "payload" => | | | | | null | | | | | ] | | code | Symfony\Component\Validator\Constraints\Length | Default, AuthCallbackDto | [ | | | | | "charset" => | | | | | "UTF-8", | | | | | "charsetMessage" => "This | | | | | value does not match the expected {{ charset }} charset.", | | | | | "countUnit" => | | | | | "codepoints", | | | | | "exactMessage" => "This | | | | | value should have exactly {{ limit }} character.|This value should have exactly | | | | | {{ limit }} characters.", | | | | | "max" => | | | | | null, | | | | | "maxMessage" => "This value | | | | | is too long. It should have {{ limit }} character or less.|This value is too | | | | | long. It should have {{ limit }} characters or less.", | | | | | "min" => 5, | | | | | "minMessage" => "This value | | | | | is too short. It should have {{ limit }} character or more.|This value is too | | | | | short. It should have {{ limit }} characters or more.", | | | | | "normalizer" => | | | | | null, | | | | | "payload" => | | | | | null | | | | | ] | | session_state | property options | | [ | | | | | "cascadeStrategy" => | | | | | "None", | | | | | "autoMappingStrategy" => | | | | | "None", | | | | | "traversalStrategy" => | | | | | "None" | | | | | ] | | session_state | Symfony\Component\Validator\Constraints\NotBlank | Default, AuthCallbackDto | [ | | | | | "allowNull" => | | | | | false, | | | | | "message" => "The | | | | | `session_state` value cannot be blank", | | | | | "normalizer" => | | | | | null, | | | | | "payload" => | | | | | null | | | | | ] | | session_state | Symfony\Component\Validator\Constraints\Length | Default, AuthCallbackDto | [ | | | | | "charset" => | | | | | "UTF-8", | | | | | "charsetMessage" => "This | | | | | value does not match the expected {{ charset }} charset.", | | | | | "countUnit" => | | | | | "codepoints", | | | | | "exactMessage" => "This | | | | | value should have exactly {{ limit }} character.|This value should have exactly | | | | | {{ limit }} characters.", | | | | | "max" => | | | | | null, | | | | | "maxMessage" => "This value | | | | | is too long. It should have {{ limit }} character or less.|This value is too | | | | | long. It should have {{ limit }} characters or less.", | | | | | "min" => 5, | | | | | "minMessage" => "This value | | | | | is too short. It should have {{ limit }} character or more.|This value is too | | | | | short. It should have {{ limit }} characters or more.", | | | | | "normalizer" => | | | | | null, | | | | | "payload" => | | | | | null | | | | | ] | +---------------+--------------------------------------------------+--------------------------+---------------------------------------------------------------------------------+ 我可以更新 DTO 的构造函数以进行一些标准的 Webmozart 静态断言调用,但当应用程序按预期运行时,这是不必要的。 ... public function __construct( #[Assert\NotBlank] #[Assert\Length(min: 5)] private string $code, #[Assert\NotBlank] #[Assert\Length(min: 5)] private string $session_state, ) { \Webmozart\Assert\Assert::notEmpty($this->code); \Webmozart\Assert\Assert::minLength($code, 5); \Webmozart\Assert\Assert::notEmpty($this->session_state); \Webmozart\Assert\Assert::minLength($this->session_state, 5); } ... 那些 Assert 属性来自 Symfony Validator 组件。 PHP 不会自动在其上运行业务逻辑。但它确实在框架中做到了这一点。 您需要将其传递给 symfony 验证器。 但是,由于您正在测试控制器,因此您可以创建一个 WebTestCase 来代替。您可以通过发送请求并验证响应来(功能上)测试您的控制器。简而言之,就像您的应用程序是一个“黑匣子”一样运行功能测试。 有关更多信息,请参阅本章:https://symfony.com/doc/current/testing.html#write-your-first-application-test 如果你真的不想要这个,并且你真的想测试你的 Dto。您必须使用 Symfony 验证器验证您的 Dto。 class AuhCallbackDtoTest extends TestCase { public function testInvalidDto(): void { $validator = Validation::createValidator(); $dto = new AuthCallbackDto("", ""); $errors = $validator->validate($dto); $this->assertCount(2, (array)$errors); // there are 2 errors // You can check specifically for the error and the error message too } } 仅供参考,我还没有真正测试过这一点。因此 Validation::createValidator() 可能需要一些额外的配置来理解这些构造函数属性,但我并不完全确定。

回答 1 投票 0

PhpStorm 无法解析 PHPUnit 版本输出。非常奇怪的错误

我正在尝试使用 Laravel 项目的 Vagrant box 在 Windows 10 上的 PhpStorm 2017.1 中设置 PHPUnit。我已经设置了一个远程口译员,我已经使用它几个月了,一切......

回答 4 投票 0

如何模拟您正在使用 Prophecy 测试的类中的方法?

我想第一次使用Prophecy(“phpspec/prophecy-phpunit”)为我的类创建单元测试。我想测试一个调用同一服务中另一个函数的函数,这是代码...

回答 3 投票 0

如何使用 PHP 反射来设置静态属性?

我正在使用 PHPUnit 制作一个模拟类进行测试。 类项目扩展对象{ 受保护的静态$_cache; } 我非常确定嘲笑会做类似的事情(如果我错了,请纠正我......

回答 2 投票 0

typo3 extbase 在 phpunit 测试中使用 toArray

我有一个 extbase 扩展(typo3 4.5) $test = $this->testRepository->findAll(); $this->视图->分配('测试', 数组合并( 数组('0'=>'选择'), $测试->

回答 1 投票 0

如何处理“有效负载无效”。在 Gitlab CI/CD 管道中?

我正在尝试在 GitLab CI/CD 中为我的(Laravel 11 + React Typescript with Inertiajs)运行 testJob,但最终总是出现以下错误。我已确保在 CI/CD 变量中设置 APP_KEY...

回答 1 投票 0

如何在 Laravel 5.8 中使用 PHPUnit 的方法设置

我曾经使用 PHPUnit 的方法设置来为我的测试方法创建一个实例。但在 Laravel 5.8 中我做不到 我已经尝试了两种方法,它的工作原理是为每个方法创建一个实例,如何显示 b...

回答 3 投票 0

如果“framework.test”配置未设置为 true,则无法创建功能测试中使用的客户端

我正在尝试使用WebTestCase应用功能测试。 CalculatorControllerTest 类扩展了 WebTestCase { 公共函数 testSumPost() { $client = static::createClient(); ...

回答 2 投票 0

使用命令 phpunit 和 artisan 运行测试之间的区别

我有一个 Laravel 项目,其中编写了一些单元测试。 当我使用 phpvendor/bin/phpunit 运行这些测试时,结果如下所示: 现在,当我使用 php artisan test 运行测试时

回答 1 投票 0

如何在测试执行期间模拟实例化类的类函数

我似乎无法专心在 phpunit 中进行嘲笑。我有一个这样的测试,我尝试模拟我的 Curl 包装类“CurlRequest”的执行方法。 /** @测试 */ 公共职能

回答 1 投票 0

phpunit - 不使用注入进行模拟?

我正在学习 phpunit 并注意到很多示例似乎是模拟一个类,然后将其注入另一个类,然后测试该方法。我想知道如果我想不注射就测试怎么办? 对...

回答 1 投票 0

在 Netbeans 7.1 下运行 Symfony 2 应用程序的 PHPUnit 测试

我花了一些时间来弄清楚如何配置 Netbeans 7.1 以与 Symfony 2 和 PHPUnit 一起使用,但我没有成功。当我尝试在控制台下运行任何测试时,没有问题。但是当你...

回答 3 投票 0

如何更改使用工厂创建的嵌套实体的默认值?

我的数据模型如下所示: 一个订单包含 1 到 N 个产品,一个产品有一个类别。 我有 3 个工厂,每个实体一个:OrderFactory、ProductFactory、CategoryFactory。在

回答 1 投票 0

PHPUnit 中是否有相当于 SimpleTest 的“部分模拟”功能?

我正在尝试将一堆测试从 SimpleTest 迁移到 PHPUnit,我想知道 SimpleTest 的部分模拟是否有等效项。 我似乎在文档中找不到任何内容......

回答 5 投票 0

当我们使用 PHPUnit 时,控制台中的 --uses 参数到底有什么作用以及如何使用?

我在使用控制台时遇到了 PHPUnit 的参数。 如何使用它以及使用名为 --uses 的参数做什么? 具体来说,我的意思是文档的链接: https://docs.phpunit.de/en...

回答 1 投票 0

如何在单元测试中将模型绑定到请求

我正在努力将模型绑定到单元测试中的请求,以便可以在表单请求中检索模型的关系。 这是表格请求: TimeSlotUpdateRequest 类扩展了

回答 2 投票 0

loginUser 在功能测试中因子域失败

我的堆栈:Symfony 7 / php 8.3 / phpunit 9.6 我有几个带有子域的防火墙。我尝试测试一下: 正面: 主机: ^(app\.mydomain\.localhost)|(app\.mydomain)$ 拍拍...

回答 1 投票 0

在 phpunit 中断言具有一定容差的相等时间

我刚刚开始使用 phpunit,这是我现在没有什么问题的地方。 我正在为其编写测试的类有一个 Logger 方法,调用该方法时,会将时间戳添加到属性中,如下所示: $t...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.