Question

I'm testing a class with PHPSpec which is going fine until I wanted to create a mock for a class that has static functions.

Class I'm testing:

<?php

namespace App\Service;

class PaymentService
{

    public function paymentVerification($orderId, array $data)
    {
        ...

        // Get the payment details
        $payment = \PayPal\Api\Payment::get($data['payKey'], $apiContext);

        ...
    }
}

PHPSpec class:

<?php

namespace App\Spec\Service;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class PaymentServiceSpec extends ObjectBehavior
{
    /**
     * @param \PayPal\API\Payment $payment
     */
    function it_should_return_false_when_the_payment_verification_failed($payment)
    {
        ...
        // This throws a PHP Fatal error:  Call to undefined method PhpSpec\Wrapper\Collaborator::get() in
        $payment::get(Argument::exact($data['payKey']), Argument::exact($apiContext))->shouldReturn(array('foobar'));

        ...

        $this->paymentVerification($orderId, $data)->shouldReturn(false);
    }
}

How can I mock \PayPal\Api\Payment::get($data['payKey'], $apiContext); ? Currently this throws a PHP Fatal error: Call to undefined method PhpSpec\Wrapper\Collaborator::get()

How can this be done correctly? Thanks in advance!

Était-ce utile?

La solution

You should avoid doing it. Create a thin layer which will wrap the paypal library. Mock your own stuff.

See https://github.com/phpspec/prophecy/pull/20#issuecomment-18133965

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top