Blog

  • Get An Introduction To Functional Programming At TechWeekend 5

    5th edition of the TechWeekend is going to be about Functional Programming and if are hearing this term for first time, or if you are like me who has heard about it, but never got time to look into the details then you should definitely attend it.techweekend

    Event is on 18th Dec 2010, ie this Saturday from 10 AM – 1 PM, at  505, A-Wing, Ground Floor MCCIA Trade Tower( ICC ) on SB Road.

    Here is the quick agenda,

    1. Why you should care about functional programming – by Dhananjay Nene. His id on twitter is @dnene and he currently writes code for and advises Vayana Enterprises in his role as its Chief Architect.
    2. An Introduction to Erlang – by Bhasker Kode. On twitter he is @bosky101 and he is the CEO and Co-Founder of Pune-based Hover Technologies.
    3. Clojure & its solution to the Expression Problem – By Baishampayan Ghose. On Twitter he is @ghoseb, and he is the co-founder & CTO of http://Paisa.com

    Ohh.. and before I forget you need to register( don’t worry it is free) for the event.

    See you guys on Saturday.

  • PHP5 patch for FunctionList plugin of Notepad + +

    As you may know Notepad++ is my preferred development tool for PHP, and two months back I found FunctionList plugin that shows list of function in a opened PHP file, and it increased my productivity almost immediately.

    Only drawback was that it showed just function list and not variables.

    And today I found this neat patch of this plugin by Geoffray Warnants which now makes it even better with icons and also showing variable.

    This is how it looked before

    functionlist-default

    This is how it looks after the patch

    functionlist-php-patched

    Download the patch here (post is in French, scroll down to download the patch)

  • Testing Extjs Application With Selenium : Few Pointers

    On a project that I am working on we needed to create few automated tests using selenium. Our frontend is completely written in Extjs.

    Being new to testing using selenium, I searched the web and here are few useful advices that I found.

    The most comprehensive one was by Ates Goral on stackoverflow.

    The biggest hurdle in testing ExtJS with Selenium is that ExtJS doesn’t render standard HTML elements and the Selenium IDE will naively (and rightfully) generate commands targeted at elements that just act as decor — superfluous elements that help ExtJS with the whole desktop-look-and-feel. Here are a few tips and tricks that I’ve gathered while writing automated Selenium test against an ExtJS app.

    General Tips

    Locating Elements

    When generating Selenium test cases by recording user actions with Selenium IDE on Firefox, Selenium will base the recorded actions on the ids of the HTML elements. However, for most clickable elements, ExtJS uses generated ids like “ext-gen-345” which are likely to change on a subsequent visit to the same page, even if no code changes have been made. After recording user actions for a test, there needs to be a manual effort to go through all such actions that depend on generated ids and to replace them. There are two types of replacements that can be made:

    Replacing an Id Locator with a CSS or XPath Locator

    CSS locators begin with “css=” and XPath locators begin with “//” (the “xpath=” prefix is optional). CSS locators are less verbose and are easier to read and should be preferred over XPath locators. However, there can be cases where XPath locators need to be used because a CSS locator simply can’t cut it.

    Executing JavaScript

    Some elements require more than simple mouse/keyboard interactions due to the complex rendering carried out by ExtJS. For example, a Ext.form.CombBox is not really a <select> element but a text input with a detached drop-down list that’s somewhere at the bottom of the document tree. In order to properly simulate a ComboBox selection, it’s possible to first simulate a click on the drop-down arrow and then to click on the list that appears. However, locating these elements through CSS or XPath locators can be cumbersome. An alternative is to locate the ComoBox component itself and call methods on it to simulate the selection:

    var combo = Ext.getCmp('genderComboBox'); // returns the ComboBox components
    combo.setValue('female'); // set the value
    combo.fireEvent('select'); // because setValue() doesn't trigger the event
    

    In Selenium the runScript command can be used to perform the above operation in a more concise form:

    with (Ext.getCmp('genderComboBox')) { setValue('female'); fireEvent('select'); }
    

    Coping with AJAX and Slow Rendering

    Selenium has “*AndWait” flavors for all commands for waiting for page loads when a user action results in page transitions or reloads. However, since AJAX fetches don’t involve actual page loads, these commands can’t be used for synchronization. The solution is to make use of visual clues like the presence/absence of an AJAX progress indicator or the appearance of rows in a grid, additional components, links etc. For example:

    Command: waitForElementNotPresent
    Target: css=div:contains('Loading...')
    

    Sometimes an element will appear only after a certain amount of time, depending on how fast ExtJS renders components after a user action results in a view change. Instead of using arbitary delays with the pause command, the ideal method is to wait until the element of interest comes within our grasp. For example, to click on an item after waiting for it to appear:

    Command: waitForElementPresent
    Target: css=span:contains('Do the funky thing')
    Command: click
    Target: css=span:contains('Do the funky thing')
    

    Relying on arbitrary pauses is not a good idea since timing differences that result from running the tests in different browsers or on different machines will make the test cases flaky.

    Non-clickable Items

    Some elements can’t be triggered by the click command. It’s because the event listener is actually on the container, watching for mouse events on its child elements, that eventually bubble up to the parent. The tab control is one example. To click on the a tab, you have to simulate a mouseDown event at the tab label:

    Command: mouseDownAt
    Target: css=.x-tab-strip-text:contains('Options')
    Value: 0,0
    

    Field Validation

    Form fields (Ext.form.* components) that have associated regular expressions or vtypes for validation will trigger validation with a certain delay (see the validationDelay property which is set to 250ms by default), after the user enters text or immediately when the field loses focus — or blurs (see thevalidateOnDelay property). In order to trigger field validation after issuing the type Selenium command to enter some text inside a field, you have to do either of the following:

    • Triggering Delayed Validation ExtJS fires off the validation delay timer when the field receives keyup events. To trigger this timer, simply issue a dummy keyup event (it doesn’t matter which key you use as ExtJS ignores it), followed by a short pause that is longer than the validationDelay:
      Command: keyUp
      Target: someTextArea
      Value: x
      Command: pause
      Target: 500
      
    • Triggering Immediate Validation You can inject a blur event into the field to trigger immediate validation:
      Command: runScript
      Target: someComponent.nameTextField.fireEvent("blur")
      

    Checking for Validation Results

    Following validation, you can check for the presence or absence of an error field:

    Command: verifyElementNotPresent   
    Target: //*[@id="nameTextField"]/../*[@class="x-form-invalid-msg" and not(contains(@style, "display: none"))]
    
    Command: verifyElementPresent   
    Target: //*[@id="nameTextField"]/../*[@class="x-form-invalid-msg" and not(contains(@style, "display: none"))]
    

    Note that the “display: none” check is necessary because once an error field is shown and then it needs to be hidden, ExtJS will simply hide error field instead of entirely removing it from the DOM tree.

    Check out PHPCamp a place to share news, views and articles that are useful to PHP community.

    Element-specific Tips

    Clicking an Ext.form.Button

    • Option 1
    • Command: click Target: css=button:contains('Save')
      Selects the button by its caption
    • Option 2
    • Command: click Target: css=#save-options button 
       Selects the button by its id

    Selecting a Value from an Ext.form.ComboBox

    Command: runScript
    Target: with (Ext.getCmp('genderComboBox')) { setValue('female'); fireEvent('select'); }
    

    First sets the value and then explicitly fires the select event in case there are observers.

    Second useful tip was about how to continue to run the test when some test fails, it was by Patrick Lightbody

    try {
        selenium.waitForPageToLoad(timeout);
    } catch (e) {
        // this will happen after 90 seconds
        // todo: recover and send the browser to the the next URL

    Another useful tip that I found was by radu that solved the issues caused by auto-generated id by ExtJS.

    Selenium tests for ExtJS should rely on CSS selectors.

    //table[contains(@class,'seleniumOkButton')]

    Finally the most important and useful is the documentation of Testing_Selenium which lists all the supported functions of selenium RC.

    One more thing version 0.4.4 of Testing_Selenium pear pacakge has a missing function getNumber check this bug report to get that function.

  • Installing PEAR and PHPUnit on WAMP and Windows 7

    In the project that i am currently working on, we decided to use PHPUnit for doing our unit testing, and i found that it was not a straight forward thing to install that I had thought it would be. I had to start by installing Pear, and as soon as i type ‘go-pear’ in command prompt and pressed enter key I got my first error.

    So here are the steps needed to install PEAR and PHPUnit error free on WAMP.

    So let’s start with PEAR, please note my Wampserver is installed on drive ‘H’,  substitute it with your own. Also when this tutorial was written, php was in php5.3.0 folder, please use the path as per your current setup.

    (more…)

  • Simple Way To Add Global Exception Handling In CodeIgniter

    I am working on a project where we needed to capture exceptions at a global level instead of doing it at every step as they were not critical, but important for us to know.

    The idea was that whenever such an exception occur on production we should send an email to developers mailing list so that someone can investigate it.

    As usual I did a quick google search and i found two forum posts in CodeIgniter and one on stackoverflow, but they all fall short as CodeIgniter does not set’s any default exception handlers they way it sets the native error handler.

    So here is a quick tutorial on how you can do that.
    (more…)

  • CodeIgniter 2.0 Is Baking

    Just when I was loosing all hopes about CodeIgniter, yesterday EllisLab announced about their move to assembla and mercurial, in that there was a small but significant news about CodeIgniter 2.0.

    A quick look into the change log revealed

    PHP 4 support is deprecated.  Features new to 2.0.0 may not be support PHP 4, and all legacy features will no longer support PHP 4 as of 2.1.0.

    This is what I was waiting for. What it means, as pointed by Phil Sturgeon and Elliot Haughin, is that CI 2.0 will not run on PHP 4, but more importantly, it can now take full advantages of PHP 5.

    One other thing that I liked is

    Added ability to set "Package" paths - specific paths where the Loader and Config classes should try to look first for a requested file.  This allows distribution of sub-applications with their own libraries, models, config files, etc. in a single "package" directory.  

    Which means that now we can create common area where we can keep helpers, views and libraries that needs to be shared between two applications.

    While there are many more updates these two got me excited.

    For now let’s wait for them to release, meanwhile you can sign up for Bitbucket and follow the updates for CodeIgniter Project.

  • How to get Latitude/Longitude from an address (or Geocoding ) using PHP

    While Google’s documents for maps API does good job in showing how to get lat/long from an address in JavaScript they do not really show any example of doing the same with PHP.

     

    So to make you life simple here is a small script in PHP that does that.

     

     

    $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=573/1,+Jangli+Maharaj+Road,+Deccan+Gymkhana,+Pune,+Maharashtra,+India&sensor=false');
    
    $output= json_decode($geocode);
    
    $lat = $output->results[0]->geometry->location->lat;
    $long = $output->results[0]->geometry->location->lng;

     

    The line above makes a request to Google maps API. Passes the address, and receives the response in JSON format.

    The URL has following options

    http://maps.google.com/maps/api/geocode/output?parameters

    where output can be 1) JSON or 2) XML

    For more details about parameters check out the Google’s geocoding documentation.

  • Backup your WordPress blog to Rackspace Cloud Files

     

    Update :  This plugin is not available any more. You may want to check http://pluginbuddy.com/purchase/backupbuddy/

     

    We all have heard of importance of regular backups, but for a long time I had neglected it for my blog. But recently things happened that prompted me to take backups.

    While I was looking for option to keep backups, I found Rackspace Cloud Files to be a very affordable option, this prompted me to write wpSimpleBackup a WordpPress plug-in that  will take the backups to cloud files, and then I also added support to take backup to Amazon S3 or FTP to another server. But unlike my other plugins, this one is not free.

     

  • PHPCamp is Back

    Preparations are on for second edition of India’s biggest un-conference for PHP developers.

    365418978

    It is going to be held on 9 January 2010 at Symbiosis Centre For Distance Learning, Pune.

    Check out the talks and if you think you can give a better one then please propose your talk.

    If you have not registered yet, then it is the right time to register. Like last year we hope to have a big gathering and lot’s of fun.

  • How To Fix MySQL Error – Error Code 30

    Yesterday was one of those days when things that can go wrong went wrong and today was MySQL day, and problem when accessing any table on a live site resulted in following error

    MySQL Error - Can't create/write to file '/tmp/#sql_7d3f_0.MYI' (Errcode: 30)

    It took me some time to figure out that the error was due to /usr/tmpDSK getting corrupted, and and not really a problem with MySQL or our database as we were thinking all this time, and as usual google came to rescue.

    Here are the steps to fix this for cpanel users…  run the following commands in the order mentioned

      
    /usr/sbin/lsof /tmp
    /bin/umount -l /tmp
    /bin/umount -l /var/tmp
    /bin/rm -fv /usr/tmpDSK
    /scripts/securetmp

    This will create a new /tmp partition for you and the problem will go away. By the way error code 30 means that file system is read only.