lundi 21 mars 2016

Dependency on mock method with PHPUnit

I'm attempting to write PHPUnit tests for an Email abstraction class i'm using. The class interacts with the Mailgun API but I don't want to touch this in my test, I just want to return the response I would expect from Mailgun.

Within my test I have a setup method:

class EmailTest extends PHPUnit_Framework_TestCase
{

    private $emailService;

    public function setUp()
    {
        $mailgun = $this->getMockBuilder('SlmMail\Service\MailgunService')
                        ->disableOriginalConstructor()
                        ->getMock();

        $mailgun->method('send')
                ->willReturn('<2342423@sandbox54533434.mailgun.org>');

        $this->emailService = new Email($mailgun);
        parent::setUp();
    }

    public function testEmailServiceCanSend()
    {
        $output = $this->emailService->send("me@test.com");
        var_dump($output);
    }
}

var_dump($output); is currently outputting NULL rather than the string i'm expecting. The method send i'm stubbing in the mock object has a dependency through an argument, and when I call $mailgun->send() directly it errors based on this so I wonder if this is what is failing behind the scenes. Is there a way to pass this argument in, or should I approach this a different way?

Aucun commentaire:

Enregistrer un commentaire