Magento API V2 Soap unit testing

Featured Image © flaivoloka@sxc.hu

In previous article, I wrote about setting up environment for Magento unit testing. Let’s expand our tests to test some Magento’s API calls with soap.

Let’s add first tests in our Tests/customerCustomerTest.php

<?php
 
require_once 'PHPUnit/Autoload.php';
require_once '../app/Mage.php';
 
class customerCustomerTest extends PHPUnit_Framework_TestCase {
 
private $local_url_v1 = "http://192.168.1.91/api/soap/?wsdl=1";
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);
   $result = $cli->customerCustomerList($session_id);
 
   $this->assertTrue(is_array($result));
   foreach ($result as $res) {
      $this->assertObjectHasAttribute('customer_id', $res);
   }
 }
 
}

Here we added two test methods: first one id login and it’s neccesary to return session Id for using in other API calls. Also it is neccesary to add mobile user with appropiate roles in administration of your Magento installation.

Hope that this article was useful to you.

Cheers.


3 comments

  1. hello sir,
    in android while i store session in string and use it from other class or method or in other pages then its give me nullpointer exception. so can u make clear that i have to get session every time while i want to call different function from different class and all.

  2. As far as I know, unit test methods shouldn’t be independent from each other? I think the combination of testLogin with testCoreCustomerList_V2 is far away from the best approach.

    1. @Rafael, thanks for your comment. I am almost sure that you can provide a better example when using Unit tests for API and same time re-using this code to be able to make client calls for dumping responses and testing individual API method to not write api client separately from unit tests. Also I am sure that visitors will be thankful if you paste better solution here in comments :-).

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>.