I'd like to test a vallidation rule for passwords that has different requirements depending on the environment you're in: anything goes in 'local', but passwords must be 'strong' in production.
The rule:
class Password implements Rule
{
...
public function passes($attribute, $value)
{
$pattern = app()->environment('local')
? '/.+/'
: '/^(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[‘~!@#$%\^&\*\(\)\-_=\+\{\[\]\}\\`\/])\S*$/';
return (bool)preg_match($pattern, $value);
}
...
I have a test that checks the rule in the testing environment:
class PasswordValidationTest extends TestCase
public function setUp()
{
parent::setUp();
$this->u = factory(\App\User::class)->create();
$this->rule = new Password();
}
/**
* @test
*/
public function validates_strong()
{
$this->assertFalse($this->rule->passes('password', 'testthislongpassowrd'));
}
/**
* @test
*/
public function validates_strong()
{
// Change the environment
$this->assertTrue($this->rule->passes('password', 'test'));
}
}
Now I need to test the rule in the 'local' environment. How can I change the environment returned by app()->environment() for just this test now that putenv has been disabled?
Thanks!