3

I have a Laravel application that uses mysql as its main database and I also use some redis connection on some parts for faster loading of data.

Currently I'm creating unit tests using phpunit in laravel. However the method that I'm trying to create unit test with has redis connection - Redis::connection(). The expected assertion should be a 200 response but because of that redis connection it is returning 500 response. How can I enable redis on php unit so I can create a unit test for it?

my current phpunit.xml are configured like this:

<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>

Here's the error log:

[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()
...
saionachi
  • 397
  • 1
  • 5
  • 21
  • Since there's no redis configuration in your phpunit.xml it will use the default configuration from `config/cache.php` so make sure that is correct as well. – apokryfos Mar 17 '21 at 09:20
  • @apokryfos under config/cache.php here's the redis config..`'redis' => ['driver' => 'redis','connection' => 'cache'],` is it correct? – saionachi Mar 17 '21 at 09:27
  • Oops. I forgot that they moved this some time ago. It's now in `config/databases.php` under `redis`. I would imagine the connection used by default is the one under `default` – apokryfos Mar 17 '21 at 09:28
  • ahh yes it is configured correctly based on its .env values – saionachi Mar 17 '21 at 09:29
  • Are the `.env` values read? I thought they weren't in favour of `phpunit.xml` values – apokryfos Mar 17 '21 at 09:30
  • in normal page load..it was read..but in phpunit it is not being read I think? because it returns 500 response when I'm trying to have redis connection – saionachi Mar 17 '21 at 09:31
  • so may be there's something I need to configure on phpunit.xml to enable redis..that's what I'm asking – saionachi Mar 17 '21 at 09:33
  • Lines in the config like e.g. `env('REDIS_HOST', '127.0.0.1')` that if the environment variable `REDIS_HOST` is set, use that otherwise fallback to `127.0.0.1` basically if the fallback is correct then you don't need to set anything in your environment variables, however in the cases where it's not you need to set the environment variable in `phpunit.xml` like e.g. `` if you aren't using phpredis – apokryfos Mar 17 '21 at 09:35
  • As I said, on normal page load redis was read and redis content was loaded successfully. It means that the config is set and correct, right? its just that when I run the test case I created in phpunit the line `Redis::connection()` causes the 500 response. – saionachi Mar 17 '21 at 09:39
  • could you write the error log stack trace? – Abilogos Mar 22 '21 at 08:49
  • generating in `/storage/log` – Abilogos Mar 22 '21 at 08:50
  • @Abilogos added the error log on the question. It seems like it is not detecting PHP Redis on phpunit but it is working on normal mode – saionachi Mar 29 '21 at 12:19

1 Answers1

2

If your local (the .env work) you should create one .env.testing and do PHPUnit tests using the --env=testing option like it says in docs.

francisco
  • 1,387
  • 2
  • 12
  • 23
  • yes I did this already. but still redis is still not working – saionachi Mar 29 '21 at 12:16
  • I see from log error "Please make sure the PHP Redis extension is installed and enabled". You should follow that problem on this [question ](https://stackoverflow.com/questions/61168156/logicexception-please-make-sure-the-php-redis-extension-is-installed-and-enable) – francisco Mar 29 '21 at 14:30