Xdebug of Laravel Dusk in Netbeans on Homestead - laravel

In Laravel Homestead, I've been able to use Xdebug for unit tests, feature tests, poking around in the browser, etc.
But it hangs when I try to use Xdebug for Dusk (tests in tests/Browser folder).
I thought these questions might help, but I still haven't gotten it working:
debugging laravel artisan from PHPStorm with homestead
Xdebug laravel artisan commands
I've tried various approaches, including:
export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_connect_back=0 remote_host=10.0.2.2"
php artisan dusk
and
export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_host=10.0.2.2"
php -dxdebug.remote_autostart=on -dxdebug.remote_connect_back=off -dxdebug.remote_host=10.0.2.2 artisan dusk
and more.
I've enabled the "Stop at First Line" debugging option in Netbeans, and Netbeans does successfully stop at the first executable PHP line in the artisan file.
Therefore I think export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_connect_back=0 remote_host=10.0.2.2" is correctly set up.
But after I click the "play" button to allow the code to continue, Netbeans just says "netbeans-xdebug running" in the bottom right while the console just hangs with a cursor flashing under this line: php artisan dusk tests/Browser/ExampleTest.php
How do I need to change my usage of Xdebug for getting it to work in Dusk too?

It doesn't work because Dusk executes the actual PHPUnit test in a separate process, so it doesn't know about XDEBUG_CONFIG.
In principle, Dusk tests still work when you execute them directly (phpunit tests/Browser/ExampleTest.php). The main feature of php artisan dusk are custom .env.dusk[.local] files.
If you don't require that, you can try calling them directly. Then Xdebug should behave the same way it does with all your other PHPUnit tests.

As in my answer here, there is an option of running php -dxdebug.remote_enable=1 -dxdebug.remote_host=10.0.2.2 -dxdebug.remote_port=9000 -dxdebug.remote_handler=dbgp artisan my:command
You can also add those parameters to xdebug.ini like so:
zend_extension_ts = "./php/ext/php_xdebug<-version-number>.dll"
xdebug.remote_enable=1
xdebug.remote_host=10.0.2.2
; Port number must match debugger port number in NetBeans IDE Tools > Options > PHP
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.idekey=netbeans-xdebug (although I think it should work without this too)
Try removing break at first line option. Also try running debug directly from console with those options. And finally check that the correct port is set to listen in NetBeans and xdebug since that could cause problems too.

Related

Sail - Initiate Xdebug session from command line

So the last version of Sails makes it very easy to use Xdebug. Basically just had to define SAIL_XDEBUG_MODE in the .env file, configure path mapping in PhpStorm, activate listening and all was set - it works perfectly from the browser.
Now, how should I go if I want Xdebug to activate when I'm using the command line? Like when using artisan commands for seeding, or even better when using custom artisan commands created to run scripts to update some data... I can't find any arguments to add to my sail artisan myOwnCommand that would tell Xdebug it has to activate.
I'm working on Windows 11 with WSL2.
Thanks ahead!
Thanks to Derick's suggestion, I found out a solution. Prepending a sail call with anything wouldn't help, since sail calls scripts in the docker container, and your environment variable wouldn't be set there. But since it was just about setting an environment variable, it can be easily done in docker-compose file.
I just had to add PHP_IDE_CONFIG: 'serverName=0.0.0.0' in the environment section of my Laravel service. Of course replace 0.0.0.0 with your own server name. Then, instead of running sail artisan test or sail artisan my:command you replace artisan with debug, as stated in the docs.
Now you can use command debug to run with Xdebug (ex. sail debug myOwnCommand).
Here is documentation: https://laravel.com/docs/9.x/sail#xdebug-cli-usage

Laravel Serve command does not respect --env parameter

To be able to run Browser tests directly in my IDE (without using the artisan dusk command), I want to run php artisan serve --env=dusk.local. While it indeed starts the local PHP server, it uses the wrong database. It uses the database specified in .env not the one in .env.dusk.local.
I ran php artisan cache:clear thousands of times, but it doesn't change anything.
Running things like php artisan migrate --env=... works.
Is there a way to achieve my goal without needing to rename my .env.dusk.local file to .env before each test?
This is a bug in Laravel 5.8: https://github.com/laravel/framework/issues/27828
There is currently no solution (other than downgrading to Laravel 5.7).
It has been fixed in the latest release 5.8.7.

Laravel stopped working locally, all artisan commands and composer commands will not work

I have been working on a project for about a month. There have been no major issues. We are about to launch.
An error was reported that a route was not working as intended on staging. I tried to replicate it locally by putting the route into my browser ( but obviously with the local domain path ) and it didnt work. this is the order of events:
that route was causing a permanent redirect. Code looked fine but i noticed the intended get did not seem to be hitting so I put a dd('test'); after it and refreshed and saw the test. So it was bypassing its intended route::get, which was not happening before.
I tried figuring this out, clearing cache and the normal things that could cause this. I then killed the server ( locally ) shut down terminal and restarted it, now everytime I try to run anything it responds with http://localhost
CO!!##!##!##:directoryproject username$ php artisan cache:clear
"http://localhost"
CO!!##!##!##:directoryproject username$ php artisan config:clear
"http://localhost"
CO!!##!##!##:directoryproject username$ php artisan serve
"http://localhost"
CO!!##!##!##:directoryproject username$
i cannot get anything to work. I tried checking out a different branch, nothing. Composer update, install nothing. I keep getting:
Script #php artisan package:discover handling the post-autoload-dump event returned with error code 1
Any help would be greatly appreciated at this point.
You've likely got a dd statement somewhere like dd(request()->getHost()); or similar. Do a recursive search starting in the app's root directory for the pattern dd(. If that doesn't yield anything, start at the very top pulic/index.php and put in dd(__DIR__); statements and attempting to access the app. Then move onto the service providers, http kernel, etc. until you can locate the culprit code.

Laravel-Php artisan serve url (127.0.0.1:8000) vs localhost/laravelproject/public

I want to access my laravel project.I run php artisan serve and access the 127.0.0.1:8000 in browser.
But i learned that I can also check my project using the localhost/laravelproject/public url whithout running php artisan serve.
Question: What is the point of using php artisan serve?
No point in two different methods like you mentioned run laravel by "php artisan serve" and by "project url" followed by localhost. But advantage of "php artisan serve" is you can run you laravel project without putting in htdocs/www directory i.e servers root directory. You can put laravel project anywhere where you want and run through the artisan command.
I found some information you may find interesting:
https://www.quora.com/How-can-I-use-php-artisan-serve
But in simple words, php artisan serve is an easy way to create a php server and that is what laravel needs to run.
You could do the same with "php -S 8080 (which would start a php web server (single threaded) in the current directory on port 8080)"
Also if you have already a php server running with apache or nginx it would not be necessary any of the commands.
Hope you find this helpful.
The `Serve command is just a shortcut for the PHP Builtin Webserver, something PHP has out of the box, so the point of using it is to start testing your application as fast as you could, you just need to install PHP, Composer and your application is up (if you don't need anything else, of course). But if you already have Nginx installed, there is no point at all, just use it.
It's not wise to use the Builtin Webserver in production.

How can I force artisan to serve with local environment?

I tried
php artisan serve --env=local
But its still serving the development site.
Laravel development server started on http://localhost:8000
I know I can edit the app config, but for the project I'm working on I need to switch between the two modes often and thats going to be a pain.
ps. I'm using 4.2
It's a bit hacky, but I got it to work like this.
Put at the very top of artisan
if(in_array('serve', $argv))
if(in_array('--env=local', $argv))
file_put_contents('.isLocal','');
else if(file_exists('.isLocal'))
unlink('.isLocal');
And replace the existing environment detection logic with this
$env = $app->detectEnvironment(array(
'local' => file_exists('.isLocal'),
));
Basically artisan will now create an .isLocal file whenever the --env=local option is set and delete it when its not. Make sure to add .isLocal to your .gitignore

Resources