gpt4 book ai didi

laravel - 如何在 Laravel 的 PHPUnit 中启用 Redis 连接

转载 作者:行者123 更新时间:2023-12-05 04:51:56 33 4
gpt4 key购买 nike

我有一个 Laravel 应用程序,它使用 mysql 作为其主要数据库,我还在某些部分使用了一些 redis 连接以更快地加载数据。

目前我正在 laravel 中使用 phpunit 创建单元测试。但是,我尝试创建单元测试的方法具有 redis 连接 - Redis::connection()。预期断言应该是 200 响应,但由于该 redis 连接,它返回 500 响应。如何在 php 单元上启用 redis,以便为其创建单元测试?

我当前的 phpunit.xml 配置如下:

<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="mysql"/>
<server name="DB_DATABASE" value="MY_DB_NAME"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
</php>

这是错误日志:

[2021-03-29 12:12:53] testing.ERROR: Please make sure the PHP Redis extension is installed and enabled. {"userId":84,"exception":"[object] (LogicException(code: 0): Please make sure the PHP Redis extension is installed and enabled. at /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:77)
[stacktrace]
#0 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Support/helpers.php(433): Illuminate\\Redis\\Connectors\\PhpRedisConnector->Illuminate\\Redis\\Connectors\\{closure}(Object(Illuminate\\Support\\Facades\\Redis))
#1 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php(105): tap(Object(Illuminate\\Support\\Facades\\Redis), Object(Closure))
#2 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php(28): Illuminate\\Redis\\Connectors\\PhpRedisConnector->createClient(Array)
#3 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php(32): Illuminate\\Redis\\Connectors\\PhpRedisConnector->Illuminate\\Redis\\Connectors\\{closure}()
#4 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Redis/RedisManager.php(110): Illuminate\\Redis\\Connectors\\PhpRedisConnector->connect(Array, Array)
#5 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Redis/RedisManager.php(90): Illuminate\\Redis\\RedisManager->resolve('default')
#6 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Illuminate\\Redis\\RedisManager->connection()
#7 /home/johndoe/bo2-dev/app/Http/Controllers/DataContentOfficerController.php(419): Illuminate\\Support\\Facades\\Facade::__callStatic('connection', Array)
#8 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\DataContentOfficerController->manual_qc_confirm()
#9 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction('manual_qc_confi...', Array)
#10 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php(239): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Http\\Controllers\\DataContentOfficerController), 'manual_qc_confi...')
#11 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php(196): Illuminate\\Routing\\Route->runController()
#12 /home/johndoe/bo2-dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php(685): Illuminate\\Routing\\Route->run()
...

最佳答案

如果您的本地(.env 工作)您应该创建一个 .env.testing 并使用 --env=testing 进行 PHPUnit 测试code> 选项,如 docs 中所说.

关于laravel - 如何在 Laravel 的 PHPUnit 中启用 Redis 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66670110/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com