Home » Wordpress » Why Can't PHPUnit UnitTest My WordPress Website

Why Can't PHPUnit UnitTest My WordPress Website

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have been researching this issue for days and although many people ask this question I never found a clear and concise answer.

I am running:

  • PHP Unit: Version 3.7.21
  • WordPress: Version 3.5.1
  • OS: Ubuntu 11.10
  • MySQL: Version 5.1.69-0ubuntu0.11.10.1
  • PHP: Version 5.3.6-13ubuntu3.10

Long story short, I have the basic wordPress core downloaded into a directory on my computer. I have successfully downloaded and installed pear and phpunit. I tested phpunit by writing basic tests on simple php files which worked fine. My issue is when I run phpunit on a wordpress site (which I have not changed the core WP files of).

I get the error:

“PHP Fatal error: Call to a member function main() on a non-member
object in /< root_directory>/functions.php on line 779”.

This error corresponds to this code-segment:

function wp( $query_vars = '' ) {
    global $wp, $wp_query, $wp_the_query;
    $wp->main( $query_vars ); // Line 779

    if ( !isset($wp_the_query) )
        $wp_the_query = $wp_query;
}

After some intense internet crawling I found many people had errors on this exact line so I know there is some general thing like an environment setting or our setup process that gave us all this error.

Some solutions I have read and tried that did not work for me:

  • Change the required file in your “index.php” file.
  • Place a require of “load.php” above the head in your “index.php” file.
  • Re-install WordPress
  • Change boot-strap and phpunit environments.

I have been a web-developer for 5 years but I have never used WordPress for one of my clients before. Once I can get this set up writing the unit tests will be easy; but I have no idea what is causing this issue. Any links to guides/tutorials or clear explanations would be greatly appreciated. Thank you for your time.

Answers:

There is a WordPress test suite, which your question suggests you might have missed:

http://make.wordpress.org/core/handbook/automated-testing/

Keep in mind, with that being highlighted, that WP was not designed in such a way that it or anything related to it can or is supposed to be tested. The modus operandi has, for years, been to commit code with no tests at all. It’s admittedly improving over the years, albeit at a slow pace.

Related questions:

What are the best practices of custom application WordPress development?

WordPress Plugin: How do I avoid "tight coupling"?

Leave a Reply

Your email address will not be published. Required fields are marked *