Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"null" driver not settable via env variable #34

Closed
harrygr opened this issue Aug 22, 2016 · 18 comments
Closed

"null" driver not settable via env variable #34

harrygr opened this issue Aug 22, 2016 · 18 comments

Comments

@harrygr
Copy link

harrygr commented Aug 22, 2016

When trying to set the scout engine to the "null" implementation (e.g. say as a fallback for when no env variable exists: 'driver' => env('SCOUT_DRIVER', 'null') or for tests) the string "null" is parsed into a php null value instead resulting in an instantiation error.

Suggest either renaming the null driver so something else like nullsearcher unless there is a way in phpunit.xml and .env it can be set as a string.

@harrygr harrygr changed the title "null" driver not settable "null" driver not settable via env variable Aug 22, 2016
@morloderex
Copy link
Contributor

Use double qoutes on the enviroment variable.

@harrygr
Copy link
Author

harrygr commented Aug 24, 2016

How would one do that in a phpunit.xml file where the values are already between quotes?

<env name="SCOUT_DRIVER" value="null" />

Presumably I could use single quotes to wrap it but I haven't yet tested if it works:

<env name="SCOUT_DRIVER" value='"null"' />

@dwightwatson
Copy link
Contributor

Using double quotes on the environment variable doesn't work. I've been trying to do this to disable Laravel Scout in my test environment. Dotenv must take the string "null" to become null as well.

SCOUT_DRIVER="null"
>>> env('SCOUT_DRIVER');
=> null

My solution in the meantime has been to set the default driver to "null" in the config/scout.php file and then set it to "algolia" through an environment variable in production, but clearly this isn't great.

I suspect this issue will also affect the "null" Broadcast driver.

@talvbansal
Copy link
Contributor

@dwightwatson I've managed to set my driver to null using your method as well as the <env name="SCOUT_DRIVER" value='"null"' /> method too.

However my unit tests are DIRE slow after this:

25seconds -> 7mins

Are slow unit tests something you're seeing too?

It may well be because I'm using the TntSearch driver however I wanted to see if this is a scout issue or their implementation issue. Although using their native implementation not the scout wrapper has no impact on testing speed.

@harrygr
Copy link
Author

harrygr commented Oct 5, 2016

@dwightwatson that's an OK suggestion but there's a problem when, for development, you have it set to "algolia" in your .env phpunit will fall back on that value if no value is set in phpunit.xml. Basically it means that if you have your local driver set to algolia there's no easy way to override that for tests. Will be a fine solution for CI though where you can just omit the driver env variable completely.

@thebrubaker
Copy link

I agree,it would be good to change this "null" option to "none" or "disable" or some equivalent that won't confuse .env to typecasting null from a string to a null value.

@talvbansal
Copy link
Contributor

We've found a way around this that disables the auto indexing by default during unit tests, when we get around to writing the searching unit tests we can re enable it for those particular tests.

I think we can close this issue now?

@dwightwatson
Copy link
Contributor

You've found a solution using the PHPUnit configuration file, but this is still an issue with Scout configuration in general. I don't think this issue should be closed, and I think another option name should be chosen (or, support literal null as being the "null" driver).

@talvbansal
Copy link
Contributor

Ahh totally sorry I thought I was responding to an issue on a different repo RE the scout implementation within that project sorry ignore my post above

@monsterdream
Copy link

monsterdream commented Nov 11, 2016

Hello, I found a solution. You need to setup it in reversed way. Instead in your .env file put

SCOUT_DRIVER=null 

and in config/Scout.php

'driver' => env('SCOUT_DRIVER', 'algolia')

Put in .env

SCOUT_DRIVER=algolia

and in config/Scout.php

'driver' => env('SCOUT_DRIVER', 'null')

Of course then you can just remove it from local environment and it will means that it will use null.

@edcs
Copy link

edcs commented Nov 22, 2016

I've had the same problem in my projects too - 'null' is a silly name to use for anything that goes through .env since Laravel turns it into a null. My workaround to get it to work nicely with PHPUnit is this (will only work on PHP 7.0 and above though):

phpunit.xml

<env name="SCOUT_DRIVER" value="null"/>

config/scout.php

'driver' => env('SCOUT_DRIVER') ?? 'null',

I was considering doing a PR to fix this problem by renaming the 'null' driver to something else. Wondering if it's worth my time though as I don't want to spend a bunch of time on it for to be rejected.

@harrygr
Copy link
Author

harrygr commented Nov 22, 2016

@edcs Your workaround is basically the same as the others suggest. Passing a 2nd argument into env() has the same effect.

This workaround fails however in the case where your local .env file has the driver set to something else (e.g. alogolia). Then, simply omitting SCOUT_DRIVER from phpunit.xml will not mean your tests will use the 'null' driver as laravel will fall back on the .env file and try to use algolia. This means that when you run your tests locally you'll be using the driver you set for development work.

It should be fine for CI though where you tend not to set the SCOUT_DRIVER at all.

@calebporzio
Copy link

Any solution here? It seems crazy that this hasn't been resolved...

@pactode
Copy link

pactode commented Apr 22, 2017

@calebporzio You have to use double quotes on the SCOUT_DRIVER null value. Like this:
<env name="SCOUT_DRIVER" value='"null"'/>

@calebporzio
Copy link

Thanks I think I just removed the reference to it and it defaults to null. Still not happy with the proposed solutions, but Oh well - thanks for the tip!

@c5n8
Copy link

c5n8 commented May 23, 2017

Try add this to your .env file just like what works in phpunit.xml file

SCOUT_DRIVER='"null"'

This way we don't have to 'hack' the scout config file

@mattstauffer
Copy link

This is no longer an issue; works fine after this:

#224

@browner12
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests