Phpunit Logo

Aunque no suele pasar y se deba evitar, el  testeo de funciones privadas o protected en clases a veces es necesario.

Esta clase la utilizo para tener una guia en los casos de clases que tienen funciones privadas y/o protegidas a las cuales necesito hacerles algún tipo de test unitario.

 IMPORTANTE: Esta clase funciona con la version PHPUnit 5.3.4 y Debian 8.4

La clase de ejemplo es la siguiente:

 

 

<?php
    // File for test
    //require_once(realpath(dirname(__FILE__))."/../Class/AlertApkDownloadClass.php");
    class NameClassTest extends PHPUnit_Framework_TestCase
    {

        /**
         * Call protected/private method of a class.
         *
         * @param object &$object    Instantiated object that we will run method on.
         * @param string $methodName Method name to call
         * @param array  $parameters Array of parameters to pass into method.
         *
         * @return mixed Method return.
         */
        function invokeMethod(&$object, $methodName, array $parameters = array())
        {
            $reflection = new \ReflectionClass(get_class($object));
            $method = $reflection->getMethod($methodName);
            $method->setAccessible(true);

            return $method->invokeArgs($object, $parameters);
        }

        /**
         * Instance object for test.
         *
         *
         * @return object Returns the object for test.
         */
        function instanceObject()
        {
            $Object = new NameClass();
            return $Object;
        }

        /**
         * Test if Class exists.
         */
        function testExistsClass()
        {
            $this->assertTrue(class_exists("NameClass"));
        }

        /**
         * Test if function with wrong params return false.
         */
        function testFunctionWithWrongParamsReturnFalse()
        {
            $this->assertFalse($this->invokeMethod($this->instanceObject(),"function",array("param1","param2")));
        }

        /**
         * Test if function with right params return true.
         */
        function testFunctionWithRightParamsReturnTrue()
        {
            $this->assertTrue($this->invokeMethod($this->instanceObject(),"function",array("param1","param2")));
        }

        /**
         * Test if function return array.
         */
        function testFunctionReturnArray()
        {
            $this->assertInternalType("array",$this->invokeMethod($this->instanceObject(),"getArray",array()));
        }

        /**
         * Test if function with right params return null.
         */
        function testFunctionWithRightParamsReturnNull()
        {
            $this->assertNull($this->invokeMethod($this->instanceObject(),"function",array("param1","param2")));
        }

        /**
         * Test if private/protected function with empty values return exception.
         */
        function testFunctionWithEmptyValueReturnsException()
        {
            try{
                $this->invokeMethod($this->instanceObject(),"function",array(null,null));
            }catch(Exception $e){
                $this->assertEquals("Cant pass empty values",$e->getMessage());
            }
        }

        /**
        *  Test if function returns array and return array.
        *
        * @return mixed Method return.
        */
        function testFunctionGetAllReturnArray(){
            $result = $this->invokeMethod($this->instanceObject(),"getAll");
            $this->assertInternalType("array",$result);
            return $result;
        }

        /**
         *  Test if function return bool with right param array that is return of the function test testFunctionGetAllReturnArray.
         *
         * @param array  $parameters Array of parameters to pass into method.
         *
         * @depends testFunctionGetAllReturnArray
         */
        function testSetAllWithRightParamsReturnBool($arrayGetAll){
            $this->assertInternalType("bool",$this->invokeMethod($this->instanceObject(),"setAll",array($arrayGetAll)));
        }

    }

 

 

 

 

  • No se han encontrado comentarios

Deja tus comentarios

Enviar un comentario como invitado

0 / 300 Restricción de Carateres
El texto debe estar entre 2-300 caracteres
Sus comentarios están sujetos a moderación por un administrador.