PHP: Peridot and Propel

Wanted to share a little configuration I just finished making for some Peridot and Leo -based tests which work with Propel models.

A bunch of these tests write data to the database using the Propel models, and I need a clean slate DB before each test. I also need a clean slate HTTP session. Here’s some code ( from peridot.php ) that uses transactions to achieve that:

class MyScope extends Scope {
  function __construct( $emitter ) {
    $emitter->on( 'peridot.execute', function() {
      $this->conn = Propel::getConnection();
    } );

    $emitter->on( 'test.start', function() {
      self::destroySession();
      // have to rollback and begin here.
      // it was unreliable when I rolled back on test.end
      $this->conn->rollback();
      $this->conn->beginTransaction();
    } );
  }
  private static function destroySession() {
    session_start();
    $_SESSION =[];

    // If it's desired to kill the session, also delete the session cookie.
    // Note: This will destroy the session, and not just the session data!
    if (ini_get("session.use_cookies")) {
      $params = session_get_cookie_params();
      setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
      );
    }

    session_destroy();
  }
}

return function($emitter) {
  $gmsScope = new GmsScope( $emitter );

  //add this scope into every suite
  $emitter->on('suite.start', function($test) use ($gmsScope) {
    $test->getScope()->peridotAddChildScope($gmsScope);
  });
};

Code from TDD Kata 1 session last night

Most attendees reported having fun last night at the TDD Kata 1 session at SkullSpace, the latest Winnipeg PHP UG meetup. 😉

Here’s the code we produced last night, plus a very basic example of how to make functions throw exceptions in PHP that we did not get to last night.

Do you have an implementation I could learn from?

An OO PHP implementation of a collection of cachable functions

I don’t know about you, but I always get a bit excited when I get to use PHP’s magic methods.

Here’s an implementation I did at work for a class which contains multiple functions whose result needs to only be “calculated” every few minutes, but the results could be retrieved many times by different people within that period ( i.e. the functions are “cachable” ).

See it here.

New Version of the ProjectPier Project Management Web Application

I’ve been using the Basecamp project management web app at work for a while, and although I like certain features, I do not want to use that for my freelance work. I mostly do not want to use it because there is an excellent alternative that is very close to Basecamp in it’s functionality: ProjectPier, which presents a long-awaited opportunity for me to work on the code of an Open Source project.

The excellent PHP project management web application ProjectPier saw a new release, version 0.8.6, on December 31st, 2010. There has been a great number of new features added, and now the system is even more like Basecamp.

I’ve contacted the lead developer, and he is interested in learning more about me, and collaborating. The features I am currently most interested in adding are first the things that it does not have and basecamp does. I expect they will all be done in the theme. I plan to then move on to adding some features that I would like to have in a project management tool that we use at Canada’s Web Shop, which will be additions that will require more extensive work, up to and including additions to the database schema.

Should be fun!

New Project Management Tool for my Freelance Work

Bigger Playground for my Website Building Projects

I’ve recently moved my domain, bernardic.ca, to Network Redux web hosting servers. Chris Lowry originally registered the domain for me, in return for some advice I gave him and out of the goodness of his heart. The network redux account is provided by Mark Johnston for free. Mark is my friend and mentor who I met at a call centre I previously worked at. The new hosting allows for more websites to be hosted, so I installed a project management web app written in PHP, called Project Pier.

Professional Project Management Tool

This app provides me with a way to enter projects I am working on, enter associated tasks, and indicate when the tasks have been completed. I can create accounts for clients, so they can keep up to date with the status of the project, add new tasks, and share files. I hope this will improve the communication, as well as act as a reminder of what is left to be done, and it should work well as a central repository of information and files related to a particular project. It is fairly easy to install and use, and since it is written in PHP, I will probably tweak it to my need, and contribute bug fixes & feature enhancements back to the project.

First Blog Post at Canada’s Web Shop

My new employer, Canada’s Web Shop, encourages its employees to blog and tweet through the company’s blog and twitter accounts. They encourage everyone to tweet twice a week, and we take turns blogging weekly. Some people tweet daily, and blog once each week. There is an opening for another weekly blogger, since an employee who was blogging weekly left recently. Is this a good opportunity for me?

Recently, it was my turn to blog, and I wrote an article about PHP development support tools that I like to use.

Mai V2

Mai decided to refresh the look of her portfolio site, and created another clean and crisp design, ready to be implemented into her website.

Mai is a visual designer originally from Japan, currently living in the U.S.A. We met through ProgrammerMeetDesigner.com

I’ve created a WordPress-based site and developed a WordPress theme based on the PSDs she provided.

Technologies used:  PHP, HTML, CSS, JavaScript, jQuery, MySQL, WordPress.

Wordpress theme developed for MaiKSDesign.com
Wordpress theme developed for MaiKSDesign.com

Refactoring PHP

Here are some common opportunities for improving the maintainability of code through refactoring that I find when reading code.

  1. Replace Duplicated HTML with Iterating Over an Array

    The purpose of most PHP code is to output HTML that the web server will send back to the browser. If you have a table that has 20 rows, and all those rows have the same structure, but the data in each row is different, you could write it 2 ways:

    1. Duplicated HTML

           <tr>
             <td align="center" valign="middle" class="messageFormTd3">2010-05-04</td>
             <td align="center" valign="middle" class="messageFormTd3">Copenhagen</td>
             <td align="left" valign="top" class="messageFormTd3">City Hall</td>
             <td align="center" class="messageFormTd5">10:15</td>
           </tr>
           <tr>
             <td align="center" valign="middle" class="messageFormTd3">2010-05-02</td>
             <td align="center" valign="middle" class="messageFormTd3">Zagreb</td>
             <td align="left" valign="top" class="messageFormTd3">Main Square</td>
             <td align="center" class="messageFormTd5">12:30</td>
           </tr>
    2. Iterating Over an Array

                 <?php foreach (array(
                   array('date' => '2010-05-04', 'city' => 'Copenhagen', 'venue' => 'City Hall', 'time' => '10:15'),
                   array('date' => '2010-05-02', 'city' => 'Zagreb', 'venue' => 'Main Square', 'time' => '12:30'),
                 ) as $event) { ?>
                 <tr>
                   <td align="center" valign="middle" class="messageFormTd3"><?php echo $event['date'] ?></td>
                   <td align="center" valign="middle" class="messageFormTd3"><?php echo $event['city'] ?></td>
                   <td align="left" valign="top" class="messageFormTd3"><?php echo $event['venue'] ?></td>
                   <td align="center" class="messageFormTd5"><?php echo $event['time'] ?></td>
                 </tr>
                 <?php } ?>