Jump to content

Unit test for modules, hooks or pages


rmenu
 Share

Recommended Posts

Hi,

I jumped into PW a few months ago and now is the moment when i want/need to have unit tests. No really on the CMS part but more likely on the modules or pages classes i create.

However i was not able to find a "how to" to do so. How do i get fake data (factory for users),  bootstrap everything to be able to call the method inside a module or unit test a hook applied a core module?

I dont get a clear idea of how to achieve that under PW.

As an example, let say i created a survey Module. and let say i have a fct like this within my module
 

<?php namespace ProcessWire;


class Survey extends WireData implements Module

public function__construct() {
 [blablabla]
}

public function getSurveyUrl($lang)
{
    $surveyPage = $this->pages->get('template=survey);
    return $surveyPage->localUrl($lang);
}

How would i create a unit test for that?

Thanks in advance 

Note: i ve already read the FrancisChung post but it looks like a work around (for front end) rather that the "comme il faut" way

Link to comment
Share on other sites

Unfortunately I believe there is no best practice and very little tools and helpers regarding testing. I have some experience with Laravel and there testing is first class citizen and is very easy to get started. I think @teppo has done some testing with few of his modules if I remember correctly?

Link to comment
Share on other sites

On 7/15/2020 at 12:45 AM, apeisa said:

I think @teppo has done some testing with few of his modules if I remember correctly?

I added tests for two of my modules, VersionControl and ProcessChangelog, back in the day. VersionControlTests is the more recent project, though I haven't touched or used it in years. There's only one test class in that project, and sadly I'm pretty sure it goes against so many best practices that I probably wouldn't consider it a very good starting point ?

While I did use PHPUnit, it wasn't really unit testing — more like integration testing. The test class starts from a blank ProcessWire installation with required module files included, installs the module, sets up language support and adds some languages, makes changes to page(s) and after each one checks what was stored in the database, etc. In the end it attempts to restore the site to its original "untouched" condition, so that new tests can be started.

In my experience ProcessWire involves so many interconnected parts and processes that creating "good enough" mock data would've been a major pain, and still wouldn't really have answered the question of "does this module work as expected in all supported ProcessWire versions, different operating systems, and different database system(s/ versions)". Of course there's still need for unit testing, but in my case it just didn't seem like the best approach ?

As for current testing best practices with ProcessWire, I'd definitely check out Process Nette Tester. And — this is very opinionated, sorry in advance! — I'd probably steer away from PHPUnit. I mean... I'm sure it's an amazing tool once you really get to know it, but the more I've worked (read: fought) with it, the more frustrated I've become. In my humble opinion it's not particularly developer friendly, and there are too many limitations.

Again, this might be a result of using it for wrong type of testing, so take it with a grain of salt. I just feel that there are now better options out there.

  • Like 2
Link to comment
Share on other sites

3 hours ago, rmenu said:

Overall, does this mean most of the PW code is not tested?

In the sense that no automated/programmatic tests are used: most likely yes.

Third party modules often don't come with any tests, and as for sites — well, I don't really know, but my assumption is that many don't have tests. ProcessWire can be used for a lot of stuff, but the most common use case are still bespoke websites, and for those testing is rarely a key requirement. Hence the gap in testing support compared to pure web application frameworks such as Laravel ?

As for core code, I don't really know. Ryan used to use a set of tests for the Selector engine, but I can't say for sure what the status of that project is.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Late reply here, but I have some PHPunit tests in my IndieAuth module: https://github.com/gRegorLove/ProcessWire-IndieAuth/tree/main/ProcessIndieAuth/tests

bootstrap.php: loads the Composer dependencies and ProcessWire via its index.php

ClientIdTest.php: I think this file is older and needs to be updated, but it still gives an idea how I load the module and test methods within it.

ServerTest.php: newer file which shows testing additional classes in the module. In this instance, this is like any generic PHPunit testing since that class is static methods and isn't calling ProcessWire methods.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...