Created
February 19, 2017 08:34
-
-
Save xorik/89da2c3c065d1b6b6d10d915191305f2 to your computer and use it in GitHub Desktop.
phpspec demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Email | |
{ | |
protected $body; | |
public function setBody($body) | |
{ | |
$this->body = $body; | |
} | |
public function getBody() | |
{ | |
return $this->body; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class EmailFactory | |
{ | |
public function createEmail($body) | |
{ | |
$email = new Email(); | |
$email->setBody($body); | |
return $email; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
interface EmailService | |
{ | |
public function send(Email $email); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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