了解PHP对象类型比较

时间:2014-12-29 18:05:11

标签: php oop tdd

继续通过Kent Beck的测试驱动开发实例,并用PHP重写示例。

第13章描述了一个测试,如果2个对象属于同一类型,则该测试应返回true。在前面的章节中,评估是有效的,但是对于这个例子,我无法通过,而且我不确定它为什么会失败。

给定实现Expression接口的类“Sum”:

    class Sum implements Expression {
    public $augend;
    public $addend;


    public function __construct($augend, $addend)
    {

        $this->augend = $augend;
        $this->addend = $addend;

    }

     // impl of Expression interface, but this smells to me, dupe implementation
    // also in Money
    public function plus($addend) {
        return new Sum($this, $addend);
    }

    public function reduce($to) {         
        $amount = $this->augend->amount + $this->addend->amount;

        return new Money($amount, $to);
    }
}

表达:

    interface Expression {
    public function plus($addend);

    public function reduce($to);

}

我正在尝试在Bank对象(称为reduce)上调用一个方法,该对象的第一个参数是一个Sum对象,它有自己的reduce实现。但是,Java示例将第一个arg指定为接口,而不是具体的类:

    class Bank {

    // the book defines the $source param as type Expression, which is legal 
    // in Java but not in PHP
    public function reduce($source, $to) {

       return $source->reduce($to);
    }
}

最后,我的Money课程:

    class Money implements Expression {
    public $amount;
    public $currency;

    public function __construct($amount, $currency) {
        $this->amount = $amount;
        $this->currency = $currency;
    }

    public function currency(){
        return $this->currency;
    }

    public function equals($compareObject) {

        return $this->amount == $compareObject->amount
         && $this->currency() == $compareObject->currency();
    }

    // static factory method that returns Dollar 
    // (reduces dependence on subclasses)
    static function dollar($amount) {
        return new Dollar($amount, "USD");
    }

    static function franc($amount) {
        return new Franc($amount, "CHF");
    }

    public function times($multiplier) {
        return new Money($this->amount * $multiplier, $this->currency);
    }


     // impl of Expression interface
    public function plus($addend) {
        return new Sum($this, $addend);
    }

    public function reduce($to) {
        return $this;
    }
}

运行此测试时:

$sum = new Sum(Money::dollar(3), Money::dollar(4));
$bank = new Bank();

$result = $bank->reduce($sum, "USD");

$this->assertEquals(Money::dollar(7), $result); //FAIL

断言失败,声明$ result是类型Money而不是类型Dollar,即使我已经验证每个对象的属性匹配:

$this->assertEquals(Money::dollar(7)->amount, $result->amount);


$this->assertEquals(Money::dollar(7)->currency, $result->currency);

这是因为缺乏将对象本质地转换为特定类型的能力吗?我没有更改子类的实现,之前的测试仍然通过:

$five = Money::dollar(5);
$this->assertEquals(new Money(10, "USD"), $five->times(2));   
$this->assertEquals(new Money(15, "USD"), $five->times(3));
$this->assertEquals(get_class($five), "Dollar");

2 个答案:

答案 0 :(得分:0)

两个不同类型的对象永远不能相等。 PHP不会让对象决定它是否等于另一个对象,就像java使用等于方法一样。

http://php.net/manual/en/language.oop5.object-comparison.php

在您的测试用例中,您可以自己调用eqauls方法。

$this->assertsTrue( $result->equals(Money::dollar(7)) );

答案 1 :(得分:0)

这是一个架构问题,而不是使用phpunit的问题。我给了你两个简单的解决方案,但你当然应该用一些设计模式改进你的代码。

<强> 1

如果你必须通过&#34; USD&#34;定义课程Dollar有什么意义呢?到构造函数?请改用班级Money

// static factory method that returns Dollar 
// (reduces dependence on subclasses)
static function dollar($amount) {
    return new Money($amount, "USD");
}

static function franc($amount) {
    return new Money($amount, "CHF");
}

public function times($multiplier) {
    return new Money($this->amount * $multiplier, $this->currency);
}

<强> 2

这不是Dollar之和与某个货币可能返回Money的逻辑(如果您没有转换机制)。您可以像这样更改reduce的方法Sum

public function reduce() {         
    $money = clone $this->augend;
    $money->amount = $this->augend->amount + $this->addend->amount;

    return $money;
}

<强> ---

使用这些解决方案,您的测试将按预期工作:

$sum = new Sum(Money::dollar(3), Money::dollar(4));
$bank = new Bank();

$result = $bank->reduce($sum, "USD");

$this->assertEquals(Money::dollar(7), $result); // OK

因为这个断言检查对象的类和属性的相等性。