When I run my PHPUnit tests, Xdebug generates a nice code coverage report which shows me exactly how many times each line of code was executed in each of my PHP files.
I want to get the same report for my web site under, say, a week of normal use, so that I can find lines of PHP on my site that might not be used any more. Rather than unit tests running my PHP code, it would be the web server, but I should be able to get the same report, right?
How do I set up Xdebug to collect data and generate a code coverage report on a live site?
I'll answer my own question. Xdebug provides a xdebug_start_code_coverage() call to start collecting code coverage information, and a xdebug_get_code_coverage() call to retrieve information about what code was covered. This is easily applicable to unit testing, because you're generally only concerned with what code was covered by the run of a sequence of tests in a controlled situation. (Turn on coverage, run the tests, then you've got the coverage results.) But for arbitrary hits on a web server application, you'd probably need to start code coverage in the preDispatch and then have the postDispatch write the stats to a database (or store them in some other way) so that later you could collate the results into a report. That's not handled by Xdebug.
Xdebug can collect profiling information in cachegrind format, so I'll see if I can use that to help find what code hasn't been called.
Related
I am fuzzing a PHP web application for vulnerabilities and I want to the source code coverage over a period of time (at certain time intervals) for a certain part of the programme (specific PHP files). For example, after 10 seconds 5% of that source code is touched, after 20 seconds 6%, etc. I have looked into PHPUnit and XDEBUG. I was able to generate a report with PHPUnit, but this only gives me information about how much source code was touched at the end. Does anyone know a tool that can give me periodically information on source code coverage of a PHP application or how to use XDEBUG to extract this kind of information?
I recently started web development. The course I took was to install WAMP and start developing right away. I used an atom text editor, this -combined with wamp- proved to be a very fast way to write client-side code(HTML, CSS, Javascript).
But when I started to write serverside PHP things got a little messy. I should probably explain my site's structure here.
I keep separate PHP, CSS, javascript files for every page on the client side, for the server side a have 2 different types of PHP files:
Files that only perform a specific operation on the database(For example returning "5 more answers"). These are always called by AJAX requests.
Files that load the page for the first time. These are only used when the user opens the page for the first time, they do necessary database queries and return the page. Later requests always go to the 1st type of PHP files.
Now regarding my problem. I debugged until now by printing variables to the screen with var_dump() or echoing. But this started to become too slow as the data I work with grew. I wonder if there is a way of debugging which will let me but a breakpoint in one of my PHP files. Then, when I open it on the browser, on the localhost I created using WAMP, will let me go through the PHP file step by step.
I have been dealing with this issue for 3 days, I tried to make it work with Eclipse IDE but couldn't find a way. Also, there seems to be no tutorials or Q&A on the internet regarding the issue.
Breakpoint debugging opens a whole new world, and is the natural step after var_dump() debugging. Not only does it speed up development, but it provides much more information about your code, as you can step through each line and see what values have been set at each step, and how they evolve as your program executes its code. This means you can track the entirety of the values at different stages with one run - imagine tracking all variables at each point using var_dump()!
Although choosing an IDE is a personal decision based on personal taste, i strongly recommend you try out PhpStorm. If you can get a student licence go for it.
PhpStorm has extensive documentation & tutorials on all features in the IDE, debugging is no exception:
https://www.jetbrains.com/help/phpstorm/configuring-xdebug.html
https://www.youtube.com/watch?v=GokeXqI93x8
I don't know of a specific solution to your issue. I'm not exactly sure what you're doing but as a quick tip, I find add the following snippet to the top of the file useful as it will highly error more easily rather than browser just say nope.
error_reporting(E_ALL);
ini_set('display_errors', 'On');
Hope this help you a bit.
I tried out what's recommended in comments and answers. I first tried Netbeans. To be fair it disappointed me. Download kept getting stuck at 100%, even for different versions. When I stopped downloading and went ahead to create a php project, there was missing parts I guess. I couldn't even manage to create a php project. But that might just be me not being able to do it.
Then I followed #leuquim's answer and #Alex Howansky's comment and downloaded PHPStorm. And I got it to work in no more than 20 minutes. I downloaded it with a student's licence. For people who want to use PHPStorm with WAMP here's a Youtube tutorial:
https://www.youtube.com/watch?v=CxX4vnZFbZU
One thing to note in the video is that, maker of the video chooses PHP Web Application in the Run Configurations. That has been changed to PHP Web Page.
I have been tasked with setting up a website on various environments for different stages of evaluation (dev/test/staging/etc).
On our staging environment however, it seems there is some difference preventing the PHP script from finishing, so the page is never delivered to the browser.
I'm wondering if there is a way I can output to log some sort of stack trace or backtrace upon cutting the connection, or is there some other method to find out what exactly PHP is doing at any given point in the script's life cycle?
It's a Drupal site, so it involves a lot of code I'm not familiar with, and could take hours to sprinkle die; commands throughout to see where the script is loading to.
I understand I should probably be looking at the differences in environments, however all should have very similar configuration (Ubuntu 11.04) and the staging environment seems entirely happy to serve other PHP sites whilst this particular site is refusing to finish. If anything this staging site has more resources available that other environments which are not having problems.
UPDATE: Sorry all, found the problem in the end. The staging environment was on a VLAN that was not permitted to access itself via public IP, and for whatever reason (still confused about this) it was trying to access itself as part of the page load and never completing the request. Setting a hosts file entry for 127.0.0.1 fixed the issue.
Debugging an issue like this step-by-step using a tool like xDebug is an option, but will probably take a long time -- finding where to put the breakpoints is going to be on about the same level as working out where to put die statements around the code. The debugger option is a better way of doing it, but won't save much in comparison, when you have a problem like this where you have an unknown blocker somewhere in large amounts of unknown code.
But xDebug also has a profiler tool which can show you what functions were called during the program run, how long they took, and highlight where the bottlenecks are. This will probably be a better place to start. Just configure xDebug to generate a profiler trace, and then use kCacheGrind to view the trace in a graphical environment.
If your program is getting stuck in a loop or something specific is taking a long time to complete, this will pinpoint the problem almost straight away; you'll be able to see exactly which function is taking the time, and what the call chain looks like to get to it.
It's quite possible that once you've seen that, you'll be able to find the problem just by looking at the relevant code. But if you can't, you can then use xDebug's step-thru debugger to analyse the function as it runs and see what the variables are set to to see why it's looping.
xDebug can be found here: http://www.xdebug.org/
Use xDebug.
Its very easy to install and use.
it has few options like breakpoints and step by step to track status of PHP script before finishes loading
and you can download xDebug from here http://www.xdebug.org/
step by step tutoril for set up xdebug is availble at sachithsays.blogspot.com/
I want to get code coverage for a web site.
I need to do manual test instead of writing code.
The source code of the web site and xdebug, phpunit are on a linux server.now I start apache and open the web site.
I just don't know how to get the coverage of my manual test.
what I expect is this:
Make some config on server
Open my web site and do many manual test
Use some method to get the code coverage
Dose phpunit + xdebug can do this?
I need a report, html is good.
PS. I am a newer... my English and skill is not very well... so Please be patient, Thanks very much
Simply follow all steps in the PHPUnit Manual, Code-Coverage Analysis.
If you want to do code coverage with manual fronted tests. Hava a look at Selenium IDE together with PHP Unit.
I came across this thread on SO which talks about PHP code coverage tools - Code Coverage tools for PHP
I have never worked on PHP and have been writing Selenium UI tests using java against an application which has been written in PHP.
While going through the thread I mentioned above I felt (I might be wrong) that those PHP code coverage tools are to be used when there are unit tests written in PHP Unit and one wants to find how well Unit tests cover the application.
I am looking for a solution where I execute my Selenium tests which are written and java and there would be some hook in PHP application code base which gives some sort of report about application code base which was executed and one which was not.
Is it possible to do this?
Just add an auto-prepend script which calls xdebug_start_code_coverage() and registers a shutdown function which logs the output of xdebug_get_code_coverage()
Then analyse your data later.