0

I have followed wordpress office blog -->https://make.wordpress.org/cli/handbook/plugin-unit-tests/ to implement unit testing in plugin. I have been able to write a simple test on one of my method of my class. Eg is :

    <?php 

    Class Classtobetested{

        public $flag = true;

        function __construct(){

        }

        public function returntruefunction(){
            return true;
        }


        public function getfeedData($category,$remotecategory,$savedfeeddata,$identifier){

            /* Suppose someclass whose method is used here */

            $someObj = new SomeClass();
            $result = $someObj->SomeClass($category);
            if(is_array($result)){
                foreach ($result as $key => $value) {
                    $items = $this->convertProductToItem($value);
                    if($items==false){
                        $this->flag =false;

                        /* Lets suppose this is the log function with keeps the log of product with some error*/

                        $this->logactivity($item->name);
                    }
                }

                if($this->flag==false){
                    return false;
                }else{
                    return true;
                }
            }
            return false;
        }

        public function convertProductToItem($data){
            if(is_object($data)){

                /* It writes the item to some file */

                $file = fopen('somefilename', 'w');
                if(fwrite($file, $data)){
                    return true;
                }
                return false;
            }
            return false;
        }

    }


    Class SomeClass{

        function __construct(){

        }

        public function SomeClass($category){
            /* 
             *This is the method which will get products from woo-commerce and returns to the getfeedData 
             * Lets Say $products are the product array
             */

            return $products;

        }
    }

     ?>

And this is the tests that i have written for returntruefunction() method which provides me successfull tests results.

    <?php
    /**
     * Class SampleTest
     *
     * @package ***********Somepackage********
     */

    /**
     * Sample test case.
     */

    /*This is previously defined class imported*/

    require_once "previously defined class";

    class SampleTest extends WP_UnitTestCase
    {

        /**
         * A single example test.
         */
        public function test_sample()
        {
            $check = new Classtobetested();
            $this->assertTrue(true, $check->returntruefunction());
        }
    }

Now I want to write test for method, getfeedData. Is my method suitable to perform unit testing, or should i make it more simpler ? If so how can i do that, Please help me with this. Any kinds of help are appreciated. Thanks.

Tests result for returntruefunction is enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.