Slide 1

Slide 1 text

Testing with Craft

Slide 2

Slide 2 text

Who dis? Tech lead @ Global Network Group Discord & Github: @gtettelaar

Slide 3

Slide 3 text

Testing What’s it all about?

Slide 4

Slide 4 text

Everyone tests their code already 4

Slide 5

Slide 5 text

Let’s improve our application 5

Slide 6

Slide 6 text

“ You can’t sleep well if you are not confident that your last commit didn’t take down the whole application. 6

Slide 7

Slide 7 text

“The best developers are lazy energy efficient”

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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 ❤

Slide 10

Slide 10 text

Anatomy of a test class Let’s have a look at what a test *usually* looks like

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

(Unit) Demo time

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

(Functional) Demo time

Slide 15

Slide 15 text

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 ❤

Slide 16

Slide 16 text

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?

Slide 17

Slide 17 text

“ Test your application’s business logic - not it’s implementation. 17

Slide 18

Slide 18 text

Top tip: 18

Slide 19

Slide 19 text

Craft’s tests Craft CMS actually has tests of it’s own since 3.2.

Slide 20

Slide 20 text

Demo time. let’s look at Craft’s tests

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

That’s it Well… Yes but no….

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

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.