Uncategorized

Introducing skid: stupid-simple unit testing for PHP

I have a confession to make: I’m tired of PHPUnit. It’s overly complex for what I want to do: assert that a piece of code either passes (returns true) or fails (returns false). $this->assertTrue(/*...*/); is pretty much the only assertion I ever use these days, because it’s a lot easier for me to write $this->assertTrue(array_key_exists($key, $array)); than to try to remember, “Okay, $this->assertKeyExists… no, that’s not it. Um, $this->assertArrayHasKey, got it.” And don’t even get me started on exception testing. How is this…

more expressive or less confusing than this?

So, I wrote my own. I call it skid (find it on github). It’s very simple and rough, but it does what it needs to: pass the script a file and it will run the test methods and tell you if the assertions passed or failed. Some things skid doesn’t do:

  • Class mocks. Just create a simple concrete class that extends the abstract class or interface that you want to test, with as much or little logic as you want in the implemented methods. Voila, you have yourself a mock.
  • Unnecessarily verbose assertion methods. In fact, there’s just one: ASSERT(). The script does a little voodoo so that it can display exactly what code was in the assert, because knowing that a bit of code returned false without an easy way to tell what the code was isn’t very useful.

I wouldn’t really recommend putting skid to any kind of production use (like I said, very simple and rough); I consider it a proof of concept for a better world of unit testing, where unit tests actually HELP you with testing, instead of just adding MORE work to your already overloaded task list.

Maybe the way I look at unit testing is not for everyone. If you have something to add (or maybe if you just think I’m an idiot), use the comment box below!

Reviews

Hello, Trello

Used Trello yet? From the website:

Trello is a collaboration tool that organizes your projects into boards. In one glance, Trello tells you what’s being worked on, who’s working on what, and where something is in a process.

It was launched a couple days ago by Fog Creek Software, and I’ll admit it: I’m sold. I’m not huge on the collaboration/workflow scene, but I was looking for something simple to manage a few ideas I’ve been working on, and Trello came at exactly the right time.

The idea behind the app is solid: add or edit tasks — or cards, in Trello-speak (Trellish?) — in phases (lists) for your project (board), and Trello will magically update them in realtime for anyone else viewing. It’s kind of spooky to see something you just typed appear on three different screens almost simultaneously. And the best part is that it was designed from the ground up to be cross-platform compatible, so any device with a web browser can participate.

That’s the general idea. The first thing you’ll notice on login is that the site still feels very much like a late-cycle beta. Which is fine; I’m a big believer in “launch early and patch often,” just be prepared for some tarnish on the chrome. With that said, Trello knows what it wants to be when it grows up, and provides a very solid core that the devs will be able to expand on. And expand they are; they’ve even set up a Trello Development board and are being very transparent about the development process, allowing users to vote and comment on bugs, ideas and issues — if that’s not meta, I don’t know what is!

In conclusion, Trello is awesome, and rocked my world by being the right tool at the right time and in the right place (let’s be honest, the web is the right place for everything these days). Going forward, there are definitely a list of features I’d like to see to make it more usable — like a better due-date system (currently the highest-voted card on the dev board), a way to filter visible activity on a card (I don’t really want to scroll through 100+ votes to find comments), and so on — but even so, I’m confident that given a little time, Trello will grow out of its awkward teenager phase into the powerhouse that it is promising to be.

So go on! Go check it out. It’s free, as private or public as you want it to be, and it’s simple to get started. And, added bonus, you can sign up with your Google account; one less username and password floating around that you have to remember. Have fun!

Code Snippets

Quick Symfony2 deployment script

I find myself doing the same things a lot: add a vendor to the deps file, write some code, run the vendors install, update database, etc. etc. I got tired of it, so I put together a quick deployment script. It’s not perfect, but it should get you pointed in the right direction. Enjoy!

Rules of Thumb

Rule of Thumb #37: Know your method return types

Do you see anything wrong with this piece of code (aside from the fact that it’s completely out of context)?

Yeah, neither did I. It’s a method from the fantastic FriendsOfSymfony CommentBundle for Symfony2, and it cost me two hours of debugging.

See, the bundle allows for comments to be upvoted and downvoted, but so far there hasn’t been any form of rate-limiting merged into the master branch. One user can spam votes on a comment, completely negating the value of the metric. After a little bit of correspondence with one of the developers in a pull request for a different implementation of the same feature, I decided to create a validation constraint that would make sure a user’s vote was unique based on both user id and comment id.

The cookbook documentation is pretty clear, so it didn’t take long. I fired up a debug session, created a test comment, upvoted once, upvoted twice, confident that nothing would happen… and the upvote registered. Hmm.

I checked the database table; everything seemed to be in order, so I did a couple quick tests to be sure that my validator was failing properly. It was. Even did a quick var_dump(“foo”); die(); to make sure that the validator was actually being called during the validation process (I’m not proud). It was. I begrudgingly set some breakpoints and started stepping through the code line-by-line.

I’ll spare you the gory details, but at some point I realized that even though my ConstraintViolation was being added to the stack and the validator was telling me that it was failing, the code was still proceeding through the bundle’s vote creator as if the validator had passed. I eventually checked the Validator#validate() method and discovered that it wasn’t returning a true/false value as I’d expected: it was returning a ConstraintViolationList collection.

As it turns out, if (!$obj) { /* … / } is very different from if (!$bool) { / … */ }. Very different. Changed the one line of code from a ! logical operator to a count() method call, and the whole thing worked like a charm.

Rule of Thumb #37: Know your method return types. Would’ve saved me a few hours in running down code. On the upside, I now know a lot more about Symfony2’s constraint validation process!

Uncategorized

echo($hello_world);

Hello, world! I am Problematic. It’s true. Well, technically I’m Derek Stobbe, hailing from Provo, UT, and I’ll spare you any more details about my personal life. I am a web developer by trade and a computer scientist at heart. I got started coding (does that need sarcastic quotes around it?) with QBasic on a five-dollar computer purchased at a garage sale, quickly got bored of that (“what do you mean, ‘BASIC doesn’t compile’?”), and moved on to Visual Basic. Not much of an upgrade there; we’ll call those “the dark days.”

I broke into the web development field when a buddy and I decided, right out of highschool, that we wanted to put together a (now long defunct) gaming website. Didn’t know anything about server-side scripting besides what’d I’d picked up from Dreamweaver’s code view (leave me alone, okay?), so we pooled together our resources and put half down to have some guy I found on the internet put something together for us. Cue a long wait. A loooong wait. Or at least, it seemed like it at the time. But seriously, a week would go by, and he would respond to my daily emails asking for updates (yes, I was an irritating client once, too) with a vague promise of, “soon, soon.”

I knew what technologies he was using (mostly because my webhost at the time only supported PHP4 and MySQL), so I started playing around, trying to figure out how to do what I wanted to do. It took forever, but I finally cobbled something together, and it was glorious. Sure, it was horribly designed and hacky and I’m sure riddled with injection holes, but it was mine. I told our guy, “hey don’t worry about it, we don’t need your work after all” and started bragging to all my friends about the website I’d made. Incidentally, about an hour later I got a tarball and an email demanding the other half of his payment. Jerk.

Got a Real Job™ doing web work with my uncle’s company not long after that, and haven’t looked back since. I briefly toyed with the idea of moving into Java or C#, or even C++ for game development (a dream of mine for a long time), but there’s just something about the web that I can’t let go of. Maybe it’s the fact that a well-built and timely web app can go viral in a way that nothing else really can (except for meme pictures and stupid youtube videos, but don’t get me started). Or maybe it’s the delusions of grandeur that I have about running a large and active community. I don’t really know, but whatever it is, I’m in the web market for better or for worse (right now things seem decidedly biased toward for better, but who am I to judge?)

I welcome feedback and user interaction (like I said, my highest fantasy is a healthy web community), so drop me a line at: djstobbe@gmail.com, or use the nifty comment box below! Let’s chat.