Laravel Dusk: how to use in-memory DB for testing - php

What I've been trying is to use in-memory database while testing with Laravel Dusk.
Here we have a file, .env.dusk.local, with the following values.
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
Here is a snippet of a browser testing file.
class ViewOrderTest extends DuskTestCase
{
use DatabaseMigrations;
/** #test */
public function user_can_view_their_order()
{
$order = factory(Order::class)->create();
$this->browse(function (Browser $browser) use ($order) {
$browser->visit('/orders/' . $order->id);
$browser->assertSee('Order ABC'); //Order name
});
}
}
When php artisan dusk is executed, Dusk starts browser testing.
However, Dusk seems to be accessing my local DB, because there is an order name on the testing browser which only exists in my local DB, while 'Order ABC' is expected to be displayed on the browser.
According to the doc, Laravel Dusk allows us to set the environmental variables.
To force Dusk to use its own environment file when running tests, create a .env.dusk.{environment} file in the root of your project. For example, if you will be initiating the dusk command from your local environment, you should create a .env.dusk.local file.
I don't feel that Dusk is accessing the seperate DB.
Any advice will be appreciated.

You can't use :memory: database while Laravel dusk browser testing. Your development server and dusk testing runs on separate processes. dust test cannot access to memory of process running on development server.
Best solution it to create sqlite file database for testing.
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => database_path('sqlite.testing.database'),
'prefix' => '',
],
Create sqlite.testing.database file inside database folder.
Make sure to run development server before running tests using
php artisan serve --env dusk.local

You need a connection in config/database.php
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
Then in your phpunit.xml file use:
<env name="DB_DEFAULT" value="sqlite_testing" />
or in your tests use:
putenv('DB_DEFAULT=sqlite_testing');
Don't forget to use the RefreshDatabase trait to reset the database before each test.

Related

Laravel8 laravelcollective/remote can I create a dynamic connection

I'm migrating our inhouse backup app onto Laravel8, so far so good. I'm interested in using the laravelcollective/remote facade (SSH) to ssh onto the remote server and run some commands which it looks like this would be very good at (rather than using php exec() methods the current backup app uses).
My question however is, can i build an array/object from the database and use these details as a connection without having to manually maintain the config/remote.php file? Maintaining this with any server changes will be a nightmare to maintain as we frequently update users and sites are added removed on a regular basis! any ideas? As mentioned we are storing the ssh creds in the database which is populated via a connection form within the app.
I've built a simple test function in a controller and stepped into this with my debugger. I expected to see the array/object which is created from the config/remote file and was planning to just add new items but i couldn't find any array/objects containing the default empty production config set as default in the config/remote.php file.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use SSH;
class SecureServerController extends Controller
{
public function test() {
SSH::into()->run([
'cd /var/www',
'git pull origin master'
], function($line)
{
echo $line.PHP_EOL;
});
}
}
The following is the route used:
use App\Http\Controllers\SecureServerController;
Route::get('/test', [SecureServerController::class, 'test']);
thanks
*** EDIT ***
SO I had a look at the code for the SSH facade and found I could create a config file and pass this via the connect function:
$config = [
'host' => '123.123.123.123',
'username' => 'the-user',
'password' => 'a-password'
];
SSH::connect($config)->run([
'cd /var/www',
'git pull origin master'
], function($line)
{
echo $line.PHP_EOL;
});
However i see no way to use any port except 22. almost all our servers use a non default port as an additional level of obfuscation.

PHP - Access data for .env laravel

I am starting the program with Laravel and I have in my ".env" file my access data to the server:
USER_SERVER=user
PASSWORD_SERVER=123456789
I have my role in my controller:
public function connectServer()
{
$connection=ssh2_connect(172.17.2.33, 22);
ssh2_auth_password($connection, USER_SERVER, PASSWORD_SERVER);
$stream=ssh2_exec($connection, "df -h");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
}
My role above has problems. What is the correct way to call the user and password of my ".env" file for my function?
You should use the configuration system for these values. You can add to a current configuration file in the config folder or create your own that returns an associative array. We will use the env() helper to pull the values you set in the .env file:
<?php
return [
...
'server' => [
'user' => env('USER_SERVER', 'some default value if you would like'),
'password' => env('PASSWORD_SERVER', ...),
],
...
];
If you added this key to the services.php file in the config folder you would then access this configuration value via the Config facade or the config() helper function:
// if we want a value from the services.php file
Config::get('services.server.user');
config('services.server.user');
// if you want all of them for that server array
$config = config('services.server');
echo $config['user'];
Using the configuration system this way allows you to use the "config caching" that Laravel provides, as you will not have any env() calls in your application, except for in the config files, which can be cached. [When configuration caching is in place it doesn't load the env]
"If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null." - Laravel 7.x Docs - Configuration - Configuration Caching
Laravel 7.x Docs - Configuration - Accessing Configuration Values config()
Laravel 7.x Docs - Configuraiton - Retrieving Environmental Configuration env()

laravel | How to confirm which caching driver is being used?

I am trying to use redis in my application but I am not sure if my app is using redis or file driver as I can't create tags but I can create normal keys fine.
I have set CACHE_DRIVER=redis and also in my cache.php I have:
'default' => env('CACHE_DRIVER', 'redis'),
also in my database.php there is:
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
The reasons for my suspicion are I cannot create tags and running redis-cli flushall under homestead(ssh) does not seem to get rid of the cahce. I had to use Cache::flush() in laravel instead.
So How can I effectively find out which cache driver my application is using?
Its pretty simple , you can use redis cli monitor command to check the get/set are happening or not
redis-cli monitor
And try to run the application .. if u able to see the keys then redis cache is running
u can also check the redis key by following command
redis-cli
Then enter following
keys *
I hope its useful.
You should simply query your Redis DB like so:
redis-cli
Then, being on redis console:
SELECT <DB-NUMBER>
KEYS *
If you see some keys like
1) PREFIX:tag:TAG:key
2) PREFIX:tag:DIFFERENT-TAG:key
it is quite a hint that Laravel is using Redis as its cache backend. Otherwise, take a look at
<YOUR-APP-DIR>/storage/framework/cache
If you find some files/sub-folders in there, it is quite a hint, Laravel is using file-based caching.

Laravel, 'env' in database config

i was configuring my sqlite connection in framework laravel. While using 'php artisan migrate' command i've got a message that there is no connection.
I changed this two lines of my code
'default' => env('DB_CONNECTION', 'sqlite'),
'database' => env('DB_DATABASE', database_path('database.sqlite'))
to
'default' => 'sqlite',
'database' => database_path('database.sqlite'),
Now everything works fine, but my question is what does the ENV function do??
Am i right to delete this?
In Laravel env() is the helper function, which gets the value of an environment variable or returns a default value:
Example:
$env = env('DB_CONNECTION');
// Return a default value if the variable doesn't exist...
$env = env('DB_CONNECTION', 'sqllite');
To give your application a speed boost, you should cache all of your configuration files into a single file using the php artisan config:cache. Which cache the env values as well, so in order to take effect of cached values changes one must have to clear it by using php artisan cache:clear
The env function gets the value of an environment variable from your .env file or returns a default value which is the second argument.
For more information read: Documentation

Laravel not creating log file.

I'm learning Laravel 4.0 to develop a webserver.
I'm using a LAMP stack (Apache 2, php 5.5).
I can't find the log file where Log::error() calls writes to.
As far as I know it's supposed to be to app/storage/logs/log-cli-.txt but there are no files there.
Here is the code:
app/commands/MsgCommand.php
public function fire(){
Log::error('messages - log');
}
It's called from artisan:
app/start/artisan.php
Artisan::add(new MsgCommand());
Am I looking in the right place?
How can I check that this is indeed the right folder (i.e. where is it configured)? To check for faulty installation or setup.
Thanks to marcanuy, I am now sure it is writing to app/storage/logs.
Also I found out it writes fine if I call the command through artisan. Running on apache 2 nothing happens though. I'm starting to think I set up the command wrong.
By default app/storage is the location for log files, storage folder is configured in bootstrap/paths.php
'storage' => __DIR__.'/../app/storage',
Also be sure that this folder is writable by the web server.
The problem was permissions.
User permissions for the www-var user in the ../app/storage
And MySQL settings: Creating a user corresponding to what is set in the app/config/database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'your host',
'database' => 'your db',
'username' => 'your user',
'password' => 'your pass',
),

Categories