Drupal 8: Debugging tests with XDebug and PHPStorm - cennot step through - php

I'm trying to debug my Drupal 8 tests with XDebug, but if I run them with XDebug switched on, I cannot step into the test. I execute the following command:
vendor/bin/phpunit -c core
modules/permissions_by_term/tests/src/Kernel/SelectTermTest.php
PHPUnit reports me here:
"Can't find a source position. Server name 'localhost' doesn't exist."
My settings in the PHP.ini file are looking as follows:
xdebug.remote_enable=true xdebug.profiler_enable=0
xdebug.idekey=PHPSTORM xdebug.max_nesting_level=256
xdebug.remote_autostart=true
Can anybody share here some experience?

I got it to run. I had to do some more settings.
First, create a new PHP Remote Debug under the Run Configurations (The ZeroConfiguration didn't work for me). Define a Server with the name you want (here "TestServer") and enter PHPSTORM as Ide Key.
Also see the Jetbrains documentation: https://confluence.jetbrains.com/display/PhpStorm/Debugging+PHP+Web+Applications+with+Run+Debug+Configurations
Enable the option "Break at first line in PHP scripts" in under the RUN menu .
It can be possible that you need to override the file mapping. (In my case PHPStorm is not able to detect file mapping automatically) (See: https://www.jetbrains.com/help/phpstorm/10.0/override-server-path-mappings-dialog.html). It's is within same dialog where the server was created.
Start the Remote Debug Session with clicking on the corresponding icon.
Then you should be able to run PHPUnit with this command:
PHP_IDE_CONFIG="serverName=TestServer" XDEBUG_CONFIG="idekey=PHPSTORM" vendor/phpunit/phpunit/phpunit -c YOUPHPUNITXMLCONFIG
Set the PHP_IDE_CONFIG corresponding to your settings.
Set breakpoints where you want to stop.
I hope I could help you.
Nico

Related

How to change XDEBUG_CONFIG idekey value in PhpStorm?

I have configured PhpStorm to connect to a remote web server. I am running phpunit for unit tests. I have xdebug running and have created an ssh tunnel for debugging. I know xdebug is running as I am able to click and run a phpunit test (with breakpoint set) by just clicking the run button (after turning on listening for PHP debug connections). However if I instead choose the Debug option in PhpStorm I get connection errors.
Enabling debug messages to the PhpStorm log file I see that the command being sent is:
DEBUG - ellij.ssh.SshConnectionService - Executing SSH command:
cd /[path to project files];
exec env "IDE_PHPUNIT_CUSTOM_LOADER"="/[path to autoloader]/autoload.php" "JETBRAINS_REMOTE_RUN"="1" "XDEBUG_CONFIG"="idekey=17650" /usr/bin/php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9002 -dxdebug.remote_host=[ip address] /[path to phpunit]/phpunit/phpunit --configuration /[path to phpunit xml file]/phpunit.xml --filter '/(::myTestClassMethod)( .*)?$/' 'another method being tested' /[path to unit test file]/myUnitTest.php --teamcity within SshjSshConnection([blah]#[foo])#bar
My php.ini settings are expecting idekey=PHPSTORM but I'm seeing a numeric key in the command line that keeps changing for each attempt.
I'm assuming these two values for idekey need to match. If that's correct how do I do that?
Thanks
They don't need to match. In fact, PhpStorm is just overriding it here so that it can match against what it expects.
I don't understand why it does that, as IDE Keys are (currently) only used in combination with DBGp proxies.
As you don't describe what "connection errors" are, it's not really possible to provide an answer to that part of the question yet.

PhpStorm won't open xdebug profiler snapshot file

=========================================================
Ignore this post. A much simpler statement of this issue can be found at PHP auto_prepend_file causes xdebug profiler to fail
=========================================================
Original Post (and updates)
When I try to open an xdebug profiler snapshot within PhpStorm using:
menu bar >> Tools >> Analyze xdebug profiler snapshot...
I select the file xDebug created but I'm faced with this error:
Error: Incorrect profiler snapshot format: Incorrect name format
I did not rename the snapshot. It is the default name: cachegrind.out.8008
php.ini
<!-- language:lang-none -->
[xdebug]
zend_extension="php_xdebug-2.5.5-5.6-vc11.dll"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.profiler_enable=1
xdebug.profiler_output_dir="C:\websites\xdebug"
; xdebug.profiler_output_name= ; Commented out to allow default name
xdebug.idekey=PHPSTORM
Why won't PhpStorm open the snapshot? Should I set a custom xdebug.profiler_output_name option?
Update
I closed all my projects, upgraded from PhpStorm 2017.1.4 to 2017.2, restarted Apache, deleted the old snapshots and created a new one. The new one opens without trouble.
Update 2
Seems that if I run a file from the command line by calling >php script.php, the generated snapshot can be opened. If I run the same file from the browser (meaning I go through the Apache webserver: localhost/script.php) the snapshot cannot be opened.
Update 3
I also notice that when I execute from the browser, I can't delete the snapshot until I force-stop the Apache process. The PHP file I'm profiling has no instructions: just <?php. Below are the two snapshots created:
When executed from the command line (PhpStorm opens this file without problem):
version: 1
creator: xdebug 2.5.5 (PHP 5.6.1-dev)
cmd: C:\path\to\script.php
part: 1
positions: line
events: Time
fl=(1) C:\path\to\script.php
fn=(1) {main}
summary: 0
1 0
When executed from the browser (PhpStorm cannot open this snapshot):
version: 1
creator: xdebug 2.5.5 (PHP 5.6.1-dev)
cmd: C:\path\to\script.php
part: 1
positions: line
events: Time
fl=(2) C:\path\to\script.php
fn=(1)
summary: 0
1 0
Update 4
The culprit is the auto_prepend_file ini setting. I had the following set in Apache Vhosts:
php_value auto_prepend_file "C:\path\to\init.php"
This file is run before Apache executes the script that was called from the browser. When I run the script from the command line, this setting (which is in my Apache Vhosts config) never takes effect so init.php doesn't run. Conversely, if I move the setting to php.ini where it also affects command-line execution, then snapshots generated from the command-line also stop working.
If I remove the auto_prepend_file setting, the generated profiler snapshot can be open without problem in PhpStorm. Of course this isn't a fix since I need the auto_prepend_file to execute for my application to work properly.

How to set php executable path php.validate.executablePath in vscode when php is set inside docker container?

I have a development environment based in docker.
Everytime that I open VSCode I get this message:
Cannot validate since no PHP executable is set. Use the setting
'php.validate.executablePath' to configure the PHP executable.
Somebody know how to set php.validate.executablePath in this case?
I have no idea how to do it.
Here is a screenshot.
Don't forget to escape \
You don't have to add it to the Path
For linux users:
if you don't have PHP installed then first download, then in terminal type
$ whereis php
and it will show path for php executable (It will be either in /usr/bin/php or usr/local/bin/php) which you can copy from terminal.
In VScode goto settings.json file and paste
php.validate.executablePath: /usr/bin/php
In my case using XAMPP is like this
"php.validate.executablePath": "C:/xampp/php/php.exe"
hope that answer your question
Sure, easy. Just follow what it's says.
Go to File> Preferences > Settings ... it will open the settings.json file.
add the following code:
{"php.validate.executablePath": "Here you put your PHP.exe path"}
You have to know where te PHP.exe file is in your computer, search in the php>bin folder.
Hope it works for you
I had the same problem but found the fix here.
In Windows:
Go To System Properties
Go To Advanced Tab
Click "Environment Variables"
Select Path
Add a new path that points to your php 7 executable:
My case, I'm using Laragon, so you should search where is your php.exe.
By VSCode just add.
"php.executablePath": "C:\\laragon\\bin\\php\\php-7.2.19-Win32-VC15-x64\\php.exe"
on settings.json.
VSCode
Linux Users:
Step 1:
In your terminal type
whereis php
In my case "php: /opt/lampp/bin/php"
Step 2:
{
"php.validate.executablePath": "/opt/lampp/bin/php"
}
Download PHP, in my case, I downloaded from here:
http://windows.php.net/download/
Copy and paste the files to a location on your computer, and set the path:
"php.validate.executablePath": "C:/php/php.exe"
"php.validate.executablePath": "C:/php/php.exe" wont work due to incorect escaping
try this;
"php.validate.executablePath": "C:\\php\\php.exe"
If you get this on windows in VS code click on the blue 'open settings' button. Near the bottom of the screen this opens there's a link to enter the settings, click this to open the settings.json file. Edit the file to look like this:
{
"php.executablePath" : "C:/php-7.4.2-nts-Win32-vc15-x64/php.exe",
"php.validate.executablePath" : "C:/php-7.4.2-nts-Win32-vc15-x64/php.exe"
}
where C:/php-7.4.2-nts-Win32-vc15-x64 is the path to your PHP dir
I don't know if this helps but I ran into the same problem with the extension PHP Intelephense in Visual Studio Code. What I did is read the instructions so the extension could work properly. So I disabled PHP Language Features by searching "#builtin PHP" in the extensions. Also, other (3rd party) PHP extensions that provide similar functionality should also be disabled for best results. Finally, I added glob patterns for non-standard PHP file extensions to the files.associations setting. For example: "files.associations": { "*.module": "PHP" }
Doing this made the error go away. I'm new to PHP too, let me know if this is the correct way of solving this problem with PHP Intelephense.
If you're using VScode and have installed Php through MAMP, the default path should be:
C:/MAMP/bin/php/php7.4.1/php.exe
/ not \
Regarding the docker-specific revision of the question, I found this thread in the VS Code PHP Intellisense git repo issue that explains how to create a wrapper script. It will involve mapping volumes, though, so take that into account.
https://github.com/felixfbecker/vscode-php-intellisense/issues/150#issuecomment-330374124
I have ubuntu and XAMPP.
The following worked for me.
Press CTRL + P
Type settings.json and hit ENTER.
Add the following line in settings.json
{
...
"php.validate.executablePath": "/opt/lampp/bin/php",
...
}
Make sure to proper syntax.
I simply used double forward slashes and it worked, like so:
"php.validate.executablePath": "C:\\xampp\\php\\php.exe",
Unix / Linux / Mac / WSL ,
Step 1 : Find PHP path
whereis php
Response will show you where your php directories are.
user#User:~$ whereis php
php: /usr/bin/php /usr/bin/php8.0 /usr/bin/php8.1 /usr/lib/php /etc/php /usr/share/php8.0-opcache
/usr/share/php8.0-xml /usr/share/php8.1-curl /usr/share/php8.1-opcache /usr/share/php8.0-gd
/usr/share/php7.0-gd /usr/share/php7.0-common /usr/share/php8.0-readline /usr/share/php8.0-mysql
/usr/share/php8.0-zip /usr/share/php8.1-mbstring /usr/share/php8.1-xml /usr/share/php8.0-mbstring
/usr/share/php8.0-common /usr/share/php8.0-curl /usr/share/php8.1-readline /usr/share/php8.1-common
/usr/share/man/man1/php.1.gz
Step 2 : Configure VS-Code
Go to -> "Preference -> Settings"
Search for -> "php path"
Then click -> "Edit in settings.json"
In my case I had multiple.So I chose the one I wanted from the available versions and my VS-Code looked like this
"php.validate.executablePath": "/usr/bin/php8.1",
IF using Laragon...Dont forget to change \ to /
"php.executablePath": "C:/laragon/bin/php/php-7.4.5-Win32-vc15-x64/php.exe",
Go to 'Preference' -> 'Settings'
Search for> php path
Then click Edit in settings.json
Then just put your directory with (DOUBLE Backslash).
Here is an example of mine:
C:\\php\\php.exe
Here is the format:
drive:\\folder_name\\php.exe
This should fix the problem.
php.validate.executablePath in laragon
In my case using laragon is like this
"php.validate.executablePath": "C:\\laragon\\bin\\php\\php-7.4.19-Win32-vc15-x64\\php.exe"
php-7.4.19-Win32-vc15-x64 : Note that this part of your file name may be different
I also had this error. I have installed Laragon and this addition in setting.json worked for me.
"php.executablePath":"C:/laragon/bin/php/php-7.2.19-Win32-VC15-x64/php.exe"
"php.validate.executablePath": "C:\xamp\php\php.exe"
this is working for this case
For Wampserver:
Inside settings.json
"php.validate.executablePath": "C:/wamp64/bin/php/php7.4.26/php.exe"
If php is inside a docker container and you don't need it or don't want to install it on you local system, you can create a php wrapper script like it is suggested here: https://github.com/felixfbecker/vscode-php-intellisense/issues/471
I prefer to use the remote container Microsoft extension on vscode and work inside the container itself.
I've used such solution:
First, create a php file inside /usr/local/bin/ and make it executable:
sudo touch /usr/local/bin/php
sudo chmod +x /usr/local/bin/php
Update content of it with:
docker exec $(docker ps --filter name=-app --format '{{.ID}}') php "$#"
So this will find a container with the name -app at the end. I'm using it along with the docker compose where I have php inside some container which has -app at the end of the name. You can check more about filter here.
And then update php.validate.executablePath with /usr/local/bin/php:
{
"php.validate.executablePath": "/usr/local/bin/php"
}
I also got this error message. The path to php.exe was already set in my System Environment Variables.
The error went away when I commented out "php.validate.executablePath": "c:/path/to/php7.2.10/php.exe", in the settings.json file in VSCode.

phabricator on redhat's openshift

I installed phabricator on openshift using a quickstart from github
https://github.com/CodeBlock/phabricator-openshift-quickstart
I got it running up fine, but I now have two issues:
1.) A setup issue that says apc.stat is enabled and that must be disabled in
/var/lib/openshift/my-user-hash/php/configuration/etc/php.ini
however I cannot access that with sudo command(sudo permission denied), even if I open it normally, I didn't find any apc.stat settings in the php.ini
2.) I can't figure out how set the local path for tracking repositories for diffusion. It says
I must give a path which should be read-writable by phabricator, I tried to give the persistent storage location ..data/ , but it gives me an exception as follows :
Unhandled Exception ("CommandException")
Command failed with error #1!
COMMAND
(cd '../data/' && HOME='/var/lib/openshift/my-user-hash/app-root/runtime/repo/phabricator/support/empty/' git cat-file --batch)
STDOUT
(empty)
STDERR
sh: line 0: cd: ../data/: No such file or directory
how do I fix this?
1) According to Num Duong answer, it seems like u currently could not resolve this issue, probably u should wait for openshift php.ini permissions policy changes.
Anyway this is minor non blocking issue.
2) Modify config file and re-deploy to openshift.
Look for available phabricator options here
U need smth like this: 'repository.default-local-path' => getenv('OPENSHIFT_DATA_DIR'),
P.S. Anyway there is one particular issue with phabricator on openshift that possibly never got resolved: cloning git repositories from OpenShift instances into Phabricator do not
work by SSH, due to permissions regarding the ssh configuration.

$PATH environment variable for apache2 on mac

I am trying to get apache/php to recognize the path to my git. I have been researching and looking in all corners of the web and cannot find how to do this. Basically, no matter what I try, when I run echo phpinfo(); the Apache Environment path does not change from /usr/bin:/bin:/usr/sbin:/sbin. And when I run system('echo $PATH'); in PHP, it reads the same.
System Information:
Mac OSX (Lion)
Apache 2 (running as _www)
PHP 5.3.6
Here is what I have tried editing so far:
/etc/profile
~/.bash_profile
~/.profile
/etc/path
/etc/path.d/{NEW_FILE}
Nothing I have tried so far has changed the $PATH variable. Any ideas?
SOLUTION
So here is the final solution. I edited the
/System/Library/LaunchDaemons/org.apache.httpd.plist
and added
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin</string>
</dict>
You can set the PATH environment variable in /System/Library/LaunchDaemons/org.apache.httpd.plist.
More in the docs.
Did you update the PATH environment variable of user '_www'? Apache will read environment variables from the user runs itself. Or, it looks like you didn't restart apache after updating PATH environment variable.
Check out the older discussion :
How do I add paths to the Apache PATH variable?
Setting environment variables in OS X?
And if you want to modify environment variable in PHP, getenv() and putenv() can be a better choice.
getenv : http://php.net/manual/en/function.getenv.php
putenv : http://www.php.net/manual/en/function.putenv.php
$path = getenv('PATH');
putenv( "PATH=$path:/new_path_that_you_want_to_add" );
Important note for El Capitan (Apologies for the new answer - I don't have enough Rep to comment)
On OSX 10.11, the /System/Library folder is protected, so the files can't be edited.
You need to:
Reboot into Recovery Mode (hold CMD + r after the startup sound)
Once in recovery mode, go to Utilities > Terminal
Run:
csrutil disable
Reboot back into OSX - you should now be able to change the files
Once done, go back to recovery mode and run
csrutil enable
Hope that helps
I created this gist that helped me out from the information above:
https://gist.github.com/srayhunter/5208619
My problem was that PHP was not finding a program that we had installed under /usr/local/bin. Once we did the above it all worked and played nice on mac osx.
for ubuntu server, in /etc/apache2/envvars,
for centos server, in /etc/sysconfig/httpd,
to add:
export PATH=<your php cli path>
and restart apache
A similar problem to what I was having installing Derby. The way I solved it was by opening TextEdit. Select File > Open at this point press Shift + Command + . , this will allow you to view all the documents. Head to the user directory and search for a file called ".profile" . Open it and add the export VARIABLE= Value line for example:
export DERBY_HOME=/opt/local/share/java/derby/
Save the document and restart your terminal to see if the changes went into affect.

Categories