Magento API V2 Soap unit testing

Magento API V2 Soap unit testing © 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.

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

Adding gallery tab to a CMS page Antun Martinovic
Antun Martinovic, | 5

Adding gallery tab to a CMS page

Implementing javascript minifier Tomislav Nikcevski
Tomislav Nikcevski, | 5

Implementing javascript minifier

Introduction to Magento REST and oAuth Darko Goles
Darko Goles, | 11

Introduction to Magento REST and oAuth

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

Tell us about your project

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