11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(旧版) private,protectedなメソッドを呼び出してテストする

Last updated at Posted at 2014-03-03

注意

2018年8月にprivate,protectedなメソッドを呼び出してテストする(新)を書いたので、そちらを参考にしてください。


どうしてもpublicにしたくないメソッドをテストしたいなー、ってときに無理やりメソッドを呼び出す。

/**
 * メソッドと同じように呼べる無名函数を返す
 */
function callMethodAsPublic($class, $method)
{
    $ref = new \ReflectionMethod($class, $method);
    $ref->setAccessible(true);
    $obj = (gettype($class) === 'object') ? $class : null;

    return function () use ($class, $ref, $obj) {
        $args = func_get_args();
        return $ref->invokeArgs($obj, $args);
    };
}

静的メソッドでも動的メソッドでも呼べるようにした。でもバッドノウハウくさくてうーんって感じだ。うーん…

うーんって感じなので、作ったはいいものの、自分でも使ってない。

gistにも置いた

Testklass.php
<?php

class Testklass
{
   public function publicMethod ($str)
   {
        return "public: $str";
   }

   public static function publicStaticMethod ($str)
   {
        return "public+static: $str";
   }

   private function privateMethod ($str)
   {
        return "private: $str";
   }

   private static function privateStaticMethod ($str)
   {
        return "private+static: $str";
   }

   protected function protectedMethod ($str)
   {
        return "protected: $str";
   }

   protected static function protectedStaticMethod ($str)
   {
        return "protected+static: $str";
   }
}
TestklassTest.php
<?php
require_once './Testklass.php';

class TestklassTest extends PHPUnit_Framework_TestCase
{
    public static function methodAsPublic ($class, $method)
    {
        $ref = new \ReflectionMethod($class, $method);
        $ref->setAccessible(true);
        $obj = (gettype($class) === 'object') ? $class : null;

        return function () use ($class, $ref, $obj) {
            $args = func_get_args();
            return $ref->invokeArgs($obj, $args);
        };
    }

    public function test_publicMethod ()
    {
        $method = self::methodAsPublic(new Testklass, 'publicMethod');
        $actual = $method('$str');
        $expected = 'public: $str';

        $this->assertSame($expected, $actual);
    }

    public function test_publicStaticMethod ()
    {
        $method = self::methodAsPublic('Testklass', 'publicStaticMethod');
        $actual = $method('$str');
        $expected = 'public+static: $str';

        $this->assertSame($expected, $actual);
    }

    public function test_privateMethod ()
    {
        $method = self::methodAsPublic(new Testklass, 'privateMethod');
        $actual = $method('$str');
        $expected = 'private: $str';

        $this->assertSame($expected, $actual);
    }

    public function test_privateStaticMethod ()
    {
        $method = self::methodAsPublic('Testklass', 'privateStaticMethod');
        $actual = $method('$str');
        $expected = 'private+static: $str';

        $this->assertSame($expected, $actual);
    }

    public function test_protectedMethod ()
    {
        $method = self::methodAsPublic(new Testklass, 'protectedMethod');
        $actual = $method('$str');
        $expected = 'protected: $str';

        $this->assertSame($expected, $actual);
    }

    public function test_protectedStaticMethod ()
    {
        $method = self::methodAsPublic('Testklass', 'protectedStaticMethod');
        $actual = $method('$str');
        $expected = 'protected+static: $str';

        $this->assertSame($expected, $actual);
    }
}

当然だけどテスト用途以外に使ってはならない(戒め)。

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?