几天以来,我一直在努力让 Notification::assertSentTo() 方法在 Laravel 5.6 应用程序中重置密码电子邮件的功能测试中发挥作用,但仍然收到以下代码的持续失败:
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserPasswordResetTest extends TestCase
{
public function test_submit_password_reset_request()
{
$user = factory("App\User")->create();
$this->followingRedirects()
->from(route('password.request'))
->post(route('password.email'), [ "email" => $user->email ]);
Notification::assertSentTo($user, ResetPassword::class);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种想法,包括直接在使用列表中使用 Illuminate\Support\Testing\Fakes\NotificationFake 。在任何尝试中,测试都会失败
Error: Call to undefined method Illuminate\Notifications\Channels\MailChannel::assertSentTo()
Run Code Online (Sandbox Code Playgroud)
期待任何有助于成功测试的提示。问候并保重!
我正在尝试使用更好的 phpunit 扩展从 vs code 在 docker 容器上运行 php 单元测试,但我无法让它工作。
到目前为止我所拥有的:-
docker-compose.yml:-
version: '3.1'
services:
php:
build:
context: .
dockerfile: .docker/Dockerfile
image: laraboard
ports:
- 8000:80
restart: always
volumes:
- .:/var/www/html
networks:
- laraboard
mysql:
image: mysql:8.0
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: laraboard
MYSQL_USER: root
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: password
networks:
- laraboard
phpmyadmin:
depends_on:
- mysql
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8001:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_PASSWORD: password
networks:
- laraboard
networks:
laraboard:
volumes:
db_data: …Run Code Online (Sandbox Code Playgroud) 我有一个 Laravel 8 应用程序,我想在其中测试从 HTTP API 调用返回的 JSON 的一部分。我的响应对象如下所示:
{
"data": [
{
"name": "Cumque ex quos.",
"createdAt": "2020-12-29T17:15:32.000000Z",
"updatedAt": "2020-12-29T17:15:32.000000Z",
"startAt": "2021-01-18 17:15:32",
"endAt": "2021-01-18 17:15:32",
"startedAt": null,
"status": "Running",
"range": {
"type": "percentage",
"max": 0,
"min": 0
},
},
{
"name": "Cumque ex quos 2.",
"createdAt": "2020-12-29T17:15:32.000000Z",
"updatedAt": "2020-12-29T17:15:32.000000Z",
"startAt": "2021-01-18 17:15:32",
"endAt": "2021-01-18 17:15:32",
"startedAt": null,
"status": "Pending",
"range": {
"type": "percentage",
"max": 20,
"min": 100
},
},
],
"other_keys" [ ... ];
}
Run Code Online (Sandbox Code Playgroud)
我有兴趣测试键返回的数组的结构data。这是我正在使用的失败测试: …
单元测试对我来说比功能测试更困难,我正在尝试编写与我的中间件相关的单元测试,但是出了什么问题。有人可以帮我修复这个错误,或者为这个中间件编写一个正常的单元测试。
我的中间件
<?php
namespace App\Http\Middleware;
use App\Services\SettingsService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
final class Locale
{
private SettingsService $settingsService;
public function __construct(SettingsService $settingsService)
{
$this->settingsService = $settingsService;
}
public function handle(Request $request, Closure $next): mixed
{
$locale = $this->settingsService->get('language', app()->getLocale());
app()->setLocale($locale);
return $next($request);
}
}
Run Code Online (Sandbox Code Playgroud)
我的测试
<?php
namespace Tests\Unit;
use App\Http\Middleware\Locale;
use App\Services\SettingsService;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
class LocaleMiddlewareTest extends TestCase
{
/**
* A basic unit test example.
*
* @return void
*/
public function test_example()
{
$request …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用匿名类测试和模拟一段代码。这是代码:
<?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class FetchPromoCodeService
{
private $client;
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
public function getPromoCodeList(): array
{
$response = $this->client->request(
'GET', 'https://xxxxxxxxx.mockapi.io/test/list'
);
return $response->toArray();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试类:
<?php
namespace App\Tests\Service;
use App\Service\FetchPromoCodeService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
class FetchPromoCodeServiceTest extends TestCase
{
public function testGetPromoCodeList()
{
$fetchPromoCodeService = new class () extends FetchPromoCodeService {
public $client;
public function __construct($client)
{
$this->client = (new JsonResponse(['a' => 1, 'b' => 2]))->getContent();
parent::__construct($client);
}
}; …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 Laravel,并且我是单元/功能测试的新手,所以我想知道在编写测试时处理更复杂的功能案例的最佳方法是什么?
我们来看这个测试示例:
// tests/Feature/UserConnectionsTest.php
public function testSucceedIfConnectAuthorised()
{
$connection = factory(Connection::class)->make([
'sender_id' => 1,
'receiver_id' => 2,
'accepted' => false,
'connection_id' => 5,
]);
$user = factory(User::class)->make([
'id' => 1,
]);
$response = $this->actingAs($user)->post(
'/app/connection-request/accept',
[
'accept' => true,
'request_id' => $connection->id,
]
);
$response->assertLocation('/')->assertStatus(200);
}
Run Code Online (Sandbox Code Playgroud)
所以我们遇到了这样的情况,两个用户之间有一些连接系统。数据库中有一个Connection由其中一位用户创建的条目。现在要使其成功连接,第二个用户必须批准它。问题在于UserController通过以下方式接受这一点connectionRequest:
// app/Http/Controllers/Frontend/UserController.php
public function connectionRequest(Request $request)
{
// we check if the user isn't trying to accept the connection
// that he initiated himself
$connection = …Run Code Online (Sandbox Code Playgroud) 我有一个带有大量测试的 Laravel 项目。我使用 pcov 来计算代码覆盖率,大约需要 4 分钟。但 pcov 不支持分支覆盖,所以我决定使用 xdebug。
使用 xdebug 测试执行,使用代码覆盖率但不使用 --path-coverage(分支覆盖率)大约需要 8 分钟。
但是使用 xdebug、代码覆盖率和 --path-coverage(分支覆盖率)测试执行需要超过 2 个小时,甚至不能等到结束:
INFO[2021-09-14 21:33:24] Executing runtests with coverage xdebug
XDEBUG_MODE=coverage
php artisan test --parallel --processes=8 --verbose --passthru=--path-coverage tests/Feature --coverage-text
Warming cache for static analysis ... done [00:00.071]
............S................................................ 61 / 1180 ( 5%)
............................................................. 122 / 1180 ( 10%)
............................................................. 183 / 1180 ( 15%)
............................................................. 244 / 1180 ( 20%)
.......
INFO[2021-09-15 00:00:05] finished in 2h 26m 40.458565176s
Run Code Online (Sandbox Code Playgroud)
所以我的问题是 --path-coverage …
引用https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices,“默认情况下,PHPUnit 转换 PHP 错误、警告和通知在执行异常测试期间触发”。考虑到这一点,这是我的单元测试:
<?php
class DemoTest extends PHPUnit\Framework\TestCase
{
public function testDemo()
{
try {
trigger_error('zzz', E_USER_DEPRECATED);
} catch (\Throwable $e) {}
}
}
Run Code Online (Sandbox Code Playgroud)
当我vendor/bin/phpunit在 PHP 8.0.9 上使用 PHPUnit 9.4.0 运行时,我得到以下信息(输出的不相关部分已被删除):
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.
R 1 / 1 (100%)
Run Code Online (Sandbox Code Playgroud)
当我vendor/bin/phpunit在 PHPUnit 9.4.0 和 PHP 8.1.0 上运行时,我得到以下信息:
Deprecated: PHPUnit\Runner\DefaultTestResultCache implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old …Run Code Online (Sandbox Code Playgroud) 我需要在我的 symfony5 项目中模拟 S3Client,以便能够引发异常并测试我的代码对这些异常的反应。我们使用 aws/aws-sdk-php 版本 3.*
\n7 年前,有人遇到了同样的问题,我尝试遵循这个解决方案 [ PHPUnit - Mock S3Client not running well ],但出现错误。
\n我现在所做的:
\n在我的服务中:
\nclass AwsS3Service implements AwsS3ServiceInterface\n{\n private S3Client $s3Client;\n ....\n\n public function __construct(ParameterBagInterface $params)\n {\n [ ... ]\n\n $this->s3Client = $this->getS3client();\n }\n\n public function getS3client(): S3Client\n {\n return (new Sdk($this->config))->createS3();\n }\n\nRun Code Online (Sandbox Code Playgroud)\n所以我有一个公共方法,我可以模拟它并使其返回一个模拟的 S3Client。
\n在我的测试中我执行以下操作:
\n $this->awsS3Service = $this->createMock(AwsS3ServiceInterface::class);\n\n $command = $this->createMock(Command::class);\n /** @var S3Client $s3Client */\n $s3Client = $this->createMock(S3Client::class);\n\n $s3Client->method(\'listObjectsV2\')\n ->willThrowException(new S3Exception(\'VALIDATION ERROR\', $command));\n $s3Client->method(\'putObject\')\n ->willThrowException(new …Run Code Online (Sandbox Code Playgroud) 当我检查方法 in dd(\Auth()::check());case时,它会返回,但是当我使用in方法 in时,它会返回。testLoginTrueLoginTesttruedd(\Auth()::check());testClientCreateFormDisplayedClientTestfalse
那么如何在另一个测试用例中获取登录用户呢?在每个测试用例之前都需要登录用户吗?