Mockery OneToMany模拟并检查对象是否相同

时间:2017-03-26 18:20:42

标签: php unit-testing doctrine-orm phpunit mockery

我有一段代码,我想测试一下。我需要测试的功能如下:

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
     */
    public function validate()
    {
        if (null !== $this->getIssuer() &&
            null !== $this->getPurchaser() &&
            $this->getIssuer() === $this->getPurchaser()) {
            throw new BadRequestHttpException('issuer_same_as_purchaser');
        }

        // Payment date should be equal or larger than issue date. You're not supposed to issue an
        // invoice in the future with payment date equal to now.
        // Todo: This should be optional - in some cases, it may be ok to do this.
        // Todo: Maybe we should change the payment date to issue date instead of throwing an exception?
        // Maybe make it optional?
        if (null !== $this->paymentDate
            && null !== $this->issueDate
            && $this->paymentDate < $this->issueDate) {
            // Payment date cannot be before issue date
            throw new BadRequestHttpException('payment_date.before_issue_date');
        }
    }

现在,我需要让$this->getIssuer() === $this->getPurchaser()平等。我这样嘲笑:

    public function testValidate()
    {
        $invoice = new Invoice();
        $contractor = new Contractor();
        $issuer = \Mockery::mock($contractor, ['getId' => '123']);

        /** @var Invoice $invoiceIssuerSameAsPurchaser */
        $invoiceIssuerSameAsPurchaser = \Mockery::mock(
            $invoice,
            [
                'getIssuer' => '123',
                'getPurchaser' => '123'
            ]
        );

        $invoiceIssuerSameAsPurchaser->validate();
    }

当我启动validate()功能时,它甚至无法进入if,因为我正在检查getIssuergetPurchaser是否不是空值。我怎样才能说服PHP,在这两个函数下设置了对象?

0 个答案:

没有答案