How to exchange documents between tests in PHPUnit

I am doing functional testing on WordPress, so I am reading HTML and using Mastermind / HTML5 to transform the test. However, tests are now slowing down because the HTML document takes about 1s to load per test. I would like to share the tool between tests, so I don't have to parse for each test. But I have one limitation, the method that gets the html from is in the parent class, which is a non-statistical method

https://core.trac.wordpress.org/browser/trunk/tests/phpunit/includes/testcase.php?rev=32953#L328

What choice do I need to share the device between tests.

Here is my sample code

class Testcase extends WP_UnitTestCase {

    public function setUp() {
        parent::setUp();
    }

    public function get_dom( $path ) {
        $html = $this->go_to( $path ); // I cannot change this method
        // do some html parsing and return DOM
    }

}

      

Here's my sample test

class Testcase1 extends Testcase {
     public setUp(){
          $this->dom = $this->get_dom('/')
     }
     public test_1() {
     }

     public test_2() {
     }
}

      

I was thinking about creating a get_dom

static method , so it will just be called once, but as far as I know, a static method cannot call a non-static method. I'm right? and if so, is there anyway I could share the tool between tests?

+3


source to share


1 answer


Do you mean the "dom" data cache? Try the following:



public function setUp() {
     static $dom;
     if (!isset($dom)) {
         $dom = $this->get_dom('/');
     }

     $this->dom = $dom;
}

      

+1


source







All Articles