1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHPUnit】テストクラスに汎用的なBaseTestClassを作ろう

Last updated at Posted at 2025-03-07

はじめに

各テストクラスで有効化処理書いたり面倒なので、
テストクラスの継承元のBaseTestClassを作って楽がしたい。

コード

BaseTestCase
<?php

namespace Tests;

use Tests\TestCase;
use ReflectionClass;

class BaseTestCase extends TestCase
{
    protected $testsDirPath;

    public function __construct()
    {
        parent::__construct();
        return $this->testsDirPath = __DIR__;
    }

    /* Execute a private or protected function with params.
    *
    * @param  object $instance
    * @param  string $functionName
    * @param  array  $params
    * @return mixed 
    */
    protected function executePrivateFunction(object $instance, string $functionName, array $params = [])
    {
        $method = $this->enablePrivateFunction($instance, $functionName);
        return $method->invokeArgs($instance, $params);
    }

    /* Enable a private or protected function
    *
    * @param  object $instance
    * @param  string $functionName
    * @return object $method
    */
    protected function enablePrivateFunction(object $instance, string $functionName): object
    {
        $reflection = new ReflectionClass($instance);
        $method = $reflection->getMethod($functionName);
        $method->setAccessible(true);
        return $method;
    }

    /* Get a private or protected property
    *
    * @param  object $instance
    * @param  string $propertyName
    * @return mixed
    */
    protected function getPrivateProperty(object $instance, string $propertyName)
    {
        $property = $this->enablePrivateProperty($instance, $propertyName);
        return $property->getValue($instance);
    }

    /* Set a private or protected property
    *
    * @param  object $instance
    * @param  string $propertyName
    * @param  mixed  $value The value you want to overwrite.
    * @return mixed
    */
    protected function setPrivateProperty(object $instance, string $propertyName, $value)
    {
        $property = $this->enablePrivateProperty($instance, $propertyName);
        return $property->setValue($instance, $value);
    }

    /* Enable a private or protected property
    *
    * @param  object $instance
    * @param  string $propertyName
    * @return object $method
    */
    protected function enablePrivateProperty(object $instance, string $propertyName): object
    {
        $reflectionClass = new ReflectionClass($instance);
        $property = $reflectionClass->getProperty($propertyName);
        $property->setAccessible(true);
        return $property;
    }
}

各function説明

executePrivateFunction()

privateやprotectedなfunctionをそのままテスト実施するとエラー出るので有効化したついでにテスト自体も実行する

enablePrivateFunction()

↑のfunctionを有効化している部分

getPrivateProperty()

privateやprotectedなpropertyを取得するfunction。ゲッター

setPrivateProperty()

privateやprotectedなpropertyにはそのままだとセットできないのでセッター

enablePrivateProperty

↑のpropertyを有効化している部分

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?