Skip to content

Instantly share code, notes, and snippets.

@xorik
Created February 19, 2017 08:34
Show Gist options
  • Save xorik/89da2c3c065d1b6b6d10d915191305f2 to your computer and use it in GitHub Desktop.
Save xorik/89da2c3c065d1b6b6d10d915191305f2 to your computer and use it in GitHub Desktop.
phpspec demo
<?php
namespace App;
class Email
{
protected $body;
public function setBody($body)
{
$this->body = $body;
}
public function getBody()
{
return $this->body;
}
}
<?php
namespace App;
class EmailFactory
{
public function createEmail($body)
{
$email = new Email();
$email->setBody($body);
return $email;
}
}
<?php
namespace spec\App;
use App\Email;
use App\EmailFactory;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class EmailFactorySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(EmailFactory::class);
}
function it_creates_email_with_body_content()
{
$body = 'abc';
$email = $this->createEmail($body);
$email->shouldBeAnInstanceOf(Email::class);
$email->getBody()->shouldBeEqualTo($body);
}
}
<?php
namespace App;
interface EmailService
{
public function send(Email $email);
}
<?php
namespace App;
class Service
{
protected $emailService;
protected $emailFactory;
public function __construct(EmailService $emailService, EmailFactory $emailFactory)
{
$this->emailService = $emailService;
$this->emailFactory = $emailFactory;
}
public function testFunc()
{
$email = $this->emailFactory->createEmail('abc');
$this->emailService->send($email);
}
}
<?php
namespace spec\App;
use App\Email;
use App\EmailFactory;
use App\EmailService;
use App\Service;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ServiceSpec extends ObjectBehavior
{
function it_is_initializable(EmailService $emailService, EmailFactory $emailFactory)
{
$this->beConstructedWith($emailService, $emailFactory);
$this->shouldHaveType(Service::class);
}
function it_creates_email_with_body_abc(EmailService $emailService, EmailFactory $emailFactory, Email $email)
{
$this->beConstructedWith($emailService, $emailFactory);
$emailFactory->createEmail('abc')->willReturn($email);
$emailService->send($email)->shouldBeCalled();
$this->testFunc();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment