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

Dot all 2019 | Testing with Craft | Giel Tettelaar

Giel Tettelaar
September 19, 2019

Dot all 2019 | Testing with Craft | Giel Tettelaar

Giel Tettelaar

September 19, 2019

Other Decks in Technology


  1. “ You can’t sleep well if you are not confident

    that your last commit didn’t take down the whole application. 6
  2. Two main types of testing Unit Computer oriented Done through

    PHPUnit Test small part of your code You write a lot of these. Pretty fast For Modules, Plugins and/or any seperate - Test results of PHP functions/classes Primarily for your services. Functional From perspective of user Tests more generally functionality Simulates a web request Write less of these Slower For Modules, Plugins, Sites and/or any controller or twig template. Your twig templates. Only for testing Controllers. 9 All testing is done with Codeception ❤
  3. Anatomy of a test class Let’s have a look at

    what a test *usually* looks like
  4. Running your tests 11 Tests run through Console. Output printed

    to console in real time. You will get an overview of any tests that failed and why. https://codeception.com/docs/reference/Commands
  5. Just some possible assertions PHP Unit assertSame() assertNotSame() assertArrayHasKey() assertContains()

    assertGreaterThan() assertInstanceOf() assertStringEndsWith() https://phpunit.readthedocs.io/en/8.3/as sertions.html Craft assertion methods assertElementsExist() expectEvent() assertTestFailed() assertPushedToQueue() https://docs.craftcms.com/v3/testing/framewor k/assertion-helpers.htm
  6. Just some possible methods Codeception amOnPage() grabCookie() grabPageSource() see() seeCurrentUrlEquals()

    submitForm() https://codeception.com/docs/modules/ Symfony Thanks Symfony❤ Craft specific amLoggedInAs() amOnRoute() https://codeception.com/docs/modules/ Yii2 Thanks Yii2 ❤
  7. What to test? 16 Unit • Go high up the

    call-stack • If it can be functional, make it functional (i.e. validators). • Restructure for testing. • Don’t test non-essential methods When testing validate() should you also test the return result of the rules() of your model. Functional • Test functionality • Don’t test UI specific. Test the features. • Controller functionality. When building a website should you check that a button is given a class or that clicking on it performs an action you want to perform? You know your app. Where could it break?
  8. It’s really easy 22 Manual • Really good getting started

    guide on Github/Craft docs which covers all steps to get started. • Craft’s testing suite • https://docs.craftcms.co m/v3/testing/testing-cra ft/getting-started.html Automatic A test suite will be added in a directory of your choosing. Also coming to a pluginfactory.io near you
  9. CI/CD 23 Continuous Integration: • Runs your tests automatically (i.e

    when a PR is opened or some code is committed). • Get notified • Mostly free to implement. • See the Craft testing docs or Craft’s own tests. Continuous Delivery/Deployment • Everything from CI • Cancel builds if tests fail. • Either automatic deploys or by pushing a button. • Reduces risk of site breaking builds. Still, don’t deploy on a friday
  10. What can it do? 24 Project config support Setup a

    fresh Database before tests Extends the Yii2 codeception module Module and plugin support Mocking support including testing Craft in full isolation Console command testing Run (site) Migrations before tests Ability to configure Craft in exactly the way you want for tests Email testing without sending emails Fixtures Event testing
  11. Where to go from here 26 • Craft documentation •

    #testing-craft (Discord) • Codeception documentation • Yii2 codeception documentation • Craftquest.io • Craft CMS Github • Campaign Plugin (Ben Croker) • Craft Commerce • To be released plugins.
  12. Thanks! 27 Shoutouts (never really angry) Brad Pixel and Tonic

    team. Ben Croker Andrew Welch Aaron Bushnell Takalani Radali All of you! ← Doge, for getting me through talk prep.