Magento with xDebug, web services API and testUnit

Magento with xDebug, web services API and testUnit © Merreper@sxc.hu

Using xDebug from NetBeans is pretty straightforward and after installing xDebug from their website does not require some special configuration in order to make it work.
If you visit the link above, you will notice that it’s enough to paste the source code of your phpinfo() and you will get directions what xDebug binary to download and how to set it.

Assuming you did it already, let’s make it work in our NetBeans IDE.
Open NetBeans and go to ‘Tools/Options’ and click on ‘PHP’ tab.
On ‘General’ tab inside, you will find options for xDebug configuration inside Netbeans. I just left all values predefined.

You have few options there to choose, and choose what you like, but make sure that port in which xDebug is set to run in php.ini is the same entered here.

Now, let’s test if xDebug is working. Assuming that you already have your Magento project opened in editor, just hit ‘Ctrl+F5’ to start debugging session. What should happen is that your browser window should be opened wit URL like this:

http://yourLocalMagentoUrl/index.php?XDEBUG_SESSION_START=netbeans-xdebug

you can notice XDEBUG_SESSION START parameter inside URL.
If you did all like I wrote and noting like this happens, then try to reconfigure your xDebug and/or NetBeans to make it work properly.

If you read my last article, you will find some example how to call Magento V2 Soap web services from test unit with SoapClient.

So, what’s the current problem?

If you set some breakpoints inside your code that will be executed, debugger will stop there and you can debug your or Magento/’s source code, look at variables window etc, but what will happen if we need to debug server- side of Magento’s web services or our extension to API V2 and all that from out test unit where SoapClient calls are implemented.

How to tell to debugger to start the session and handle web service calls on server – side?

To do so, the easier way I found is:

1. Start debugging with ‘Ctrl+F5’ and browser window opens …
2. Leave that browser window open and add XDEBUG_SESSION cookie to your soap client inside your unit test:

<?php
 
require_once 'PHPUnit/Autoload.php';
require_once '../app/Mage.php';
 
class customerCustomerTest extends PHPUnit_Framework_TestCase {
 
    private $local_url_v2 = "http://192.168.1.91/api/v2_soap/?wsdl=1";
 
    private $api_url_v1;
    private $api_url_v2;
 
    public function setUp() {
        Mage::app('default');
 
        $this->setApiUrlV2($this->local_url_v2);
 
    }
 
    public function getApiUrlV2() {
        return $this->api_url_v2;
    }
 
    public function setApiUrlV2($api_url_v2) {
        $this->api_url_v2 = $api_url_v2;
    }
 
    public function testLogin() {
        $cli = new SoapClient($this->api_url_v2);
 
        $username = 'mobile';
        $password = 'mobile123';
        $result = $cli->login($username, $password);
        $session_id = isset($result) ? $result : null;
 
        $this->assertNotNull($session_id);
        return $session_id;
    }
 
    public function testCoreCustomerList_V2() {
 
        $session_id = $this->testLogin();
        $cli = new SoapClient($this->api_url_v2);
        $cli->__setCookie('XDEBUG_SESSION', 'netbeans-xdebug');
        $result = $cli->customerCustomerList($session_id);
 
        $this->assertTrue(is_array($result));
        foreach ($result as $res) {
            $this->assertObjectHasAttribute('customer_id', $res);
        }
    }

Now set the breakpoints inside reachable code and run your tests with ‘Alt+F6’.

Your debugger will stop at your and debugging can start ….. 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

How to generate SSH keys for Git authorization Hrvoje Ivancic
, | 21

How to generate SSH keys for Git authorization

(Not only) Magento .gitignore generator Darko Goles
Darko Goles, | 11

(Not only) Magento .gitignore generator

Magento API V2 Soap unit testing Darko Goles
Darko Goles, | 3

Magento API V2 Soap unit testing

5 comments

  1. [code]$cli->__setCookie(‘XDEBUG_SESSION’, ‘netbeans-xdebug’);[/code]
    is this the key thing for the xdebug to work with SOAP?

  2. Nevermind, just added the string “‘?XDEBUG_SESSION_START=PHPSTORM'” to URL, and now it works 🙂

  3. However, I don’t know how to debug REST API calls. I tried to pass custom header to the fetch method, but for some reason that doesn’t work.

    Have you tried to debug REST with xDebug?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.