got units? @ osidays 2011 india 11-20-2011
DESCRIPTION
A TDD tutorial on implementing Dijkstra's shortest path algorithm in PHP with test-driven development and PHPUnit.TRANSCRIPT
![Page 1: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/1.jpg)
Got units?Bengaluru, 20-22 November 2011
![Page 2: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/2.jpg)
Bengaluru, 20-22 November 2011
TDD
![Page 3: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/3.jpg)
Bengaluru, 20-22 November 2011
REDadd a test, it won't pass
![Page 4: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/4.jpg)
Bengaluru, 20-22 November 2011
GREENimplement the necessary code to make the test pass
![Page 5: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/5.jpg)
Bengaluru, 20-22 November 2011
REFACTORpolish the code
![Page 6: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/6.jpg)
Bengaluru, 20-22 November 2011
TDD-ing Dijkstra'sshortest path algorithm
in PHP
![Page 7: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/7.jpg)
Bengaluru, 20-22 November 2011
![Page 8: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/8.jpg)
Bengaluru, 20-22 November 2011
![Page 9: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/9.jpg)
Bengaluru, 20-22 November 2011
![Page 10: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/10.jpg)
Bengaluru, 20-22 November 2011
![Page 11: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/11.jpg)
Bengaluru, 20-22 November 2011
![Page 12: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/12.jpg)
Bengaluru, 20-22 November 2011
![Page 13: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/13.jpg)
Bengaluru, 20-22 November 2011
![Page 14: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/14.jpg)
Bengaluru, 20-22 November 2011
![Page 15: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/15.jpg)
Bengaluru, 20-22 November 2011
![Page 16: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/16.jpg)
Bengaluru, 20-22 November 2011
github.com/odino/osidays
![Page 17: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/17.jpg)
Bengaluru, 20-22 November 2011
git checkout $step
![Page 18: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/21.jpg)
Bengaluru, 20-22 November 2011
Step 4● implement the solve method
![Page 22: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/22.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/31.jpg)
Bengaluru, 20-22 November 2011
Step 14● implementation
![Page 32: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/32.jpg)
Bengaluru, 20-22 November 2011
Step 15● enter Mocking objects
![Page 33: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/34.jpg)
Bengaluru, 20-22 November 2011
Step 17● removed Graph class
![Page 35: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/35.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/36.jpg)
Bengaluru, 20-22 November 2011
Step 19● enter Data Providers
![Page 37: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/37.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/38.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/39.jpg)
Alessandro Nadalin
Bengaluru, 20-22 November 2011
![Page 40: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/40.jpg)
odino.org
Bengaluru, 20-22 November 2011
![Page 41: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/41.jpg)
a
Bengaluru, 20-22 November 2011
![Page 42: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/42.jpg)
a
Bengaluru, 20-22 November 2011
@_odino_ #osidays
![Page 43: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/43.jpg)
a
Bengaluru, 20-22 November 2011
REST in peace: tomorrow, 12.45
![Page 44: Got units? @ Osidays 2011 India 11-20-2011](https://reader033.vdocuments.us/reader033/viewer/2022060107/554bbf8eb4c90594278b5094/html5/thumbnails/44.jpg)
a
Bengaluru, 20-22 November 2011
@_odino_
Thank YOU!