exercise 0 - muzinic
TRANSCRIPT
![Page 1: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/1.jpg)
With Docker gitclone<repository>cdphplonghorn-pragmatic-tddmakemaketest
OK(2tests,2assertions)
EXERCISE 0Repository [email protected]:lmuzinic/phplonghorn-pragmatic-tdd.git
Alpine image, vendor dir ~10 MB, you can use your mobile data if WIFI does not work.
With PHP 7.1+ gitclone<repository>cdphplonghorn-pragmatic-tdd/appcomposerinstallvendor/bin/phpunit
OK(2tests,2assertions)
![Page 2: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/2.jpg)
PRAGMATIC TDD
![Page 3: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/3.jpg)
@lmuzinicLuka Muzinic
HELLO
![Page 4: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/4.jpg)
WORKSHOP RULESASK QUESTIONS IF YOU STILL DO NOT UNDERSTAND, ASK QUESTIONS AGAIN DISCUSS RIGHT NOW, DO NOT WAIT FOR THE “RIGHT MOMENT”
![Page 5: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/5.jpg)
WHY WE NEED TESTING?
![Page 6: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/6.jpg)
![Page 7: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/7.jpg)
WHY AM I HERE?
![Page 8: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/8.jpg)
ARE WE SOFTWARE TESTERS?
![Page 9: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/9.jpg)
AND YET WE KEEP ON SAYING WE WRITE TESTS…
![Page 10: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/10.jpg)
WHERE CAN I GET MORE OF THOSE TESTS?
![Page 11: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/11.jpg)
EXCUSES, EXCUSES
TESTS SLOW US DOWNWE WILL NEVER GET TO 100% CODE COVERAGE WE DO NOT HAVE TIME TO LEARN TESTING, WE’RE TO BUSY SHIPPING CODE
![Page 12: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/12.jpg)
EXCUSES, EXCUSESTESTS SLOW US DOWN?
![Page 13: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/13.jpg)
DO YOU OFTEN DO THIS? ~phptest.php
OR THIS http://localhost/test_problem.php
STOP TESTFILEING
![Page 14: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/14.jpg)
EXCUSES, EXCUSESWE WILL NEVER GET TO 100% CODE COVERAGE
![Page 15: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/15.jpg)
CODE COVERAGE
![Page 16: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/16.jpg)
CODE COVERAGE
![Page 17: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/17.jpg)
CODE COVERAGE
![Page 18: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/18.jpg)
CODE COVERAGE
![Page 19: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/19.jpg)
20 > 80
![Page 20: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/20.jpg)
EXCUSES, EXCUSESWE DO NOT HAVE TIME TO LEARN TESTING, WE’RE TO BUSY SHIPPING CODE
![Page 21: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/21.jpg)
//@TODO: STANDSTILL
![Page 22: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/22.jpg)
DON’T BE SCARED OF PHPUNIT*IT IS JUST A CODE RUNNER UNIT, INTEGRATION OR ACCEPTANCE TESTS SMOKE TESTS WEBSITE SCRAPER …
![Page 23: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/23.jpg)
IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?
TESTS ANSWER QUESTIONS
![Page 24: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/24.jpg)
IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?
TESTS ANSWER QUESTIONS
![Page 25: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/25.jpg)
IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?
TESTS ANSWER QUESTIONS
![Page 26: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/26.jpg)
IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?
TESTS ANSWER QUESTIONS
![Page 27: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/27.jpg)
IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?
TESTS ANSWER QUESTIONS
![Page 28: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/28.jpg)
IS MY CODE WORKING CORRECTLY? WILL MY CODE WORK CORRECTLY? HOW IS MY CODE SUPPOSE TO WORK? IS MY CODE DESIGNED WELL? CAN I REFACTOR MY CODE?
TESTS ANSWER QUESTIONS
![Page 29: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/29.jpg)
HOW TO WRITE TESTS?GIVEN WHEN THEN SETUP EXERCISE VERIFY TEARDOWN
![Page 30: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/30.jpg)
TEST CASE ANATOMY class TakeMeOutTest extends TestCase { private $favorites; private $rivals;
public function setUp(): void { $this->favorites = Team::create('Houston Astros'); $this->rivals = Team::create('Texas Rangers'); }
public function testFavoriteTeam() { $this->assertSame('Houston Astros', $this->favorites->getName()); }
public function testNotSoFavoriteTeam() { $this->assertSame('Texas Rangers', $this->rivals->getName()); } }
![Page 31: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/31.jpg)
WORKSHOP RULESASK QUESTIONS IF YOU STILL DO NOT UNDERSTAND, ASK QUESTIONS AGAIN DISCUSS RIGHT NOW, DO NOT WAIT FOR THE “RIGHT MOMENT”
![Page 32: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/32.jpg)
![Page 33: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/33.jpg)
With Docker gitclone<repository>cdphplonghorn-pragmatic-tddmakemaketest
OK(2tests,2assertions)
EXERCISE 0Repository [email protected]:lmuzinic/phplonghorn-pragmatic-tdd.githttps://github.com/lmuzinic/phplonghorn-pragmatic-tdd
With PHP 7.1+ gitclone<repository>cdphplonghorn-pragmatic-tdd/appcomposerinstallvendor/bin/php
OK(2tests,2assertions)
![Page 34: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/34.jpg)
EXERCISE 1BINARY GAPhttps://app.codility.com/programmers/lessons/1-iterations/binary_gap/ Find longest sequence of zeros in binary representation of an integer.
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
How many tests we need to write?
![Page 35: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/35.jpg)
EXERCISE 1 cont.BINARY GAP
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Reuse existing TakeMeOutTest.phpto add more test cases.
![Page 36: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/36.jpg)
SO FAR SO GOOD?THIS IS EXTREMELY SIMPLIFIED EXAMPLE - our applications are a bit bigger and a bit more complicated
![Page 37: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/37.jpg)
BASEBALLSOMEWHAT SIMPLIFIED - two teams, Home team and Away team play a Match - at the end of the game, each team holds a Score
- score consists of Runs, Hits and Errors - winner of the Match is a team that scored more Runs
- winner gets recorded with a Win - looser gets recorded with a Loss
- repeat for desired number of times - best team is one with biggest Percentage (Win/Total number of Matches)
![Page 38: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/38.jpg)
![Page 39: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/39.jpg)
![Page 40: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/40.jpg)
DISCUSSIONIMPLEMENT A BASEBALL LEAGUE MANAGEMENT APP
- We want to display standings table on our website
WHAT IS YOUR BIGGEST CONCERN?
![Page 41: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/41.jpg)
EXERCISE 2START IMPLEMENTING STANDINGS
Write a test for getting sorted standings Talk about domain Implementation
![Page 42: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/42.jpg)
EXERCISE 2 cont.START IMPLEMENTING STANDINGS
Create tests/Domain/League/LeagueTest.php
![Page 43: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/43.jpg)
EXERCISE 3IMPLEMENT TEAM POSITION
An object that will hold position inside the league table Focus just on this class, use --filter
![Page 44: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/44.jpg)
EXERCISE 3 cont.IMPLEMENT TEAM POSITION
Create tests/Domain/Team/PositionTest.php
Possible test casestestGetPercentageWithNoGamestestGetPercentageAfterRecordedWintestGetPercentageAfterRecordedWinAndLoss…
![Page 45: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/45.jpg)
GO TO VACATIONWHO WILL IMPLEMENT STANDINGS?
Your colleagues that stayed in the office. Checkout branch step-2
![Page 46: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/46.jpg)
EXERCISE 4REFACTOR
Move checking, creating and returning a team Position into separate method Move formatting into League Move storing matches to repository
![Page 47: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/47.jpg)
SO FAR SO GOOD?REPOSITORIES ARE USUALLY SLOW
They make a costly database call. Add sleep(1) to each method and run the test suite again.
![Page 48: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/48.jpg)
EXERCISE 5MOCK THE REPOSITORY INSIDE LeagueTest.php
$this->matchRepository = $this ->getMockBuilder(MatchRepository::class) ->disableOriginalConstructor() ->getMock();
INRODUCE STUBS $this->matchRepository->method('findAll')->willReturn([$match]);
INTRODUCE SPIES $this->matchRepository->expects($this->atLeastOnce())->method('save');
SPLIT INTO SEPARATE TEST CLASSES OR METHODS
![Page 49: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/49.jpg)
SO FAR SO GOOD?THERE WERE FEW BUGS
- someone entered a Team with no name - someone created a Match between same Team
![Page 50: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/50.jpg)
EXERCISE 6EXCEPTIONS
- Match between same Team - Team with empty name
![Page 51: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/51.jpg)
EXERCISE 6 cont.EXCEPTIONS
- Match between same Team - Team with empty name
Introduce src/Domain/Exception/BallGameException.phpsrc/Domain/Exception/MatchBetweenSameTeamException.phpsrc/Domain/Exception/TeamWithEmptyNameException.php
Test with @expectedException\BallGame\Domain\Exception\BallGameException@expectedException\BallGame\Domain\Exception\MatchBetweenSameTeamException@expectedException\BallGame\Domain\Exception\TeamWithEmptyNameException
![Page 52: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/52.jpg)
EXERCISE 7CODECOVERAGE & CRAP
Run codecoverage ~maketest~vendor/bin/phpunit--coverage-htmlvar/coverage
Explain CRAP CRAP = CC2 x U3 + CC
![Page 53: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/53.jpg)
SO FAR SO GOOD?THERE WAS THIS ONE EDGE CASE
- two teams were tied at the end of the season, they had an equal percentage and our system could not decide who should be first
- we should do something about it for next season
![Page 54: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/54.jpg)
EXERCISE 8NEXT YEAR FEATURES
League manager wants to change the rules for scoring - teams that have equal percentage, sort by wins (more wins moves up). Keep the possibility to show scoring for last year as well.
![Page 55: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/55.jpg)
EXERCISE 8 cont.SIMPLE RULEBOOK
Introduce src/Domain/RuleBook/RuleBookInterface.php
Introduce method decide(TeamPosition$teamA,TeamPosition$teamB)
Extract sorting logic into separate class, cover all cases with tests. tests/Domain/RuleBook/SimpleRuleBookTest.php
Test cases testDecideReturnsLessThanZeroWhenFirstTeamHasMorePointsThanSecondtestDecideReturnsGreaterThanZeroWhenSecondTeamHasMorePointsThanFirsttestDecideReturnsZeroWhenTeamsHaveEqualPoints
![Page 56: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/56.jpg)
EXERCISE 9ADVANCED RULEBOOK
Write the test. Write the implementation. Push the both rulebooks into standings tests.
![Page 57: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/57.jpg)
EXERCISE 9 cont.ADVANCED RULEBOOK
Write the test. Write the implementation. Push the both rulebooks into standings tests.
Tests tests/Domain/Standings/StandingsWithSimpleRuleBookTest.phptests/Domain/Standings/StandingsWithAdvancedRuleBookTest.php
Implement scenarios
![Page 58: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/58.jpg)
WORKSHOP RECAP
![Page 59: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/59.jpg)
@lmuzinic luka.muzinic.net
luka.muzinic.net/pdf/longhorn-pragmatic-tdd.pdf
Luka Muzinic
QUESTIONS? /r/AMA?
![Page 60: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/60.jpg)
READING & LIBRARIESReading list https://www.devmynd.com/blog/five-factor-testing/ https://martinfowler.com/articles/practical-test-pyramid.html https://dev.to/theobendixson/the-problem-that-unit-tests-solve-b2l https://blog.liplex.de/testing-private-and-protected-methods-with-phpunit/
Libraries https://github.com/sebastianbergmann/phpunit https://github.com/phpspec/phpspec https://github.com/Codeception/Codeception https://github.com/phpstan/phpstan https://github.com/infection/infection
![Page 61: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/61.jpg)
HOMEWORKCOMPLETE EXERCISES - do Exercise 1 - find more on Codility, CodeWars, Advent of code, Exercism - follow along/google/ask
STATISTICS - extend results table, add GB, Home, Away, L5
![Page 62: EXERCISE 0 - Muzinic](https://reader034.vdocuments.us/reader034/viewer/2022042302/625a6e955fa04d260068ea5f/html5/thumbnails/62.jpg)
Luka Muzinic
THANK YOU
Photos by Les Anderson, Joshua Earle, Ian Espinosa and Tom Roberts on Unsplash
@lmuzinic luka.muzinic.net
luka.muzinic.net/pdf/longhorn-pragmatic-tdd.pdf