From 0cc5df4e95285d0309f33aa3c119b2c3afd182b1 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Sun, 15 Jul 2018 11:10:08 +0200 Subject: [PATCH] TASK: Last changes for first talk --- .gitignore | 5 + .../Repository/FrontendUserRepository.php | 2 +- .../Controller/FrontendUserControllerTest.php | 2 +- composer.json | 4 + readme.rst | 257 +++++++++++++++++- 5 files changed, 256 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 14eb692..f950a50 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,8 @@ index.php typo3 /Tests /Results +infection* +phpunit.xml.dist +debug-log.txt +per-mutator.md +summary-log.txt diff --git a/Classes/Domain/Repository/FrontendUserRepository.php b/Classes/Domain/Repository/FrontendUserRepository.php index 93bfa3f..c143aa4 100644 --- a/Classes/Domain/Repository/FrontendUserRepository.php +++ b/Classes/Domain/Repository/FrontendUserRepository.php @@ -1,5 +1,5 @@ diff --git a/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php b/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php index d3ed019..542116a 100644 --- a/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php +++ b/Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php @@ -1,5 +1,5 @@ diff --git a/composer.json b/composer.json index fc4b5a3..20a6ce8 100644 --- a/composer.json +++ b/composer.json @@ -21,5 +21,9 @@ "typo3/cms": { "web-dir": "web" } + }, + "require-dev": { + "phpunit/phpunit": "^6.5", + "infection/infection": "^0.9.0" } } diff --git a/readme.rst b/readme.rst index 1177024..319bf4a 100644 --- a/readme.rst +++ b/readme.rst @@ -1,23 +1,45 @@ Testing Talk ============ +This is about automated testing, for PHP. + +Everyone is testing already, by hand. This involves: + +* Unit testing + +* Functional testing + +* Acceptance testing + +* Browser testing + +* … + +All of the above is already done by you, so … NO PANIC! + + +This is "The Hitchhiker's Guide to […]" testing. + +Table of contents: + +.. contents:: :local: + +Start +----- + Clean everything:: - rm -rf composer.lock vendor web Tests + rm -rf composer.lock vendor web Tests phpunit.xml.dist infection.json.dist -Install dependencies --------------------- +Install dependencies using composer:: -Using composer:: + composer install --no-dev + +Installation development dependencies using composer:: composer install -PHPUnit Installation --------------------- - -Install phpunit:: - - composer require --dev phpunit/phpunit ^6.5 +This also includes composer, see: composer.json Why 6.x? We use 6.x to support PHP 7.0. @@ -25,6 +47,8 @@ Check installation:: ./vendor/bin/phpunit --version + ./vendor/bin/phpunit Tests/Unit/ + Links: * https://phpunit.de/ @@ -46,6 +70,14 @@ Execute the first test:: ./vendor/bin/phpunit Tests/Unit/ +Execute with colors:: + + ./vendor/bin/phpunit --color Tests/Unit/ + +Execute with info about executed tests:: + + ./vendor/bin/phpunit --color --debug Tests/Unit/ + What's in the test? ------------------- @@ -65,6 +97,44 @@ Create test for controller Introduction to mocking ^^^^^^^^^^^^^^^^^^^^^^^ +What is mocking, or a mock? + + In object-oriented programming, mock objects are simulated objects that mimic the + behavior of real objects in controlled ways. + + A programmer typically creates a mock object to test the behavior of some other + object, in much the same way that a car designer uses a crash test dummy to + simulate the dynamic behavior of a human in vehicle impacts. + + — https://en.wikipedia.org/wiki/Mock_object + +* https://phpunit.de/manual/6.5/en/test-doubles.html + +* https://en.wikipedia.org/wiki/Mock_object + +Example mock: + +.. code-block:: php + + getMockBuilder(ViewInterface::class)->getMock(); + + $viewMock->expects($this->once()) + ->method('assign') + ->with('frontendUser', $frontendUserMock); + } + } + + ?> Add the test ^^^^^^^^^^^^ @@ -77,7 +147,7 @@ We want to test the controller now:: Execute all tests:: - ./vendor/bin/phpunit Tests/Unit/ + ./vendor/bin/phpunit --color --debug Tests/Unit/ Alternative output ------------------ @@ -85,7 +155,7 @@ Alternative output testdox Used as "agile" output:: - ./vendor/bin/phpunit Tests/Unit/ --testdox-html Results/testdox.html + ./vendor/bin/phpunit Tests/Unit/ --color --testdox-html Results/testdox.html xdg-open Results/testdox.html xml @@ -101,3 +171,166 @@ html Coverage Benefits of tests ----------------- + +#. Detect new bugs. + +#. Make sure the same bug does not occur a 2nd time. + +#. Reproduce bug. + +#. Speed up development. + +#. Show how to use the written code. + +#. Allow co-worker, in pull request, to see what you expect. + And how you understood the feature-request. + +#. Write code without working system, by using tests instead. + +#. Allow more secure refactoring. + +Automate test execution +----------------------- + +Existing tests are great. If they are executed. + +Tests which exist are code, if they are not executed, they are dead code. + +Tests costs money, so get the money back by executing tests. + +The easiest way is to have an CI (=Continuous Integration). + +E.g.: + +* Jenkins + +* Travis + +* Gitlab CI + +* Bitbucket Pipelines + +* Bamboo + +* Circle CI + +* … + +See: https://awesomelists.top/#/repos/ciandcd/awesome-ciandcd + +Use `phpunit.xml.dist`:: + + cp Resources/Private/Configs/phpunit.xml.dist phpunit.xml.dist + + ./vendor/bin/phpunit + +Metrics +------- + +Code Coverage +^^^^^^^^^^^^^ + +Most of the time counts only number of executed lines. + +This helps to find untested code, nothing more! +100% covered lines does not mean you are testing all circumstances, +just every line at least once. + +E.g.: + +.. code-block:: php + + + +Will have 100% if all lines are executed, that is even if we do not provide `$var2`. +We have to test the possible cases, not only all lines. + +* https://stackoverflow.com/a/90021/1888377 + +* https://www.martinfowler.com/bliki/TestCoverage.html + +* https://phpunit.de/manual/6.5/en/code-coverage-analysis.html + +Crap +^^^^ + +Is not: + + https://img.devrant.com/devrant/rant/r_1046201_T68wf.jpg + + — https://devrant.com/search?term=code+reviews + + +Is: change risk anti pattern score + Combines complexity and test coverage. + +Different kinds of tests +------------------------ + +* https://stackoverflow.com/a/4145576/1888377 + +* http://www.getlaura.com/testing-unit-vs-integration-vs-regression-vs-acceptance/ + +* https://en.wikipedia.org/wiki/Category:Software_testing + Lists: Acid tests, Unit testing, A/B testing, Acceptance testing, Ad hoc testing, + Agile testing, All-pairs testing, API testing, Black-box testing & White-box + testing, Boundary testing, Cloud testing, Compatibility testing, Component-based + usability testing, … + +Unit Tests +^^^^^^^^^^ + +What we did above. White box test of small pieces of code. + +Functional Tests +^^^^^^^^^^^^^^^^ + +Involves multiple code parts, database, file system and further components, e.g. web +server. + +Acceptance Tests +^^^^^^^^^^^^^^^^ + +Tests from user view, e.g. via browser. + +Mutation testing +^^^^^^^^^^^^^^^^ + +Tests how easy it is to break test:: + + cp Resources/Private/Configs/infection.json.dist infection.json.dist + + ./vendor/bin/infection + +* https://infection.github.io/ + +* https://infection.github.io/guide/mutators.html + +* https://en.wikipedia.org/wiki/Mutation_testing + +Summary +------- + +Start writing tests, small unit tests. + +Automate execution of tests. + +Improve. + +Further reading +--------------- + +* https://phpunit.de/ + +* https://awesomelists.top/#repos/ziadoz/awesome-php + +* https://en.wikipedia.org/wiki/Category:Software_testing + +* Source code of open source projects, like TYPO3: + https://github.com/TYPO3/TYPO3.CMS/tree/master/typo3/sysext/core/Tests