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

Dot all 2019 | Testing with Craft | Giel Tettelaar

B4bbdf007541ece4cf1122a6ba76ac88?s=47 Giel Tettelaar
September 19, 2019

Dot all 2019 | Testing with Craft | Giel Tettelaar


Giel Tettelaar

September 19, 2019


  1. Testing with Craft

  2. Who dis? Tech lead @ Global Network Group Discord &

    Github: @gtettelaar
  3. Testing What’s it all about?

  4. Everyone tests their code already 4

  5. Let’s improve our application 5

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

    that your last commit didn’t take down the whole application. 6
  7. “The best developers are lazy energy efficient”

  8. Well.. How? Let’s get some practical tests going.

  9. 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 ❤
  10. Anatomy of a test class Let’s have a look at

    what a test *usually* looks like
  11. 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
  12. (Unit) Demo time

  13. 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
  14. (Functional) Demo time

  15. 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 ❤
  16. 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?
  17. “ Test your application’s business logic - not it’s implementation.

  18. Top tip: 18

  19. Craft’s tests Craft CMS actually has tests of it’s own

    since 3.2.
  20. Demo time. let’s look at Craft’s tests

  21. How do i start? Craft actually has a testing Framework!

  22. 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
  23. 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
  24. 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
  25. That’s it Well… Yes but no….

  26. 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.
  27. 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.