vendredi 17 février 2017

How to get property from stub function argument?

I have a service, which should create an email class object and pass it to the third class (email-sender).

I want to check body of email, which generates by the function.

Service.php

class Service
{
    /** @var EmailService */
    protected $emailService;

    public function __construct(EmailService $emailService)
    {
        $this->emailService = $emailService;
    }

    public function testFunc()
    {
        $email = new Email();
        $email->setBody('abc'); // I want to test this attribute

        $this->emailService->send($email);
    }
}

Email.php:

class Email
{
    protected $body;

    public function setBody($body)
    {
        $this->body = $body;
    }
    public function getBody()
    {
        return $this->body;
    }
}

EmailService.php

interface EmailService
{
    public function send(Email $email);
}

So I create a stub class for emailService and email. But I can't validate the body of the email. I also can't check if $email->setBody() was called, because email is created inside the tested function

class ServiceSpec extends ObjectBehavior
{
    function it_creates_email_with_body_abc(EmailService $emailService, Email $email)
    {
        $this->beConstructedWith($emailService);

        $emailService->send($email);
        $email->getBody()->shouldBe('abc');
        $this->testFunc();
    }
}

I got this:

Call to undefined method Prophecy\Prophecy\MethodProphecy::shouldBe() in /private/tmp/phpspec/spec/App/ServiceSpec.php on line 18 

In real app the body is generated, so I want to test, if it's generated correctly. How can I do that?

Aucun commentaire:

Enregistrer un commentaire