我可以集中相同但需要两个不同的方法名称才能正常工作的代码吗?


Can I centralize code that's identical but needs two different method names to properly work?

在我的工作中,我有很多数据要导入,这些数据来自各种来源,这些数据倾向于提供"不干净"的信息(缺少字段,到处都是错别字,你能想到的)。因此,我构建了一个框架来自动化数据适合生产所需的各种验证。这个东西是用PHP构建的,我从PHPUnit那里得到了很多灵感,关于我的东西是如何工作的。这是我基础课程的一部分:

abstract class importUnitTest
{
    public function __construct($nomTable, $conn, $niveauOutput = self::OUTPUT_ALL)
    {
        $this->nomTable = $nomTable;
        $this->conn = $conn;
        $this->testsReussis = true;
        $this->niveauOutput = $niveauOutput;
        $this->sql = "";
        $methods = get_class_methods($this);
        echo "<table class='tblUnitTest' id='"unitTestResults'" border='1'>'n";
        forEach($methods as $method)
        {
            if($method != '__construct' && preg_match("/^test/", $method))
            {
                $this->{$method}();
            }
        }
        echo "</table>'n";
    }
}

基本上,它的作用是,在实例化时,它将遍历我类的每个方法并执行所有以测试开头的方法。因此,当我有要测试的新型文件时,我只需扩展一个类并将我的验证编码如下:

class FACT_UnitTests_BT_BC  extends importUnitTest
{
    public function test__Assureur__NotNullOrEmpty($actif = true)
    {
        $res1 = $this->champNotNull($this->getNomChamp(__FUNCTION__), $actif); 
        $res2 = $this->champNotEmpty($this->getNomChamp(__FUNCTION__), $actif);
        if(!$actif)
            $resultat = self::TEST_DESACTIVE;
        else
            $resultat = ($res1 == self::TEST_REUSSI && $res2 == self::TEST_REUSSI) ? self::TEST_REUSSI : self::TEST_ECHOUE;
        $this->genererOutput($this->getNomTestCourant(__FUNCTION__), $resultat);
        $this->testsReussis = ($this->testsReussis && ($resultat !== self::TEST_ECHOUE));
        $this->sql = "";
    }
    public function test__NoAssureur__NotNull($actif = true)
    {
        $resultat = $this->champNotNull($this->getNomChamp(__FUNCTION__), $actif);
        $this->genererOutput($this->getNomTestCourant(__FUNCTION__), $resultat);
        $this->testsReussis = ($this->testsReussis && ($resultat !== self::TEST_ECHOUE));
        $this->sql = "";
    }
    public function test__NoClient__NotNull($actif = true)
    {
        $resultat = $this->champNotNull($this->getNomChamp(__FUNCTION__), $actif);
        $this->genererOutput($this->getNomTestCourant(__FUNCTION__), $resultat);
        $this->testsReussis = ($this->testsReussis && ($resultat !== self::TEST_ECHOUE));
        $this->sql = "";
    }
}

这一切都完美运行,并允许我自动化几乎任何我想要的验证。但是,您可能注意到方法"NoAssureur__NotNull"和"NoClient__NotNull"具有完全相同的代码。在正常情况下,相同的代码=相同的功能...但在这种特殊情况下,我需要函数名称才能正确确定我正在测试的字段(您可能猜到一个是 NoClient,另一个是 NoAssureur)。

最近,我在一次测试中发现了一个小错误......我不得不回去在一百多个地方更正它(因为我显然是在复制粘贴)。这在维护方面是相当不可接受的。那么有没有更好的做事方式呢?有没有办法集中相同的测试代码?

提前感谢,

大须

我会使用字段参数在抽象类中设置测试代码,并使用参数在子类中调用它。

像这样的东西(省略了部分实际代码,有点不确定,因为我不知道你的其他方法在哪里):

abstract class importUnitTest
{
    public function isNotNull($field,$actif) {
        $resultat = $this->champNotNull($this->getNomChamp($field), $actif);
        $this->genererOutput($this->getNomTestCourant($field), $resultat);
        $this->testsReussis = ($this->testsReussis && ($resultat !== self::TEST_ECHOUE));
        $this->sql = "";
    }
//[...] omitting existing code
}

class FACT_UnitTests_BT_BC  extends importUnitTest
{
   // Omission of actual code
    public function test__NoAssureur__NotNull($actif = true)
    {
        $this->isNotNull(__FUNCTION__, $actif)
    }
    public function test__NoClient__NotNull($actif = true)
    {
        $this->isNotNull(__FUNCTION__, $actif)
    }
}

这又是我头顶上的东西,我可能错了;)