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);
  });
};
Advertisements

Start learning to become a web developer with the Flatiron School Prework

I ran across theĀ Flatiron School of programming a while back, and I thought it was pretty cool. They have a “pre-work” list of resources for their web development course. I expect it is quite good.

I ran across Flatiron school when I listened to a podcast the founder of the school did. I think it is pretty inspiring.

OK, enjoy learning to program. I’m pretty excited for you!

p.s. This post was based on an email reply I wrote to a reader of this blog who asked me for some resources to start learning Ruby on Rails.

“CSS for Beginners: How to style your website using CSS” – this Saturday

If you’ve been looking to learn how to use CSS, and you’re in Winnipeg, come out to a workshop that will teach you just that, this Saturday.

The workshop is put on by the great people behind Ladies Learning Code, so you can be sure that the atmosphere will be great, and your teachers and peers supportive.

Let me know what you learned if you go.

WinniVote Rspec Feature Tests

I spent several hours today creating the specs for WinniVote’s signup feature.

I feel it is a valuable investment of my time for several reasons:

  • WinniVote’s current tests do not feel high quality, so hopefully my examples help other WinniVote devs.
  • I’ve been thinking about what quality tests look like for a while now, but have not had a chance to get feedback from other developers, so I’m interested what others think about my style.
  • I look forward to the further evolution of my style based on the feedback I might receive.

Working with Laravel, or “Stay out of my way, but be there for me”

I got the opportunity to work with the Laravel framework on a new piece of software we are writing at work.

My favorite part so far is that I’ve managed to stay away from things I don’t yet need, like ORM/migrations and controllers-as-classes, while at the same time things like routing or caching were available for me when I needed them. If you’re interested at peeking at how I am using it, let me know.

I think my ideal framework at this moment would have all the pieces turned off, but available for me to turn on when I decide I want to use them.