0

I'm working on the Liquid Messages plugin. There are some basic PHPUnit tests existing for it and when I run phpunit all pass except for one. It errors out:

There was 1 error:
1) GCS_Async_Test::test_class_access
Exception: Invalid GC_Sermons_Plugin property: async
/long/path/wp-content/plugins/lqd-messages/gc-sermons.php:330
/long/path/wp-content/plugins/lqd-messages/tests/test-async.php:10

The weird thing is that gc-sermons.php contains a number of properties similar to this one. They are initialized like so:

protected $sermons
protected $taxonomies
protected $async

Instances of the classes are attached to the properties:

        public function plugin_classes()
        {
            require_once self::$path . 'functions.php';

            // Attach other plugin classes to the base plugin class.
            $this->sermons = new GCS_Sermons($this);
            $this->taxonomies = new GCS_Taxonomies($this->sermons);
            $this->async = new GCS_Async($this);
            $this->shortcodes = new GCS_Shortcodes($this);
        }

If we look at GCS_Sermons, which is similar to GCS_Async in that it extends another class we see:

class GCS_Sermons extends GCS_Post_Types_Base {

Essentially the same as GCS_Async:

class GCS_Async extends WP_Async_Task {

If we look at the tests we see that the same test is run against GCS_Sermons as GCS_Async, but only fails with GCS_Async:

    function test_class_access() {
        $this->assertTrue( gc_sermons()->sermons instanceof GCS_Sermons );
    }

    function test_class_access() {
        $this->assertTrue( gc_sermons()->async instanceof GCS_Async );
    }

Any ideas why?

0

1 Answer 1

3
+50

the async property is marked as protected, so that usually signifies that it is related more to your implementation's details rather than functionality. Ideally you would write your tests to test the public methods you have exposed. Testing that the class property you set in the constructor of one class is an instanceof what was initialized doesn't provide any constructive feedback tbh. Testing for instance that prepare_data in that class actually prepared the data the way you expected, would let you know that the class instance was instantiated.

That aside though - it looks like you have a PHP magic __get throwing an exception on L319: https://github.com/LiquidChurch/lqd-messages/blob/master/gc-sermons.php#L319

So just add async handling for that property because it can't be accessed outside of the class.

Just another note it's best to use assertInstanceOf instead of instanceof for your testcase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.