Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pain-Free Unit Testing in ExpressionEngine

Stephen Lewis
September 30, 2010

Pain-Free Unit Testing in ExpressionEngine

A guide to using unit testing and TDD to develop ExpressionEngine add-ons, and the reasons why this is a damned fine idea.

Stephen Lewis

September 30, 2010
Tweet

More Decks by Stephen Lewis

Other Decks in Programming

Transcript

  1. class My_math { ! public function sum($a, $b) ! {

    ! ! return $a + $b; ! } } public function test_sum() { ! $math = new My_math(); ! $this->assertEqual($math->sum(3, 4), 7); }
  2. SimpleTest Assert Methods assertTrue assertFalse assertNull assertNotNull assertIsA assertNotA assertEqual

    assertNotEqual assertWithinMargin assertOutsideMargin assertIdentical assertNotIdentical assertReference assertClone assertPattern assertNoPattern expectError assert
  3. class Test_my_math extends Testee_unit_test_base_class { ! ! public function test_sum()

    { $math = new My_math(); $this->assertEqual($math->sum(3, 4), 7); } ! ! }
  4. http://en.wikipedia.org/wiki/unit_testing ...a method by which individual units of source code

    are tested to determine if they are fit for use. individual units
  5. class My_math { public function sum($a, $b) { return $a

    + $b; }! } class My_math_mock {! public function sum() {} }
  6. Config Database DB Forge DB Query Result DB Utilities Extensions

    Functions Input Language Loader Output Session EE2 Mock Objects
  7. public function test_delete_member_by_id() { $model = new My_model(); $member_id =

    '10'; ! ! $this->EE->db->expectOnce( 'delete', array('members', array('member_id' => $member_id) )); $this->EE->db->expectOnce('affected_rows', array()); $this->EE->db->setReturnValue('affected_rows', 1); ! ! $this->assertIdentical( $model->delete_member_by_id($member_id), TRUE); }
  8. http://en.wikipedia.org/wiki/test_driven_development ...a software development process that relies on the repetition

    of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.
  9. Register new member. Confirm registration via email. Pickr runs in

    response to hook. Get Flickr username from database. Get Flickr user ID from API. Get Flickr user information via API. Construct Flickr "buddy icon" URL. Save buddy icon URL to database.
  10. Register new member. Confirm registration via email. Pickr runs in

    response to hook. Get Flickr username from database. Get Flickr user ID from API. Get Flickr user information via API. Construct Flickr "buddy icon" URL. Save buddy icon URL to database.
  11. Standard Approach Problem Unit Testing / TDD Solution Fixed development

    sequence. Unit test individual units of code, independently of each other. Difficult to test thoroughly. Test before code. Impossible to test edge-cases. Mock objects. Lots of up-front decisions. Defer the big decisions. Makes me want to gouge my eyes out with a spoon. Actually rather pleasant.
  12. “Test-Driven Development: By Example” by Kent Beck. If you are

    a genius, you don't need these rules. If you are a dolt, the rules won't help. For the vast majority of us in between, following these… simple rules can lead us to work much more closely to our potential.