Top Banner
Got units? Bengaluru, 20-22 November 2011
44

Got units? @ Osidays 2011 India 11-20-2011

May 08, 2015

Download

Technology

A TDD tutorial on implementing Dijkstra's shortest path algorithm in PHP with test-driven development and PHPUnit.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Got units? @ Osidays 2011 India 11-20-2011

Got units?Bengaluru, 20-22 November 2011

Page 2: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

TDD

Page 3: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

REDadd a test, it won't pass

Page 4: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

GREENimplement the necessary code to make the test pass

Page 5: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

REFACTORpolish the code

Page 6: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

TDD-ing Dijkstra'sshortest path algorithm

in PHP

Page 7: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 8: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 9: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 10: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 11: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 12: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 13: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 14: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 15: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Page 16: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

github.com/odino/osidays

Page 17: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

git checkout $step

Page 18: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 0/1● create the directory structure● add Symfony2 classloader as submodule● init and update submodule● (optional) create an empty class

Page 19: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 2● write the test with the smallest

amount of code you think it'snecessary

Page 20: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 3● add the Vertex class● add the Graph class● add an empty solve() method

for the algorithm class

Page 21: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 4● implement the solve method

Page 22: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 5● added all the methods

needed by the Dijkstra::solve()● need to implement Graph::calculatePotentials()

Page 23: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 6● a test is added for the Graph class

Page 24: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 7● calculatePotentials() is tested● half of our test-suite passes● switch back to the Dijkstra's test

Page 25: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 8● minor tweaks to make all the tests pass

Page 26: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 9● potentials are assigned in the Graph class

but they should belong to the algorithm,what about refactoring the code?

● since we move the only tested method of Graphinto Dijkstra's class, we can delete the GraphTest

Page 27: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 10● since we like to be OO, we can connect

vertices through objects, and notarrays anymore

Page 28: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 11● we now need to fix the code which

uses vertices' connections as arrays

Page 29: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 12● the library seems pretty complete● PHPDoc is added

Page 30: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 13● Oooops, we forgot to test a scenario:

in Dijkstra's algorithm we need verticesconnected by positive distance

● a test is added, verifying an exceptionis raised through annotations

Page 31: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 14● implementation

Page 32: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 15● enter Mocking objects

Page 33: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 16● Dijkstra::getGraph() seems to be useless

so we probably don't need any Graph class

Page 34: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 17● removed Graph class

Page 35: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 18● some tests rely on it, so we need to

eliminate old references to Graph

Page 36: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 19● enter Data Providers

Page 37: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 20● what about calculating the path between

not-connected vertices?It should return null, so code is refactored

Page 38: Got units? @ Osidays 2011 India 11-20-2011

Bengaluru, 20-22 November 2011

Step 21● Dijkstra::solve() seems to do too

much things, so we split the method● first demand potentials' calculation● second demand path finding● third demand raising the negative-potentials'

exception● enter phploc● enter code-coverage

Page 39: Got units? @ Osidays 2011 India 11-20-2011

Alessandro Nadalin

Bengaluru, 20-22 November 2011

Page 40: Got units? @ Osidays 2011 India 11-20-2011

odino.org

Bengaluru, 20-22 November 2011

Page 41: Got units? @ Osidays 2011 India 11-20-2011

a

Bengaluru, 20-22 November 2011

Page 42: Got units? @ Osidays 2011 India 11-20-2011

a

Bengaluru, 20-22 November 2011

@_odino_ #osidays

Page 43: Got units? @ Osidays 2011 India 11-20-2011

a

Bengaluru, 20-22 November 2011

REST in peace: tomorrow, 12.45

Page 44: Got units? @ Osidays 2011 India 11-20-2011

a

Bengaluru, 20-22 November 2011

@_odino_

Thank YOU!