February 2010

Setup PHPunit for testing Zend Framework application

Setup PHPunit for testing Zend Framework application

This was how I set up PHPUnit with my Zend Framework application. I'm very new to testing and have never set it up before.

<

ol>

  • Need to get PHPunit installed on the Apple Mac
    Try typing this in the command line (btw I prefer iTerm over the built in terminal).
    whereis phpunit
  • I tried a few different ways to install php, I'm not sure which one eventually installed it, but.... yeah I know sounds dumb, but when you try a bunch of different ways and then it 'just starts working' you lose track which way actually fixed it. Besides it was two weeks ago, I can't remember that far back. I do know that one of the has Xdebug turned on to generate the nice reports of code coverage.

    For example.

    Shane:tests shane$ whereis phpunit
    /usr/bin/phpunit
    Shane:tests shane$ phpunit --version
    PHPUnit 3.4.6 by Sebastian Bergmann.
     
    Shane:tests shane$ 

  • Now setup your environment. This is the hard part, since everyone does it different. The trouble is figuring out which pieces need to go where and why.
  • Bonus
    This will create a neat little tree of your folder structure

    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/' 

    So I have this structure for my application

       |-application
       |---configs
       |---controllers
       |---models
       |-----DbTable
       |---views
       |-----helpers
       |-----scripts
       |-------error
       |-------index
       |-library
       |-public
       |-tests
       |---application
       |-----controllers
       |-----models
       |---library
       |---log
       |-----report

    Notice the "test" folder with subfolders? Good.

    We now have the necessary folder, here are files

    tests/phpunit.xml

    <phpunit bootstrap="./bootstrap.php" color="true">
        <testsuite name="My Test Suite">
            <directory>./</directory>
        </testsuite>
     
        <filter>

    Working with SVN branches

    Working with SVN branches

    To keep all may changes different from the main development I've created a subversion branch. Now I want to keep the branch up to date with changes from the main trunk.

    Get all the history of this particular branch.

    svn log --stop-on-copy
    ------------------------------------------------------------------------
    r421 | sstillwell | 2010-01-22 18:40:42 -0600 (Fri, 22 Jan 2010) | 1 line
     
    merged changes from trunk to branch, 406:420
    ------------------------------------------------------------------------
    r420 | sstillwell | 2010-01-22 18:30:52 -0600 (Fri, 22 Jan 2010) | 1 line

    Italic fonts in Firefox Interface [solved]

    Italic fonts in Firefox Interface [solved]

    A coworker had this problem. After installing a plugin-update, the fonts on her Firefox interface were all italic. We combed over the preferences to no avail, then a simple Google search revealed the answer.

    1. Open Font Book
    2. Find Lucida Grand Regular
    3. Right Click > Reveal in Finder
    4. Delete it. AAHHHHH!!!! Yes, I'm sure, delete it,
    5. Witness the true power of a fully operational Mac Station, it suddenly recreates the font for you. Amazing

    Now of course you need to restart Firefox, and everything was cool.

    Credit:

    Unlocking iPhone 2G for Apple Firmware 3.1.2

    Unlocking iPhone 2G for Apple Firmware 3.1.2

    Here are some instructions for unlocking the Apple iPhone 2G model with firmware version 3.1.2. I ran into a few problems, but having unlocked dozens of these from day one, I've yet to brick one.

    <

    ol>

  • Download the firmware version from apple. You want the 3.1.2 version 250MB
  • Connect the phone to iTunes
  • Hold down the OPTION button and click the "Restore" button. This will prompt you for a firmware file, located the file you just downloaded
  • Sometimes the upgrade gives an error message after its done, I've yet to have a problem so you can safely ignore it if you get one.
  • Download blackra1n by geohot
  • Unzip and run blackra1n with the iPHone plugged into the computer. It only takes a minute and after it reboots the iPhone will be jailbroken with a new app on the springboard
    IMG_0202.PNG
  • Run blackra1n on the iPhone and install Cydia
  • Run Cydia... it will update it self several times, you just want graphical mode and essential updates
  • When this is done you'll want to install BootNeuter using Cydia
  • BootNeuter2.png

  • Run the BootNeuter app and you'll see this screen, then click 'flash'. When I performed this, the phone seemed to freeze. I tried rebooting the phone, but it became 100% unresponsive. After 5 minutes it rebooted and it seems it worked.
  • bootneuter.png

    Zend Framework Pagination

    Zend Framework Pagination

    This is a way to have Zend Framework Pagination (Paginator) working so that it remembers the query across mulitiple pages. This seemed like such a simple and obvious task, but the solution is not so obvious. The trick is to test if 'isPost()' then save the query to the session variable. That way when a visitor travels to page two we have the query in the session variable. No URL tricks or other tom foolery.

    /IndexController.php

        public function searchAction()
        {
            $value = $this->_request->getPost('query');
     
            // Start a session
            $session = new Zend_Session_Namespace('value');
     
            // If $value is a post then the search has just been submitted.
            if ($this->getRequest()->isPost()) {
                $session->value = $value;
            }
     
            // Get the select from Zend_DB
            $amazon = new Model_Example();
            $result = $amazon->searchExample($session->value);
     
            // Assign Paginator data to view
            $this->view->paginator = $this->_addPaginator($result);
            $this->view->query = $session->value;
            $this->_helper->viewRenderer('list');
        }
     
        private function _addPaginator($select)
        {
            $page = $this->_getParam('page', 1);
            $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
            $paginator->setItemCountPerPage(self::ItemCountPerPage)
                      ->setCurrentPageNumber($page)
                      ->setPageRange(5);
            return $paginator;
        }

    /layout/scripts/layout.phtml

    <div id="search"><form action="/index/search" method="POST"><input type="text" name="query" id="query" value="<?php echo $this->query; ?>"><input type="submit" name="Submit" value="Submit"></form></div>

    /views/scripts/index/list.phtml

    <?php if($this->paginator): ?>
    <div><?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?></div>
     
    <?php foreach($this->paginator as $item): ?>
    <!-- list your item data -->
    <?php endforeach; ?>
     
    <div><?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?></div>
    <?php endif; ?>

    Credit:
    http://www.zfforums.com/zend-framework-components-13/databases-20/proble...

    Fix Slow SSH login time

    Fix Slow SSH login time

    Interesting that since moving to a new ISP my login times to a particular Linux server is 20-30 seconds. I figured it had to do with reverse DNS somehow. Sure enough....

    /etc/ssh/sshd_config

    UseDNS no

    That made the logins quick again and that makes me HAPPY!

    Credit:
    http://www.netadmintools.com/art605.html

    Drupal Simplied Contact form

    Drupal Simplied Contact form

    A site I'm working on has a simple contact us form in the footer of every page. I didn't want the long drawn out contact form with all the name, subject, email, message fields. Here is what I did.

    Installed the formblock module

    Easy enough, install the module, enable the block so that the contact form now shows in the footer.

    Simply the contact form

    Now we want to hide some of the fields in the contact form.

    In your template.php file, you'll want to add the following lines.

    function basic_theme(){
      return array(
        'contact_mail_page' => array(
          'arguments' => array('form' => NULL)
          )
        );
    }
     
    /**
     *  Hide and autofill some fields in the contact form.
     *
     */
     
    function basic_contact_mail_page($form){
     
      $form['subject']['#value'] = "Website question";
      $form['subject']['#type'] = 'hidden';
      $form['name']['#type'] = 'hidden';
      $form['name']['#value'] = 'Website Customer';
      return drupal_render($form);
    }

    As you can see I'm using the Basic theme from Drupal. If you're using a different theme you'll want to change the word basic to your own theme (e.g. blueprint).

    Now you have a simple contact form.

    drupalcontact.png

    Ignoring Files and Folders in Git

    Ignoring Files and Folders in Git

    Just a hint, it's a lot easier than Subversion. My love for Subversion grows colder each day.

    In your repository root directory you'll want to create a file called .gitignore. In that file you'll want to specify files and folders to ignore. For example.

    Ignore the entire Magento /media folder.

    media/**/*

    The root repository .gitignore file will act as a global but can be overridden in subdirecties by creating a .gitignorein those directories.

    A Gotcha!
    Empty directories do not get tracked by Git. If you want them to be tracked, they need to have something in them. Usually doing a touch .gitignore is enough to keep the folder tracked.

    This one stumped me for a little bit, anyway, it pays to read.

    Credit:
    http://www.gitready.com/beginner/2009/01/19/ignoring-files.html
    http://github.com/guides/ignore-for-git